blob: 04636552e98acb9f82a8d593199efba81560d3f7 [file] [log] [blame]
Jesse Barnes79e53942008-11-07 14:24:08 -08001/*
2 * Copyright © 2006-2007 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 */
26
Daniel Vetter618563e2012-04-01 13:38:50 +020027#include <linux/dmi.h>
Jesse Barnesc1c7af62009-09-10 15:28:03 -070028#include <linux/module.h>
29#include <linux/input.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080030#include <linux/i2c.h>
Shaohua Li7662c8b2009-06-26 11:23:55 +080031#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Jesse Barnes9cce37f2010-08-13 15:11:26 -070033#include <linux/vgaarb.h>
Wu Fengguange0dac652011-09-05 14:25:34 +080034#include <drm/drm_edid.h>
David Howells760285e2012-10-02 18:01:07 +010035#include <drm/drmP.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080036#include "intel_drv.h"
Chris Wilson5d723d72016-08-04 16:32:35 +010037#include "intel_frontbuffer.h"
David Howells760285e2012-10-02 18:01:07 +010038#include <drm/i915_drm.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080039#include "i915_drv.h"
Imre Deakdb18b6a2016-03-24 12:41:40 +020040#include "intel_dsi.h"
Jesse Barnese5510fa2010-07-01 16:48:37 -070041#include "i915_trace.h"
Xi Ruoyao319c1d42015-03-12 20:16:32 +080042#include <drm/drm_atomic.h>
Matt Roperc196e1d2015-01-21 16:35:48 -080043#include <drm/drm_atomic_helper.h>
David Howells760285e2012-10-02 18:01:07 +010044#include <drm/drm_dp_helper.h>
45#include <drm/drm_crtc_helper.h>
Matt Roper465c1202014-05-29 08:06:54 -070046#include <drm/drm_plane_helper.h>
47#include <drm/drm_rect.h>
Keith Packardc0f372b32011-11-16 22:24:52 -080048#include <linux/dma_remapping.h>
Alex Goinsfd8e0582015-11-25 18:43:38 -080049#include <linux/reservation.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080050
Daniel Vetter5a21b662016-05-24 17:13:53 +020051static bool is_mmio_work(struct intel_flip_work *work)
52{
53 return work->mmio_work.func;
54}
55
Matt Roper465c1202014-05-29 08:06:54 -070056/* Primary plane formats for gen <= 3 */
Damien Lespiau568db4f2015-05-12 16:13:18 +010057static const uint32_t i8xx_primary_formats[] = {
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010058 DRM_FORMAT_C8,
59 DRM_FORMAT_RGB565,
Matt Roper465c1202014-05-29 08:06:54 -070060 DRM_FORMAT_XRGB1555,
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010061 DRM_FORMAT_XRGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070062};
63
64/* Primary plane formats for gen >= 4 */
Damien Lespiau568db4f2015-05-12 16:13:18 +010065static const uint32_t i965_primary_formats[] = {
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010066 DRM_FORMAT_C8,
67 DRM_FORMAT_RGB565,
68 DRM_FORMAT_XRGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070069 DRM_FORMAT_XBGR8888,
Damien Lespiau6c0fd452015-05-19 12:29:16 +010070 DRM_FORMAT_XRGB2101010,
71 DRM_FORMAT_XBGR2101010,
72};
73
74static const uint32_t skl_primary_formats[] = {
75 DRM_FORMAT_C8,
76 DRM_FORMAT_RGB565,
77 DRM_FORMAT_XRGB8888,
78 DRM_FORMAT_XBGR8888,
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010079 DRM_FORMAT_ARGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070080 DRM_FORMAT_ABGR8888,
81 DRM_FORMAT_XRGB2101010,
Matt Roper465c1202014-05-29 08:06:54 -070082 DRM_FORMAT_XBGR2101010,
Kumar, Maheshea916ea2015-09-03 16:17:09 +053083 DRM_FORMAT_YUYV,
84 DRM_FORMAT_YVYU,
85 DRM_FORMAT_UYVY,
86 DRM_FORMAT_VYUY,
Matt Roper465c1202014-05-29 08:06:54 -070087};
88
Matt Roper3d7d6512014-06-10 08:28:13 -070089/* Cursor formats */
90static const uint32_t intel_cursor_formats[] = {
91 DRM_FORMAT_ARGB8888,
92};
93
Jesse Barnesf1f644d2013-06-27 00:39:25 +030094static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020095 struct intel_crtc_state *pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +030096static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020097 struct intel_crtc_state *pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030098
Jesse Barneseb1bfe82014-02-12 12:26:25 -080099static int intel_framebuffer_init(struct drm_device *dev,
100 struct intel_framebuffer *ifb,
101 struct drm_mode_fb_cmd2 *mode_cmd,
102 struct drm_i915_gem_object *obj);
Daniel Vetter5b18e572014-04-24 23:55:06 +0200103static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
104static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +0200105static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +0200106static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -0700107 struct intel_link_m_n *m_n,
108 struct intel_link_m_n *m2_n2);
Daniel Vetter29407aa2014-04-24 23:55:08 +0200109static void ironlake_set_pipeconf(struct drm_crtc *crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +0200110static void haswell_set_pipeconf(struct drm_crtc *crtc);
Jani Nikula391bf042016-03-18 17:05:40 +0200111static void haswell_set_pipemisc(struct drm_crtc *crtc);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200112static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200113 const struct intel_crtc_state *pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200114static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200115 const struct intel_crtc_state *pipe_config);
Daniel Vetter5a21b662016-05-24 17:13:53 +0200116static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
117static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
Ville Syrjälä65edccc2016-10-31 22:37:01 +0200118static void skl_init_scalers(struct drm_i915_private *dev_priv,
119 struct intel_crtc *crtc,
120 struct intel_crtc_state *crtc_state);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +0200121static void skylake_pfit_enable(struct intel_crtc *crtc);
122static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
123static void ironlake_pfit_enable(struct intel_crtc *crtc);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +0200124static void intel_modeset_setup_hw_state(struct drm_device *dev);
Ville Syrjälä2622a082016-03-09 19:07:26 +0200125static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
Ville Syrjälä4e5ca602016-05-11 22:44:44 +0300126static int ilk_max_pixel_rate(struct drm_atomic_state *state);
Imre Deak324513c2016-06-13 16:44:36 +0300127static int bxt_calc_cdclk(int max_pixclk);
Damien Lespiaue7457a92013-08-08 22:28:59 +0100128
Ma Lingd4906092009-03-18 20:13:27 +0800129struct intel_limit {
Ander Conselvan de Oliveira4c5def92016-05-04 12:11:58 +0300130 struct {
131 int min, max;
132 } dot, vco, n, m, m1, m2, p, p1;
133
134 struct {
135 int dot_limit;
136 int p2_slow, p2_fast;
137 } p2;
Ma Lingd4906092009-03-18 20:13:27 +0800138};
Jesse Barnes79e53942008-11-07 14:24:08 -0800139
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300140/* returns HPLL frequency in kHz */
141static int valleyview_get_vco(struct drm_i915_private *dev_priv)
142{
143 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
144
145 /* Obtain SKU information */
146 mutex_lock(&dev_priv->sb_lock);
147 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
148 CCK_FUSE_HPLL_FREQ_MASK;
149 mutex_unlock(&dev_priv->sb_lock);
150
151 return vco_freq[hpll_freq] * 1000;
152}
153
Ville Syrjäläc30fec62016-03-04 21:43:02 +0200154int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
155 const char *name, u32 reg, int ref_freq)
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300156{
157 u32 val;
158 int divider;
159
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300160 mutex_lock(&dev_priv->sb_lock);
161 val = vlv_cck_read(dev_priv, reg);
162 mutex_unlock(&dev_priv->sb_lock);
163
164 divider = val & CCK_FREQUENCY_VALUES;
165
166 WARN((val & CCK_FREQUENCY_STATUS) !=
167 (divider << CCK_FREQUENCY_STATUS_SHIFT),
168 "%s change in progress\n", name);
169
Ville Syrjäläc30fec62016-03-04 21:43:02 +0200170 return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
171}
172
173static int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
174 const char *name, u32 reg)
175{
176 if (dev_priv->hpll_freq == 0)
177 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
178
179 return vlv_get_cck_clock(dev_priv, name, reg,
180 dev_priv->hpll_freq);
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300181}
182
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200183static int
184intel_pch_rawclk(struct drm_i915_private *dev_priv)
Daniel Vetterd2acd212012-10-20 20:57:43 +0200185{
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200186 return (I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK) * 1000;
Daniel Vetterd2acd212012-10-20 20:57:43 +0200187}
188
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200189static int
190intel_vlv_hrawclk(struct drm_i915_private *dev_priv)
Jani Nikula79e50a42015-08-26 10:58:20 +0300191{
Ville Syrjälä19ab4ed2016-04-27 17:43:22 +0300192 /* RAWCLK_FREQ_VLV register updated from power well code */
Ville Syrjälä35d38d12016-03-02 17:22:16 +0200193 return vlv_get_cck_clock_hpll(dev_priv, "hrawclk",
194 CCK_DISPLAY_REF_CLOCK_CONTROL);
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200195}
196
197static int
198intel_g4x_hrawclk(struct drm_i915_private *dev_priv)
199{
Jani Nikula79e50a42015-08-26 10:58:20 +0300200 uint32_t clkcfg;
201
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200202 /* hrawclock is 1/4 the FSB frequency */
Jani Nikula79e50a42015-08-26 10:58:20 +0300203 clkcfg = I915_READ(CLKCFG);
204 switch (clkcfg & CLKCFG_FSB_MASK) {
205 case CLKCFG_FSB_400:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200206 return 100000;
Jani Nikula79e50a42015-08-26 10:58:20 +0300207 case CLKCFG_FSB_533:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200208 return 133333;
Jani Nikula79e50a42015-08-26 10:58:20 +0300209 case CLKCFG_FSB_667:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200210 return 166667;
Jani Nikula79e50a42015-08-26 10:58:20 +0300211 case CLKCFG_FSB_800:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200212 return 200000;
Jani Nikula79e50a42015-08-26 10:58:20 +0300213 case CLKCFG_FSB_1067:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200214 return 266667;
Jani Nikula79e50a42015-08-26 10:58:20 +0300215 case CLKCFG_FSB_1333:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200216 return 333333;
Jani Nikula79e50a42015-08-26 10:58:20 +0300217 /* these two are just a guess; one of them might be right */
218 case CLKCFG_FSB_1600:
219 case CLKCFG_FSB_1600_ALT:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200220 return 400000;
Jani Nikula79e50a42015-08-26 10:58:20 +0300221 default:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200222 return 133333;
Jani Nikula79e50a42015-08-26 10:58:20 +0300223 }
224}
225
Ville Syrjälä19ab4ed2016-04-27 17:43:22 +0300226void intel_update_rawclk(struct drm_i915_private *dev_priv)
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200227{
228 if (HAS_PCH_SPLIT(dev_priv))
229 dev_priv->rawclk_freq = intel_pch_rawclk(dev_priv);
230 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
231 dev_priv->rawclk_freq = intel_vlv_hrawclk(dev_priv);
232 else if (IS_G4X(dev_priv) || IS_PINEVIEW(dev_priv))
233 dev_priv->rawclk_freq = intel_g4x_hrawclk(dev_priv);
234 else
235 return; /* no rawclk on other platforms, or no need to know it */
236
237 DRM_DEBUG_DRIVER("rawclk rate: %d kHz\n", dev_priv->rawclk_freq);
238}
239
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300240static void intel_update_czclk(struct drm_i915_private *dev_priv)
241{
Wayne Boyer666a4532015-12-09 12:29:35 -0800242 if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300243 return;
244
245 dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
246 CCK_CZ_CLOCK_CONTROL);
247
248 DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
249}
250
Chris Wilson021357a2010-09-07 20:54:59 +0100251static inline u32 /* units of 100MHz */
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200252intel_fdi_link_freq(struct drm_i915_private *dev_priv,
253 const struct intel_crtc_state *pipe_config)
Chris Wilson021357a2010-09-07 20:54:59 +0100254{
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200255 if (HAS_DDI(dev_priv))
256 return pipe_config->port_clock; /* SPLL */
257 else if (IS_GEN5(dev_priv))
258 return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
Ville Syrjäläe3b247d2016-02-17 21:41:09 +0200259 else
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200260 return 270000;
Chris Wilson021357a2010-09-07 20:54:59 +0100261}
262
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300263static const struct intel_limit intel_limits_i8xx_dac = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400264 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200265 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200266 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400267 .m = { .min = 96, .max = 140 },
268 .m1 = { .min = 18, .max = 26 },
269 .m2 = { .min = 6, .max = 16 },
270 .p = { .min = 4, .max = 128 },
271 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700272 .p2 = { .dot_limit = 165000,
273 .p2_slow = 4, .p2_fast = 2 },
Keith Packarde4b36692009-06-05 19:22:17 -0700274};
275
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300276static const struct intel_limit intel_limits_i8xx_dvo = {
Daniel Vetter5d536e22013-07-06 12:52:06 +0200277 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200278 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200279 .n = { .min = 2, .max = 16 },
Daniel Vetter5d536e22013-07-06 12:52:06 +0200280 .m = { .min = 96, .max = 140 },
281 .m1 = { .min = 18, .max = 26 },
282 .m2 = { .min = 6, .max = 16 },
283 .p = { .min = 4, .max = 128 },
284 .p1 = { .min = 2, .max = 33 },
285 .p2 = { .dot_limit = 165000,
286 .p2_slow = 4, .p2_fast = 4 },
287};
288
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300289static const struct intel_limit intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400290 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200291 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200292 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400293 .m = { .min = 96, .max = 140 },
294 .m1 = { .min = 18, .max = 26 },
295 .m2 = { .min = 6, .max = 16 },
296 .p = { .min = 4, .max = 128 },
297 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700298 .p2 = { .dot_limit = 165000,
299 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700300};
Eric Anholt273e27c2011-03-30 13:01:10 -0700301
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300302static const struct intel_limit intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400303 .dot = { .min = 20000, .max = 400000 },
304 .vco = { .min = 1400000, .max = 2800000 },
305 .n = { .min = 1, .max = 6 },
306 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100307 .m1 = { .min = 8, .max = 18 },
308 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400309 .p = { .min = 5, .max = 80 },
310 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700311 .p2 = { .dot_limit = 200000,
312 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700313};
314
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300315static const struct intel_limit intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400316 .dot = { .min = 20000, .max = 400000 },
317 .vco = { .min = 1400000, .max = 2800000 },
318 .n = { .min = 1, .max = 6 },
319 .m = { .min = 70, .max = 120 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100320 .m1 = { .min = 8, .max = 18 },
321 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400322 .p = { .min = 7, .max = 98 },
323 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700324 .p2 = { .dot_limit = 112000,
325 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700326};
327
Eric Anholt273e27c2011-03-30 13:01:10 -0700328
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300329static const struct intel_limit intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700330 .dot = { .min = 25000, .max = 270000 },
331 .vco = { .min = 1750000, .max = 3500000},
332 .n = { .min = 1, .max = 4 },
333 .m = { .min = 104, .max = 138 },
334 .m1 = { .min = 17, .max = 23 },
335 .m2 = { .min = 5, .max = 11 },
336 .p = { .min = 10, .max = 30 },
337 .p1 = { .min = 1, .max = 3},
338 .p2 = { .dot_limit = 270000,
339 .p2_slow = 10,
340 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800341 },
Keith Packarde4b36692009-06-05 19:22:17 -0700342};
343
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300344static const struct intel_limit intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700345 .dot = { .min = 22000, .max = 400000 },
346 .vco = { .min = 1750000, .max = 3500000},
347 .n = { .min = 1, .max = 4 },
348 .m = { .min = 104, .max = 138 },
349 .m1 = { .min = 16, .max = 23 },
350 .m2 = { .min = 5, .max = 11 },
351 .p = { .min = 5, .max = 80 },
352 .p1 = { .min = 1, .max = 8},
353 .p2 = { .dot_limit = 165000,
354 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700355};
356
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300357static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700358 .dot = { .min = 20000, .max = 115000 },
359 .vco = { .min = 1750000, .max = 3500000 },
360 .n = { .min = 1, .max = 3 },
361 .m = { .min = 104, .max = 138 },
362 .m1 = { .min = 17, .max = 23 },
363 .m2 = { .min = 5, .max = 11 },
364 .p = { .min = 28, .max = 112 },
365 .p1 = { .min = 2, .max = 8 },
366 .p2 = { .dot_limit = 0,
367 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800368 },
Keith Packarde4b36692009-06-05 19:22:17 -0700369};
370
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300371static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700372 .dot = { .min = 80000, .max = 224000 },
373 .vco = { .min = 1750000, .max = 3500000 },
374 .n = { .min = 1, .max = 3 },
375 .m = { .min = 104, .max = 138 },
376 .m1 = { .min = 17, .max = 23 },
377 .m2 = { .min = 5, .max = 11 },
378 .p = { .min = 14, .max = 42 },
379 .p1 = { .min = 2, .max = 6 },
380 .p2 = { .dot_limit = 0,
381 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800382 },
Keith Packarde4b36692009-06-05 19:22:17 -0700383};
384
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300385static const struct intel_limit intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400386 .dot = { .min = 20000, .max = 400000},
387 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700388 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400389 .n = { .min = 3, .max = 6 },
390 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700391 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400392 .m1 = { .min = 0, .max = 0 },
393 .m2 = { .min = 0, .max = 254 },
394 .p = { .min = 5, .max = 80 },
395 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700396 .p2 = { .dot_limit = 200000,
397 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700398};
399
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300400static const struct intel_limit intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400401 .dot = { .min = 20000, .max = 400000 },
402 .vco = { .min = 1700000, .max = 3500000 },
403 .n = { .min = 3, .max = 6 },
404 .m = { .min = 2, .max = 256 },
405 .m1 = { .min = 0, .max = 0 },
406 .m2 = { .min = 0, .max = 254 },
407 .p = { .min = 7, .max = 112 },
408 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700409 .p2 = { .dot_limit = 112000,
410 .p2_slow = 14, .p2_fast = 14 },
Keith Packarde4b36692009-06-05 19:22:17 -0700411};
412
Eric Anholt273e27c2011-03-30 13:01:10 -0700413/* Ironlake / Sandybridge
414 *
415 * We calculate clock using (register_value + 2) for N/M1/M2, so here
416 * the range value for them is (actual_value - 2).
417 */
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300418static const struct intel_limit intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700419 .dot = { .min = 25000, .max = 350000 },
420 .vco = { .min = 1760000, .max = 3510000 },
421 .n = { .min = 1, .max = 5 },
422 .m = { .min = 79, .max = 127 },
423 .m1 = { .min = 12, .max = 22 },
424 .m2 = { .min = 5, .max = 9 },
425 .p = { .min = 5, .max = 80 },
426 .p1 = { .min = 1, .max = 8 },
427 .p2 = { .dot_limit = 225000,
428 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700429};
430
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300431static const struct intel_limit intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700432 .dot = { .min = 25000, .max = 350000 },
433 .vco = { .min = 1760000, .max = 3510000 },
434 .n = { .min = 1, .max = 3 },
435 .m = { .min = 79, .max = 118 },
436 .m1 = { .min = 12, .max = 22 },
437 .m2 = { .min = 5, .max = 9 },
438 .p = { .min = 28, .max = 112 },
439 .p1 = { .min = 2, .max = 8 },
440 .p2 = { .dot_limit = 225000,
441 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800442};
443
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300444static const struct intel_limit intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700445 .dot = { .min = 25000, .max = 350000 },
446 .vco = { .min = 1760000, .max = 3510000 },
447 .n = { .min = 1, .max = 3 },
448 .m = { .min = 79, .max = 127 },
449 .m1 = { .min = 12, .max = 22 },
450 .m2 = { .min = 5, .max = 9 },
451 .p = { .min = 14, .max = 56 },
452 .p1 = { .min = 2, .max = 8 },
453 .p2 = { .dot_limit = 225000,
454 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800455};
456
Eric Anholt273e27c2011-03-30 13:01:10 -0700457/* LVDS 100mhz refclk limits. */
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300458static const struct intel_limit intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700459 .dot = { .min = 25000, .max = 350000 },
460 .vco = { .min = 1760000, .max = 3510000 },
461 .n = { .min = 1, .max = 2 },
462 .m = { .min = 79, .max = 126 },
463 .m1 = { .min = 12, .max = 22 },
464 .m2 = { .min = 5, .max = 9 },
465 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400466 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700467 .p2 = { .dot_limit = 225000,
468 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800469};
470
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300471static const struct intel_limit intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700472 .dot = { .min = 25000, .max = 350000 },
473 .vco = { .min = 1760000, .max = 3510000 },
474 .n = { .min = 1, .max = 3 },
475 .m = { .min = 79, .max = 126 },
476 .m1 = { .min = 12, .max = 22 },
477 .m2 = { .min = 5, .max = 9 },
478 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400479 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700480 .p2 = { .dot_limit = 225000,
481 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800482};
483
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300484static const struct intel_limit intel_limits_vlv = {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300485 /*
486 * These are the data rate limits (measured in fast clocks)
487 * since those are the strictest limits we have. The fast
488 * clock and actual rate limits are more relaxed, so checking
489 * them would make no difference.
490 */
491 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200492 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700493 .n = { .min = 1, .max = 7 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700494 .m1 = { .min = 2, .max = 3 },
495 .m2 = { .min = 11, .max = 156 },
Ville Syrjäläb99ab662013-09-24 21:26:26 +0300496 .p1 = { .min = 2, .max = 3 },
Ville Syrjälä5fdc9c492013-09-24 21:26:29 +0300497 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700498};
499
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300500static const struct intel_limit intel_limits_chv = {
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300501 /*
502 * These are the data rate limits (measured in fast clocks)
503 * since those are the strictest limits we have. The fast
504 * clock and actual rate limits are more relaxed, so checking
505 * them would make no difference.
506 */
507 .dot = { .min = 25000 * 5, .max = 540000 * 5},
Ville Syrjälä17fe1022015-02-26 21:01:52 +0200508 .vco = { .min = 4800000, .max = 6480000 },
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300509 .n = { .min = 1, .max = 1 },
510 .m1 = { .min = 2, .max = 2 },
511 .m2 = { .min = 24 << 22, .max = 175 << 22 },
512 .p1 = { .min = 2, .max = 4 },
513 .p2 = { .p2_slow = 1, .p2_fast = 14 },
514};
515
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300516static const struct intel_limit intel_limits_bxt = {
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200517 /* FIXME: find real dot limits */
518 .dot = { .min = 0, .max = INT_MAX },
Vandana Kannane6292552015-07-01 17:02:57 +0530519 .vco = { .min = 4800000, .max = 6700000 },
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200520 .n = { .min = 1, .max = 1 },
521 .m1 = { .min = 2, .max = 2 },
522 /* FIXME: find real m2 limits */
523 .m2 = { .min = 2 << 22, .max = 255 << 22 },
524 .p1 = { .min = 2, .max = 4 },
525 .p2 = { .p2_slow = 1, .p2_fast = 20 },
526};
527
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200528static bool
529needs_modeset(struct drm_crtc_state *state)
530{
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200531 return drm_atomic_crtc_needs_modeset(state);
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200532}
533
Imre Deakdccbea32015-06-22 23:35:51 +0300534/*
535 * Platform specific helpers to calculate the port PLL loopback- (clock.m),
536 * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
537 * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
538 * The helpers' return value is the rate of the clock that is fed to the
539 * display engine's pipe which can be the above fast dot clock rate or a
540 * divided-down version of it.
541 */
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500542/* m1 is reserved as 0 in Pineview, n is a ring counter */
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300543static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800544{
Shaohua Li21778322009-02-23 15:19:16 +0800545 clock->m = clock->m2 + 2;
546 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200547 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300548 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300549 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
550 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300551
552 return clock->dot;
Shaohua Li21778322009-02-23 15:19:16 +0800553}
554
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200555static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
556{
557 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
558}
559
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300560static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800561{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200562 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800563 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200564 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300565 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300566 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
567 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300568
569 return clock->dot;
Jesse Barnes79e53942008-11-07 14:24:08 -0800570}
571
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300572static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
Imre Deak589eca62015-06-22 23:35:50 +0300573{
574 clock->m = clock->m1 * clock->m2;
575 clock->p = clock->p1 * clock->p2;
576 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300577 return 0;
Imre Deak589eca62015-06-22 23:35:50 +0300578 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
579 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300580
581 return clock->dot / 5;
Imre Deak589eca62015-06-22 23:35:50 +0300582}
583
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300584int chv_calc_dpll_params(int refclk, struct dpll *clock)
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300585{
586 clock->m = clock->m1 * clock->m2;
587 clock->p = clock->p1 * clock->p2;
588 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300589 return 0;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300590 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
591 clock->n << 22);
592 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300593
594 return clock->dot / 5;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300595}
596
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800597#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800598/**
599 * Returns whether the given set of divisors are valid for a given refclk with
600 * the given connectors.
601 */
602
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100603static bool intel_PLL_is_valid(struct drm_i915_private *dev_priv,
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300604 const struct intel_limit *limit,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300605 const struct dpll *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800606{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300607 if (clock->n < limit->n.min || limit->n.max < clock->n)
608 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800609 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400610 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800611 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400612 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800613 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400614 INTELPllInvalid("m1 out of range\n");
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300615
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100616 if (!IS_PINEVIEW(dev_priv) && !IS_VALLEYVIEW(dev_priv) &&
617 !IS_CHERRYVIEW(dev_priv) && !IS_BROXTON(dev_priv))
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300618 if (clock->m1 <= clock->m2)
619 INTELPllInvalid("m1 <= m2\n");
620
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100621 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
622 !IS_BROXTON(dev_priv)) {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300623 if (clock->p < limit->p.min || limit->p.max < clock->p)
624 INTELPllInvalid("p out of range\n");
625 if (clock->m < limit->m.min || limit->m.max < clock->m)
626 INTELPllInvalid("m out of range\n");
627 }
628
Jesse Barnes79e53942008-11-07 14:24:08 -0800629 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400630 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800631 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
632 * connector, etc., rather than just a single range.
633 */
634 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400635 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800636
637 return true;
638}
639
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300640static int
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300641i9xx_select_p2_div(const struct intel_limit *limit,
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300642 const struct intel_crtc_state *crtc_state,
643 int target)
Jesse Barnes79e53942008-11-07 14:24:08 -0800644{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300645 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800646
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +0300647 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800648 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100649 * For LVDS just rely on its current settings for dual-channel.
650 * We haven't figured out how to reliably set up different
651 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800652 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100653 if (intel_is_dual_link_lvds(dev))
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300654 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800655 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300656 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800657 } else {
658 if (target < limit->p2.dot_limit)
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300659 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800660 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300661 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800662 }
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300663}
664
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200665/*
666 * Returns a set of divisors for the desired target clock with the given
667 * refclk, or FALSE. The returned values represent the clock equation:
668 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
669 *
670 * Target and reference clocks are specified in kHz.
671 *
672 * If match_clock is provided, then best_clock P divider must match the P
673 * divider from @match_clock used for LVDS downclocking.
674 */
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300675static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300676i9xx_find_best_dpll(const struct intel_limit *limit,
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300677 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300678 int target, int refclk, struct dpll *match_clock,
679 struct dpll *best_clock)
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300680{
681 struct drm_device *dev = crtc_state->base.crtc->dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300682 struct dpll clock;
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300683 int err = target;
Jesse Barnes79e53942008-11-07 14:24:08 -0800684
Akshay Joshi0206e352011-08-16 15:34:10 -0400685 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800686
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300687 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
688
Zhao Yakui42158662009-11-20 11:24:18 +0800689 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
690 clock.m1++) {
691 for (clock.m2 = limit->m2.min;
692 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200693 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800694 break;
695 for (clock.n = limit->n.min;
696 clock.n <= limit->n.max; clock.n++) {
697 for (clock.p1 = limit->p1.min;
698 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800699 int this_err;
700
Imre Deakdccbea32015-06-22 23:35:51 +0300701 i9xx_calc_dpll_params(refclk, &clock);
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100702 if (!intel_PLL_is_valid(to_i915(dev),
703 limit,
Chris Wilson1b894b52010-12-14 20:04:54 +0000704 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800705 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800706 if (match_clock &&
707 clock.p != match_clock->p)
708 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800709
710 this_err = abs(clock.dot - target);
711 if (this_err < err) {
712 *best_clock = clock;
713 err = this_err;
714 }
715 }
716 }
717 }
718 }
719
720 return (err != target);
721}
722
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200723/*
724 * Returns a set of divisors for the desired target clock with the given
725 * refclk, or FALSE. The returned values represent the clock equation:
726 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
727 *
728 * Target and reference clocks are specified in kHz.
729 *
730 * If match_clock is provided, then best_clock P divider must match the P
731 * divider from @match_clock used for LVDS downclocking.
732 */
Ma Lingd4906092009-03-18 20:13:27 +0800733static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300734pnv_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200735 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300736 int target, int refclk, struct dpll *match_clock,
737 struct dpll *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200738{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300739 struct drm_device *dev = crtc_state->base.crtc->dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300740 struct dpll clock;
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200741 int err = target;
742
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200743 memset(best_clock, 0, sizeof(*best_clock));
744
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300745 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
746
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200747 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
748 clock.m1++) {
749 for (clock.m2 = limit->m2.min;
750 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200751 for (clock.n = limit->n.min;
752 clock.n <= limit->n.max; clock.n++) {
753 for (clock.p1 = limit->p1.min;
754 clock.p1 <= limit->p1.max; clock.p1++) {
755 int this_err;
756
Imre Deakdccbea32015-06-22 23:35:51 +0300757 pnv_calc_dpll_params(refclk, &clock);
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100758 if (!intel_PLL_is_valid(to_i915(dev),
759 limit,
Jesse Barnes79e53942008-11-07 14:24:08 -0800760 &clock))
761 continue;
762 if (match_clock &&
763 clock.p != match_clock->p)
764 continue;
765
766 this_err = abs(clock.dot - target);
767 if (this_err < err) {
768 *best_clock = clock;
769 err = this_err;
770 }
771 }
772 }
773 }
774 }
775
776 return (err != target);
777}
778
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +0200779/*
780 * Returns a set of divisors for the desired target clock with the given
781 * refclk, or FALSE. The returned values represent the clock equation:
782 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200783 *
784 * Target and reference clocks are specified in kHz.
785 *
786 * If match_clock is provided, then best_clock P divider must match the P
787 * divider from @match_clock used for LVDS downclocking.
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +0200788 */
Ma Lingd4906092009-03-18 20:13:27 +0800789static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300790g4x_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200791 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300792 int target, int refclk, struct dpll *match_clock,
793 struct dpll *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800794{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300795 struct drm_device *dev = crtc_state->base.crtc->dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300796 struct dpll clock;
Ma Lingd4906092009-03-18 20:13:27 +0800797 int max_n;
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300798 bool found = false;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400799 /* approximately equals target * 0.00585 */
800 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800801
802 memset(best_clock, 0, sizeof(*best_clock));
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300803
804 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
805
Ma Lingd4906092009-03-18 20:13:27 +0800806 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200807 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800808 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200809 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800810 for (clock.m1 = limit->m1.max;
811 clock.m1 >= limit->m1.min; clock.m1--) {
812 for (clock.m2 = limit->m2.max;
813 clock.m2 >= limit->m2.min; clock.m2--) {
814 for (clock.p1 = limit->p1.max;
815 clock.p1 >= limit->p1.min; clock.p1--) {
816 int this_err;
817
Imre Deakdccbea32015-06-22 23:35:51 +0300818 i9xx_calc_dpll_params(refclk, &clock);
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100819 if (!intel_PLL_is_valid(to_i915(dev),
820 limit,
Chris Wilson1b894b52010-12-14 20:04:54 +0000821 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800822 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000823
824 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800825 if (this_err < err_most) {
826 *best_clock = clock;
827 err_most = this_err;
828 max_n = clock.n;
829 found = true;
830 }
831 }
832 }
833 }
834 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800835 return found;
836}
Ma Lingd4906092009-03-18 20:13:27 +0800837
Imre Deakd5dd62b2015-03-17 11:40:03 +0200838/*
839 * Check if the calculated PLL configuration is more optimal compared to the
840 * best configuration and error found so far. Return the calculated error.
841 */
842static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300843 const struct dpll *calculated_clock,
844 const struct dpll *best_clock,
Imre Deakd5dd62b2015-03-17 11:40:03 +0200845 unsigned int best_error_ppm,
846 unsigned int *error_ppm)
847{
Imre Deak9ca3ba02015-03-17 11:40:05 +0200848 /*
849 * For CHV ignore the error and consider only the P value.
850 * Prefer a bigger P value based on HW requirements.
851 */
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +0100852 if (IS_CHERRYVIEW(to_i915(dev))) {
Imre Deak9ca3ba02015-03-17 11:40:05 +0200853 *error_ppm = 0;
854
855 return calculated_clock->p > best_clock->p;
856 }
857
Imre Deak24be4e42015-03-17 11:40:04 +0200858 if (WARN_ON_ONCE(!target_freq))
859 return false;
860
Imre Deakd5dd62b2015-03-17 11:40:03 +0200861 *error_ppm = div_u64(1000000ULL *
862 abs(target_freq - calculated_clock->dot),
863 target_freq);
864 /*
865 * Prefer a better P value over a better (smaller) error if the error
866 * is small. Ensure this preference for future configurations too by
867 * setting the error to 0.
868 */
869 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
870 *error_ppm = 0;
871
872 return true;
873 }
874
875 return *error_ppm + 10 < best_error_ppm;
876}
877
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200878/*
879 * Returns a set of divisors for the desired target clock with the given
880 * refclk, or FALSE. The returned values represent the clock equation:
881 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
882 */
Zhenyu Wang2c072452009-06-05 15:38:42 +0800883static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300884vlv_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200885 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300886 int target, int refclk, struct dpll *match_clock,
887 struct dpll *best_clock)
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700888{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200889 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300890 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300891 struct dpll clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300892 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300893 /* min update 19.2 MHz */
894 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300895 bool found = false;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700896
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300897 target *= 5; /* fast clock */
898
899 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700900
901 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300902 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300903 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300904 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300905 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300906 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700907 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300908 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Imre Deakd5dd62b2015-03-17 11:40:03 +0200909 unsigned int ppm;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300910
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300911 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
912 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300913
Imre Deakdccbea32015-06-22 23:35:51 +0300914 vlv_calc_dpll_params(refclk, &clock);
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300915
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100916 if (!intel_PLL_is_valid(to_i915(dev),
917 limit,
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300918 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300919 continue;
920
Imre Deakd5dd62b2015-03-17 11:40:03 +0200921 if (!vlv_PLL_is_optimal(dev, target,
922 &clock,
923 best_clock,
924 bestppm, &ppm))
925 continue;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300926
Imre Deakd5dd62b2015-03-17 11:40:03 +0200927 *best_clock = clock;
928 bestppm = ppm;
929 found = true;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700930 }
931 }
932 }
933 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700934
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300935 return found;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700936}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700937
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200938/*
939 * Returns a set of divisors for the desired target clock with the given
940 * refclk, or FALSE. The returned values represent the clock equation:
941 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
942 */
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300943static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300944chv_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200945 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300946 int target, int refclk, struct dpll *match_clock,
947 struct dpll *best_clock)
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300948{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200949 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300950 struct drm_device *dev = crtc->base.dev;
Imre Deak9ca3ba02015-03-17 11:40:05 +0200951 unsigned int best_error_ppm;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300952 struct dpll clock;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300953 uint64_t m2;
954 int found = false;
955
956 memset(best_clock, 0, sizeof(*best_clock));
Imre Deak9ca3ba02015-03-17 11:40:05 +0200957 best_error_ppm = 1000000;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300958
959 /*
960 * Based on hardware doc, the n always set to 1, and m1 always
961 * set to 2. If requires to support 200Mhz refclk, we need to
962 * revisit this because n may not 1 anymore.
963 */
964 clock.n = 1, clock.m1 = 2;
965 target *= 5; /* fast clock */
966
967 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
968 for (clock.p2 = limit->p2.p2_fast;
969 clock.p2 >= limit->p2.p2_slow;
970 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Imre Deak9ca3ba02015-03-17 11:40:05 +0200971 unsigned int error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300972
973 clock.p = clock.p1 * clock.p2;
974
975 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
976 clock.n) << 22, refclk * clock.m1);
977
978 if (m2 > INT_MAX/clock.m1)
979 continue;
980
981 clock.m2 = m2;
982
Imre Deakdccbea32015-06-22 23:35:51 +0300983 chv_calc_dpll_params(refclk, &clock);
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300984
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100985 if (!intel_PLL_is_valid(to_i915(dev), limit, &clock))
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300986 continue;
987
Imre Deak9ca3ba02015-03-17 11:40:05 +0200988 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
989 best_error_ppm, &error_ppm))
990 continue;
991
992 *best_clock = clock;
993 best_error_ppm = error_ppm;
994 found = true;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300995 }
996 }
997
998 return found;
999}
1000
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001001bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03001002 struct dpll *best_clock)
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001003{
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02001004 int refclk = 100000;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03001005 const struct intel_limit *limit = &intel_limits_bxt;
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001006
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02001007 return chv_find_best_dpll(limit, crtc_state,
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001008 target_clock, refclk, NULL, best_clock);
1009}
1010
Ville Syrjälä525b9312016-10-31 22:37:02 +02001011bool intel_crtc_active(struct intel_crtc *crtc)
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001012{
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001013 /* Be paranoid as we can arrive here with only partial
1014 * state retrieved from the hardware during setup.
1015 *
Damien Lespiau241bfc32013-09-25 16:45:37 +01001016 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001017 * as Haswell has gained clock readout/fastboot support.
1018 *
Dave Airlie66e514c2014-04-03 07:51:54 +10001019 * We can ditch the crtc->primary->fb check as soon as we can
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001020 * properly reconstruct framebuffers.
Matt Roperc3d1f432015-03-09 10:19:23 -07001021 *
1022 * FIXME: The intel_crtc->active here should be switched to
1023 * crtc->state->active once we have proper CRTC states wired up
1024 * for atomic.
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001025 */
Ville Syrjälä525b9312016-10-31 22:37:02 +02001026 return crtc->active && crtc->base.primary->state->fb &&
1027 crtc->config->base.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001028}
1029
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001030enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1031 enum pipe pipe)
1032{
Ville Syrjälä98187832016-10-31 22:37:10 +02001033 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001034
Ville Syrjäläe2af48c2016-10-31 22:37:05 +02001035 return crtc->config->cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001036}
1037
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001038static bool pipe_dsl_stopped(struct drm_i915_private *dev_priv, enum pipe pipe)
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001039{
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001040 i915_reg_t reg = PIPEDSL(pipe);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001041 u32 line1, line2;
1042 u32 line_mask;
1043
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01001044 if (IS_GEN2(dev_priv))
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001045 line_mask = DSL_LINEMASK_GEN2;
1046 else
1047 line_mask = DSL_LINEMASK_GEN3;
1048
1049 line1 = I915_READ(reg) & line_mask;
Daniel Vetter6adfb1e2015-07-07 09:10:40 +02001050 msleep(5);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001051 line2 = I915_READ(reg) & line_mask;
1052
1053 return line1 == line2;
1054}
1055
Keith Packardab7ad7f2010-10-03 00:33:06 -07001056/*
1057 * intel_wait_for_pipe_off - wait for pipe to turn off
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001058 * @crtc: crtc whose pipe to wait for
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001059 *
1060 * After disabling a pipe, we can't wait for vblank in the usual way,
1061 * spinning on the vblank interrupt status bit, since we won't actually
1062 * see an interrupt when the pipe is disabled.
1063 *
Keith Packardab7ad7f2010-10-03 00:33:06 -07001064 * On Gen4 and above:
1065 * wait for the pipe register state bit to turn off
1066 *
1067 * Otherwise:
1068 * wait for the display line value to settle (it usually
1069 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +01001070 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001071 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001072static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001073{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001074 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001075 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001076 enum pipe pipe = crtc->pipe;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001077
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001078 if (INTEL_GEN(dev_priv) >= 4) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001079 i915_reg_t reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001080
Keith Packardab7ad7f2010-10-03 00:33:06 -07001081 /* Wait for the Pipe State to go off */
Chris Wilsonb8511f52016-06-30 15:32:53 +01001082 if (intel_wait_for_register(dev_priv,
1083 reg, I965_PIPECONF_ACTIVE, 0,
1084 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001085 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001086 } else {
Keith Packardab7ad7f2010-10-03 00:33:06 -07001087 /* Wait for the display line to settle */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001088 if (wait_for(pipe_dsl_stopped(dev_priv, pipe), 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001089 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001090 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001091}
1092
Jesse Barnesb24e7172011-01-04 15:09:30 -08001093/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +02001094void assert_pll(struct drm_i915_private *dev_priv,
1095 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001096{
Jesse Barnesb24e7172011-01-04 15:09:30 -08001097 u32 val;
1098 bool cur_state;
1099
Ville Syrjälä649636e2015-09-22 19:50:01 +03001100 val = I915_READ(DPLL(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001101 cur_state = !!(val & DPLL_VCO_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001102 I915_STATE_WARN(cur_state != state,
Jesse Barnesb24e7172011-01-04 15:09:30 -08001103 "PLL state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001104 onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001105}
Jesse Barnesb24e7172011-01-04 15:09:30 -08001106
Jani Nikula23538ef2013-08-27 15:12:22 +03001107/* XXX: the dsi pll is shared between MIPI DSI ports */
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +00001108void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
Jani Nikula23538ef2013-08-27 15:12:22 +03001109{
1110 u32 val;
1111 bool cur_state;
1112
Ville Syrjäläa5805162015-05-26 20:42:30 +03001113 mutex_lock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001114 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
Ville Syrjäläa5805162015-05-26 20:42:30 +03001115 mutex_unlock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001116
1117 cur_state = val & DSI_PLL_VCO_EN;
Rob Clarke2c719b2014-12-15 13:56:32 -05001118 I915_STATE_WARN(cur_state != state,
Jani Nikula23538ef2013-08-27 15:12:22 +03001119 "DSI PLL state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001120 onoff(state), onoff(cur_state));
Jani Nikula23538ef2013-08-27 15:12:22 +03001121}
Jani Nikula23538ef2013-08-27 15:12:22 +03001122
Jesse Barnes040484a2011-01-03 12:14:26 -08001123static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1124 enum pipe pipe, bool state)
1125{
Jesse Barnes040484a2011-01-03 12:14:26 -08001126 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001127 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1128 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001129
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001130 if (HAS_DDI(dev_priv)) {
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001131 /* DDI does not have a specific FDI_TX register */
Ville Syrjälä649636e2015-09-22 19:50:01 +03001132 u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
Paulo Zanoniad80a812012-10-24 16:06:19 -02001133 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001134 } else {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001135 u32 val = I915_READ(FDI_TX_CTL(pipe));
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001136 cur_state = !!(val & FDI_TX_ENABLE);
1137 }
Rob Clarke2c719b2014-12-15 13:56:32 -05001138 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001139 "FDI TX state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001140 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001141}
1142#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1143#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1144
1145static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1146 enum pipe pipe, bool state)
1147{
Jesse Barnes040484a2011-01-03 12:14:26 -08001148 u32 val;
1149 bool cur_state;
1150
Ville Syrjälä649636e2015-09-22 19:50:01 +03001151 val = I915_READ(FDI_RX_CTL(pipe));
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001152 cur_state = !!(val & FDI_RX_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001153 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001154 "FDI RX state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001155 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001156}
1157#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1158#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1159
1160static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1161 enum pipe pipe)
1162{
Jesse Barnes040484a2011-01-03 12:14:26 -08001163 u32 val;
1164
1165 /* ILK FDI PLL is always enabled */
Tvrtko Ursulin7e22dbb2016-05-10 10:57:06 +01001166 if (IS_GEN5(dev_priv))
Jesse Barnes040484a2011-01-03 12:14:26 -08001167 return;
1168
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001169 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001170 if (HAS_DDI(dev_priv))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001171 return;
1172
Ville Syrjälä649636e2015-09-22 19:50:01 +03001173 val = I915_READ(FDI_TX_CTL(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001174 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 -08001175}
1176
Daniel Vetter55607e82013-06-16 21:42:39 +02001177void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1178 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001179{
Jesse Barnes040484a2011-01-03 12:14:26 -08001180 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001181 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001182
Ville Syrjälä649636e2015-09-22 19:50:01 +03001183 val = I915_READ(FDI_RX_CTL(pipe));
Daniel Vetter55607e82013-06-16 21:42:39 +02001184 cur_state = !!(val & FDI_RX_PLL_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001185 I915_STATE_WARN(cur_state != state,
Daniel Vetter55607e82013-06-16 21:42:39 +02001186 "FDI RX PLL assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001187 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001188}
1189
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01001190void assert_panel_unlocked(struct drm_i915_private *dev_priv, enum pipe pipe)
Jesse Barnesea0760c2011-01-04 15:09:32 -08001191{
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001192 i915_reg_t pp_reg;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001193 u32 val;
1194 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001195 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001196
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01001197 if (WARN_ON(HAS_DDI(dev_priv)))
Jani Nikulabedd4db2014-08-22 15:04:13 +03001198 return;
1199
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01001200 if (HAS_PCH_SPLIT(dev_priv)) {
Jani Nikulabedd4db2014-08-22 15:04:13 +03001201 u32 port_sel;
1202
Imre Deak44cb7342016-08-10 14:07:29 +03001203 pp_reg = PP_CONTROL(0);
1204 port_sel = I915_READ(PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001205
1206 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1207 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1208 panel_pipe = PIPE_B;
1209 /* XXX: else fix for eDP */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01001210 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Jani Nikulabedd4db2014-08-22 15:04:13 +03001211 /* presumably write lock depends on pipe, not port select */
Imre Deak44cb7342016-08-10 14:07:29 +03001212 pp_reg = PP_CONTROL(pipe);
Jani Nikulabedd4db2014-08-22 15:04:13 +03001213 panel_pipe = pipe;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001214 } else {
Imre Deak44cb7342016-08-10 14:07:29 +03001215 pp_reg = PP_CONTROL(0);
Jani Nikulabedd4db2014-08-22 15:04:13 +03001216 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1217 panel_pipe = PIPE_B;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001218 }
1219
1220 val = I915_READ(pp_reg);
1221 if (!(val & PANEL_POWER_ON) ||
Jani Nikulaec49ba22014-08-21 15:06:25 +03001222 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
Jesse Barnesea0760c2011-01-04 15:09:32 -08001223 locked = false;
1224
Rob Clarke2c719b2014-12-15 13:56:32 -05001225 I915_STATE_WARN(panel_pipe == pipe && locked,
Jesse Barnesea0760c2011-01-04 15:09:32 -08001226 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001227 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001228}
1229
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001230static void assert_cursor(struct drm_i915_private *dev_priv,
1231 enum pipe pipe, bool state)
1232{
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001233 bool cur_state;
1234
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01001235 if (IS_845G(dev_priv) || IS_I865G(dev_priv))
Ville Syrjälä0b87c242015-09-22 19:47:51 +03001236 cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
Paulo Zanonid9d82082014-02-27 16:30:56 -03001237 else
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03001238 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001239
Rob Clarke2c719b2014-12-15 13:56:32 -05001240 I915_STATE_WARN(cur_state != state,
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001241 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001242 pipe_name(pipe), onoff(state), onoff(cur_state));
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001243}
1244#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1245#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1246
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001247void assert_pipe(struct drm_i915_private *dev_priv,
1248 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001249{
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001250 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001251 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1252 pipe);
Imre Deak4feed0e2016-02-12 18:55:14 +02001253 enum intel_display_power_domain power_domain;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001254
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001255 /* if we need the pipe quirk it must be always on */
1256 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1257 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetter8e636782012-01-22 01:36:48 +01001258 state = true;
1259
Imre Deak4feed0e2016-02-12 18:55:14 +02001260 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
1261 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001262 u32 val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni69310162013-01-29 16:35:19 -02001263 cur_state = !!(val & PIPECONF_ENABLE);
Imre Deak4feed0e2016-02-12 18:55:14 +02001264
1265 intel_display_power_put(dev_priv, power_domain);
1266 } else {
1267 cur_state = false;
Paulo Zanoni69310162013-01-29 16:35:19 -02001268 }
1269
Rob Clarke2c719b2014-12-15 13:56:32 -05001270 I915_STATE_WARN(cur_state != state,
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001271 "pipe %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001272 pipe_name(pipe), onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001273}
1274
Chris Wilson931872f2012-01-16 23:01:13 +00001275static void assert_plane(struct drm_i915_private *dev_priv,
1276 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001277{
Jesse Barnesb24e7172011-01-04 15:09:30 -08001278 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001279 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001280
Ville Syrjälä649636e2015-09-22 19:50:01 +03001281 val = I915_READ(DSPCNTR(plane));
Chris Wilson931872f2012-01-16 23:01:13 +00001282 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001283 I915_STATE_WARN(cur_state != state,
Chris Wilson931872f2012-01-16 23:01:13 +00001284 "plane %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001285 plane_name(plane), onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001286}
1287
Chris Wilson931872f2012-01-16 23:01:13 +00001288#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1289#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1290
Jesse Barnesb24e7172011-01-04 15:09:30 -08001291static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1292 enum pipe pipe)
1293{
Ville Syrjälä649636e2015-09-22 19:50:01 +03001294 int i;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001295
Ville Syrjälä653e1022013-06-04 13:49:05 +03001296 /* Primary planes are fixed to pipes on gen4+ */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001297 if (INTEL_GEN(dev_priv) >= 4) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001298 u32 val = I915_READ(DSPCNTR(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001299 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
Adam Jackson28c057942011-10-07 14:38:42 -04001300 "plane %c assertion failure, should be disabled but not\n",
1301 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001302 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001303 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001304
Jesse Barnesb24e7172011-01-04 15:09:30 -08001305 /* Need to check both planes against the pipe */
Damien Lespiau055e3932014-08-18 13:49:10 +01001306 for_each_pipe(dev_priv, i) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001307 u32 val = I915_READ(DSPCNTR(i));
1308 enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
Jesse Barnesb24e7172011-01-04 15:09:30 -08001309 DISPPLANE_SEL_PIPE_SHIFT;
Rob Clarke2c719b2014-12-15 13:56:32 -05001310 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001311 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1312 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001313 }
1314}
1315
Jesse Barnes19332d72013-03-28 09:55:38 -07001316static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1317 enum pipe pipe)
1318{
Ville Syrjälä649636e2015-09-22 19:50:01 +03001319 int sprite;
Jesse Barnes19332d72013-03-28 09:55:38 -07001320
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001321 if (INTEL_GEN(dev_priv) >= 9) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001322 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001323 u32 val = I915_READ(PLANE_CTL(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001324 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001325 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1326 sprite, pipe_name(pipe));
1327 }
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01001328 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001329 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjälä83c04a62016-11-22 18:02:00 +02001330 u32 val = I915_READ(SPCNTR(pipe, PLANE_SPRITE0 + sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001331 I915_STATE_WARN(val & SP_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001332 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +00001333 sprite_name(pipe, sprite), pipe_name(pipe));
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001334 }
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001335 } else if (INTEL_GEN(dev_priv) >= 7) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001336 u32 val = I915_READ(SPRCTL(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001337 I915_STATE_WARN(val & SPRITE_ENABLE,
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001338 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001339 plane_name(pipe), pipe_name(pipe));
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001340 } else if (INTEL_GEN(dev_priv) >= 5) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001341 u32 val = I915_READ(DVSCNTR(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001342 I915_STATE_WARN(val & DVS_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001343 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1344 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001345 }
1346}
1347
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001348static void assert_vblank_disabled(struct drm_crtc *crtc)
1349{
Rob Clarke2c719b2014-12-15 13:56:32 -05001350 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001351 drm_crtc_vblank_put(crtc);
1352}
1353
Ander Conselvan de Oliveira7abd4b32016-03-08 17:46:15 +02001354void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1355 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001356{
Jesse Barnes92f25842011-01-04 15:09:34 -08001357 u32 val;
1358 bool enabled;
1359
Ville Syrjälä649636e2015-09-22 19:50:01 +03001360 val = I915_READ(PCH_TRANSCONF(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001361 enabled = !!(val & TRANS_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001362 I915_STATE_WARN(enabled,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001363 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1364 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001365}
1366
Keith Packard4e634382011-08-06 10:39:45 -07001367static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1368 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001369{
1370 if ((val & DP_PORT_EN) == 0)
1371 return false;
1372
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001373 if (HAS_PCH_CPT(dev_priv)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001374 u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
Keith Packardf0575e92011-07-25 22:12:43 -07001375 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1376 return false;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001377 } else if (IS_CHERRYVIEW(dev_priv)) {
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001378 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1379 return false;
Keith Packardf0575e92011-07-25 22:12:43 -07001380 } else {
1381 if ((val & DP_PIPE_MASK) != (pipe << 30))
1382 return false;
1383 }
1384 return true;
1385}
1386
Keith Packard1519b992011-08-06 10:35:34 -07001387static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1388 enum pipe pipe, u32 val)
1389{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001390 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001391 return false;
1392
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001393 if (HAS_PCH_CPT(dev_priv)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001394 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001395 return false;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001396 } else if (IS_CHERRYVIEW(dev_priv)) {
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001397 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1398 return false;
Keith Packard1519b992011-08-06 10:35:34 -07001399 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001400 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001401 return false;
1402 }
1403 return true;
1404}
1405
1406static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1407 enum pipe pipe, u32 val)
1408{
1409 if ((val & LVDS_PORT_EN) == 0)
1410 return false;
1411
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001412 if (HAS_PCH_CPT(dev_priv)) {
Keith Packard1519b992011-08-06 10:35:34 -07001413 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1414 return false;
1415 } else {
1416 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1417 return false;
1418 }
1419 return true;
1420}
1421
1422static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1423 enum pipe pipe, u32 val)
1424{
1425 if ((val & ADPA_DAC_ENABLE) == 0)
1426 return false;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001427 if (HAS_PCH_CPT(dev_priv)) {
Keith Packard1519b992011-08-06 10:35:34 -07001428 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1429 return false;
1430 } else {
1431 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1432 return false;
1433 }
1434 return true;
1435}
1436
Jesse Barnes291906f2011-02-02 12:28:03 -08001437static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001438 enum pipe pipe, i915_reg_t reg,
1439 u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001440{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001441 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001442 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001443 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001444 i915_mmio_reg_offset(reg), pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001445
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001446 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001447 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001448 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001449}
1450
1451static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001452 enum pipe pipe, i915_reg_t reg)
Jesse Barnes291906f2011-02-02 12:28:03 -08001453{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001454 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001455 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001456 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001457 i915_mmio_reg_offset(reg), pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001458
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001459 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001460 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001461 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001462}
1463
1464static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1465 enum pipe pipe)
1466{
Jesse Barnes291906f2011-02-02 12:28:03 -08001467 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001468
Keith Packardf0575e92011-07-25 22:12:43 -07001469 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1470 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1471 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001472
Ville Syrjälä649636e2015-09-22 19:50:01 +03001473 val = I915_READ(PCH_ADPA);
Rob Clarke2c719b2014-12-15 13:56:32 -05001474 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001475 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001476 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001477
Ville Syrjälä649636e2015-09-22 19:50:01 +03001478 val = I915_READ(PCH_LVDS);
Rob Clarke2c719b2014-12-15 13:56:32 -05001479 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001480 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001481 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001482
Paulo Zanonie2debe92013-02-18 19:00:27 -03001483 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1484 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1485 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001486}
1487
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001488static void _vlv_enable_pll(struct intel_crtc *crtc,
1489 const struct intel_crtc_state *pipe_config)
1490{
1491 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1492 enum pipe pipe = crtc->pipe;
1493
1494 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
1495 POSTING_READ(DPLL(pipe));
1496 udelay(150);
1497
Chris Wilson2c30b432016-06-30 15:32:54 +01001498 if (intel_wait_for_register(dev_priv,
1499 DPLL(pipe),
1500 DPLL_LOCK_VLV,
1501 DPLL_LOCK_VLV,
1502 1))
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001503 DRM_ERROR("DPLL %d failed to lock\n", pipe);
1504}
1505
Ville Syrjäläd288f652014-10-28 13:20:22 +02001506static void vlv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001507 const struct intel_crtc_state *pipe_config)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001508{
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001509 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001510 enum pipe pipe = crtc->pipe;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001511
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001512 assert_pipe_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001513
Daniel Vetter87442f72013-06-06 00:52:17 +02001514 /* PLL is protected by panel, make sure we can write it */
Ville Syrjälä7d1a83c2016-03-15 16:39:58 +02001515 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001516
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001517 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1518 _vlv_enable_pll(crtc, pipe_config);
Daniel Vetter426115c2013-07-11 22:13:42 +02001519
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001520 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1521 POSTING_READ(DPLL_MD(pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001522}
1523
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001524
1525static void _chv_enable_pll(struct intel_crtc *crtc,
1526 const struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001527{
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001528 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001529 enum pipe pipe = crtc->pipe;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001530 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001531 u32 tmp;
1532
Ville Syrjäläa5805162015-05-26 20:42:30 +03001533 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001534
1535 /* Enable back the 10bit clock to display controller */
1536 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1537 tmp |= DPIO_DCLKP_EN;
1538 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1539
Ville Syrjälä54433e92015-05-26 20:42:31 +03001540 mutex_unlock(&dev_priv->sb_lock);
1541
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001542 /*
1543 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1544 */
1545 udelay(1);
1546
1547 /* Enable PLL */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001548 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001549
1550 /* Check PLL is locked */
Chris Wilson6b188262016-06-30 15:32:55 +01001551 if (intel_wait_for_register(dev_priv,
1552 DPLL(pipe), DPLL_LOCK_VLV, DPLL_LOCK_VLV,
1553 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001554 DRM_ERROR("PLL %d failed to lock\n", pipe);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001555}
1556
1557static void chv_enable_pll(struct intel_crtc *crtc,
1558 const struct intel_crtc_state *pipe_config)
1559{
1560 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1561 enum pipe pipe = crtc->pipe;
1562
1563 assert_pipe_disabled(dev_priv, pipe);
1564
1565 /* PLL is protected by panel, make sure we can write it */
1566 assert_panel_unlocked(dev_priv, pipe);
1567
1568 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1569 _chv_enable_pll(crtc, pipe_config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001570
Ville Syrjäläc2317752016-03-15 16:39:56 +02001571 if (pipe != PIPE_A) {
1572 /*
1573 * WaPixelRepeatModeFixForC0:chv
1574 *
1575 * DPLLCMD is AWOL. Use chicken bits to propagate
1576 * the value from DPLLBMD to either pipe B or C.
1577 */
1578 I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
1579 I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
1580 I915_WRITE(CBR4_VLV, 0);
1581 dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
1582
1583 /*
1584 * DPLLB VGA mode also seems to cause problems.
1585 * We should always have it disabled.
1586 */
1587 WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
1588 } else {
1589 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1590 POSTING_READ(DPLL_MD(pipe));
1591 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001592}
1593
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001594static int intel_num_dvo_pipes(struct drm_i915_private *dev_priv)
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001595{
1596 struct intel_crtc *crtc;
1597 int count = 0;
1598
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001599 for_each_intel_crtc(&dev_priv->drm, crtc) {
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001600 count += crtc->base.state->active &&
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03001601 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO);
1602 }
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001603
1604 return count;
1605}
1606
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001607static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001608{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001609 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001610 i915_reg_t reg = DPLL(crtc->pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001611 u32 dpll = crtc->config->dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001612
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001613 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001614
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001615 /* PLL is protected by panel, make sure we can write it */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01001616 if (IS_MOBILE(dev_priv) && !IS_I830(dev_priv))
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001617 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001618
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001619 /* Enable DVO 2x clock on both PLLs if necessary */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001620 if (IS_I830(dev_priv) && intel_num_dvo_pipes(dev_priv) > 0) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001621 /*
1622 * It appears to be important that we don't enable this
1623 * for the current pipe before otherwise configuring the
1624 * PLL. No idea how this should be handled if multiple
1625 * DVO outputs are enabled simultaneosly.
1626 */
1627 dpll |= DPLL_DVO_2X_MODE;
1628 I915_WRITE(DPLL(!crtc->pipe),
1629 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1630 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001631
Ville Syrjäläc2b63372015-10-07 22:08:25 +03001632 /*
1633 * Apparently we need to have VGA mode enabled prior to changing
1634 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1635 * dividers, even though the register value does change.
1636 */
1637 I915_WRITE(reg, 0);
1638
Ville Syrjälä8e7a65a2015-10-07 22:08:24 +03001639 I915_WRITE(reg, dpll);
1640
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001641 /* Wait for the clocks to stabilize. */
1642 POSTING_READ(reg);
1643 udelay(150);
1644
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001645 if (INTEL_GEN(dev_priv) >= 4) {
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001646 I915_WRITE(DPLL_MD(crtc->pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001647 crtc->config->dpll_hw_state.dpll_md);
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001648 } else {
1649 /* The pixel multiplier can only be updated once the
1650 * DPLL is enabled and the clocks are stable.
1651 *
1652 * So write it again.
1653 */
1654 I915_WRITE(reg, dpll);
1655 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001656
1657 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001658 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001659 POSTING_READ(reg);
1660 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001661 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001662 POSTING_READ(reg);
1663 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001664 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001665 POSTING_READ(reg);
1666 udelay(150); /* wait for warmup */
1667}
1668
1669/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001670 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001671 * @dev_priv: i915 private structure
1672 * @pipe: pipe PLL to disable
1673 *
1674 * Disable the PLL for @pipe, making sure the pipe is off first.
1675 *
1676 * Note! This is for pre-ILK only.
1677 */
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001678static void i9xx_disable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001679{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001680 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001681 enum pipe pipe = crtc->pipe;
1682
1683 /* Disable DVO 2x clock on both PLLs if necessary */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01001684 if (IS_I830(dev_priv) &&
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03001685 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO) &&
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001686 !intel_num_dvo_pipes(dev_priv)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001687 I915_WRITE(DPLL(PIPE_B),
1688 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1689 I915_WRITE(DPLL(PIPE_A),
1690 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1691 }
1692
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001693 /* Don't disable pipe or pipe PLLs if needed */
1694 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1695 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001696 return;
1697
1698 /* Make sure the pipe isn't still relying on us */
1699 assert_pipe_disabled(dev_priv, pipe);
1700
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001701 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
Daniel Vetter50b44a42013-06-05 13:34:33 +02001702 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001703}
1704
Jesse Barnesf6071162013-10-01 10:41:38 -07001705static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1706{
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001707 u32 val;
Jesse Barnesf6071162013-10-01 10:41:38 -07001708
1709 /* Make sure the pipe isn't still relying on us */
1710 assert_pipe_disabled(dev_priv, pipe);
1711
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02001712 val = DPLL_INTEGRATED_REF_CLK_VLV |
1713 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1714 if (pipe != PIPE_A)
1715 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1716
Jesse Barnesf6071162013-10-01 10:41:38 -07001717 I915_WRITE(DPLL(pipe), val);
1718 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001719}
1720
1721static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1722{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001723 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001724 u32 val;
1725
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001726 /* Make sure the pipe isn't still relying on us */
1727 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001728
Ville Syrjälä60bfe442015-06-29 15:25:49 +03001729 val = DPLL_SSC_REF_CLK_CHV |
1730 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001731 if (pipe != PIPE_A)
1732 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02001733
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001734 I915_WRITE(DPLL(pipe), val);
1735 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001736
Ville Syrjäläa5805162015-05-26 20:42:30 +03001737 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd7520482014-04-09 13:28:59 +03001738
1739 /* Disable 10bit clock to display controller */
1740 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1741 val &= ~DPIO_DCLKP_EN;
1742 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1743
Ville Syrjäläa5805162015-05-26 20:42:30 +03001744 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001745}
1746
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001747void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001748 struct intel_digital_port *dport,
1749 unsigned int expected_mask)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001750{
1751 u32 port_mask;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001752 i915_reg_t dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001753
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001754 switch (dport->port) {
1755 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001756 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001757 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001758 break;
1759 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001760 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001761 dpll_reg = DPLL(0);
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001762 expected_mask <<= 4;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001763 break;
1764 case PORT_D:
1765 port_mask = DPLL_PORTD_READY_MASK;
1766 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001767 break;
1768 default:
1769 BUG();
1770 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001771
Chris Wilson370004d2016-06-30 15:32:56 +01001772 if (intel_wait_for_register(dev_priv,
1773 dpll_reg, port_mask, expected_mask,
1774 1000))
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001775 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1776 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
Jesse Barnes89b667f2013-04-18 14:51:36 -07001777}
1778
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001779static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1780 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001781{
Ville Syrjälä98187832016-10-31 22:37:10 +02001782 struct intel_crtc *intel_crtc = intel_get_crtc_for_pipe(dev_priv,
1783 pipe);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001784 i915_reg_t reg;
1785 uint32_t val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001786
Jesse Barnes040484a2011-01-03 12:14:26 -08001787 /* Make sure PCH DPLL is enabled */
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02001788 assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
Jesse Barnes040484a2011-01-03 12:14:26 -08001789
1790 /* FDI must be feeding us bits for PCH ports */
1791 assert_fdi_tx_enabled(dev_priv, pipe);
1792 assert_fdi_rx_enabled(dev_priv, pipe);
1793
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01001794 if (HAS_PCH_CPT(dev_priv)) {
Daniel Vetter23670b322012-11-01 09:15:30 +01001795 /* Workaround: Set the timing override bit before enabling the
1796 * pch transcoder. */
1797 reg = TRANS_CHICKEN2(pipe);
1798 val = I915_READ(reg);
1799 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1800 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001801 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001802
Daniel Vetterab9412b2013-05-03 11:49:46 +02001803 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001804 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001805 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001806
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001807 if (HAS_PCH_IBX(dev_priv)) {
Jesse Barnese9bcff52011-06-24 12:19:20 -07001808 /*
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001809 * Make the BPC in transcoder be consistent with
1810 * that in pipeconf reg. For HDMI we must use 8bpc
1811 * here for both 8bpc and 12bpc.
Jesse Barnese9bcff52011-06-24 12:19:20 -07001812 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001813 val &= ~PIPECONF_BPC_MASK;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03001814 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_HDMI))
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001815 val |= PIPECONF_8BPC;
1816 else
1817 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001818 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001819
1820 val &= ~TRANS_INTERLACE_MASK;
1821 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001822 if (HAS_PCH_IBX(dev_priv) &&
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03001823 intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001824 val |= TRANS_LEGACY_INTERLACED_ILK;
1825 else
1826 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001827 else
1828 val |= TRANS_PROGRESSIVE;
1829
Jesse Barnes040484a2011-01-03 12:14:26 -08001830 I915_WRITE(reg, val | TRANS_ENABLE);
Chris Wilson650fbd82016-06-30 15:32:57 +01001831 if (intel_wait_for_register(dev_priv,
1832 reg, TRANS_STATE_ENABLE, TRANS_STATE_ENABLE,
1833 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001834 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08001835}
1836
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001837static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001838 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001839{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001840 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001841
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001842 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001843 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001844 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001845
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001846 /* Workaround: set timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001847 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01001848 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001849 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001850
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001851 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001852 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001853
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001854 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1855 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001856 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001857 else
1858 val |= TRANS_PROGRESSIVE;
1859
Daniel Vetterab9412b2013-05-03 11:49:46 +02001860 I915_WRITE(LPT_TRANSCONF, val);
Chris Wilsond9f96242016-06-30 15:32:58 +01001861 if (intel_wait_for_register(dev_priv,
1862 LPT_TRANSCONF,
1863 TRANS_STATE_ENABLE,
1864 TRANS_STATE_ENABLE,
1865 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02001866 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001867}
1868
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001869static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1870 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001871{
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001872 i915_reg_t reg;
1873 uint32_t val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001874
1875 /* FDI relies on the transcoder */
1876 assert_fdi_tx_disabled(dev_priv, pipe);
1877 assert_fdi_rx_disabled(dev_priv, pipe);
1878
Jesse Barnes291906f2011-02-02 12:28:03 -08001879 /* Ports must be off as well */
1880 assert_pch_ports_disabled(dev_priv, pipe);
1881
Daniel Vetterab9412b2013-05-03 11:49:46 +02001882 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001883 val = I915_READ(reg);
1884 val &= ~TRANS_ENABLE;
1885 I915_WRITE(reg, val);
1886 /* wait for PCH transcoder off, transcoder state */
Chris Wilsona7d04662016-06-30 15:32:59 +01001887 if (intel_wait_for_register(dev_priv,
1888 reg, TRANS_STATE_ENABLE, 0,
1889 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001890 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01001891
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01001892 if (HAS_PCH_CPT(dev_priv)) {
Daniel Vetter23670b322012-11-01 09:15:30 +01001893 /* Workaround: Clear the timing override chicken bit again. */
1894 reg = TRANS_CHICKEN2(pipe);
1895 val = I915_READ(reg);
1896 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1897 I915_WRITE(reg, val);
1898 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001899}
1900
Maarten Lankhorstb7076542016-08-23 16:18:08 +02001901void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001902{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001903 u32 val;
1904
Daniel Vetterab9412b2013-05-03 11:49:46 +02001905 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001906 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02001907 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001908 /* wait for PCH transcoder off, transcoder state */
Chris Wilsondfdb4742016-06-30 15:33:00 +01001909 if (intel_wait_for_register(dev_priv,
1910 LPT_TRANSCONF, TRANS_STATE_ENABLE, 0,
1911 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001912 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001913
1914 /* Workaround: clear timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001915 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01001916 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001917 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Jesse Barnes92f25842011-01-04 15:09:34 -08001918}
1919
Ville Syrjälä65f21302016-10-14 20:02:53 +03001920enum transcoder intel_crtc_pch_transcoder(struct intel_crtc *crtc)
1921{
1922 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1923
1924 WARN_ON(!crtc->config->has_pch_encoder);
1925
1926 if (HAS_PCH_LPT(dev_priv))
1927 return TRANSCODER_A;
1928 else
1929 return (enum transcoder) crtc->pipe;
1930}
1931
Jesse Barnes92f25842011-01-04 15:09:34 -08001932/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001933 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02001934 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08001935 *
Paulo Zanoni03722642014-01-17 13:51:09 -02001936 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001937 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08001938 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02001939static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001940{
Paulo Zanoni03722642014-01-17 13:51:09 -02001941 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01001942 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanoni03722642014-01-17 13:51:09 -02001943 enum pipe pipe = crtc->pipe;
Ville Syrjälä1a70a7282015-10-29 21:25:50 +02001944 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001945 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001946 u32 val;
1947
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03001948 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
1949
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001950 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001951 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001952 assert_sprites_disabled(dev_priv, pipe);
1953
Jesse Barnesb24e7172011-01-04 15:09:30 -08001954 /*
1955 * A pipe without a PLL won't actually be able to drive bits from
1956 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1957 * need the check.
1958 */
Ville Syrjälä09fa8bb2016-08-05 20:41:34 +03001959 if (HAS_GMCH_DISPLAY(dev_priv)) {
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03001960 if (intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DSI))
Jani Nikula23538ef2013-08-27 15:12:22 +03001961 assert_dsi_pll_enabled(dev_priv);
1962 else
1963 assert_pll_enabled(dev_priv, pipe);
Ville Syrjälä09fa8bb2016-08-05 20:41:34 +03001964 } else {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001965 if (crtc->config->has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08001966 /* if driving the PCH, we need FDI enabled */
Ville Syrjälä65f21302016-10-14 20:02:53 +03001967 assert_fdi_rx_pll_enabled(dev_priv,
1968 (enum pipe) intel_crtc_pch_transcoder(crtc));
Daniel Vetter1a240d42012-11-29 22:18:51 +01001969 assert_fdi_tx_pll_enabled(dev_priv,
1970 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08001971 }
1972 /* FIXME: assert CPU port conditions for SNB+ */
1973 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08001974
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001975 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001976 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02001977 if (val & PIPECONF_ENABLE) {
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001978 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1979 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
Chris Wilson00d70b12011-03-17 07:18:29 +00001980 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02001981 }
Chris Wilson00d70b12011-03-17 07:18:29 +00001982
1983 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02001984 POSTING_READ(reg);
Ville Syrjäläb7792d82015-12-14 18:23:43 +02001985
1986 /*
1987 * Until the pipe starts DSL will read as 0, which would cause
1988 * an apparent vblank timestamp jump, which messes up also the
1989 * frame count when it's derived from the timestamps. So let's
1990 * wait for the pipe to start properly before we call
1991 * drm_crtc_vblank_on()
1992 */
1993 if (dev->max_vblank_count == 0 &&
1994 wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
1995 DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001996}
1997
1998/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001999 * intel_disable_pipe - disable a pipe, asserting requirements
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002000 * @crtc: crtc whose pipes is to be disabled
Jesse Barnesb24e7172011-01-04 15:09:30 -08002001 *
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002002 * Disable the pipe of @crtc, making sure that various hardware
2003 * specific requirements are met, if applicable, e.g. plane
2004 * disabled, panel fitter off, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002005 *
2006 * Will wait until the pipe has shut down before returning.
2007 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002008static void intel_disable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002009{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002010 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002011 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002012 enum pipe pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002013 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002014 u32 val;
2015
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03002016 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
2017
Jesse Barnesb24e7172011-01-04 15:09:30 -08002018 /*
2019 * Make sure planes won't keep trying to pump pixels to us,
2020 * or we might hang the display.
2021 */
2022 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002023 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07002024 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002025
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002026 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002027 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00002028 if ((val & PIPECONF_ENABLE) == 0)
2029 return;
2030
Ville Syrjälä67adc642014-08-15 01:21:57 +03002031 /*
2032 * Double wide has implications for planes
2033 * so best keep it disabled when not needed.
2034 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002035 if (crtc->config->double_wide)
Ville Syrjälä67adc642014-08-15 01:21:57 +03002036 val &= ~PIPECONF_DOUBLE_WIDE;
2037
2038 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002039 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2040 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Ville Syrjälä67adc642014-08-15 01:21:57 +03002041 val &= ~PIPECONF_ENABLE;
2042
2043 I915_WRITE(reg, val);
2044 if ((val & PIPECONF_ENABLE) == 0)
2045 intel_wait_for_pipe_off(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002046}
2047
Ville Syrjälä832be822016-01-12 21:08:33 +02002048static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
2049{
2050 return IS_GEN2(dev_priv) ? 2048 : 4096;
2051}
2052
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002053static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
2054 uint64_t fb_modifier, unsigned int cpp)
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002055{
2056 switch (fb_modifier) {
2057 case DRM_FORMAT_MOD_NONE:
2058 return cpp;
2059 case I915_FORMAT_MOD_X_TILED:
2060 if (IS_GEN2(dev_priv))
2061 return 128;
2062 else
2063 return 512;
2064 case I915_FORMAT_MOD_Y_TILED:
2065 if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
2066 return 128;
2067 else
2068 return 512;
2069 case I915_FORMAT_MOD_Yf_TILED:
2070 switch (cpp) {
2071 case 1:
2072 return 64;
2073 case 2:
2074 case 4:
2075 return 128;
2076 case 8:
2077 case 16:
2078 return 256;
2079 default:
2080 MISSING_CASE(cpp);
2081 return cpp;
2082 }
2083 break;
2084 default:
2085 MISSING_CASE(fb_modifier);
2086 return cpp;
2087 }
2088}
2089
Ville Syrjälä832be822016-01-12 21:08:33 +02002090unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
2091 uint64_t fb_modifier, unsigned int cpp)
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002092{
Ville Syrjälä832be822016-01-12 21:08:33 +02002093 if (fb_modifier == DRM_FORMAT_MOD_NONE)
2094 return 1;
2095 else
2096 return intel_tile_size(dev_priv) /
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002097 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002098}
2099
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002100/* Return the tile dimensions in pixel units */
2101static void intel_tile_dims(const struct drm_i915_private *dev_priv,
2102 unsigned int *tile_width,
2103 unsigned int *tile_height,
2104 uint64_t fb_modifier,
2105 unsigned int cpp)
2106{
2107 unsigned int tile_width_bytes =
2108 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
2109
2110 *tile_width = tile_width_bytes / cpp;
2111 *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
2112}
2113
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002114unsigned int
2115intel_fb_align_height(struct drm_device *dev, unsigned int height,
Ville Syrjälä832be822016-01-12 21:08:33 +02002116 uint32_t pixel_format, uint64_t fb_modifier)
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002117{
Ville Syrjälä832be822016-01-12 21:08:33 +02002118 unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
2119 unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
2120
2121 return ALIGN(height, tile_height);
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002122}
2123
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002124unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
2125{
2126 unsigned int size = 0;
2127 int i;
2128
2129 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
2130 size += rot_info->plane[i].width * rot_info->plane[i].height;
2131
2132 return size;
2133}
2134
Daniel Vetter75c82a52015-10-14 16:51:04 +02002135static void
Ville Syrjälä3465c582016-02-15 22:54:43 +02002136intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
2137 const struct drm_framebuffer *fb,
2138 unsigned int rotation)
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002139{
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03002140 if (drm_rotation_90_or_270(rotation)) {
Ville Syrjälä2d7a2152016-02-15 22:54:47 +02002141 *view = i915_ggtt_view_rotated;
2142 view->params.rotated = to_intel_framebuffer(fb)->rot_info;
2143 } else {
2144 *view = i915_ggtt_view_normal;
2145 }
2146}
2147
Ville Syrjälä603525d2016-01-12 21:08:37 +02002148static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002149{
2150 if (INTEL_INFO(dev_priv)->gen >= 9)
2151 return 256 * 1024;
Ville Syrjälä985b8bb2015-06-11 16:31:15 +03002152 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
Wayne Boyer666a4532015-12-09 12:29:35 -08002153 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002154 return 128 * 1024;
2155 else if (INTEL_INFO(dev_priv)->gen >= 4)
2156 return 4 * 1024;
2157 else
Ville Syrjälä44c59052015-06-11 16:31:16 +03002158 return 0;
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002159}
2160
Ville Syrjälä603525d2016-01-12 21:08:37 +02002161static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
2162 uint64_t fb_modifier)
2163{
2164 switch (fb_modifier) {
2165 case DRM_FORMAT_MOD_NONE:
2166 return intel_linear_alignment(dev_priv);
2167 case I915_FORMAT_MOD_X_TILED:
2168 if (INTEL_INFO(dev_priv)->gen >= 9)
2169 return 256 * 1024;
2170 return 0;
2171 case I915_FORMAT_MOD_Y_TILED:
2172 case I915_FORMAT_MOD_Yf_TILED:
2173 return 1 * 1024 * 1024;
2174 default:
2175 MISSING_CASE(fb_modifier);
2176 return 0;
2177 }
2178}
2179
Chris Wilson058d88c2016-08-15 10:49:06 +01002180struct i915_vma *
2181intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002182{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002183 struct drm_device *dev = fb->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002184 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002185 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002186 struct i915_ggtt_view view;
Chris Wilson058d88c2016-08-15 10:49:06 +01002187 struct i915_vma *vma;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002188 u32 alignment;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002189
Matt Roperebcdd392014-07-09 16:22:11 -07002190 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2191
Ville Syrjälä603525d2016-01-12 21:08:37 +02002192 alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002193
Ville Syrjälä3465c582016-02-15 22:54:43 +02002194 intel_fill_fb_ggtt_view(&view, fb, rotation);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002195
Chris Wilson693db182013-03-05 14:52:39 +00002196 /* Note that the w/a also requires 64 PTE of padding following the
2197 * bo. We currently fill all unused PTE with the shadow page and so
2198 * we should always have valid PTE following the scanout preventing
2199 * the VT-d warning.
2200 */
Chris Wilson48f112f2016-06-24 14:07:14 +01002201 if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
Chris Wilson693db182013-03-05 14:52:39 +00002202 alignment = 256 * 1024;
2203
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002204 /*
2205 * Global gtt pte registers are special registers which actually forward
2206 * writes to a chunk of system memory. Which means that there is no risk
2207 * that the register values disappear as soon as we call
2208 * intel_runtime_pm_put(), so it is correct to wrap only the
2209 * pin/unpin/fence and not more.
2210 */
2211 intel_runtime_pm_get(dev_priv);
2212
Chris Wilson058d88c2016-08-15 10:49:06 +01002213 vma = i915_gem_object_pin_to_display_plane(obj, alignment, &view);
Chris Wilson49ef5292016-08-18 17:17:00 +01002214 if (IS_ERR(vma))
2215 goto err;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002216
Chris Wilson05a20d02016-08-18 17:16:55 +01002217 if (i915_vma_is_map_and_fenceable(vma)) {
Chris Wilson49ef5292016-08-18 17:17:00 +01002218 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2219 * fence, whereas 965+ only requires a fence if using
2220 * framebuffer compression. For simplicity, we always, when
2221 * possible, install a fence as the cost is not that onerous.
2222 *
2223 * If we fail to fence the tiled scanout, then either the
2224 * modeset will reject the change (which is highly unlikely as
2225 * the affected systems, all but one, do not have unmappable
2226 * space) or we will not be able to enable full powersaving
2227 * techniques (also likely not to apply due to various limits
2228 * FBC and the like impose on the size of the buffer, which
2229 * presumably we violated anyway with this unmappable buffer).
2230 * Anyway, it is presumably better to stumble onwards with
2231 * something and try to run the system in a "less than optimal"
2232 * mode that matches the user configuration.
2233 */
2234 if (i915_vma_get_fence(vma) == 0)
2235 i915_vma_pin_fence(vma);
Vivek Kasireddy98072162015-10-29 18:54:38 -07002236 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002237
Chris Wilson49ef5292016-08-18 17:17:00 +01002238err:
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002239 intel_runtime_pm_put(dev_priv);
Chris Wilson058d88c2016-08-15 10:49:06 +01002240 return vma;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002241}
2242
Chris Wilsonfb4b8ce2016-04-28 09:56:35 +01002243void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
Chris Wilson1690e1e2011-12-14 13:57:08 +01002244{
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002245 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002246 struct i915_ggtt_view view;
Chris Wilson058d88c2016-08-15 10:49:06 +01002247 struct i915_vma *vma;
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002248
Matt Roperebcdd392014-07-09 16:22:11 -07002249 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2250
Ville Syrjälä3465c582016-02-15 22:54:43 +02002251 intel_fill_fb_ggtt_view(&view, fb, rotation);
Chris Wilson05a20d02016-08-18 17:16:55 +01002252 vma = i915_gem_object_to_ggtt(obj, &view);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002253
Chris Wilson49ef5292016-08-18 17:17:00 +01002254 i915_vma_unpin_fence(vma);
Chris Wilson058d88c2016-08-15 10:49:06 +01002255 i915_gem_object_unpin_from_display_plane(vma);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002256}
2257
Ville Syrjäläef78ec92015-10-13 22:48:39 +03002258static int intel_fb_pitch(const struct drm_framebuffer *fb, int plane,
2259 unsigned int rotation)
2260{
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03002261 if (drm_rotation_90_or_270(rotation))
Ville Syrjäläef78ec92015-10-13 22:48:39 +03002262 return to_intel_framebuffer(fb)->rotated[plane].pitch;
2263 else
2264 return fb->pitches[plane];
2265}
2266
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002267/*
Ville Syrjälä6687c902015-09-15 13:16:41 +03002268 * Convert the x/y offsets into a linear offset.
2269 * Only valid with 0/180 degree rotation, which is fine since linear
2270 * offset is only used with linear buffers on pre-hsw and tiled buffers
2271 * with gen2/3, and 90/270 degree rotations isn't supported on any of them.
2272 */
2273u32 intel_fb_xy_to_linear(int x, int y,
Ville Syrjälä29490562016-01-20 18:02:50 +02002274 const struct intel_plane_state *state,
2275 int plane)
Ville Syrjälä6687c902015-09-15 13:16:41 +03002276{
Ville Syrjälä29490562016-01-20 18:02:50 +02002277 const struct drm_framebuffer *fb = state->base.fb;
Ville Syrjälä6687c902015-09-15 13:16:41 +03002278 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
2279 unsigned int pitch = fb->pitches[plane];
2280
2281 return y * pitch + x * cpp;
2282}
2283
2284/*
2285 * Add the x/y offsets derived from fb->offsets[] to the user
2286 * specified plane src x/y offsets. The resulting x/y offsets
2287 * specify the start of scanout from the beginning of the gtt mapping.
2288 */
2289void intel_add_fb_offsets(int *x, int *y,
Ville Syrjälä29490562016-01-20 18:02:50 +02002290 const struct intel_plane_state *state,
2291 int plane)
Ville Syrjälä6687c902015-09-15 13:16:41 +03002292
2293{
Ville Syrjälä29490562016-01-20 18:02:50 +02002294 const struct intel_framebuffer *intel_fb = to_intel_framebuffer(state->base.fb);
2295 unsigned int rotation = state->base.rotation;
Ville Syrjälä6687c902015-09-15 13:16:41 +03002296
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03002297 if (drm_rotation_90_or_270(rotation)) {
Ville Syrjälä6687c902015-09-15 13:16:41 +03002298 *x += intel_fb->rotated[plane].x;
2299 *y += intel_fb->rotated[plane].y;
2300 } else {
2301 *x += intel_fb->normal[plane].x;
2302 *y += intel_fb->normal[plane].y;
2303 }
2304}
2305
2306/*
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002307 * Input tile dimensions and pitch must already be
2308 * rotated to match x and y, and in pixel units.
2309 */
Ville Syrjälä66a2d922016-02-05 18:44:05 +02002310static u32 _intel_adjust_tile_offset(int *x, int *y,
2311 unsigned int tile_width,
2312 unsigned int tile_height,
2313 unsigned int tile_size,
2314 unsigned int pitch_tiles,
2315 u32 old_offset,
2316 u32 new_offset)
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002317{
Ville Syrjäläb9b24032016-02-08 18:28:00 +02002318 unsigned int pitch_pixels = pitch_tiles * tile_width;
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002319 unsigned int tiles;
2320
2321 WARN_ON(old_offset & (tile_size - 1));
2322 WARN_ON(new_offset & (tile_size - 1));
2323 WARN_ON(new_offset > old_offset);
2324
2325 tiles = (old_offset - new_offset) / tile_size;
2326
2327 *y += tiles / pitch_tiles * tile_height;
2328 *x += tiles % pitch_tiles * tile_width;
2329
Ville Syrjäläb9b24032016-02-08 18:28:00 +02002330 /* minimize x in case it got needlessly big */
2331 *y += *x / pitch_pixels * tile_height;
2332 *x %= pitch_pixels;
2333
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002334 return new_offset;
2335}
2336
2337/*
Ville Syrjälä66a2d922016-02-05 18:44:05 +02002338 * Adjust the tile offset by moving the difference into
2339 * the x/y offsets.
2340 */
2341static u32 intel_adjust_tile_offset(int *x, int *y,
2342 const struct intel_plane_state *state, int plane,
2343 u32 old_offset, u32 new_offset)
2344{
2345 const struct drm_i915_private *dev_priv = to_i915(state->base.plane->dev);
2346 const struct drm_framebuffer *fb = state->base.fb;
2347 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
2348 unsigned int rotation = state->base.rotation;
2349 unsigned int pitch = intel_fb_pitch(fb, plane, rotation);
2350
2351 WARN_ON(new_offset > old_offset);
2352
2353 if (fb->modifier[plane] != DRM_FORMAT_MOD_NONE) {
2354 unsigned int tile_size, tile_width, tile_height;
2355 unsigned int pitch_tiles;
2356
2357 tile_size = intel_tile_size(dev_priv);
2358 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2359 fb->modifier[plane], cpp);
2360
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03002361 if (drm_rotation_90_or_270(rotation)) {
Ville Syrjälä66a2d922016-02-05 18:44:05 +02002362 pitch_tiles = pitch / tile_height;
2363 swap(tile_width, tile_height);
2364 } else {
2365 pitch_tiles = pitch / (tile_width * cpp);
2366 }
2367
2368 _intel_adjust_tile_offset(x, y, tile_width, tile_height,
2369 tile_size, pitch_tiles,
2370 old_offset, new_offset);
2371 } else {
2372 old_offset += *y * pitch + *x * cpp;
2373
2374 *y = (old_offset - new_offset) / pitch;
2375 *x = ((old_offset - new_offset) - *y * pitch) / cpp;
2376 }
2377
2378 return new_offset;
2379}
2380
2381/*
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002382 * Computes the linear offset to the base tile and adjusts
2383 * x, y. bytes per pixel is assumed to be a power-of-two.
2384 *
2385 * In the 90/270 rotated case, x and y are assumed
2386 * to be already rotated to match the rotated GTT view, and
2387 * pitch is the tile_height aligned framebuffer height.
Ville Syrjälä6687c902015-09-15 13:16:41 +03002388 *
2389 * This function is used when computing the derived information
2390 * under intel_framebuffer, so using any of that information
2391 * here is not allowed. Anything under drm_framebuffer can be
2392 * used. This is why the user has to pass in the pitch since it
2393 * is specified in the rotated orientation.
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002394 */
Ville Syrjälä6687c902015-09-15 13:16:41 +03002395static u32 _intel_compute_tile_offset(const struct drm_i915_private *dev_priv,
2396 int *x, int *y,
2397 const struct drm_framebuffer *fb, int plane,
2398 unsigned int pitch,
2399 unsigned int rotation,
2400 u32 alignment)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002401{
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002402 uint64_t fb_modifier = fb->modifier[plane];
2403 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
Ville Syrjälä6687c902015-09-15 13:16:41 +03002404 u32 offset, offset_aligned;
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002405
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002406 if (alignment)
2407 alignment--;
2408
Ville Syrjäläb5c65332016-01-12 21:08:31 +02002409 if (fb_modifier != DRM_FORMAT_MOD_NONE) {
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002410 unsigned int tile_size, tile_width, tile_height;
2411 unsigned int tile_rows, tiles, pitch_tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002412
Ville Syrjäläd8433102016-01-12 21:08:35 +02002413 tile_size = intel_tile_size(dev_priv);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002414 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2415 fb_modifier, cpp);
2416
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03002417 if (drm_rotation_90_or_270(rotation)) {
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002418 pitch_tiles = pitch / tile_height;
2419 swap(tile_width, tile_height);
2420 } else {
2421 pitch_tiles = pitch / (tile_width * cpp);
2422 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002423
Ville Syrjäläd8433102016-01-12 21:08:35 +02002424 tile_rows = *y / tile_height;
2425 *y %= tile_height;
Chris Wilsonbc752862013-02-21 20:04:31 +00002426
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002427 tiles = *x / tile_width;
2428 *x %= tile_width;
Ville Syrjäläd8433102016-01-12 21:08:35 +02002429
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002430 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2431 offset_aligned = offset & ~alignment;
Chris Wilsonbc752862013-02-21 20:04:31 +00002432
Ville Syrjälä66a2d922016-02-05 18:44:05 +02002433 _intel_adjust_tile_offset(x, y, tile_width, tile_height,
2434 tile_size, pitch_tiles,
2435 offset, offset_aligned);
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002436 } else {
Chris Wilsonbc752862013-02-21 20:04:31 +00002437 offset = *y * pitch + *x * cpp;
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002438 offset_aligned = offset & ~alignment;
2439
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002440 *y = (offset & alignment) / pitch;
2441 *x = ((offset & alignment) - *y * pitch) / cpp;
Chris Wilsonbc752862013-02-21 20:04:31 +00002442 }
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002443
2444 return offset_aligned;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002445}
2446
Ville Syrjälä6687c902015-09-15 13:16:41 +03002447u32 intel_compute_tile_offset(int *x, int *y,
Ville Syrjälä29490562016-01-20 18:02:50 +02002448 const struct intel_plane_state *state,
2449 int plane)
Ville Syrjälä6687c902015-09-15 13:16:41 +03002450{
Ville Syrjälä29490562016-01-20 18:02:50 +02002451 const struct drm_i915_private *dev_priv = to_i915(state->base.plane->dev);
2452 const struct drm_framebuffer *fb = state->base.fb;
2453 unsigned int rotation = state->base.rotation;
Ville Syrjäläef78ec92015-10-13 22:48:39 +03002454 int pitch = intel_fb_pitch(fb, plane, rotation);
Ville Syrjälä8d970652016-01-28 16:30:28 +02002455 u32 alignment;
2456
2457 /* AUX_DIST needs only 4K alignment */
2458 if (fb->pixel_format == DRM_FORMAT_NV12 && plane == 1)
2459 alignment = 4096;
2460 else
2461 alignment = intel_surf_alignment(dev_priv, fb->modifier[plane]);
Ville Syrjälä6687c902015-09-15 13:16:41 +03002462
2463 return _intel_compute_tile_offset(dev_priv, x, y, fb, plane, pitch,
2464 rotation, alignment);
2465}
2466
2467/* Convert the fb->offset[] linear offset into x/y offsets */
2468static void intel_fb_offset_to_xy(int *x, int *y,
2469 const struct drm_framebuffer *fb, int plane)
2470{
2471 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
2472 unsigned int pitch = fb->pitches[plane];
2473 u32 linear_offset = fb->offsets[plane];
2474
2475 *y = linear_offset / pitch;
2476 *x = linear_offset % pitch / cpp;
2477}
2478
Ville Syrjälä72618eb2016-02-04 20:38:20 +02002479static unsigned int intel_fb_modifier_to_tiling(uint64_t fb_modifier)
2480{
2481 switch (fb_modifier) {
2482 case I915_FORMAT_MOD_X_TILED:
2483 return I915_TILING_X;
2484 case I915_FORMAT_MOD_Y_TILED:
2485 return I915_TILING_Y;
2486 default:
2487 return I915_TILING_NONE;
2488 }
2489}
2490
Ville Syrjälä6687c902015-09-15 13:16:41 +03002491static int
2492intel_fill_fb_info(struct drm_i915_private *dev_priv,
2493 struct drm_framebuffer *fb)
2494{
2495 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
2496 struct intel_rotation_info *rot_info = &intel_fb->rot_info;
2497 u32 gtt_offset_rotated = 0;
2498 unsigned int max_size = 0;
2499 uint32_t format = fb->pixel_format;
2500 int i, num_planes = drm_format_num_planes(format);
2501 unsigned int tile_size = intel_tile_size(dev_priv);
2502
2503 for (i = 0; i < num_planes; i++) {
2504 unsigned int width, height;
2505 unsigned int cpp, size;
2506 u32 offset;
2507 int x, y;
2508
2509 cpp = drm_format_plane_cpp(format, i);
2510 width = drm_format_plane_width(fb->width, format, i);
2511 height = drm_format_plane_height(fb->height, format, i);
2512
2513 intel_fb_offset_to_xy(&x, &y, fb, i);
2514
2515 /*
Ville Syrjälä60d5f2a2016-01-22 18:41:24 +02002516 * The fence (if used) is aligned to the start of the object
2517 * so having the framebuffer wrap around across the edge of the
2518 * fenced region doesn't really work. We have no API to configure
2519 * the fence start offset within the object (nor could we probably
2520 * on gen2/3). So it's just easier if we just require that the
2521 * fb layout agrees with the fence layout. We already check that the
2522 * fb stride matches the fence stride elsewhere.
2523 */
2524 if (i915_gem_object_is_tiled(intel_fb->obj) &&
2525 (x + width) * cpp > fb->pitches[i]) {
2526 DRM_DEBUG("bad fb plane %d offset: 0x%x\n",
2527 i, fb->offsets[i]);
2528 return -EINVAL;
2529 }
2530
2531 /*
Ville Syrjälä6687c902015-09-15 13:16:41 +03002532 * First pixel of the framebuffer from
2533 * the start of the normal gtt mapping.
2534 */
2535 intel_fb->normal[i].x = x;
2536 intel_fb->normal[i].y = y;
2537
2538 offset = _intel_compute_tile_offset(dev_priv, &x, &y,
2539 fb, 0, fb->pitches[i],
Daniel Vettercc926382016-08-15 10:41:47 +02002540 DRM_ROTATE_0, tile_size);
Ville Syrjälä6687c902015-09-15 13:16:41 +03002541 offset /= tile_size;
2542
2543 if (fb->modifier[i] != DRM_FORMAT_MOD_NONE) {
2544 unsigned int tile_width, tile_height;
2545 unsigned int pitch_tiles;
2546 struct drm_rect r;
2547
2548 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2549 fb->modifier[i], cpp);
2550
2551 rot_info->plane[i].offset = offset;
2552 rot_info->plane[i].stride = DIV_ROUND_UP(fb->pitches[i], tile_width * cpp);
2553 rot_info->plane[i].width = DIV_ROUND_UP(x + width, tile_width);
2554 rot_info->plane[i].height = DIV_ROUND_UP(y + height, tile_height);
2555
2556 intel_fb->rotated[i].pitch =
2557 rot_info->plane[i].height * tile_height;
2558
2559 /* how many tiles does this plane need */
2560 size = rot_info->plane[i].stride * rot_info->plane[i].height;
2561 /*
2562 * If the plane isn't horizontally tile aligned,
2563 * we need one more tile.
2564 */
2565 if (x != 0)
2566 size++;
2567
2568 /* rotate the x/y offsets to match the GTT view */
2569 r.x1 = x;
2570 r.y1 = y;
2571 r.x2 = x + width;
2572 r.y2 = y + height;
2573 drm_rect_rotate(&r,
2574 rot_info->plane[i].width * tile_width,
2575 rot_info->plane[i].height * tile_height,
Daniel Vettercc926382016-08-15 10:41:47 +02002576 DRM_ROTATE_270);
Ville Syrjälä6687c902015-09-15 13:16:41 +03002577 x = r.x1;
2578 y = r.y1;
2579
2580 /* rotate the tile dimensions to match the GTT view */
2581 pitch_tiles = intel_fb->rotated[i].pitch / tile_height;
2582 swap(tile_width, tile_height);
2583
2584 /*
2585 * We only keep the x/y offsets, so push all of the
2586 * gtt offset into the x/y offsets.
2587 */
Ville Syrjälä66a2d922016-02-05 18:44:05 +02002588 _intel_adjust_tile_offset(&x, &y, tile_size,
2589 tile_width, tile_height, pitch_tiles,
2590 gtt_offset_rotated * tile_size, 0);
Ville Syrjälä6687c902015-09-15 13:16:41 +03002591
2592 gtt_offset_rotated += rot_info->plane[i].width * rot_info->plane[i].height;
2593
2594 /*
2595 * First pixel of the framebuffer from
2596 * the start of the rotated gtt mapping.
2597 */
2598 intel_fb->rotated[i].x = x;
2599 intel_fb->rotated[i].y = y;
2600 } else {
2601 size = DIV_ROUND_UP((y + height) * fb->pitches[i] +
2602 x * cpp, tile_size);
2603 }
2604
2605 /* how many tiles in total needed in the bo */
2606 max_size = max(max_size, offset + size);
2607 }
2608
2609 if (max_size * tile_size > to_intel_framebuffer(fb)->obj->base.size) {
2610 DRM_DEBUG("fb too big for bo (need %u bytes, have %zu bytes)\n",
2611 max_size * tile_size, to_intel_framebuffer(fb)->obj->base.size);
2612 return -EINVAL;
2613 }
2614
2615 return 0;
2616}
2617
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002618static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002619{
2620 switch (format) {
2621 case DISPPLANE_8BPP:
2622 return DRM_FORMAT_C8;
2623 case DISPPLANE_BGRX555:
2624 return DRM_FORMAT_XRGB1555;
2625 case DISPPLANE_BGRX565:
2626 return DRM_FORMAT_RGB565;
2627 default:
2628 case DISPPLANE_BGRX888:
2629 return DRM_FORMAT_XRGB8888;
2630 case DISPPLANE_RGBX888:
2631 return DRM_FORMAT_XBGR8888;
2632 case DISPPLANE_BGRX101010:
2633 return DRM_FORMAT_XRGB2101010;
2634 case DISPPLANE_RGBX101010:
2635 return DRM_FORMAT_XBGR2101010;
2636 }
2637}
2638
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002639static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2640{
2641 switch (format) {
2642 case PLANE_CTL_FORMAT_RGB_565:
2643 return DRM_FORMAT_RGB565;
2644 default:
2645 case PLANE_CTL_FORMAT_XRGB_8888:
2646 if (rgb_order) {
2647 if (alpha)
2648 return DRM_FORMAT_ABGR8888;
2649 else
2650 return DRM_FORMAT_XBGR8888;
2651 } else {
2652 if (alpha)
2653 return DRM_FORMAT_ARGB8888;
2654 else
2655 return DRM_FORMAT_XRGB8888;
2656 }
2657 case PLANE_CTL_FORMAT_XRGB_2101010:
2658 if (rgb_order)
2659 return DRM_FORMAT_XBGR2101010;
2660 else
2661 return DRM_FORMAT_XRGB2101010;
2662 }
2663}
2664
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002665static bool
Daniel Vetterf6936e22015-03-26 12:17:05 +01002666intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2667 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002668{
2669 struct drm_device *dev = crtc->base.dev;
Paulo Zanoni3badb492015-09-23 12:52:23 -03002670 struct drm_i915_private *dev_priv = to_i915(dev);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002671 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002672 struct drm_i915_gem_object *obj = NULL;
2673 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Damien Lespiau2d140302015-02-05 17:22:18 +00002674 struct drm_framebuffer *fb = &plane_config->fb->base;
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002675 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2676 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2677 PAGE_SIZE);
2678
2679 size_aligned -= base_aligned;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002680
Chris Wilsonff2652e2014-03-10 08:07:02 +00002681 if (plane_config->size == 0)
2682 return false;
2683
Paulo Zanoni3badb492015-09-23 12:52:23 -03002684 /* If the FB is too big, just don't use it since fbdev is not very
2685 * important and we should probably use that space with FBC or other
2686 * features. */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002687 if (size_aligned * 2 > ggtt->stolen_usable_size)
Paulo Zanoni3badb492015-09-23 12:52:23 -03002688 return false;
2689
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002690 mutex_lock(&dev->struct_mutex);
2691
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002692 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2693 base_aligned,
2694 base_aligned,
2695 size_aligned);
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002696 if (!obj) {
2697 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002698 return false;
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002699 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002700
Chris Wilson3e510a82016-08-05 10:14:23 +01002701 if (plane_config->tiling == I915_TILING_X)
2702 obj->tiling_and_stride = fb->pitches[0] | I915_TILING_X;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002703
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002704 mode_cmd.pixel_format = fb->pixel_format;
2705 mode_cmd.width = fb->width;
2706 mode_cmd.height = fb->height;
2707 mode_cmd.pitches[0] = fb->pitches[0];
Daniel Vetter18c52472015-02-10 17:16:09 +00002708 mode_cmd.modifier[0] = fb->modifier[0];
2709 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002710
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002711 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002712 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002713 DRM_DEBUG_KMS("intel fb init failed\n");
2714 goto out_unref_obj;
2715 }
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002716
Jesse Barnes46f297f2014-03-07 08:57:48 -08002717 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002718
Daniel Vetterf6936e22015-03-26 12:17:05 +01002719 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002720 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002721
2722out_unref_obj:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01002723 i915_gem_object_put(obj);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002724 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002725 return false;
2726}
2727
Daniel Vetter5a21b662016-05-24 17:13:53 +02002728/* Update plane->state->fb to match plane->fb after driver-internal updates */
2729static void
2730update_state_fb(struct drm_plane *plane)
2731{
2732 if (plane->fb == plane->state->fb)
2733 return;
2734
2735 if (plane->state->fb)
2736 drm_framebuffer_unreference(plane->state->fb);
2737 plane->state->fb = plane->fb;
2738 if (plane->state->fb)
2739 drm_framebuffer_reference(plane->state->fb);
2740}
2741
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002742static void
Daniel Vetterf6936e22015-03-26 12:17:05 +01002743intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2744 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002745{
2746 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002747 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002748 struct drm_crtc *c;
2749 struct intel_crtc *i;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002750 struct drm_i915_gem_object *obj;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002751 struct drm_plane *primary = intel_crtc->base.primary;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002752 struct drm_plane_state *plane_state = primary->state;
Matt Roper200757f2015-12-03 11:37:36 -08002753 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
2754 struct intel_plane *intel_plane = to_intel_plane(primary);
Matt Roper0a8d8a82015-12-03 11:37:38 -08002755 struct intel_plane_state *intel_state =
2756 to_intel_plane_state(plane_state);
Daniel Vetter88595ac2015-03-26 12:42:24 +01002757 struct drm_framebuffer *fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002758
Damien Lespiau2d140302015-02-05 17:22:18 +00002759 if (!plane_config->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002760 return;
2761
Daniel Vetterf6936e22015-03-26 12:17:05 +01002762 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002763 fb = &plane_config->fb->base;
2764 goto valid_fb;
Damien Lespiauf55548b2015-02-05 18:30:20 +00002765 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002766
Damien Lespiau2d140302015-02-05 17:22:18 +00002767 kfree(plane_config->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002768
2769 /*
2770 * Failed to alloc the obj, check to see if we should share
2771 * an fb with another CRTC instead
2772 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002773 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002774 i = to_intel_crtc(c);
2775
2776 if (c == &intel_crtc->base)
2777 continue;
2778
Matt Roper2ff8fde2014-07-08 07:50:07 -07002779 if (!i->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002780 continue;
2781
Daniel Vetter88595ac2015-03-26 12:42:24 +01002782 fb = c->primary->fb;
2783 if (!fb)
Matt Roper2ff8fde2014-07-08 07:50:07 -07002784 continue;
2785
Daniel Vetter88595ac2015-03-26 12:42:24 +01002786 obj = intel_fb_obj(fb);
Chris Wilson058d88c2016-08-15 10:49:06 +01002787 if (i915_gem_object_ggtt_offset(obj, NULL) == plane_config->base) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002788 drm_framebuffer_reference(fb);
2789 goto valid_fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002790 }
2791 }
Daniel Vetter88595ac2015-03-26 12:42:24 +01002792
Matt Roper200757f2015-12-03 11:37:36 -08002793 /*
2794 * We've failed to reconstruct the BIOS FB. Current display state
2795 * indicates that the primary plane is visible, but has a NULL FB,
2796 * which will lead to problems later if we don't fix it up. The
2797 * simplest solution is to just disable the primary plane now and
2798 * pretend the BIOS never had it enabled.
2799 */
Ville Syrjälä936e71e2016-07-26 19:06:59 +03002800 to_intel_plane_state(plane_state)->base.visible = false;
Matt Roper200757f2015-12-03 11:37:36 -08002801 crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
Ville Syrjälä2622a082016-03-09 19:07:26 +02002802 intel_pre_disable_primary_noatomic(&intel_crtc->base);
Matt Roper200757f2015-12-03 11:37:36 -08002803 intel_plane->disable_plane(primary, &intel_crtc->base);
2804
Daniel Vetter88595ac2015-03-26 12:42:24 +01002805 return;
2806
2807valid_fb:
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002808 plane_state->src_x = 0;
2809 plane_state->src_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002810 plane_state->src_w = fb->width << 16;
2811 plane_state->src_h = fb->height << 16;
2812
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002813 plane_state->crtc_x = 0;
2814 plane_state->crtc_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002815 plane_state->crtc_w = fb->width;
2816 plane_state->crtc_h = fb->height;
2817
Rob Clark1638d302016-11-05 11:08:08 -04002818 intel_state->base.src = drm_plane_state_src(plane_state);
2819 intel_state->base.dst = drm_plane_state_dest(plane_state);
Matt Roper0a8d8a82015-12-03 11:37:38 -08002820
Daniel Vetter88595ac2015-03-26 12:42:24 +01002821 obj = intel_fb_obj(fb);
Chris Wilson3e510a82016-08-05 10:14:23 +01002822 if (i915_gem_object_is_tiled(obj))
Daniel Vetter88595ac2015-03-26 12:42:24 +01002823 dev_priv->preserve_bios_swizzle = true;
2824
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002825 drm_framebuffer_reference(fb);
2826 primary->fb = primary->state->fb = fb;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002827 primary->crtc = primary->state->crtc = &intel_crtc->base;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002828 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01002829 atomic_or(to_intel_plane(primary)->frontbuffer_bit,
2830 &obj->frontbuffer_bits);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002831}
2832
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002833static int skl_max_plane_width(const struct drm_framebuffer *fb, int plane,
2834 unsigned int rotation)
2835{
2836 int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
2837
2838 switch (fb->modifier[plane]) {
2839 case DRM_FORMAT_MOD_NONE:
2840 case I915_FORMAT_MOD_X_TILED:
2841 switch (cpp) {
2842 case 8:
2843 return 4096;
2844 case 4:
2845 case 2:
2846 case 1:
2847 return 8192;
2848 default:
2849 MISSING_CASE(cpp);
2850 break;
2851 }
2852 break;
2853 case I915_FORMAT_MOD_Y_TILED:
2854 case I915_FORMAT_MOD_Yf_TILED:
2855 switch (cpp) {
2856 case 8:
2857 return 2048;
2858 case 4:
2859 return 4096;
2860 case 2:
2861 case 1:
2862 return 8192;
2863 default:
2864 MISSING_CASE(cpp);
2865 break;
2866 }
2867 break;
2868 default:
2869 MISSING_CASE(fb->modifier[plane]);
2870 }
2871
2872 return 2048;
2873}
2874
2875static int skl_check_main_surface(struct intel_plane_state *plane_state)
2876{
2877 const struct drm_i915_private *dev_priv = to_i915(plane_state->base.plane->dev);
2878 const struct drm_framebuffer *fb = plane_state->base.fb;
2879 unsigned int rotation = plane_state->base.rotation;
Daniel Vettercc926382016-08-15 10:41:47 +02002880 int x = plane_state->base.src.x1 >> 16;
2881 int y = plane_state->base.src.y1 >> 16;
2882 int w = drm_rect_width(&plane_state->base.src) >> 16;
2883 int h = drm_rect_height(&plane_state->base.src) >> 16;
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002884 int max_width = skl_max_plane_width(fb, 0, rotation);
2885 int max_height = 4096;
Ville Syrjälä8d970652016-01-28 16:30:28 +02002886 u32 alignment, offset, aux_offset = plane_state->aux.offset;
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002887
2888 if (w > max_width || h > max_height) {
2889 DRM_DEBUG_KMS("requested Y/RGB source size %dx%d too big (limit %dx%d)\n",
2890 w, h, max_width, max_height);
2891 return -EINVAL;
2892 }
2893
2894 intel_add_fb_offsets(&x, &y, plane_state, 0);
2895 offset = intel_compute_tile_offset(&x, &y, plane_state, 0);
2896
2897 alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
2898
2899 /*
Ville Syrjälä8d970652016-01-28 16:30:28 +02002900 * AUX surface offset is specified as the distance from the
2901 * main surface offset, and it must be non-negative. Make
2902 * sure that is what we will get.
2903 */
2904 if (offset > aux_offset)
2905 offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
2906 offset, aux_offset & ~(alignment - 1));
2907
2908 /*
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002909 * When using an X-tiled surface, the plane blows up
2910 * if the x offset + width exceed the stride.
2911 *
2912 * TODO: linear and Y-tiled seem fine, Yf untested,
2913 */
2914 if (fb->modifier[0] == I915_FORMAT_MOD_X_TILED) {
2915 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
2916
2917 while ((x + w) * cpp > fb->pitches[0]) {
2918 if (offset == 0) {
2919 DRM_DEBUG_KMS("Unable to find suitable display surface offset\n");
2920 return -EINVAL;
2921 }
2922
2923 offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
2924 offset, offset - alignment);
2925 }
2926 }
2927
2928 plane_state->main.offset = offset;
2929 plane_state->main.x = x;
2930 plane_state->main.y = y;
2931
2932 return 0;
2933}
2934
Ville Syrjälä8d970652016-01-28 16:30:28 +02002935static int skl_check_nv12_aux_surface(struct intel_plane_state *plane_state)
2936{
2937 const struct drm_framebuffer *fb = plane_state->base.fb;
2938 unsigned int rotation = plane_state->base.rotation;
2939 int max_width = skl_max_plane_width(fb, 1, rotation);
2940 int max_height = 4096;
Daniel Vettercc926382016-08-15 10:41:47 +02002941 int x = plane_state->base.src.x1 >> 17;
2942 int y = plane_state->base.src.y1 >> 17;
2943 int w = drm_rect_width(&plane_state->base.src) >> 17;
2944 int h = drm_rect_height(&plane_state->base.src) >> 17;
Ville Syrjälä8d970652016-01-28 16:30:28 +02002945 u32 offset;
2946
2947 intel_add_fb_offsets(&x, &y, plane_state, 1);
2948 offset = intel_compute_tile_offset(&x, &y, plane_state, 1);
2949
2950 /* FIXME not quite sure how/if these apply to the chroma plane */
2951 if (w > max_width || h > max_height) {
2952 DRM_DEBUG_KMS("CbCr source size %dx%d too big (limit %dx%d)\n",
2953 w, h, max_width, max_height);
2954 return -EINVAL;
2955 }
2956
2957 plane_state->aux.offset = offset;
2958 plane_state->aux.x = x;
2959 plane_state->aux.y = y;
2960
2961 return 0;
2962}
2963
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002964int skl_check_plane_surface(struct intel_plane_state *plane_state)
2965{
2966 const struct drm_framebuffer *fb = plane_state->base.fb;
2967 unsigned int rotation = plane_state->base.rotation;
2968 int ret;
2969
2970 /* Rotate src coordinates to match rotated GTT view */
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03002971 if (drm_rotation_90_or_270(rotation))
Daniel Vettercc926382016-08-15 10:41:47 +02002972 drm_rect_rotate(&plane_state->base.src,
Ville Syrjäläda064b42016-10-24 19:13:04 +03002973 fb->width << 16, fb->height << 16,
2974 DRM_ROTATE_270);
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002975
Ville Syrjälä8d970652016-01-28 16:30:28 +02002976 /*
2977 * Handle the AUX surface first since
2978 * the main surface setup depends on it.
2979 */
2980 if (fb->pixel_format == DRM_FORMAT_NV12) {
2981 ret = skl_check_nv12_aux_surface(plane_state);
2982 if (ret)
2983 return ret;
2984 } else {
2985 plane_state->aux.offset = ~0xfff;
2986 plane_state->aux.x = 0;
2987 plane_state->aux.y = 0;
2988 }
2989
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002990 ret = skl_check_main_surface(plane_state);
2991 if (ret)
2992 return ret;
2993
2994 return 0;
2995}
2996
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002997static void i9xx_update_primary_plane(struct drm_plane *primary,
2998 const struct intel_crtc_state *crtc_state,
2999 const struct intel_plane_state *plane_state)
Jesse Barnes81255562010-08-02 12:07:50 -07003000{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00003001 struct drm_i915_private *dev_priv = to_i915(primary->dev);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003002 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3003 struct drm_framebuffer *fb = plane_state->base.fb;
Jesse Barnes81255562010-08-02 12:07:50 -07003004 int plane = intel_crtc->plane;
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02003005 u32 linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07003006 u32 dspcntr;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003007 i915_reg_t reg = DSPCNTR(plane);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02003008 unsigned int rotation = plane_state->base.rotation;
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003009 int x = plane_state->base.src.x1 >> 16;
3010 int y = plane_state->base.src.y1 >> 16;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03003011
Ville Syrjäläf45651b2014-08-08 21:51:10 +03003012 dspcntr = DISPPLANE_GAMMA_ENABLE;
3013
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03003014 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03003015
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00003016 if (INTEL_GEN(dev_priv) < 4) {
Ville Syrjäläf45651b2014-08-08 21:51:10 +03003017 if (intel_crtc->pipe == PIPE_B)
3018 dspcntr |= DISPPLANE_SEL_PIPE_B;
3019
3020 /* pipesrc and dspsize control the size that is scaled from,
3021 * which should always be the user's requested size.
3022 */
3023 I915_WRITE(DSPSIZE(plane),
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003024 ((crtc_state->pipe_src_h - 1) << 16) |
3025 (crtc_state->pipe_src_w - 1));
Ville Syrjäläf45651b2014-08-08 21:51:10 +03003026 I915_WRITE(DSPPOS(plane), 0);
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01003027 } else if (IS_CHERRYVIEW(dev_priv) && plane == PLANE_B) {
Ville Syrjäläc14b0482014-10-16 20:52:34 +03003028 I915_WRITE(PRIMSIZE(plane),
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003029 ((crtc_state->pipe_src_h - 1) << 16) |
3030 (crtc_state->pipe_src_w - 1));
Ville Syrjäläc14b0482014-10-16 20:52:34 +03003031 I915_WRITE(PRIMPOS(plane), 0);
3032 I915_WRITE(PRIMCNSTALPHA(plane), 0);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03003033 }
3034
Ville Syrjälä57779d02012-10-31 17:50:14 +02003035 switch (fb->pixel_format) {
3036 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07003037 dspcntr |= DISPPLANE_8BPP;
3038 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02003039 case DRM_FORMAT_XRGB1555:
Ville Syrjälä57779d02012-10-31 17:50:14 +02003040 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07003041 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02003042 case DRM_FORMAT_RGB565:
3043 dspcntr |= DISPPLANE_BGRX565;
3044 break;
3045 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02003046 dspcntr |= DISPPLANE_BGRX888;
3047 break;
3048 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02003049 dspcntr |= DISPPLANE_RGBX888;
3050 break;
3051 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02003052 dspcntr |= DISPPLANE_BGRX101010;
3053 break;
3054 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02003055 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07003056 break;
3057 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01003058 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07003059 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02003060
Ville Syrjälä72618eb2016-02-04 20:38:20 +02003061 if (INTEL_GEN(dev_priv) >= 4 &&
3062 fb->modifier[0] == I915_FORMAT_MOD_X_TILED)
Ville Syrjäläf45651b2014-08-08 21:51:10 +03003063 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07003064
Ville Syrjälädf0cd452016-11-14 18:53:59 +02003065 if (rotation & DRM_ROTATE_180)
3066 dspcntr |= DISPPLANE_ROTATE_180;
3067
Ville Syrjälä4ea7be22016-11-14 18:54:00 +02003068 if (rotation & DRM_REFLECT_X)
3069 dspcntr |= DISPPLANE_MIRROR;
3070
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +01003071 if (IS_G4X(dev_priv))
Ville Syrjäläde1aa622013-06-07 10:47:01 +03003072 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
3073
Ville Syrjälä29490562016-01-20 18:02:50 +02003074 intel_add_fb_offsets(&x, &y, plane_state, 0);
Jesse Barnes81255562010-08-02 12:07:50 -07003075
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00003076 if (INTEL_GEN(dev_priv) >= 4)
Daniel Vetterc2c75132012-07-05 12:17:30 +02003077 intel_crtc->dspaddr_offset =
Ville Syrjälä29490562016-01-20 18:02:50 +02003078 intel_compute_tile_offset(&x, &y, plane_state, 0);
Daniel Vettere506a0c2012-07-05 12:17:29 +02003079
Ville Syrjäläf22aa142016-11-14 18:53:58 +02003080 if (rotation & DRM_ROTATE_180) {
Ville Syrjälädf0cd452016-11-14 18:53:59 +02003081 x += crtc_state->pipe_src_w - 1;
3082 y += crtc_state->pipe_src_h - 1;
Ville Syrjälä4ea7be22016-11-14 18:54:00 +02003083 } else if (rotation & DRM_REFLECT_X) {
3084 x += crtc_state->pipe_src_w - 1;
Sonika Jindal48404c12014-08-22 14:06:04 +05303085 }
3086
Ville Syrjälä29490562016-01-20 18:02:50 +02003087 linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
Ville Syrjälä6687c902015-09-15 13:16:41 +03003088
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00003089 if (INTEL_GEN(dev_priv) < 4)
Ville Syrjälä6687c902015-09-15 13:16:41 +03003090 intel_crtc->dspaddr_offset = linear_offset;
3091
Paulo Zanoni2db33662015-09-14 15:20:03 -03003092 intel_crtc->adjusted_x = x;
3093 intel_crtc->adjusted_y = y;
3094
Sonika Jindal48404c12014-08-22 14:06:04 +05303095 I915_WRITE(reg, dspcntr);
3096
Ville Syrjälä01f2c772011-12-20 00:06:49 +02003097 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00003098 if (INTEL_GEN(dev_priv) >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01003099 I915_WRITE(DSPSURF(plane),
Ville Syrjälä6687c902015-09-15 13:16:41 +03003100 intel_fb_gtt_offset(fb, rotation) +
3101 intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01003102 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02003103 I915_WRITE(DSPLINOFF(plane), linear_offset);
Ville Syrjäläbfb81042016-11-07 22:20:57 +02003104 } else {
3105 I915_WRITE(DSPADDR(plane),
3106 intel_fb_gtt_offset(fb, rotation) +
3107 intel_crtc->dspaddr_offset);
3108 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003109 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07003110}
3111
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003112static void i9xx_disable_primary_plane(struct drm_plane *primary,
3113 struct drm_crtc *crtc)
Jesse Barnes17638cd2011-06-24 12:19:23 -07003114{
3115 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003116 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes17638cd2011-06-24 12:19:23 -07003117 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003118 int plane = intel_crtc->plane;
3119
3120 I915_WRITE(DSPCNTR(plane), 0);
3121 if (INTEL_INFO(dev_priv)->gen >= 4)
3122 I915_WRITE(DSPSURF(plane), 0);
3123 else
3124 I915_WRITE(DSPADDR(plane), 0);
3125 POSTING_READ(DSPCNTR(plane));
3126}
3127
3128static void ironlake_update_primary_plane(struct drm_plane *primary,
3129 const struct intel_crtc_state *crtc_state,
3130 const struct intel_plane_state *plane_state)
3131{
3132 struct drm_device *dev = primary->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003133 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003134 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3135 struct drm_framebuffer *fb = plane_state->base.fb;
Jesse Barnes17638cd2011-06-24 12:19:23 -07003136 int plane = intel_crtc->plane;
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02003137 u32 linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07003138 u32 dspcntr;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003139 i915_reg_t reg = DSPCNTR(plane);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02003140 unsigned int rotation = plane_state->base.rotation;
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003141 int x = plane_state->base.src.x1 >> 16;
3142 int y = plane_state->base.src.y1 >> 16;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03003143
Ville Syrjäläf45651b2014-08-08 21:51:10 +03003144 dspcntr = DISPPLANE_GAMMA_ENABLE;
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03003145 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03003146
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003147 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläf45651b2014-08-08 21:51:10 +03003148 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
3149
Ville Syrjälä57779d02012-10-31 17:50:14 +02003150 switch (fb->pixel_format) {
3151 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07003152 dspcntr |= DISPPLANE_8BPP;
3153 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02003154 case DRM_FORMAT_RGB565:
3155 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07003156 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02003157 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02003158 dspcntr |= DISPPLANE_BGRX888;
3159 break;
3160 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02003161 dspcntr |= DISPPLANE_RGBX888;
3162 break;
3163 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02003164 dspcntr |= DISPPLANE_BGRX101010;
3165 break;
3166 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02003167 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07003168 break;
3169 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01003170 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07003171 }
3172
Ville Syrjälä72618eb2016-02-04 20:38:20 +02003173 if (fb->modifier[0] == I915_FORMAT_MOD_X_TILED)
Jesse Barnes17638cd2011-06-24 12:19:23 -07003174 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07003175
Ville Syrjälädf0cd452016-11-14 18:53:59 +02003176 if (rotation & DRM_ROTATE_180)
3177 dspcntr |= DISPPLANE_ROTATE_180;
3178
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003179 if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03003180 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07003181
Ville Syrjälä29490562016-01-20 18:02:50 +02003182 intel_add_fb_offsets(&x, &y, plane_state, 0);
Ville Syrjälä6687c902015-09-15 13:16:41 +03003183
Daniel Vetterc2c75132012-07-05 12:17:30 +02003184 intel_crtc->dspaddr_offset =
Ville Syrjälä29490562016-01-20 18:02:50 +02003185 intel_compute_tile_offset(&x, &y, plane_state, 0);
Ville Syrjälä6687c902015-09-15 13:16:41 +03003186
Ville Syrjälädf0cd452016-11-14 18:53:59 +02003187 /* HSW+ does this automagically in hardware */
3188 if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv) &&
3189 rotation & DRM_ROTATE_180) {
3190 x += crtc_state->pipe_src_w - 1;
3191 y += crtc_state->pipe_src_h - 1;
Sonika Jindal48404c12014-08-22 14:06:04 +05303192 }
3193
Ville Syrjälä29490562016-01-20 18:02:50 +02003194 linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
Ville Syrjälä6687c902015-09-15 13:16:41 +03003195
Paulo Zanoni2db33662015-09-14 15:20:03 -03003196 intel_crtc->adjusted_x = x;
3197 intel_crtc->adjusted_y = y;
3198
Sonika Jindal48404c12014-08-22 14:06:04 +05303199 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07003200
Ville Syrjälä01f2c772011-12-20 00:06:49 +02003201 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01003202 I915_WRITE(DSPSURF(plane),
Ville Syrjälä6687c902015-09-15 13:16:41 +03003203 intel_fb_gtt_offset(fb, rotation) +
3204 intel_crtc->dspaddr_offset);
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003205 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00003206 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
3207 } else {
3208 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
3209 I915_WRITE(DSPLINOFF(plane), linear_offset);
3210 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07003211 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07003212}
3213
Ville Syrjälä7b49f942016-01-12 21:08:32 +02003214u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
3215 uint64_t fb_modifier, uint32_t pixel_format)
Damien Lespiaub3218032015-02-27 11:15:18 +00003216{
Ville Syrjälä7b49f942016-01-12 21:08:32 +02003217 if (fb_modifier == DRM_FORMAT_MOD_NONE) {
3218 return 64;
3219 } else {
3220 int cpp = drm_format_plane_cpp(pixel_format, 0);
Damien Lespiaub3218032015-02-27 11:15:18 +00003221
Ville Syrjälä27ba3912016-02-15 22:54:40 +02003222 return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
Damien Lespiaub3218032015-02-27 11:15:18 +00003223 }
3224}
3225
Ville Syrjälä6687c902015-09-15 13:16:41 +03003226u32 intel_fb_gtt_offset(struct drm_framebuffer *fb,
3227 unsigned int rotation)
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003228{
Ville Syrjälä6687c902015-09-15 13:16:41 +03003229 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Daniel Vetterce7f1722015-10-14 16:51:06 +02003230 struct i915_ggtt_view view;
Chris Wilson058d88c2016-08-15 10:49:06 +01003231 struct i915_vma *vma;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003232
Ville Syrjälä6687c902015-09-15 13:16:41 +03003233 intel_fill_fb_ggtt_view(&view, fb, rotation);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003234
Chris Wilson058d88c2016-08-15 10:49:06 +01003235 vma = i915_gem_object_to_ggtt(obj, &view);
3236 if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
3237 view.type))
3238 return -1;
3239
Chris Wilsonbde13eb2016-08-15 10:49:07 +01003240 return i915_ggtt_offset(vma);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003241}
3242
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02003243static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
3244{
3245 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003246 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02003247
3248 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
3249 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
3250 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02003251}
3252
Chandra Kondurua1b22782015-04-07 15:28:45 -07003253/*
3254 * This function detaches (aka. unbinds) unused scalers in hardware
3255 */
Maarten Lankhorst05832362015-06-15 12:33:48 +02003256static void skl_detach_scalers(struct intel_crtc *intel_crtc)
Chandra Kondurua1b22782015-04-07 15:28:45 -07003257{
Chandra Kondurua1b22782015-04-07 15:28:45 -07003258 struct intel_crtc_scaler_state *scaler_state;
3259 int i;
3260
Chandra Kondurua1b22782015-04-07 15:28:45 -07003261 scaler_state = &intel_crtc->config->scaler_state;
3262
3263 /* loop through and disable scalers that aren't in use */
3264 for (i = 0; i < intel_crtc->num_scalers; i++) {
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02003265 if (!scaler_state->scalers[i].in_use)
3266 skl_detach_scaler(intel_crtc, i);
Chandra Kondurua1b22782015-04-07 15:28:45 -07003267 }
3268}
3269
Ville Syrjäläd2196772016-01-28 18:33:11 +02003270u32 skl_plane_stride(const struct drm_framebuffer *fb, int plane,
3271 unsigned int rotation)
3272{
3273 const struct drm_i915_private *dev_priv = to_i915(fb->dev);
3274 u32 stride = intel_fb_pitch(fb, plane, rotation);
3275
3276 /*
3277 * The stride is either expressed as a multiple of 64 bytes chunks for
3278 * linear buffers or in number of tiles for tiled buffers.
3279 */
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03003280 if (drm_rotation_90_or_270(rotation)) {
Ville Syrjäläd2196772016-01-28 18:33:11 +02003281 int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
3282
3283 stride /= intel_tile_height(dev_priv, fb->modifier[0], cpp);
3284 } else {
3285 stride /= intel_fb_stride_alignment(dev_priv, fb->modifier[0],
3286 fb->pixel_format);
3287 }
3288
3289 return stride;
3290}
3291
Chandra Konduru6156a452015-04-27 13:48:39 -07003292u32 skl_plane_ctl_format(uint32_t pixel_format)
3293{
Chandra Konduru6156a452015-04-27 13:48:39 -07003294 switch (pixel_format) {
Damien Lespiaud161cf72015-05-12 16:13:17 +01003295 case DRM_FORMAT_C8:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003296 return PLANE_CTL_FORMAT_INDEXED;
Chandra Konduru6156a452015-04-27 13:48:39 -07003297 case DRM_FORMAT_RGB565:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003298 return PLANE_CTL_FORMAT_RGB_565;
Chandra Konduru6156a452015-04-27 13:48:39 -07003299 case DRM_FORMAT_XBGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003300 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
Chandra Konduru6156a452015-04-27 13:48:39 -07003301 case DRM_FORMAT_XRGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003302 return PLANE_CTL_FORMAT_XRGB_8888;
Chandra Konduru6156a452015-04-27 13:48:39 -07003303 /*
3304 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
3305 * to be already pre-multiplied. We need to add a knob (or a different
3306 * DRM_FORMAT) for user-space to configure that.
3307 */
3308 case DRM_FORMAT_ABGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003309 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
Chandra Konduru6156a452015-04-27 13:48:39 -07003310 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07003311 case DRM_FORMAT_ARGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003312 return PLANE_CTL_FORMAT_XRGB_8888 |
Chandra Konduru6156a452015-04-27 13:48:39 -07003313 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07003314 case DRM_FORMAT_XRGB2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003315 return PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07003316 case DRM_FORMAT_XBGR2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003317 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07003318 case DRM_FORMAT_YUYV:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003319 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
Chandra Konduru6156a452015-04-27 13:48:39 -07003320 case DRM_FORMAT_YVYU:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003321 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
Chandra Konduru6156a452015-04-27 13:48:39 -07003322 case DRM_FORMAT_UYVY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003323 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
Chandra Konduru6156a452015-04-27 13:48:39 -07003324 case DRM_FORMAT_VYUY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003325 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
Chandra Konduru6156a452015-04-27 13:48:39 -07003326 default:
Damien Lespiau4249eee2015-05-12 16:13:16 +01003327 MISSING_CASE(pixel_format);
Chandra Konduru6156a452015-04-27 13:48:39 -07003328 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01003329
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003330 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003331}
3332
3333u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
3334{
Chandra Konduru6156a452015-04-27 13:48:39 -07003335 switch (fb_modifier) {
3336 case DRM_FORMAT_MOD_NONE:
3337 break;
3338 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003339 return PLANE_CTL_TILED_X;
Chandra Konduru6156a452015-04-27 13:48:39 -07003340 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003341 return PLANE_CTL_TILED_Y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003342 case I915_FORMAT_MOD_Yf_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003343 return PLANE_CTL_TILED_YF;
Chandra Konduru6156a452015-04-27 13:48:39 -07003344 default:
3345 MISSING_CASE(fb_modifier);
3346 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01003347
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003348 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003349}
3350
3351u32 skl_plane_ctl_rotation(unsigned int rotation)
3352{
Chandra Konduru6156a452015-04-27 13:48:39 -07003353 switch (rotation) {
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +03003354 case DRM_ROTATE_0:
Chandra Konduru6156a452015-04-27 13:48:39 -07003355 break;
Sonika Jindal1e8df162015-05-20 13:40:48 +05303356 /*
3357 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
3358 * while i915 HW rotation is clockwise, thats why this swapping.
3359 */
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +03003360 case DRM_ROTATE_90:
Sonika Jindal1e8df162015-05-20 13:40:48 +05303361 return PLANE_CTL_ROTATE_270;
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +03003362 case DRM_ROTATE_180:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003363 return PLANE_CTL_ROTATE_180;
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +03003364 case DRM_ROTATE_270:
Sonika Jindal1e8df162015-05-20 13:40:48 +05303365 return PLANE_CTL_ROTATE_90;
Chandra Konduru6156a452015-04-27 13:48:39 -07003366 default:
3367 MISSING_CASE(rotation);
3368 }
3369
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003370 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003371}
3372
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003373static void skylake_update_primary_plane(struct drm_plane *plane,
3374 const struct intel_crtc_state *crtc_state,
3375 const struct intel_plane_state *plane_state)
Damien Lespiau70d21f02013-07-03 21:06:04 +01003376{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003377 struct drm_device *dev = plane->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003378 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003379 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3380 struct drm_framebuffer *fb = plane_state->base.fb;
Ville Syrjälä8e816bb2016-11-22 18:01:59 +02003381 enum plane_id plane_id = to_intel_plane(plane)->id;
3382 enum pipe pipe = to_intel_plane(plane)->pipe;
Ville Syrjäläd2196772016-01-28 18:33:11 +02003383 u32 plane_ctl;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003384 unsigned int rotation = plane_state->base.rotation;
Ville Syrjäläd2196772016-01-28 18:33:11 +02003385 u32 stride = skl_plane_stride(fb, 0, rotation);
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02003386 u32 surf_addr = plane_state->main.offset;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003387 int scaler_id = plane_state->scaler_id;
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02003388 int src_x = plane_state->main.x;
3389 int src_y = plane_state->main.y;
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003390 int src_w = drm_rect_width(&plane_state->base.src) >> 16;
3391 int src_h = drm_rect_height(&plane_state->base.src) >> 16;
3392 int dst_x = plane_state->base.dst.x1;
3393 int dst_y = plane_state->base.dst.y1;
3394 int dst_w = drm_rect_width(&plane_state->base.dst);
3395 int dst_h = drm_rect_height(&plane_state->base.dst);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003396
3397 plane_ctl = PLANE_CTL_ENABLE |
3398 PLANE_CTL_PIPE_GAMMA_ENABLE |
3399 PLANE_CTL_PIPE_CSC_ENABLE;
3400
Chandra Konduru6156a452015-04-27 13:48:39 -07003401 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3402 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003403 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Chandra Konduru6156a452015-04-27 13:48:39 -07003404 plane_ctl |= skl_plane_ctl_rotation(rotation);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003405
Ville Syrjälä6687c902015-09-15 13:16:41 +03003406 /* Sizes are 0 based */
3407 src_w--;
3408 src_h--;
3409 dst_w--;
3410 dst_h--;
3411
Paulo Zanoni4c0b8a82016-08-19 19:03:23 -03003412 intel_crtc->dspaddr_offset = surf_addr;
3413
Ville Syrjälä6687c902015-09-15 13:16:41 +03003414 intel_crtc->adjusted_x = src_x;
3415 intel_crtc->adjusted_y = src_y;
Paulo Zanoni2db33662015-09-14 15:20:03 -03003416
Ville Syrjälä8e816bb2016-11-22 18:01:59 +02003417 I915_WRITE(PLANE_CTL(pipe, plane_id), plane_ctl);
3418 I915_WRITE(PLANE_OFFSET(pipe, plane_id), (src_y << 16) | src_x);
3419 I915_WRITE(PLANE_STRIDE(pipe, plane_id), stride);
3420 I915_WRITE(PLANE_SIZE(pipe, plane_id), (src_h << 16) | src_w);
Chandra Konduru6156a452015-04-27 13:48:39 -07003421
3422 if (scaler_id >= 0) {
3423 uint32_t ps_ctrl = 0;
3424
3425 WARN_ON(!dst_w || !dst_h);
Ville Syrjälä8e816bb2016-11-22 18:01:59 +02003426 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(plane_id) |
Chandra Konduru6156a452015-04-27 13:48:39 -07003427 crtc_state->scaler_state.scalers[scaler_id].mode;
3428 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3429 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3430 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3431 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
Ville Syrjälä8e816bb2016-11-22 18:01:59 +02003432 I915_WRITE(PLANE_POS(pipe, plane_id), 0);
Chandra Konduru6156a452015-04-27 13:48:39 -07003433 } else {
Ville Syrjälä8e816bb2016-11-22 18:01:59 +02003434 I915_WRITE(PLANE_POS(pipe, plane_id), (dst_y << 16) | dst_x);
Chandra Konduru6156a452015-04-27 13:48:39 -07003435 }
3436
Ville Syrjälä8e816bb2016-11-22 18:01:59 +02003437 I915_WRITE(PLANE_SURF(pipe, plane_id),
Ville Syrjälä6687c902015-09-15 13:16:41 +03003438 intel_fb_gtt_offset(fb, rotation) + surf_addr);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003439
Ville Syrjälä8e816bb2016-11-22 18:01:59 +02003440 POSTING_READ(PLANE_SURF(pipe, plane_id));
Damien Lespiau70d21f02013-07-03 21:06:04 +01003441}
3442
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003443static void skylake_disable_primary_plane(struct drm_plane *primary,
3444 struct drm_crtc *crtc)
3445{
3446 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003447 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä8e816bb2016-11-22 18:01:59 +02003448 enum plane_id plane_id = to_intel_plane(primary)->id;
3449 enum pipe pipe = to_intel_plane(primary)->pipe;
Lyude62e0fb82016-08-22 12:50:08 -04003450
Ville Syrjälä8e816bb2016-11-22 18:01:59 +02003451 I915_WRITE(PLANE_CTL(pipe, plane_id), 0);
3452 I915_WRITE(PLANE_SURF(pipe, plane_id), 0);
3453 POSTING_READ(PLANE_SURF(pipe, plane_id));
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003454}
3455
Jesse Barnes17638cd2011-06-24 12:19:23 -07003456/* Assume fb object is pinned & idle & fenced and just update base pointers */
3457static int
3458intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3459 int x, int y, enum mode_set_atomic state)
3460{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003461 /* Support for kgdboc is disabled, this needs a major rework. */
3462 DRM_ERROR("legacy panic handler not supported any more.\n");
Jesse Barnes17638cd2011-06-24 12:19:23 -07003463
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003464 return -ENODEV;
Jesse Barnes81255562010-08-02 12:07:50 -07003465}
3466
Daniel Vetter5a21b662016-05-24 17:13:53 +02003467static void intel_complete_page_flips(struct drm_i915_private *dev_priv)
3468{
3469 struct intel_crtc *crtc;
3470
Chris Wilson91c8a322016-07-05 10:40:23 +01003471 for_each_intel_crtc(&dev_priv->drm, crtc)
Daniel Vetter5a21b662016-05-24 17:13:53 +02003472 intel_finish_page_flip_cs(dev_priv, crtc->pipe);
3473}
3474
Ville Syrjälä75147472014-11-24 18:28:11 +02003475static void intel_update_primary_planes(struct drm_device *dev)
3476{
Ville Syrjälä75147472014-11-24 18:28:11 +02003477 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003478
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003479 for_each_crtc(dev, crtc) {
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003480 struct intel_plane *plane = to_intel_plane(crtc->primary);
Maarten Lankhorst73974892016-08-05 23:28:27 +03003481 struct intel_plane_state *plane_state =
3482 to_intel_plane_state(plane->base.state);
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003483
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003484 if (plane_state->base.visible)
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003485 plane->update_plane(&plane->base,
3486 to_intel_crtc_state(crtc->state),
3487 plane_state);
Ville Syrjälä96a02912013-02-18 19:08:49 +02003488 }
3489}
3490
Maarten Lankhorst73974892016-08-05 23:28:27 +03003491static int
3492__intel_display_resume(struct drm_device *dev,
3493 struct drm_atomic_state *state)
3494{
3495 struct drm_crtc_state *crtc_state;
3496 struct drm_crtc *crtc;
3497 int i, ret;
3498
3499 intel_modeset_setup_hw_state(dev);
Tvrtko Ursulin29b74b72016-11-16 08:55:39 +00003500 i915_redisable_vga(to_i915(dev));
Maarten Lankhorst73974892016-08-05 23:28:27 +03003501
3502 if (!state)
3503 return 0;
3504
3505 for_each_crtc_in_state(state, crtc, crtc_state, i) {
3506 /*
3507 * Force recalculation even if we restore
3508 * current state. With fast modeset this may not result
3509 * in a modeset when the state is compatible.
3510 */
3511 crtc_state->mode_changed = true;
3512 }
3513
3514 /* ignore any reset values/BIOS leftovers in the WM registers */
3515 to_intel_atomic_state(state)->skip_intermediate_wm = true;
3516
3517 ret = drm_atomic_commit(state);
3518
3519 WARN_ON(ret == -EDEADLK);
3520 return ret;
3521}
3522
Ville Syrjälä4ac2ba22016-08-05 23:28:29 +03003523static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
3524{
Ville Syrjäläae981042016-08-05 23:28:30 +03003525 return intel_has_gpu_reset(dev_priv) &&
3526 INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv);
Ville Syrjälä4ac2ba22016-08-05 23:28:29 +03003527}
3528
Chris Wilsonc0336662016-05-06 15:40:21 +01003529void intel_prepare_reset(struct drm_i915_private *dev_priv)
Ville Syrjälä75147472014-11-24 18:28:11 +02003530{
Maarten Lankhorst73974892016-08-05 23:28:27 +03003531 struct drm_device *dev = &dev_priv->drm;
3532 struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
3533 struct drm_atomic_state *state;
3534 int ret;
3535
Maarten Lankhorst73974892016-08-05 23:28:27 +03003536 /*
3537 * Need mode_config.mutex so that we don't
3538 * trample ongoing ->detect() and whatnot.
3539 */
3540 mutex_lock(&dev->mode_config.mutex);
3541 drm_modeset_acquire_init(ctx, 0);
3542 while (1) {
3543 ret = drm_modeset_lock_all_ctx(dev, ctx);
3544 if (ret != -EDEADLK)
3545 break;
3546
3547 drm_modeset_backoff(ctx);
3548 }
3549
3550 /* reset doesn't touch the display, but flips might get nuked anyway, */
Maarten Lankhorst522a63d2016-08-05 23:28:28 +03003551 if (!i915.force_reset_modeset_test &&
Ville Syrjälä4ac2ba22016-08-05 23:28:29 +03003552 !gpu_reset_clobbers_display(dev_priv))
Ville Syrjälä75147472014-11-24 18:28:11 +02003553 return;
3554
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003555 /*
3556 * Disabling the crtcs gracefully seems nicer. Also the
3557 * g33 docs say we should at least disable all the planes.
3558 */
Maarten Lankhorst73974892016-08-05 23:28:27 +03003559 state = drm_atomic_helper_duplicate_state(dev, ctx);
3560 if (IS_ERR(state)) {
3561 ret = PTR_ERR(state);
3562 state = NULL;
3563 DRM_ERROR("Duplicating state failed with %i\n", ret);
3564 goto err;
3565 }
3566
3567 ret = drm_atomic_helper_disable_all(dev, ctx);
3568 if (ret) {
3569 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
3570 goto err;
3571 }
3572
3573 dev_priv->modeset_restore_state = state;
3574 state->acquire_ctx = ctx;
3575 return;
3576
3577err:
Chris Wilson08536952016-10-14 13:18:18 +01003578 drm_atomic_state_put(state);
Ville Syrjälä75147472014-11-24 18:28:11 +02003579}
3580
Chris Wilsonc0336662016-05-06 15:40:21 +01003581void intel_finish_reset(struct drm_i915_private *dev_priv)
Ville Syrjälä75147472014-11-24 18:28:11 +02003582{
Maarten Lankhorst73974892016-08-05 23:28:27 +03003583 struct drm_device *dev = &dev_priv->drm;
3584 struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
3585 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
3586 int ret;
3587
Daniel Vetter5a21b662016-05-24 17:13:53 +02003588 /*
3589 * Flips in the rings will be nuked by the reset,
3590 * so complete all pending flips so that user space
3591 * will get its events and not get stuck.
3592 */
3593 intel_complete_page_flips(dev_priv);
3594
Maarten Lankhorst73974892016-08-05 23:28:27 +03003595 dev_priv->modeset_restore_state = NULL;
3596
Ville Syrjälä75147472014-11-24 18:28:11 +02003597 /* reset doesn't touch the display */
Ville Syrjälä4ac2ba22016-08-05 23:28:29 +03003598 if (!gpu_reset_clobbers_display(dev_priv)) {
Maarten Lankhorst522a63d2016-08-05 23:28:28 +03003599 if (!state) {
3600 /*
3601 * Flips in the rings have been nuked by the reset,
3602 * so update the base address of all primary
3603 * planes to the the last fb to make sure we're
3604 * showing the correct fb after a reset.
3605 *
3606 * FIXME: Atomic will make this obsolete since we won't schedule
3607 * CS-based flips (which might get lost in gpu resets) any more.
3608 */
3609 intel_update_primary_planes(dev);
3610 } else {
3611 ret = __intel_display_resume(dev, state);
3612 if (ret)
3613 DRM_ERROR("Restoring old state failed with %i\n", ret);
3614 }
Maarten Lankhorst73974892016-08-05 23:28:27 +03003615 } else {
3616 /*
3617 * The display has been reset as well,
3618 * so need a full re-initialization.
3619 */
3620 intel_runtime_pm_disable_interrupts(dev_priv);
3621 intel_runtime_pm_enable_interrupts(dev_priv);
3622
Imre Deak51f59202016-09-14 13:04:13 +03003623 intel_pps_unlock_regs_wa(dev_priv);
Maarten Lankhorst73974892016-08-05 23:28:27 +03003624 intel_modeset_init_hw(dev);
3625
3626 spin_lock_irq(&dev_priv->irq_lock);
3627 if (dev_priv->display.hpd_irq_setup)
3628 dev_priv->display.hpd_irq_setup(dev_priv);
3629 spin_unlock_irq(&dev_priv->irq_lock);
3630
3631 ret = __intel_display_resume(dev, state);
3632 if (ret)
3633 DRM_ERROR("Restoring old state failed with %i\n", ret);
3634
3635 intel_hpd_init(dev_priv);
Ville Syrjälä75147472014-11-24 18:28:11 +02003636 }
3637
Chris Wilson08536952016-10-14 13:18:18 +01003638 if (state)
3639 drm_atomic_state_put(state);
Maarten Lankhorst73974892016-08-05 23:28:27 +03003640 drm_modeset_drop_locks(ctx);
3641 drm_modeset_acquire_fini(ctx);
3642 mutex_unlock(&dev->mode_config.mutex);
Ville Syrjälä75147472014-11-24 18:28:11 +02003643}
3644
Chris Wilson8af29b02016-09-09 14:11:47 +01003645static bool abort_flip_on_reset(struct intel_crtc *crtc)
3646{
3647 struct i915_gpu_error *error = &to_i915(crtc->base.dev)->gpu_error;
3648
3649 if (i915_reset_in_progress(error))
3650 return true;
3651
3652 if (crtc->reset_count != i915_reset_count(error))
3653 return true;
3654
3655 return false;
3656}
3657
Chris Wilson7d5e3792014-03-04 13:15:08 +00003658static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3659{
Daniel Vetter5a21b662016-05-24 17:13:53 +02003660 struct drm_device *dev = crtc->dev;
3661 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +02003662 bool pending;
3663
Chris Wilson8af29b02016-09-09 14:11:47 +01003664 if (abort_flip_on_reset(intel_crtc))
Daniel Vetter5a21b662016-05-24 17:13:53 +02003665 return false;
3666
3667 spin_lock_irq(&dev->event_lock);
3668 pending = to_intel_crtc(crtc)->flip_work != NULL;
3669 spin_unlock_irq(&dev->event_lock);
3670
3671 return pending;
Chris Wilson7d5e3792014-03-04 13:15:08 +00003672}
3673
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003674static void intel_update_pipe_config(struct intel_crtc *crtc,
3675 struct intel_crtc_state *old_crtc_state)
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003676{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00003677 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003678 struct intel_crtc_state *pipe_config =
3679 to_intel_crtc_state(crtc->base.state);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003680
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003681 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
3682 crtc->base.mode = crtc->base.state->mode;
3683
3684 DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
3685 old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
3686 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003687
3688 /*
3689 * Update pipe size and adjust fitter if needed: the reason for this is
3690 * that in compute_mode_changes we check the native mode (not the pfit
3691 * mode) to see if we can flip rather than do a full mode set. In the
3692 * fastboot case, we'll flip, but if we don't update the pipesrc and
3693 * pfit state, we'll end up with a big fb scanned out into the wrong
3694 * sized surface.
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003695 */
3696
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003697 I915_WRITE(PIPESRC(crtc->pipe),
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003698 ((pipe_config->pipe_src_w - 1) << 16) |
3699 (pipe_config->pipe_src_h - 1));
3700
3701 /* on skylake this is done by detaching scalers */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00003702 if (INTEL_GEN(dev_priv) >= 9) {
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003703 skl_detach_scalers(crtc);
3704
3705 if (pipe_config->pch_pfit.enabled)
3706 skylake_pfit_enable(crtc);
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01003707 } else if (HAS_PCH_SPLIT(dev_priv)) {
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003708 if (pipe_config->pch_pfit.enabled)
3709 ironlake_pfit_enable(crtc);
3710 else if (old_crtc_state->pch_pfit.enabled)
3711 ironlake_pfit_disable(crtc, true);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003712 }
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003713}
3714
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003715static void intel_fdi_normal_train(struct drm_crtc *crtc)
3716{
3717 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003718 struct drm_i915_private *dev_priv = to_i915(dev);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003719 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3720 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003721 i915_reg_t reg;
3722 u32 temp;
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003723
3724 /* enable normal train */
3725 reg = FDI_TX_CTL(pipe);
3726 temp = I915_READ(reg);
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01003727 if (IS_IVYBRIDGE(dev_priv)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003728 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3729 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003730 } else {
3731 temp &= ~FDI_LINK_TRAIN_NONE;
3732 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003733 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003734 I915_WRITE(reg, temp);
3735
3736 reg = FDI_RX_CTL(pipe);
3737 temp = I915_READ(reg);
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01003738 if (HAS_PCH_CPT(dev_priv)) {
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003739 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3740 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3741 } else {
3742 temp &= ~FDI_LINK_TRAIN_NONE;
3743 temp |= FDI_LINK_TRAIN_NONE;
3744 }
3745 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3746
3747 /* wait one idle pattern time */
3748 POSTING_READ(reg);
3749 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003750
3751 /* IVB wants error correction enabled */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01003752 if (IS_IVYBRIDGE(dev_priv))
Jesse Barnes357555c2011-04-28 15:09:55 -07003753 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3754 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003755}
3756
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003757/* The FDI link training functions for ILK/Ibexpeak. */
3758static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3759{
3760 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003761 struct drm_i915_private *dev_priv = to_i915(dev);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003762 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3763 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003764 i915_reg_t reg;
3765 u32 temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003766
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003767 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003768 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003769
Adam Jacksone1a44742010-06-25 15:32:14 -04003770 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3771 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003772 reg = FDI_RX_IMR(pipe);
3773 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003774 temp &= ~FDI_RX_SYMBOL_LOCK;
3775 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003776 I915_WRITE(reg, temp);
3777 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003778 udelay(150);
3779
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003780 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003781 reg = FDI_TX_CTL(pipe);
3782 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003783 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003784 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003785 temp &= ~FDI_LINK_TRAIN_NONE;
3786 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003787 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003788
Chris Wilson5eddb702010-09-11 13:48:45 +01003789 reg = FDI_RX_CTL(pipe);
3790 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003791 temp &= ~FDI_LINK_TRAIN_NONE;
3792 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003793 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3794
3795 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003796 udelay(150);
3797
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003798 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003799 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3800 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3801 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003802
Chris Wilson5eddb702010-09-11 13:48:45 +01003803 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003804 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003805 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003806 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3807
3808 if ((temp & FDI_RX_BIT_LOCK)) {
3809 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003810 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003811 break;
3812 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003813 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003814 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003815 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003816
3817 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003818 reg = FDI_TX_CTL(pipe);
3819 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003820 temp &= ~FDI_LINK_TRAIN_NONE;
3821 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003822 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003823
Chris Wilson5eddb702010-09-11 13:48:45 +01003824 reg = FDI_RX_CTL(pipe);
3825 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003826 temp &= ~FDI_LINK_TRAIN_NONE;
3827 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003828 I915_WRITE(reg, temp);
3829
3830 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003831 udelay(150);
3832
Chris Wilson5eddb702010-09-11 13:48:45 +01003833 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003834 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003835 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003836 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3837
3838 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003839 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003840 DRM_DEBUG_KMS("FDI train 2 done.\n");
3841 break;
3842 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003843 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003844 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003845 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003846
3847 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003848
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003849}
3850
Akshay Joshi0206e352011-08-16 15:34:10 -04003851static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003852 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3853 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3854 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3855 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3856};
3857
3858/* The FDI link training functions for SNB/Cougarpoint. */
3859static void gen6_fdi_link_train(struct drm_crtc *crtc)
3860{
3861 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003862 struct drm_i915_private *dev_priv = to_i915(dev);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003863 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3864 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003865 i915_reg_t reg;
3866 u32 temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003867
Adam Jacksone1a44742010-06-25 15:32:14 -04003868 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3869 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003870 reg = FDI_RX_IMR(pipe);
3871 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003872 temp &= ~FDI_RX_SYMBOL_LOCK;
3873 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003874 I915_WRITE(reg, temp);
3875
3876 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003877 udelay(150);
3878
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003879 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003880 reg = FDI_TX_CTL(pipe);
3881 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003882 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003883 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003884 temp &= ~FDI_LINK_TRAIN_NONE;
3885 temp |= FDI_LINK_TRAIN_PATTERN_1;
3886 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3887 /* SNB-B */
3888 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003889 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003890
Daniel Vetterd74cf322012-10-26 10:58:13 +02003891 I915_WRITE(FDI_RX_MISC(pipe),
3892 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3893
Chris Wilson5eddb702010-09-11 13:48:45 +01003894 reg = FDI_RX_CTL(pipe);
3895 temp = I915_READ(reg);
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01003896 if (HAS_PCH_CPT(dev_priv)) {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003897 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3898 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3899 } else {
3900 temp &= ~FDI_LINK_TRAIN_NONE;
3901 temp |= FDI_LINK_TRAIN_PATTERN_1;
3902 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003903 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3904
3905 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003906 udelay(150);
3907
Akshay Joshi0206e352011-08-16 15:34:10 -04003908 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003909 reg = FDI_TX_CTL(pipe);
3910 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003911 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3912 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003913 I915_WRITE(reg, temp);
3914
3915 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003916 udelay(500);
3917
Sean Paulfa37d392012-03-02 12:53:39 -05003918 for (retry = 0; retry < 5; retry++) {
3919 reg = FDI_RX_IIR(pipe);
3920 temp = I915_READ(reg);
3921 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3922 if (temp & FDI_RX_BIT_LOCK) {
3923 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3924 DRM_DEBUG_KMS("FDI train 1 done.\n");
3925 break;
3926 }
3927 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003928 }
Sean Paulfa37d392012-03-02 12:53:39 -05003929 if (retry < 5)
3930 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003931 }
3932 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003933 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003934
3935 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003936 reg = FDI_TX_CTL(pipe);
3937 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003938 temp &= ~FDI_LINK_TRAIN_NONE;
3939 temp |= FDI_LINK_TRAIN_PATTERN_2;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003940 if (IS_GEN6(dev_priv)) {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003941 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3942 /* SNB-B */
3943 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3944 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003945 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003946
Chris Wilson5eddb702010-09-11 13:48:45 +01003947 reg = FDI_RX_CTL(pipe);
3948 temp = I915_READ(reg);
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01003949 if (HAS_PCH_CPT(dev_priv)) {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003950 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3951 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3952 } else {
3953 temp &= ~FDI_LINK_TRAIN_NONE;
3954 temp |= FDI_LINK_TRAIN_PATTERN_2;
3955 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003956 I915_WRITE(reg, temp);
3957
3958 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003959 udelay(150);
3960
Akshay Joshi0206e352011-08-16 15:34:10 -04003961 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003962 reg = FDI_TX_CTL(pipe);
3963 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003964 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3965 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003966 I915_WRITE(reg, temp);
3967
3968 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003969 udelay(500);
3970
Sean Paulfa37d392012-03-02 12:53:39 -05003971 for (retry = 0; retry < 5; retry++) {
3972 reg = FDI_RX_IIR(pipe);
3973 temp = I915_READ(reg);
3974 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3975 if (temp & FDI_RX_SYMBOL_LOCK) {
3976 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3977 DRM_DEBUG_KMS("FDI train 2 done.\n");
3978 break;
3979 }
3980 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003981 }
Sean Paulfa37d392012-03-02 12:53:39 -05003982 if (retry < 5)
3983 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003984 }
3985 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003986 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003987
3988 DRM_DEBUG_KMS("FDI train done.\n");
3989}
3990
Jesse Barnes357555c2011-04-28 15:09:55 -07003991/* Manual link training for Ivy Bridge A0 parts */
3992static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3993{
3994 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003995 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes357555c2011-04-28 15:09:55 -07003996 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3997 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003998 i915_reg_t reg;
3999 u32 temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07004000
4001 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
4002 for train result */
4003 reg = FDI_RX_IMR(pipe);
4004 temp = I915_READ(reg);
4005 temp &= ~FDI_RX_SYMBOL_LOCK;
4006 temp &= ~FDI_RX_BIT_LOCK;
4007 I915_WRITE(reg, temp);
4008
4009 POSTING_READ(reg);
4010 udelay(150);
4011
Daniel Vetter01a415f2012-10-27 15:58:40 +02004012 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
4013 I915_READ(FDI_RX_IIR(pipe)));
4014
Jesse Barnes139ccd32013-08-19 11:04:55 -07004015 /* Try each vswing and preemphasis setting twice before moving on */
4016 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
4017 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07004018 reg = FDI_TX_CTL(pipe);
4019 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07004020 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
4021 temp &= ~FDI_TX_ENABLE;
4022 I915_WRITE(reg, temp);
4023
4024 reg = FDI_RX_CTL(pipe);
4025 temp = I915_READ(reg);
4026 temp &= ~FDI_LINK_TRAIN_AUTO;
4027 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4028 temp &= ~FDI_RX_ENABLE;
4029 I915_WRITE(reg, temp);
4030
4031 /* enable CPU FDI TX and PCH FDI RX */
4032 reg = FDI_TX_CTL(pipe);
4033 temp = I915_READ(reg);
4034 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004035 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07004036 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07004037 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07004038 temp |= snb_b_fdi_train_param[j/2];
4039 temp |= FDI_COMPOSITE_SYNC;
4040 I915_WRITE(reg, temp | FDI_TX_ENABLE);
4041
4042 I915_WRITE(FDI_RX_MISC(pipe),
4043 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
4044
4045 reg = FDI_RX_CTL(pipe);
4046 temp = I915_READ(reg);
4047 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
4048 temp |= FDI_COMPOSITE_SYNC;
4049 I915_WRITE(reg, temp | FDI_RX_ENABLE);
4050
4051 POSTING_READ(reg);
4052 udelay(1); /* should be 0.5us */
4053
4054 for (i = 0; i < 4; i++) {
4055 reg = FDI_RX_IIR(pipe);
4056 temp = I915_READ(reg);
4057 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
4058
4059 if (temp & FDI_RX_BIT_LOCK ||
4060 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
4061 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
4062 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
4063 i);
4064 break;
4065 }
4066 udelay(1); /* should be 0.5us */
4067 }
4068 if (i == 4) {
4069 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
4070 continue;
4071 }
4072
4073 /* Train 2 */
4074 reg = FDI_TX_CTL(pipe);
4075 temp = I915_READ(reg);
4076 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
4077 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
4078 I915_WRITE(reg, temp);
4079
4080 reg = FDI_RX_CTL(pipe);
4081 temp = I915_READ(reg);
4082 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4083 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07004084 I915_WRITE(reg, temp);
4085
4086 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07004087 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07004088
Jesse Barnes139ccd32013-08-19 11:04:55 -07004089 for (i = 0; i < 4; i++) {
4090 reg = FDI_RX_IIR(pipe);
4091 temp = I915_READ(reg);
4092 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07004093
Jesse Barnes139ccd32013-08-19 11:04:55 -07004094 if (temp & FDI_RX_SYMBOL_LOCK ||
4095 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
4096 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
4097 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
4098 i);
4099 goto train_done;
4100 }
4101 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07004102 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07004103 if (i == 4)
4104 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07004105 }
Jesse Barnes357555c2011-04-28 15:09:55 -07004106
Jesse Barnes139ccd32013-08-19 11:04:55 -07004107train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07004108 DRM_DEBUG_KMS("FDI train done.\n");
4109}
4110
Daniel Vetter88cefb62012-08-12 19:27:14 +02004111static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07004112{
Daniel Vetter88cefb62012-08-12 19:27:14 +02004113 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004114 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes0e23b992010-09-10 11:10:00 -07004115 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004116 i915_reg_t reg;
4117 u32 temp;
Jesse Barnesc64e3112010-09-10 11:27:03 -07004118
Jesse Barnes0e23b992010-09-10 11:10:00 -07004119 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01004120 reg = FDI_RX_CTL(pipe);
4121 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02004122 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004123 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004124 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01004125 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
4126
4127 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07004128 udelay(200);
4129
4130 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01004131 temp = I915_READ(reg);
4132 I915_WRITE(reg, temp | FDI_PCDCLK);
4133
4134 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07004135 udelay(200);
4136
Paulo Zanoni20749732012-11-23 15:30:38 -02004137 /* Enable CPU FDI TX PLL, always on for Ironlake */
4138 reg = FDI_TX_CTL(pipe);
4139 temp = I915_READ(reg);
4140 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
4141 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01004142
Paulo Zanoni20749732012-11-23 15:30:38 -02004143 POSTING_READ(reg);
4144 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07004145 }
4146}
4147
Daniel Vetter88cefb62012-08-12 19:27:14 +02004148static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
4149{
4150 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004151 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter88cefb62012-08-12 19:27:14 +02004152 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004153 i915_reg_t reg;
4154 u32 temp;
Daniel Vetter88cefb62012-08-12 19:27:14 +02004155
4156 /* Switch from PCDclk to Rawclk */
4157 reg = FDI_RX_CTL(pipe);
4158 temp = I915_READ(reg);
4159 I915_WRITE(reg, temp & ~FDI_PCDCLK);
4160
4161 /* Disable CPU FDI TX PLL */
4162 reg = FDI_TX_CTL(pipe);
4163 temp = I915_READ(reg);
4164 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
4165
4166 POSTING_READ(reg);
4167 udelay(100);
4168
4169 reg = FDI_RX_CTL(pipe);
4170 temp = I915_READ(reg);
4171 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
4172
4173 /* Wait for the clocks to turn off. */
4174 POSTING_READ(reg);
4175 udelay(100);
4176}
4177
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004178static void ironlake_fdi_disable(struct drm_crtc *crtc)
4179{
4180 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004181 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004182 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4183 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004184 i915_reg_t reg;
4185 u32 temp;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004186
4187 /* disable CPU FDI tx and PCH FDI rx */
4188 reg = FDI_TX_CTL(pipe);
4189 temp = I915_READ(reg);
4190 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
4191 POSTING_READ(reg);
4192
4193 reg = FDI_RX_CTL(pipe);
4194 temp = I915_READ(reg);
4195 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004196 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004197 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
4198
4199 POSTING_READ(reg);
4200 udelay(100);
4201
4202 /* Ironlake workaround, disable clock pointer after downing FDI */
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01004203 if (HAS_PCH_IBX(dev_priv))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08004204 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004205
4206 /* still set train pattern 1 */
4207 reg = FDI_TX_CTL(pipe);
4208 temp = I915_READ(reg);
4209 temp &= ~FDI_LINK_TRAIN_NONE;
4210 temp |= FDI_LINK_TRAIN_PATTERN_1;
4211 I915_WRITE(reg, temp);
4212
4213 reg = FDI_RX_CTL(pipe);
4214 temp = I915_READ(reg);
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01004215 if (HAS_PCH_CPT(dev_priv)) {
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004216 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4217 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
4218 } else {
4219 temp &= ~FDI_LINK_TRAIN_NONE;
4220 temp |= FDI_LINK_TRAIN_PATTERN_1;
4221 }
4222 /* BPC in FDI rx is consistent with that in PIPECONF */
4223 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004224 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004225 I915_WRITE(reg, temp);
4226
4227 POSTING_READ(reg);
4228 udelay(100);
4229}
4230
Chris Wilson5dce5b932014-01-20 10:17:36 +00004231bool intel_has_pending_fb_unpin(struct drm_device *dev)
4232{
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02004233 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson5dce5b932014-01-20 10:17:36 +00004234 struct intel_crtc *crtc;
4235
4236 /* Note that we don't need to be called with mode_config.lock here
4237 * as our list of CRTC objects is static for the lifetime of the
4238 * device and so cannot disappear as we iterate. Similarly, we can
4239 * happily treat the predicates as racy, atomic checks as userspace
4240 * cannot claim and pin a new fb without at least acquring the
4241 * struct_mutex and so serialising with us.
4242 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004243 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00004244 if (atomic_read(&crtc->unpin_work_count) == 0)
4245 continue;
4246
Daniel Vetter5a21b662016-05-24 17:13:53 +02004247 if (crtc->flip_work)
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02004248 intel_wait_for_vblank(dev_priv, crtc->pipe);
Chris Wilson5dce5b932014-01-20 10:17:36 +00004249
4250 return true;
4251 }
4252
4253 return false;
4254}
4255
Daniel Vetter5a21b662016-05-24 17:13:53 +02004256static void page_flip_completed(struct intel_crtc *intel_crtc)
Chris Wilsond6bbafa2014-09-05 07:13:24 +01004257{
4258 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +02004259 struct intel_flip_work *work = intel_crtc->flip_work;
4260
4261 intel_crtc->flip_work = NULL;
Chris Wilsond6bbafa2014-09-05 07:13:24 +01004262
4263 if (work->event)
Gustavo Padovan560ce1d2016-04-14 10:48:15 -07004264 drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
Chris Wilsond6bbafa2014-09-05 07:13:24 +01004265
4266 drm_crtc_vblank_put(&intel_crtc->base);
4267
Daniel Vetter5a21b662016-05-24 17:13:53 +02004268 wake_up_all(&dev_priv->pending_flip_queue);
Maarten Lankhorst143f73b32016-05-17 15:07:54 +02004269 queue_work(dev_priv->wq, &work->unpin_work);
Daniel Vetter5a21b662016-05-24 17:13:53 +02004270
4271 trace_i915_flip_complete(intel_crtc->plane,
4272 work->pending_flip_obj);
Chris Wilsond6bbafa2014-09-05 07:13:24 +01004273}
4274
Maarten Lankhorst5008e872015-08-18 13:40:05 +02004275static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01004276{
Chris Wilson0f911282012-04-17 10:05:38 +01004277 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004278 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst5008e872015-08-18 13:40:05 +02004279 long ret;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01004280
Daniel Vetter2c10d572012-12-20 21:24:07 +01004281 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Maarten Lankhorst5008e872015-08-18 13:40:05 +02004282
4283 ret = wait_event_interruptible_timeout(
4284 dev_priv->pending_flip_queue,
4285 !intel_crtc_has_pending_flip(crtc),
4286 60*HZ);
4287
4288 if (ret < 0)
4289 return ret;
4290
Daniel Vetter5a21b662016-05-24 17:13:53 +02004291 if (ret == 0) {
4292 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4293 struct intel_flip_work *work;
4294
4295 spin_lock_irq(&dev->event_lock);
4296 work = intel_crtc->flip_work;
4297 if (work && !is_mmio_work(work)) {
4298 WARN_ONCE(1, "Removing stuck page flip\n");
4299 page_flip_completed(intel_crtc);
4300 }
4301 spin_unlock_irq(&dev->event_lock);
4302 }
Chris Wilson5bb61642012-09-27 21:25:58 +01004303
Maarten Lankhorst5008e872015-08-18 13:40:05 +02004304 return 0;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01004305}
4306
Maarten Lankhorstb7076542016-08-23 16:18:08 +02004307void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
Ville Syrjälä060f02d2015-12-04 22:21:34 +02004308{
4309 u32 temp;
4310
4311 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
4312
4313 mutex_lock(&dev_priv->sb_lock);
4314
4315 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
4316 temp |= SBI_SSCCTL_DISABLE;
4317 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
4318
4319 mutex_unlock(&dev_priv->sb_lock);
4320}
4321
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004322/* Program iCLKIP clock to the desired frequency */
4323static void lpt_program_iclkip(struct drm_crtc *crtc)
4324{
Ville Syrjälä64b46a02016-02-17 21:41:11 +02004325 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004326 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004327 u32 divsel, phaseinc, auxdiv, phasedir = 0;
4328 u32 temp;
4329
Ville Syrjälä060f02d2015-12-04 22:21:34 +02004330 lpt_disable_iclkip(dev_priv);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004331
Ville Syrjälä64b46a02016-02-17 21:41:11 +02004332 /* The iCLK virtual clock root frequency is in MHz,
4333 * but the adjusted_mode->crtc_clock in in KHz. To get the
4334 * divisors, it is necessary to divide one by another, so we
4335 * convert the virtual clock precision to KHz here for higher
4336 * precision.
4337 */
4338 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004339 u32 iclk_virtual_root_freq = 172800 * 1000;
4340 u32 iclk_pi_range = 64;
Ville Syrjälä64b46a02016-02-17 21:41:11 +02004341 u32 desired_divisor;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004342
Ville Syrjälä64b46a02016-02-17 21:41:11 +02004343 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4344 clock << auxdiv);
4345 divsel = (desired_divisor / iclk_pi_range) - 2;
4346 phaseinc = desired_divisor % iclk_pi_range;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004347
Ville Syrjälä64b46a02016-02-17 21:41:11 +02004348 /*
4349 * Near 20MHz is a corner case which is
4350 * out of range for the 7-bit divisor
4351 */
4352 if (divsel <= 0x7f)
4353 break;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004354 }
4355
4356 /* This should not happen with any sane values */
4357 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
4358 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
4359 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
4360 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
4361
4362 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 +03004363 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004364 auxdiv,
4365 divsel,
4366 phasedir,
4367 phaseinc);
4368
Ville Syrjälä060f02d2015-12-04 22:21:34 +02004369 mutex_lock(&dev_priv->sb_lock);
4370
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004371 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004372 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004373 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
4374 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
4375 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
4376 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
4377 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
4378 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004379 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004380
4381 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004382 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004383 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
4384 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004385 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004386
4387 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004388 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004389 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004390 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004391
Ville Syrjälä060f02d2015-12-04 22:21:34 +02004392 mutex_unlock(&dev_priv->sb_lock);
4393
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004394 /* Wait for initialization time */
4395 udelay(24);
4396
4397 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
4398}
4399
Ville Syrjälä8802e5b2016-02-17 21:41:12 +02004400int lpt_get_iclkip(struct drm_i915_private *dev_priv)
4401{
4402 u32 divsel, phaseinc, auxdiv;
4403 u32 iclk_virtual_root_freq = 172800 * 1000;
4404 u32 iclk_pi_range = 64;
4405 u32 desired_divisor;
4406 u32 temp;
4407
4408 if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
4409 return 0;
4410
4411 mutex_lock(&dev_priv->sb_lock);
4412
4413 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
4414 if (temp & SBI_SSCCTL_DISABLE) {
4415 mutex_unlock(&dev_priv->sb_lock);
4416 return 0;
4417 }
4418
4419 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
4420 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
4421 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
4422 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
4423 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
4424
4425 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
4426 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
4427 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
4428
4429 mutex_unlock(&dev_priv->sb_lock);
4430
4431 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
4432
4433 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4434 desired_divisor << auxdiv);
4435}
4436
Daniel Vetter275f01b22013-05-03 11:49:47 +02004437static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4438 enum pipe pch_transcoder)
4439{
4440 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004441 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004442 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02004443
4444 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4445 I915_READ(HTOTAL(cpu_transcoder)));
4446 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4447 I915_READ(HBLANK(cpu_transcoder)));
4448 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4449 I915_READ(HSYNC(cpu_transcoder)));
4450
4451 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4452 I915_READ(VTOTAL(cpu_transcoder)));
4453 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4454 I915_READ(VBLANK(cpu_transcoder)));
4455 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4456 I915_READ(VSYNC(cpu_transcoder)));
4457 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4458 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4459}
4460
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004461static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004462{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004463 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004464 uint32_t temp;
4465
4466 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004467 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004468 return;
4469
4470 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4471 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4472
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004473 temp &= ~FDI_BC_BIFURCATION_SELECT;
4474 if (enable)
4475 temp |= FDI_BC_BIFURCATION_SELECT;
4476
4477 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004478 I915_WRITE(SOUTH_CHICKEN1, temp);
4479 POSTING_READ(SOUTH_CHICKEN1);
4480}
4481
4482static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4483{
4484 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004485
4486 switch (intel_crtc->pipe) {
4487 case PIPE_A:
4488 break;
4489 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004490 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004491 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004492 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004493 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004494
4495 break;
4496 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004497 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004498
4499 break;
4500 default:
4501 BUG();
4502 }
4503}
4504
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004505/* Return which DP Port should be selected for Transcoder DP control */
4506static enum port
4507intel_trans_dp_port_sel(struct drm_crtc *crtc)
4508{
4509 struct drm_device *dev = crtc->dev;
4510 struct intel_encoder *encoder;
4511
4512 for_each_encoder_on_crtc(dev, crtc, encoder) {
Ville Syrjäläcca05022016-06-22 21:57:06 +03004513 if (encoder->type == INTEL_OUTPUT_DP ||
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004514 encoder->type == INTEL_OUTPUT_EDP)
4515 return enc_to_dig_port(&encoder->base)->port;
4516 }
4517
4518 return -1;
4519}
4520
Jesse Barnesf67a5592011-01-05 10:31:48 -08004521/*
4522 * Enable PCH resources required for PCH ports:
4523 * - PCH PLLs
4524 * - FDI training & RX/TX
4525 * - update transcoder timings
4526 * - DP transcoding bits
4527 * - transcoder
4528 */
4529static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08004530{
4531 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004532 struct drm_i915_private *dev_priv = to_i915(dev);
Zhenyu Wang2c072452009-06-05 15:38:42 +08004533 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4534 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004535 u32 temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004536
Daniel Vetterab9412b2013-05-03 11:49:46 +02004537 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004538
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01004539 if (IS_IVYBRIDGE(dev_priv))
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004540 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4541
Daniel Vettercd986ab2012-10-26 10:58:12 +02004542 /* Write the TU size bits before fdi link training, so that error
4543 * detection works. */
4544 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4545 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4546
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004547 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07004548 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004549
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004550 /* We need to program the right clock selection before writing the pixel
4551 * mutliplier into the DPLL. */
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01004552 if (HAS_PCH_CPT(dev_priv)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004553 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004554
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004555 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004556 temp |= TRANS_DPLL_ENABLE(pipe);
4557 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02004558 if (intel_crtc->config->shared_dpll ==
4559 intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004560 temp |= sel;
4561 else
4562 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004563 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004564 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004565
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004566 /* XXX: pch pll's can be enabled any time before we enable the PCH
4567 * transcoder, and we actually should do this to not upset any PCH
4568 * transcoder that already use the clock when we share it.
4569 *
4570 * Note that enable_shared_dpll tries to do the right thing, but
4571 * get_shared_dpll unconditionally resets the pll - we need that to have
4572 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02004573 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004574
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004575 /* set transcoder timing, panel must allow it */
4576 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02004577 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004578
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004579 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004580
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004581 /* For PCH DP, enable TRANS_DP_CTL */
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01004582 if (HAS_PCH_CPT(dev_priv) &&
4583 intel_crtc_has_dp_encoder(intel_crtc->config)) {
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004584 const struct drm_display_mode *adjusted_mode =
4585 &intel_crtc->config->base.adjusted_mode;
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004586 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004587 i915_reg_t reg = TRANS_DP_CTL(pipe);
Chris Wilson5eddb702010-09-11 13:48:45 +01004588 temp = I915_READ(reg);
4589 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004590 TRANS_DP_SYNC_MASK |
4591 TRANS_DP_BPC_MASK);
Ville Syrjäläe3ef4472015-05-05 17:17:31 +03004592 temp |= TRANS_DP_OUTPUT_ENABLE;
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004593 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004594
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004595 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004596 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004597 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004598 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004599
4600 switch (intel_trans_dp_port_sel(crtc)) {
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004601 case PORT_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004602 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004603 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004604 case PORT_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004605 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004606 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004607 case PORT_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004608 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004609 break;
4610 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004611 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004612 }
4613
Chris Wilson5eddb702010-09-11 13:48:45 +01004614 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004615 }
4616
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004617 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004618}
4619
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004620static void lpt_pch_enable(struct drm_crtc *crtc)
4621{
4622 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004623 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004624 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004625 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004626
Daniel Vetterab9412b2013-05-03 11:49:46 +02004627 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004628
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004629 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004630
Paulo Zanoni0540e482012-10-31 18:12:40 -02004631 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02004632 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004633
Paulo Zanoni937bb612012-10-31 18:12:47 -02004634 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004635}
4636
Daniel Vettera1520312013-05-03 11:49:50 +02004637static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004638{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004639 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004640 i915_reg_t dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004641 u32 temp;
4642
4643 temp = I915_READ(dslreg);
4644 udelay(500);
4645 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004646 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004647 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004648 }
4649}
4650
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004651static int
4652skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4653 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4654 int src_w, int src_h, int dst_w, int dst_h)
Chandra Kondurua1b22782015-04-07 15:28:45 -07004655{
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004656 struct intel_crtc_scaler_state *scaler_state =
4657 &crtc_state->scaler_state;
4658 struct intel_crtc *intel_crtc =
4659 to_intel_crtc(crtc_state->base.crtc);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004660 int need_scaling;
Chandra Konduru6156a452015-04-27 13:48:39 -07004661
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03004662 need_scaling = drm_rotation_90_or_270(rotation) ?
Chandra Konduru6156a452015-04-27 13:48:39 -07004663 (src_h != dst_w || src_w != dst_h):
4664 (src_w != dst_w || src_h != dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004665
4666 /*
4667 * if plane is being disabled or scaler is no more required or force detach
4668 * - free scaler binded to this plane/crtc
4669 * - in order to do this, update crtc->scaler_usage
4670 *
4671 * Here scaler state in crtc_state is set free so that
4672 * scaler can be assigned to other user. Actual register
4673 * update to free the scaler is done in plane/panel-fit programming.
4674 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4675 */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004676 if (force_detach || !need_scaling) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004677 if (*scaler_id >= 0) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004678 scaler_state->scaler_users &= ~(1 << scaler_user);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004679 scaler_state->scalers[*scaler_id].in_use = 0;
4680
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004681 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4682 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4683 intel_crtc->pipe, scaler_user, *scaler_id,
Chandra Kondurua1b22782015-04-07 15:28:45 -07004684 scaler_state->scaler_users);
4685 *scaler_id = -1;
4686 }
4687 return 0;
4688 }
4689
4690 /* range checks */
4691 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4692 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4693
4694 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4695 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004696 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
Chandra Kondurua1b22782015-04-07 15:28:45 -07004697 "size is out of scaler range\n",
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004698 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004699 return -EINVAL;
4700 }
4701
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004702 /* mark this plane as a scaler user in crtc_state */
4703 scaler_state->scaler_users |= (1 << scaler_user);
4704 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4705 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4706 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4707 scaler_state->scaler_users);
4708
4709 return 0;
4710}
4711
4712/**
4713 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4714 *
4715 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004716 *
4717 * Return
4718 * 0 - scaler_usage updated successfully
4719 * error - requested scaling cannot be supported or other error condition
4720 */
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004721int skl_update_scaler_crtc(struct intel_crtc_state *state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004722{
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03004723 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004724
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004725 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +03004726 &state->scaler_state.scaler_id, DRM_ROTATE_0,
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004727 state->pipe_src_w, state->pipe_src_h,
Ville Syrjäläaad941d2015-09-25 16:38:56 +03004728 adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004729}
4730
4731/**
4732 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4733 *
4734 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004735 * @plane_state: atomic plane state to update
4736 *
4737 * Return
4738 * 0 - scaler_usage updated successfully
4739 * error - requested scaling cannot be supported or other error condition
4740 */
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004741static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4742 struct intel_plane_state *plane_state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004743{
4744
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004745 struct intel_plane *intel_plane =
4746 to_intel_plane(plane_state->base.plane);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004747 struct drm_framebuffer *fb = plane_state->base.fb;
4748 int ret;
4749
Ville Syrjälä936e71e2016-07-26 19:06:59 +03004750 bool force_detach = !fb || !plane_state->base.visible;
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004751
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004752 ret = skl_update_scaler(crtc_state, force_detach,
4753 drm_plane_index(&intel_plane->base),
4754 &plane_state->scaler_id,
4755 plane_state->base.rotation,
Ville Syrjälä936e71e2016-07-26 19:06:59 +03004756 drm_rect_width(&plane_state->base.src) >> 16,
4757 drm_rect_height(&plane_state->base.src) >> 16,
4758 drm_rect_width(&plane_state->base.dst),
4759 drm_rect_height(&plane_state->base.dst));
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004760
4761 if (ret || plane_state->scaler_id < 0)
4762 return ret;
4763
Chandra Kondurua1b22782015-04-07 15:28:45 -07004764 /* check colorkey */
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004765 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
Ville Syrjälä72660ce2016-05-27 20:59:20 +03004766 DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
4767 intel_plane->base.base.id,
4768 intel_plane->base.name);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004769 return -EINVAL;
4770 }
4771
4772 /* Check src format */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004773 switch (fb->pixel_format) {
4774 case DRM_FORMAT_RGB565:
4775 case DRM_FORMAT_XBGR8888:
4776 case DRM_FORMAT_XRGB8888:
4777 case DRM_FORMAT_ABGR8888:
4778 case DRM_FORMAT_ARGB8888:
4779 case DRM_FORMAT_XRGB2101010:
4780 case DRM_FORMAT_XBGR2101010:
4781 case DRM_FORMAT_YUYV:
4782 case DRM_FORMAT_YVYU:
4783 case DRM_FORMAT_UYVY:
4784 case DRM_FORMAT_VYUY:
4785 break;
4786 default:
Ville Syrjälä72660ce2016-05-27 20:59:20 +03004787 DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
4788 intel_plane->base.base.id, intel_plane->base.name,
4789 fb->base.id, fb->pixel_format);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004790 return -EINVAL;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004791 }
4792
Chandra Kondurua1b22782015-04-07 15:28:45 -07004793 return 0;
4794}
4795
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004796static void skylake_scaler_disable(struct intel_crtc *crtc)
4797{
4798 int i;
4799
4800 for (i = 0; i < crtc->num_scalers; i++)
4801 skl_detach_scaler(crtc, i);
4802}
4803
4804static void skylake_pfit_enable(struct intel_crtc *crtc)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004805{
4806 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004807 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004808 int pipe = crtc->pipe;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004809 struct intel_crtc_scaler_state *scaler_state =
4810 &crtc->config->scaler_state;
4811
4812 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4813
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004814 if (crtc->config->pch_pfit.enabled) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004815 int id;
4816
4817 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4818 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4819 return;
4820 }
4821
4822 id = scaler_state->scaler_id;
4823 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4824 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4825 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4826 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4827
4828 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004829 }
4830}
4831
Jesse Barnesb074cec2013-04-25 12:55:02 -07004832static void ironlake_pfit_enable(struct intel_crtc *crtc)
4833{
4834 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004835 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesb074cec2013-04-25 12:55:02 -07004836 int pipe = crtc->pipe;
4837
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004838 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004839 /* Force use of hard-coded filter coefficients
4840 * as some pre-programmed values are broken,
4841 * e.g. x201.
4842 */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01004843 if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv))
Jesse Barnesb074cec2013-04-25 12:55:02 -07004844 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4845 PF_PIPE_SEL_IVB(pipe));
4846 else
4847 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004848 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4849 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004850 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004851}
4852
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004853void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004854{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004855 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004856 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanonid77e4532013-09-24 13:52:55 -03004857
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004858 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004859 return;
4860
Maarten Lankhorst307e4492016-03-23 14:33:28 +01004861 /*
4862 * We can only enable IPS after we enable a plane and wait for a vblank
4863 * This function is called from post_plane_update, which is run after
4864 * a vblank wait.
4865 */
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004866
Paulo Zanonid77e4532013-09-24 13:52:55 -03004867 assert_plane_enabled(dev_priv, crtc->plane);
Tvrtko Ursulin86527442016-10-13 11:03:00 +01004868 if (IS_BROADWELL(dev_priv)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004869 mutex_lock(&dev_priv->rps.hw_lock);
4870 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4871 mutex_unlock(&dev_priv->rps.hw_lock);
4872 /* Quoting Art Runyan: "its not safe to expect any particular
4873 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004874 * mailbox." Moreover, the mailbox may return a bogus state,
4875 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004876 */
4877 } else {
4878 I915_WRITE(IPS_CTL, IPS_ENABLE);
4879 /* The bit only becomes 1 in the next vblank, so this wait here
4880 * is essentially intel_wait_for_vblank. If we don't have this
4881 * and don't wait for vblanks until the end of crtc_enable, then
4882 * the HW state readout code will complain that the expected
4883 * IPS_CTL value is not the one we read. */
Chris Wilson2ec9ba32016-06-30 15:33:01 +01004884 if (intel_wait_for_register(dev_priv,
4885 IPS_CTL, IPS_ENABLE, IPS_ENABLE,
4886 50))
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004887 DRM_ERROR("Timed out waiting for IPS enable\n");
4888 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004889}
4890
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004891void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004892{
4893 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004894 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanonid77e4532013-09-24 13:52:55 -03004895
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004896 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004897 return;
4898
4899 assert_plane_enabled(dev_priv, crtc->plane);
Tvrtko Ursulin86527442016-10-13 11:03:00 +01004900 if (IS_BROADWELL(dev_priv)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004901 mutex_lock(&dev_priv->rps.hw_lock);
4902 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4903 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004904 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
Chris Wilsonb85c1ec2016-06-30 15:33:02 +01004905 if (intel_wait_for_register(dev_priv,
4906 IPS_CTL, IPS_ENABLE, 0,
4907 42))
Ben Widawsky23d0b132014-04-10 14:32:41 -07004908 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004909 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004910 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004911 POSTING_READ(IPS_CTL);
4912 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004913
4914 /* We need to wait for a vblank before we can disable the plane. */
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02004915 intel_wait_for_vblank(dev_priv, crtc->pipe);
Paulo Zanonid77e4532013-09-24 13:52:55 -03004916}
4917
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004918static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004919{
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004920 if (intel_crtc->overlay) {
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004921 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004922 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004923
4924 mutex_lock(&dev->struct_mutex);
4925 dev_priv->mm.interruptible = false;
4926 (void) intel_overlay_switch_off(intel_crtc->overlay);
4927 dev_priv->mm.interruptible = true;
4928 mutex_unlock(&dev->struct_mutex);
4929 }
4930
4931 /* Let userspace switch the overlay on again. In most cases userspace
4932 * has to recompute where to put it anyway.
4933 */
4934}
4935
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004936/**
4937 * intel_post_enable_primary - Perform operations after enabling primary plane
4938 * @crtc: the CRTC whose primary plane was just enabled
4939 *
4940 * Performs potentially sleeping operations that must be done after the primary
4941 * plane is enabled, such as updating FBC and IPS. Note that this may be
4942 * called due to an explicit primary plane update, or due to an implicit
4943 * re-enable that is caused when a sprite plane is updated to no longer
4944 * completely hide the primary plane.
4945 */
4946static void
4947intel_post_enable_primary(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004948{
4949 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004950 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004951 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4952 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004953
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004954 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004955 * FIXME IPS should be fine as long as one plane is
4956 * enabled, but in practice it seems to have problems
4957 * when going from primary only to sprite only and vice
4958 * versa.
4959 */
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004960 hsw_enable_ips(intel_crtc);
4961
Daniel Vetterf99d7062014-06-19 16:01:59 +02004962 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004963 * Gen2 reports pipe underruns whenever all planes are disabled.
4964 * So don't enable underrun reporting before at least some planes
4965 * are enabled.
4966 * FIXME: Need to fix the logic to work when we turn off all planes
4967 * but leave the pipe running.
Daniel Vetterf99d7062014-06-19 16:01:59 +02004968 */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004969 if (IS_GEN2(dev_priv))
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004970 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4971
Ville Syrjäläaca7b682015-10-30 19:22:21 +02004972 /* Underruns don't always raise interrupts, so check manually. */
4973 intel_check_cpu_fifo_underruns(dev_priv);
4974 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004975}
4976
Ville Syrjälä2622a082016-03-09 19:07:26 +02004977/* FIXME move all this to pre_plane_update() with proper state tracking */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004978static void
4979intel_pre_disable_primary(struct drm_crtc *crtc)
4980{
4981 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004982 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004983 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4984 int pipe = intel_crtc->pipe;
4985
4986 /*
4987 * Gen2 reports pipe underruns whenever all planes are disabled.
4988 * So diasble underrun reporting before all the planes get disabled.
4989 * FIXME: Need to fix the logic to work when we turn off all planes
4990 * but leave the pipe running.
4991 */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004992 if (IS_GEN2(dev_priv))
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004993 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4994
4995 /*
Ville Syrjälä2622a082016-03-09 19:07:26 +02004996 * FIXME IPS should be fine as long as one plane is
4997 * enabled, but in practice it seems to have problems
4998 * when going from primary only to sprite only and vice
4999 * versa.
5000 */
5001 hsw_disable_ips(intel_crtc);
5002}
5003
5004/* FIXME get rid of this and use pre_plane_update */
5005static void
5006intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
5007{
5008 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005009 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä2622a082016-03-09 19:07:26 +02005010 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5011 int pipe = intel_crtc->pipe;
5012
5013 intel_pre_disable_primary(crtc);
5014
5015 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03005016 * Vblank time updates from the shadow to live plane control register
5017 * are blocked if the memory self-refresh mode is active at that
5018 * moment. So to make sure the plane gets truly disabled, disable
5019 * first the self-refresh mode. The self-refresh enable bit in turn
5020 * will be checked/applied by the HW only at the next frame start
5021 * event which is after the vblank start event, so we need to have a
5022 * wait-for-vblank between disabling the plane and the pipe.
5023 */
Tvrtko Ursulin49cff962016-10-13 11:02:54 +01005024 if (HAS_GMCH_DISPLAY(dev_priv)) {
Maarten Lankhorst87d43002015-04-21 17:12:54 +03005025 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03005026 dev_priv->wm.vlv.cxsr = false;
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02005027 intel_wait_for_vblank(dev_priv, pipe);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03005028 }
Maarten Lankhorst87d43002015-04-21 17:12:54 +03005029}
5030
Daniel Vetter5a21b662016-05-24 17:13:53 +02005031static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
5032{
5033 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
5034 struct drm_atomic_state *old_state = old_crtc_state->base.state;
5035 struct intel_crtc_state *pipe_config =
5036 to_intel_crtc_state(crtc->base.state);
Daniel Vetter5a21b662016-05-24 17:13:53 +02005037 struct drm_plane *primary = crtc->base.primary;
5038 struct drm_plane_state *old_pri_state =
5039 drm_atomic_get_existing_plane_state(old_state, primary);
5040
Chris Wilson5748b6a2016-08-04 16:32:38 +01005041 intel_frontbuffer_flip(to_i915(crtc->base.dev), pipe_config->fb_bits);
Daniel Vetter5a21b662016-05-24 17:13:53 +02005042
5043 crtc->wm.cxsr_allowed = true;
5044
5045 if (pipe_config->update_wm_post && pipe_config->base.active)
Ville Syrjälä432081b2016-10-31 22:37:03 +02005046 intel_update_watermarks(crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +02005047
5048 if (old_pri_state) {
5049 struct intel_plane_state *primary_state =
5050 to_intel_plane_state(primary->state);
5051 struct intel_plane_state *old_primary_state =
5052 to_intel_plane_state(old_pri_state);
5053
5054 intel_fbc_post_update(crtc);
5055
Ville Syrjälä936e71e2016-07-26 19:06:59 +03005056 if (primary_state->base.visible &&
Daniel Vetter5a21b662016-05-24 17:13:53 +02005057 (needs_modeset(&pipe_config->base) ||
Ville Syrjälä936e71e2016-07-26 19:06:59 +03005058 !old_primary_state->base.visible))
Daniel Vetter5a21b662016-05-24 17:13:53 +02005059 intel_post_enable_primary(&crtc->base);
5060 }
5061}
5062
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01005063static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
Maarten Lankhorstac21b222015-06-15 12:33:49 +02005064{
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01005065 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02005066 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005067 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01005068 struct intel_crtc_state *pipe_config =
5069 to_intel_crtc_state(crtc->base.state);
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01005070 struct drm_atomic_state *old_state = old_crtc_state->base.state;
5071 struct drm_plane *primary = crtc->base.primary;
5072 struct drm_plane_state *old_pri_state =
5073 drm_atomic_get_existing_plane_state(old_state, primary);
5074 bool modeset = needs_modeset(&pipe_config->base);
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005075 struct intel_atomic_state *old_intel_state =
5076 to_intel_atomic_state(old_state);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02005077
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01005078 if (old_pri_state) {
5079 struct intel_plane_state *primary_state =
5080 to_intel_plane_state(primary->state);
5081 struct intel_plane_state *old_primary_state =
5082 to_intel_plane_state(old_pri_state);
5083
Maarten Lankhorstfaf68d92016-06-14 14:24:20 +02005084 intel_fbc_pre_update(crtc, pipe_config, primary_state);
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +01005085
Ville Syrjälä936e71e2016-07-26 19:06:59 +03005086 if (old_primary_state->base.visible &&
5087 (modeset || !primary_state->base.visible))
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01005088 intel_pre_disable_primary(&crtc->base);
5089 }
Ville Syrjälä852eb002015-06-24 22:00:07 +03005090
Tvrtko Ursulin49cff962016-10-13 11:02:54 +01005091 if (pipe_config->disable_cxsr && HAS_GMCH_DISPLAY(dev_priv)) {
Ville Syrjälä852eb002015-06-24 22:00:07 +03005092 crtc->wm.cxsr_allowed = false;
Maarten Lankhorst2dfd1782016-02-03 16:53:25 +01005093
Ville Syrjälä2622a082016-03-09 19:07:26 +02005094 /*
5095 * Vblank time updates from the shadow to live plane control register
5096 * are blocked if the memory self-refresh mode is active at that
5097 * moment. So to make sure the plane gets truly disabled, disable
5098 * first the self-refresh mode. The self-refresh enable bit in turn
5099 * will be checked/applied by the HW only at the next frame start
5100 * event which is after the vblank start event, so we need to have a
5101 * wait-for-vblank between disabling the plane and the pipe.
5102 */
5103 if (old_crtc_state->base.active) {
Maarten Lankhorst2dfd1782016-02-03 16:53:25 +01005104 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä2622a082016-03-09 19:07:26 +02005105 dev_priv->wm.vlv.cxsr = false;
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02005106 intel_wait_for_vblank(dev_priv, crtc->pipe);
Ville Syrjälä2622a082016-03-09 19:07:26 +02005107 }
Ville Syrjälä852eb002015-06-24 22:00:07 +03005108 }
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01005109
Matt Ropered4a6a72016-02-23 17:20:13 -08005110 /*
5111 * IVB workaround: must disable low power watermarks for at least
5112 * one frame before enabling scaling. LP watermarks can be re-enabled
5113 * when scaling is disabled.
5114 *
5115 * WaCxSRDisabledForSpriteScaling:ivb
5116 */
5117 if (pipe_config->disable_lp_wm) {
5118 ilk_disable_lp_wm(dev);
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02005119 intel_wait_for_vblank(dev_priv, crtc->pipe);
Matt Ropered4a6a72016-02-23 17:20:13 -08005120 }
5121
5122 /*
5123 * If we're doing a modeset, we're done. No need to do any pre-vblank
5124 * watermark programming here.
5125 */
5126 if (needs_modeset(&pipe_config->base))
5127 return;
5128
5129 /*
5130 * For platforms that support atomic watermarks, program the
5131 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
5132 * will be the intermediate values that are safe for both pre- and
5133 * post- vblank; when vblank happens, the 'active' values will be set
5134 * to the final 'target' values and we'll do this again to get the
5135 * optimal watermarks. For gen9+ platforms, the values we program here
5136 * will be the final target values which will get automatically latched
5137 * at vblank time; no further programming will be necessary.
5138 *
5139 * If a platform hasn't been transitioned to atomic watermarks yet,
5140 * we'll continue to update watermarks the old way, if flags tell
5141 * us to.
5142 */
5143 if (dev_priv->display.initial_watermarks != NULL)
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005144 dev_priv->display.initial_watermarks(old_intel_state,
5145 pipe_config);
Ville Syrjäläcaed3612016-03-09 19:07:25 +02005146 else if (pipe_config->update_wm_pre)
Ville Syrjälä432081b2016-10-31 22:37:03 +02005147 intel_update_watermarks(crtc);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02005148}
5149
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02005150static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02005151{
5152 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02005153 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02005154 struct drm_plane *p;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02005155 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02005156
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03005157 intel_crtc_dpms_overlay_disable(intel_crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03005158
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02005159 drm_for_each_plane_mask(p, dev, plane_mask)
5160 to_intel_plane(p)->disable_plane(p, crtc);
Ville Syrjäläf98551a2014-05-22 17:48:06 +03005161
Daniel Vetterf99d7062014-06-19 16:01:59 +02005162 /*
5163 * FIXME: Once we grow proper nuclear flip support out of this we need
5164 * to compute the mask of flip planes precisely. For the time being
5165 * consider this a flip to a NULL plane.
5166 */
Chris Wilson5748b6a2016-08-04 16:32:38 +01005167 intel_frontbuffer_flip(to_i915(dev), INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02005168}
5169
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005170static void intel_encoders_pre_pll_enable(struct drm_crtc *crtc,
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005171 struct intel_crtc_state *crtc_state,
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005172 struct drm_atomic_state *old_state)
5173{
5174 struct drm_connector_state *old_conn_state;
5175 struct drm_connector *conn;
5176 int i;
5177
5178 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5179 struct drm_connector_state *conn_state = conn->state;
5180 struct intel_encoder *encoder =
5181 to_intel_encoder(conn_state->best_encoder);
5182
5183 if (conn_state->crtc != crtc)
5184 continue;
5185
5186 if (encoder->pre_pll_enable)
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005187 encoder->pre_pll_enable(encoder, crtc_state, conn_state);
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005188 }
5189}
5190
5191static void intel_encoders_pre_enable(struct drm_crtc *crtc,
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005192 struct intel_crtc_state *crtc_state,
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005193 struct drm_atomic_state *old_state)
5194{
5195 struct drm_connector_state *old_conn_state;
5196 struct drm_connector *conn;
5197 int i;
5198
5199 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5200 struct drm_connector_state *conn_state = conn->state;
5201 struct intel_encoder *encoder =
5202 to_intel_encoder(conn_state->best_encoder);
5203
5204 if (conn_state->crtc != crtc)
5205 continue;
5206
5207 if (encoder->pre_enable)
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005208 encoder->pre_enable(encoder, crtc_state, conn_state);
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005209 }
5210}
5211
5212static void intel_encoders_enable(struct drm_crtc *crtc,
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005213 struct intel_crtc_state *crtc_state,
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005214 struct drm_atomic_state *old_state)
5215{
5216 struct drm_connector_state *old_conn_state;
5217 struct drm_connector *conn;
5218 int i;
5219
5220 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5221 struct drm_connector_state *conn_state = conn->state;
5222 struct intel_encoder *encoder =
5223 to_intel_encoder(conn_state->best_encoder);
5224
5225 if (conn_state->crtc != crtc)
5226 continue;
5227
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005228 encoder->enable(encoder, crtc_state, conn_state);
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005229 intel_opregion_notify_encoder(encoder, true);
5230 }
5231}
5232
5233static void intel_encoders_disable(struct drm_crtc *crtc,
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005234 struct intel_crtc_state *old_crtc_state,
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005235 struct drm_atomic_state *old_state)
5236{
5237 struct drm_connector_state *old_conn_state;
5238 struct drm_connector *conn;
5239 int i;
5240
5241 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5242 struct intel_encoder *encoder =
5243 to_intel_encoder(old_conn_state->best_encoder);
5244
5245 if (old_conn_state->crtc != crtc)
5246 continue;
5247
5248 intel_opregion_notify_encoder(encoder, false);
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005249 encoder->disable(encoder, old_crtc_state, old_conn_state);
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005250 }
5251}
5252
5253static void intel_encoders_post_disable(struct drm_crtc *crtc,
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005254 struct intel_crtc_state *old_crtc_state,
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005255 struct drm_atomic_state *old_state)
5256{
5257 struct drm_connector_state *old_conn_state;
5258 struct drm_connector *conn;
5259 int i;
5260
5261 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5262 struct intel_encoder *encoder =
5263 to_intel_encoder(old_conn_state->best_encoder);
5264
5265 if (old_conn_state->crtc != crtc)
5266 continue;
5267
5268 if (encoder->post_disable)
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005269 encoder->post_disable(encoder, old_crtc_state, old_conn_state);
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005270 }
5271}
5272
5273static void intel_encoders_post_pll_disable(struct drm_crtc *crtc,
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005274 struct intel_crtc_state *old_crtc_state,
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005275 struct drm_atomic_state *old_state)
5276{
5277 struct drm_connector_state *old_conn_state;
5278 struct drm_connector *conn;
5279 int i;
5280
5281 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5282 struct intel_encoder *encoder =
5283 to_intel_encoder(old_conn_state->best_encoder);
5284
5285 if (old_conn_state->crtc != crtc)
5286 continue;
5287
5288 if (encoder->post_pll_disable)
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005289 encoder->post_pll_disable(encoder, old_crtc_state, old_conn_state);
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005290 }
5291}
5292
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005293static void ironlake_crtc_enable(struct intel_crtc_state *pipe_config,
5294 struct drm_atomic_state *old_state)
Jesse Barnesf67a5592011-01-05 10:31:48 -08005295{
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005296 struct drm_crtc *crtc = pipe_config->base.crtc;
Jesse Barnesf67a5592011-01-05 10:31:48 -08005297 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005298 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesf67a5592011-01-05 10:31:48 -08005299 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5300 int pipe = intel_crtc->pipe;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005301 struct intel_atomic_state *old_intel_state =
5302 to_intel_atomic_state(old_state);
Jesse Barnesf67a5592011-01-05 10:31:48 -08005303
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02005304 if (WARN_ON(intel_crtc->active))
Jesse Barnesf67a5592011-01-05 10:31:48 -08005305 return;
5306
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005307 /*
5308 * Sometimes spurious CPU pipe underruns happen during FDI
5309 * training, at least with VGA+HDMI cloning. Suppress them.
5310 *
5311 * On ILK we get an occasional spurious CPU pipe underruns
5312 * between eDP port A enable and vdd enable. Also PCH port
5313 * enable seems to result in the occasional CPU pipe underrun.
5314 *
5315 * Spurious PCH underruns also occur during PCH enabling.
5316 */
5317 if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
5318 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005319 if (intel_crtc->config->has_pch_encoder)
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005320 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
5321
5322 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02005323 intel_prepare_shared_dpll(intel_crtc);
5324
Ville Syrjälä37a56502016-06-22 21:57:04 +03005325 if (intel_crtc_has_dp_encoder(intel_crtc->config))
Ramalingam Cfe3cd482015-02-13 15:32:59 +05305326 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02005327
5328 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02005329 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +02005330
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005331 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02005332 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005333 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02005334 }
5335
5336 ironlake_set_pipeconf(crtc);
5337
Jesse Barnesf67a5592011-01-05 10:31:48 -08005338 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03005339
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005340 intel_encoders_pre_enable(crtc, pipe_config, old_state);
Jesse Barnesf67a5592011-01-05 10:31:48 -08005341
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005342 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02005343 /* Note: FDI PLL enabling _must_ be done before we enable the
5344 * cpu pipes, hence this is separate from all the other fdi/pch
5345 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02005346 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02005347 } else {
5348 assert_fdi_tx_disabled(dev_priv, pipe);
5349 assert_fdi_rx_disabled(dev_priv, pipe);
5350 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08005351
Jesse Barnesb074cec2013-04-25 12:55:02 -07005352 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08005353
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02005354 /*
5355 * On ILK+ LUT must be loaded before the pipe is running but with
5356 * clocks enabled
5357 */
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02005358 intel_color_load_luts(&pipe_config->base);
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02005359
Imre Deak1d5bf5d2016-02-29 22:10:33 +02005360 if (dev_priv->display.initial_watermarks != NULL)
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005361 dev_priv->display.initial_watermarks(old_intel_state, intel_crtc->config);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02005362 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08005363
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005364 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08005365 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005366
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005367 assert_vblank_disabled(crtc);
5368 drm_crtc_vblank_on(crtc);
5369
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005370 intel_encoders_enable(crtc, pipe_config, old_state);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02005371
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01005372 if (HAS_PCH_CPT(dev_priv))
Daniel Vettera1520312013-05-03 11:49:50 +02005373 cpt_verify_modeset(dev, intel_crtc->pipe);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02005374
5375 /* Must wait for vblank to avoid spurious PCH FIFO underruns */
5376 if (intel_crtc->config->has_pch_encoder)
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02005377 intel_wait_for_vblank(dev_priv, pipe);
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005378 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02005379 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005380}
5381
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005382/* IPS only exists on ULT machines and is tied to pipe A. */
5383static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
5384{
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005385 return HAS_IPS(to_i915(crtc->base.dev)) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005386}
5387
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005388static void haswell_crtc_enable(struct intel_crtc_state *pipe_config,
5389 struct drm_atomic_state *old_state)
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005390{
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005391 struct drm_crtc *crtc = pipe_config->base.crtc;
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00005392 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005393 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02005394 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
Jani Nikula4d1de972016-03-18 17:05:42 +02005395 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005396 struct intel_atomic_state *old_intel_state =
5397 to_intel_atomic_state(old_state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005398
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02005399 if (WARN_ON(intel_crtc->active))
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005400 return;
5401
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005402 if (intel_crtc->config->has_pch_encoder)
5403 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5404 false);
5405
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005406 intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
Imre Deak95a7a2a2016-06-13 16:44:35 +03005407
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02005408 if (intel_crtc->config->shared_dpll)
Daniel Vetterdf8ad702014-06-25 22:02:03 +03005409 intel_enable_shared_dpll(intel_crtc);
5410
Ville Syrjälä37a56502016-06-22 21:57:04 +03005411 if (intel_crtc_has_dp_encoder(intel_crtc->config))
Ramalingam Cfe3cd482015-02-13 15:32:59 +05305412 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02005413
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005414 if (!transcoder_is_dsi(cpu_transcoder))
Jani Nikula4d1de972016-03-18 17:05:42 +02005415 intel_set_pipe_timings(intel_crtc);
5416
Jani Nikulabc58be62016-03-18 17:05:39 +02005417 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02005418
Jani Nikula4d1de972016-03-18 17:05:42 +02005419 if (cpu_transcoder != TRANSCODER_EDP &&
5420 !transcoder_is_dsi(cpu_transcoder)) {
5421 I915_WRITE(PIPE_MULT(cpu_transcoder),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005422 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07005423 }
5424
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005425 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02005426 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005427 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02005428 }
5429
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005430 if (!transcoder_is_dsi(cpu_transcoder))
Jani Nikula4d1de972016-03-18 17:05:42 +02005431 haswell_set_pipeconf(crtc);
5432
Jani Nikula391bf042016-03-18 17:05:40 +02005433 haswell_set_pipemisc(crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02005434
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02005435 intel_color_set_csc(&pipe_config->base);
Daniel Vetter229fca92014-04-24 23:55:09 +02005436
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005437 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03005438
Daniel Vetter6b698512015-11-28 11:05:39 +01005439 if (intel_crtc->config->has_pch_encoder)
5440 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
5441 else
5442 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
5443
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005444 intel_encoders_pre_enable(crtc, pipe_config, old_state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005445
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005446 if (intel_crtc->config->has_pch_encoder)
Imre Deak4fe94672014-06-25 22:01:49 +03005447 dev_priv->display.fdi_link_train(crtc);
Imre Deak4fe94672014-06-25 22:01:49 +03005448
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005449 if (!transcoder_is_dsi(cpu_transcoder))
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305450 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005451
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00005452 if (INTEL_GEN(dev_priv) >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02005453 skylake_pfit_enable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005454 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07005455 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005456
5457 /*
5458 * On ILK+ LUT must be loaded before the pipe is running but with
5459 * clocks enabled
5460 */
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02005461 intel_color_load_luts(&pipe_config->base);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005462
Paulo Zanoni1f544382012-10-24 11:32:00 -02005463 intel_ddi_set_pipe_settings(crtc);
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005464 if (!transcoder_is_dsi(cpu_transcoder))
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305465 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005466
Imre Deak1d5bf5d2016-02-29 22:10:33 +02005467 if (dev_priv->display.initial_watermarks != NULL)
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005468 dev_priv->display.initial_watermarks(old_intel_state,
5469 pipe_config);
Imre Deak1d5bf5d2016-02-29 22:10:33 +02005470 else
Ville Syrjälä432081b2016-10-31 22:37:03 +02005471 intel_update_watermarks(intel_crtc);
Jani Nikula4d1de972016-03-18 17:05:42 +02005472
5473 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005474 if (!transcoder_is_dsi(cpu_transcoder))
Jani Nikula4d1de972016-03-18 17:05:42 +02005475 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005476
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005477 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02005478 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005479
Ville Syrjälä00370712016-11-14 19:44:06 +02005480 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
Dave Airlie0e32b392014-05-02 14:02:48 +10005481 intel_ddi_set_vc_payload_alloc(crtc, true);
5482
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005483 assert_vblank_disabled(crtc);
5484 drm_crtc_vblank_on(crtc);
5485
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005486 intel_encoders_enable(crtc, pipe_config, old_state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005487
Daniel Vetter6b698512015-11-28 11:05:39 +01005488 if (intel_crtc->config->has_pch_encoder) {
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02005489 intel_wait_for_vblank(dev_priv, pipe);
5490 intel_wait_for_vblank(dev_priv, pipe);
Daniel Vetter6b698512015-11-28 11:05:39 +01005491 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005492 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5493 true);
Daniel Vetter6b698512015-11-28 11:05:39 +01005494 }
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005495
Paulo Zanonie4916942013-09-20 16:21:19 -03005496 /* If we change the relative order between pipe/planes enabling, we need
5497 * to change the workaround. */
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02005498 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01005499 if (IS_HASWELL(dev_priv) && hsw_workaround_pipe != INVALID_PIPE) {
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02005500 intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
5501 intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02005502 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005503}
5504
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005505static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005506{
5507 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005508 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005509 int pipe = crtc->pipe;
5510
5511 /* To avoid upsetting the power well on haswell only disable the pfit if
5512 * it's in use. The hw state code will make sure we get this right. */
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005513 if (force || crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005514 I915_WRITE(PF_CTL(pipe), 0);
5515 I915_WRITE(PF_WIN_POS(pipe), 0);
5516 I915_WRITE(PF_WIN_SZ(pipe), 0);
5517 }
5518}
5519
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005520static void ironlake_crtc_disable(struct intel_crtc_state *old_crtc_state,
5521 struct drm_atomic_state *old_state)
Jesse Barnes6be4a602010-09-10 10:26:01 -07005522{
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005523 struct drm_crtc *crtc = old_crtc_state->base.crtc;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005524 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005525 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005526 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5527 int pipe = intel_crtc->pipe;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005528
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005529 /*
5530 * Sometimes spurious CPU pipe underruns happen when the
5531 * pipe is already disabled, but FDI RX/TX is still enabled.
5532 * Happens at least with VGA+HDMI cloning. Suppress them.
5533 */
5534 if (intel_crtc->config->has_pch_encoder) {
5535 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02005536 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005537 }
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02005538
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005539 intel_encoders_disable(crtc, old_crtc_state, old_state);
Daniel Vetterea9d7582012-07-10 10:42:52 +02005540
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005541 drm_crtc_vblank_off(crtc);
5542 assert_vblank_disabled(crtc);
5543
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005544 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005545
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005546 ironlake_pfit_disable(intel_crtc, false);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005547
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005548 if (intel_crtc->config->has_pch_encoder)
Ville Syrjälä5a74f702015-05-05 17:17:38 +03005549 ironlake_fdi_disable(crtc);
5550
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005551 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005552
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005553 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02005554 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005555
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01005556 if (HAS_PCH_CPT(dev_priv)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02005557 i915_reg_t reg;
5558 u32 temp;
5559
Daniel Vetterd925c592013-06-05 13:34:04 +02005560 /* disable TRANS_DP_CTL */
5561 reg = TRANS_DP_CTL(pipe);
5562 temp = I915_READ(reg);
5563 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5564 TRANS_DP_PORT_SEL_MASK);
5565 temp |= TRANS_DP_PORT_SEL_NONE;
5566 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005567
Daniel Vetterd925c592013-06-05 13:34:04 +02005568 /* disable DPLL_SEL */
5569 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02005570 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02005571 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005572 }
Daniel Vetterd925c592013-06-05 13:34:04 +02005573
Daniel Vetterd925c592013-06-05 13:34:04 +02005574 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005575 }
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005576
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005577 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005578 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005579}
5580
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005581static void haswell_crtc_disable(struct intel_crtc_state *old_crtc_state,
5582 struct drm_atomic_state *old_state)
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005583{
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005584 struct drm_crtc *crtc = old_crtc_state->base.crtc;
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00005585 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005586 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005587 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005588
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005589 if (intel_crtc->config->has_pch_encoder)
5590 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5591 false);
5592
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005593 intel_encoders_disable(crtc, old_crtc_state, old_state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005594
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005595 drm_crtc_vblank_off(crtc);
5596 assert_vblank_disabled(crtc);
5597
Jani Nikula4d1de972016-03-18 17:05:42 +02005598 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005599 if (!transcoder_is_dsi(cpu_transcoder))
Jani Nikula4d1de972016-03-18 17:05:42 +02005600 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005601
Ville Syrjälä00370712016-11-14 19:44:06 +02005602 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03005603 intel_ddi_set_vc_payload_alloc(crtc, false);
5604
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005605 if (!transcoder_is_dsi(cpu_transcoder))
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305606 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005607
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00005608 if (INTEL_GEN(dev_priv) >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02005609 skylake_scaler_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005610 else
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005611 ironlake_pfit_disable(intel_crtc, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005612
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005613 if (!transcoder_is_dsi(cpu_transcoder))
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305614 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005615
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005616 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005617
Maarten Lankhorstb7076542016-08-23 16:18:08 +02005618 if (old_crtc_state->has_pch_encoder)
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005619 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5620 true);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005621}
5622
Jesse Barnes2dd24552013-04-25 12:55:01 -07005623static void i9xx_pfit_enable(struct intel_crtc *crtc)
5624{
5625 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005626 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005627 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07005628
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02005629 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005630 return;
5631
Daniel Vetterc0b03412013-05-28 12:05:54 +02005632 /*
5633 * The panel fitter should only be adjusted whilst the pipe is disabled,
5634 * according to register description and PRM.
5635 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005636 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5637 assert_pipe_disabled(dev_priv, crtc->pipe);
5638
Jesse Barnesb074cec2013-04-25 12:55:02 -07005639 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5640 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005641
5642 /* Border color in case we don't scale up to the full screen. Black by
5643 * default, change to something else for debugging. */
5644 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005645}
5646
Dave Airlied05410f2014-06-05 13:22:59 +10005647static enum intel_display_power_domain port_to_power_domain(enum port port)
5648{
5649 switch (port) {
5650 case PORT_A:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005651 return POWER_DOMAIN_PORT_DDI_A_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005652 case PORT_B:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005653 return POWER_DOMAIN_PORT_DDI_B_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005654 case PORT_C:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005655 return POWER_DOMAIN_PORT_DDI_C_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005656 case PORT_D:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005657 return POWER_DOMAIN_PORT_DDI_D_LANES;
Xiong Zhangd8e19f92015-08-13 18:00:12 +08005658 case PORT_E:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005659 return POWER_DOMAIN_PORT_DDI_E_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005660 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005661 MISSING_CASE(port);
Dave Airlied05410f2014-06-05 13:22:59 +10005662 return POWER_DOMAIN_PORT_OTHER;
5663 }
5664}
5665
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005666static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
5667{
5668 switch (port) {
5669 case PORT_A:
5670 return POWER_DOMAIN_AUX_A;
5671 case PORT_B:
5672 return POWER_DOMAIN_AUX_B;
5673 case PORT_C:
5674 return POWER_DOMAIN_AUX_C;
5675 case PORT_D:
5676 return POWER_DOMAIN_AUX_D;
5677 case PORT_E:
5678 /* FIXME: Check VBT for actual wiring of PORT E */
5679 return POWER_DOMAIN_AUX_D;
5680 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005681 MISSING_CASE(port);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005682 return POWER_DOMAIN_AUX_A;
5683 }
5684}
5685
Imre Deak319be8a2014-03-04 19:22:57 +02005686enum intel_display_power_domain
5687intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02005688{
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01005689 struct drm_i915_private *dev_priv = to_i915(intel_encoder->base.dev);
Imre Deak319be8a2014-03-04 19:22:57 +02005690 struct intel_digital_port *intel_dig_port;
5691
5692 switch (intel_encoder->type) {
5693 case INTEL_OUTPUT_UNKNOWN:
5694 /* Only DDI platforms should ever use this output type */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01005695 WARN_ON_ONCE(!HAS_DDI(dev_priv));
Ville Syrjäläcca05022016-06-22 21:57:06 +03005696 case INTEL_OUTPUT_DP:
Imre Deak319be8a2014-03-04 19:22:57 +02005697 case INTEL_OUTPUT_HDMI:
5698 case INTEL_OUTPUT_EDP:
5699 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10005700 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10005701 case INTEL_OUTPUT_DP_MST:
5702 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5703 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02005704 case INTEL_OUTPUT_ANALOG:
5705 return POWER_DOMAIN_PORT_CRT;
5706 case INTEL_OUTPUT_DSI:
5707 return POWER_DOMAIN_PORT_DSI;
5708 default:
5709 return POWER_DOMAIN_PORT_OTHER;
5710 }
5711}
5712
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005713enum intel_display_power_domain
5714intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
5715{
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01005716 struct drm_i915_private *dev_priv = to_i915(intel_encoder->base.dev);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005717 struct intel_digital_port *intel_dig_port;
5718
5719 switch (intel_encoder->type) {
5720 case INTEL_OUTPUT_UNKNOWN:
Imre Deak651174a2015-11-18 15:57:24 +02005721 case INTEL_OUTPUT_HDMI:
5722 /*
5723 * Only DDI platforms should ever use these output types.
5724 * We can get here after the HDMI detect code has already set
5725 * the type of the shared encoder. Since we can't be sure
5726 * what's the status of the given connectors, play safe and
5727 * run the DP detection too.
5728 */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01005729 WARN_ON_ONCE(!HAS_DDI(dev_priv));
Ville Syrjäläcca05022016-06-22 21:57:06 +03005730 case INTEL_OUTPUT_DP:
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005731 case INTEL_OUTPUT_EDP:
5732 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
5733 return port_to_aux_power_domain(intel_dig_port->port);
5734 case INTEL_OUTPUT_DP_MST:
5735 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5736 return port_to_aux_power_domain(intel_dig_port->port);
5737 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005738 MISSING_CASE(intel_encoder->type);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005739 return POWER_DOMAIN_AUX_A;
5740 }
5741}
5742
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005743static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
5744 struct intel_crtc_state *crtc_state)
Imre Deak319be8a2014-03-04 19:22:57 +02005745{
5746 struct drm_device *dev = crtc->dev;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005747 struct drm_encoder *encoder;
Imre Deak319be8a2014-03-04 19:22:57 +02005748 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5749 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02005750 unsigned long mask;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005751 enum transcoder transcoder = crtc_state->cpu_transcoder;
Imre Deak77d22dc2014-03-05 16:20:52 +02005752
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005753 if (!crtc_state->base.active)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005754 return 0;
5755
Imre Deak77d22dc2014-03-05 16:20:52 +02005756 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5757 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005758 if (crtc_state->pch_pfit.enabled ||
5759 crtc_state->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02005760 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5761
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005762 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
5763 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5764
Imre Deak319be8a2014-03-04 19:22:57 +02005765 mask |= BIT(intel_display_port_power_domain(intel_encoder));
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005766 }
Imre Deak319be8a2014-03-04 19:22:57 +02005767
Maarten Lankhorst15e7ec22016-03-14 09:27:54 +01005768 if (crtc_state->shared_dpll)
5769 mask |= BIT(POWER_DOMAIN_PLLS);
5770
Imre Deak77d22dc2014-03-05 16:20:52 +02005771 return mask;
5772}
5773
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005774static unsigned long
5775modeset_get_crtc_power_domains(struct drm_crtc *crtc,
5776 struct intel_crtc_state *crtc_state)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005777{
Chris Wilsonfac5e232016-07-04 11:34:36 +01005778 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005779 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5780 enum intel_display_power_domain domain;
Daniel Vetter5a21b662016-05-24 17:13:53 +02005781 unsigned long domains, new_domains, old_domains;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005782
5783 old_domains = intel_crtc->enabled_power_domains;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005784 intel_crtc->enabled_power_domains = new_domains =
5785 get_crtc_power_domains(crtc, crtc_state);
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005786
Daniel Vetter5a21b662016-05-24 17:13:53 +02005787 domains = new_domains & ~old_domains;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005788
5789 for_each_power_domain(domain, domains)
5790 intel_display_power_get(dev_priv, domain);
5791
Daniel Vetter5a21b662016-05-24 17:13:53 +02005792 return old_domains & ~new_domains;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005793}
5794
5795static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
5796 unsigned long domains)
5797{
5798 enum intel_display_power_domain domain;
5799
5800 for_each_power_domain(domain, domains)
5801 intel_display_power_put(dev_priv, domain);
5802}
5803
Mika Kaholaadafdc62015-08-18 14:36:59 +03005804static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
5805{
5806 int max_cdclk_freq = dev_priv->max_cdclk_freq;
5807
5808 if (INTEL_INFO(dev_priv)->gen >= 9 ||
5809 IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
5810 return max_cdclk_freq;
5811 else if (IS_CHERRYVIEW(dev_priv))
5812 return max_cdclk_freq*95/100;
5813 else if (INTEL_INFO(dev_priv)->gen < 4)
5814 return 2*max_cdclk_freq*90/100;
5815 else
5816 return max_cdclk_freq*90/100;
5817}
5818
Ville Syrjäläb2045352016-05-13 23:41:27 +03005819static int skl_calc_cdclk(int max_pixclk, int vco);
5820
Ville Syrjälä4c75b942016-10-31 22:37:12 +02005821static void intel_update_max_cdclk(struct drm_i915_private *dev_priv)
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005822{
Tvrtko Ursulin08537232016-10-13 11:03:02 +01005823 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005824 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
Ville Syrjäläb2045352016-05-13 23:41:27 +03005825 int max_cdclk, vco;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005826
Ville Syrjäläb2045352016-05-13 23:41:27 +03005827 vco = dev_priv->skl_preferred_vco_freq;
Ville Syrjälä63911d72016-05-13 23:41:32 +03005828 WARN_ON(vco != 8100000 && vco != 8640000);
Ville Syrjäläb2045352016-05-13 23:41:27 +03005829
5830 /*
5831 * Use the lower (vco 8640) cdclk values as a
5832 * first guess. skl_calc_cdclk() will correct it
5833 * if the preferred vco is 8100 instead.
5834 */
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005835 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005836 max_cdclk = 617143;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005837 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
Ville Syrjäläb2045352016-05-13 23:41:27 +03005838 max_cdclk = 540000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005839 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
Ville Syrjäläb2045352016-05-13 23:41:27 +03005840 max_cdclk = 432000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005841 else
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005842 max_cdclk = 308571;
Ville Syrjäläb2045352016-05-13 23:41:27 +03005843
5844 dev_priv->max_cdclk_freq = skl_calc_cdclk(max_cdclk, vco);
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +01005845 } else if (IS_BROXTON(dev_priv)) {
Matt Roper281c1142016-04-05 14:37:19 -07005846 dev_priv->max_cdclk_freq = 624000;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01005847 } else if (IS_BROADWELL(dev_priv)) {
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005848 /*
5849 * FIXME with extra cooling we can allow
5850 * 540 MHz for ULX and 675 Mhz for ULT.
5851 * How can we know if extra cooling is
5852 * available? PCI ID, VTB, something else?
5853 */
5854 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5855 dev_priv->max_cdclk_freq = 450000;
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005856 else if (IS_BDW_ULX(dev_priv))
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005857 dev_priv->max_cdclk_freq = 450000;
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005858 else if (IS_BDW_ULT(dev_priv))
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005859 dev_priv->max_cdclk_freq = 540000;
5860 else
5861 dev_priv->max_cdclk_freq = 675000;
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01005862 } else if (IS_CHERRYVIEW(dev_priv)) {
Mika Kahola0904dea2015-06-12 10:11:32 +03005863 dev_priv->max_cdclk_freq = 320000;
Tvrtko Ursulin11a914c2016-10-13 11:03:08 +01005864 } else if (IS_VALLEYVIEW(dev_priv)) {
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005865 dev_priv->max_cdclk_freq = 400000;
5866 } else {
5867 /* otherwise assume cdclk is fixed */
5868 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5869 }
5870
Mika Kaholaadafdc62015-08-18 14:36:59 +03005871 dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
5872
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005873 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5874 dev_priv->max_cdclk_freq);
Mika Kaholaadafdc62015-08-18 14:36:59 +03005875
5876 DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
5877 dev_priv->max_dotclk_freq);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005878}
5879
Ville Syrjälä4c75b942016-10-31 22:37:12 +02005880static void intel_update_cdclk(struct drm_i915_private *dev_priv)
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005881{
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02005882 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev_priv);
Ville Syrjälä2f2a1212016-05-13 23:41:25 +03005883
Ville Syrjälä83d7c812016-05-13 23:41:35 +03005884 if (INTEL_GEN(dev_priv) >= 9)
Ville Syrjälä709e05c2016-05-13 23:41:33 +03005885 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz, VCO: %d kHz, ref: %d kHz\n",
5886 dev_priv->cdclk_freq, dev_priv->cdclk_pll.vco,
5887 dev_priv->cdclk_pll.ref);
Ville Syrjälä2f2a1212016-05-13 23:41:25 +03005888 else
5889 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5890 dev_priv->cdclk_freq);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005891
5892 /*
Ville Syrjäläb5d99ff2016-04-26 19:46:34 +03005893 * 9:0 CMBUS [sic] CDCLK frequency (cdfreq):
5894 * Programmng [sic] note: bit[9:2] should be programmed to the number
5895 * of cdclk that generates 4MHz reference clock freq which is used to
5896 * generate GMBus clock. This will vary with the cdclk freq.
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005897 */
Ville Syrjäläb5d99ff2016-04-26 19:46:34 +03005898 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005899 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005900}
5901
Ville Syrjälä92891e42016-05-11 22:44:45 +03005902/* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5903static int skl_cdclk_decimal(int cdclk)
5904{
5905 return DIV_ROUND_CLOSEST(cdclk - 1000, 500);
5906}
5907
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005908static int bxt_de_pll_vco(struct drm_i915_private *dev_priv, int cdclk)
5909{
5910 int ratio;
5911
5912 if (cdclk == dev_priv->cdclk_pll.ref)
5913 return 0;
5914
5915 switch (cdclk) {
5916 default:
5917 MISSING_CASE(cdclk);
5918 case 144000:
5919 case 288000:
5920 case 384000:
5921 case 576000:
5922 ratio = 60;
5923 break;
5924 case 624000:
5925 ratio = 65;
5926 break;
5927 }
5928
5929 return dev_priv->cdclk_pll.ref * ratio;
5930}
5931
Ville Syrjälä2b730012016-05-13 23:41:34 +03005932static void bxt_de_pll_disable(struct drm_i915_private *dev_priv)
5933{
5934 I915_WRITE(BXT_DE_PLL_ENABLE, 0);
5935
5936 /* Timeout 200us */
Chris Wilson95cac282016-06-30 15:33:03 +01005937 if (intel_wait_for_register(dev_priv,
5938 BXT_DE_PLL_ENABLE, BXT_DE_PLL_LOCK, 0,
5939 1))
Ville Syrjälä2b730012016-05-13 23:41:34 +03005940 DRM_ERROR("timeout waiting for DE PLL unlock\n");
Ville Syrjälä83d7c812016-05-13 23:41:35 +03005941
5942 dev_priv->cdclk_pll.vco = 0;
Ville Syrjälä2b730012016-05-13 23:41:34 +03005943}
5944
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005945static void bxt_de_pll_enable(struct drm_i915_private *dev_priv, int vco)
Ville Syrjälä2b730012016-05-13 23:41:34 +03005946{
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005947 int ratio = DIV_ROUND_CLOSEST(vco, dev_priv->cdclk_pll.ref);
Ville Syrjälä2b730012016-05-13 23:41:34 +03005948 u32 val;
5949
5950 val = I915_READ(BXT_DE_PLL_CTL);
5951 val &= ~BXT_DE_PLL_RATIO_MASK;
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005952 val |= BXT_DE_PLL_RATIO(ratio);
Ville Syrjälä2b730012016-05-13 23:41:34 +03005953 I915_WRITE(BXT_DE_PLL_CTL, val);
5954
5955 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5956
5957 /* Timeout 200us */
Chris Wilsone084e1b2016-06-30 15:33:04 +01005958 if (intel_wait_for_register(dev_priv,
5959 BXT_DE_PLL_ENABLE,
5960 BXT_DE_PLL_LOCK,
5961 BXT_DE_PLL_LOCK,
5962 1))
Ville Syrjälä2b730012016-05-13 23:41:34 +03005963 DRM_ERROR("timeout waiting for DE PLL lock\n");
Ville Syrjälä83d7c812016-05-13 23:41:35 +03005964
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005965 dev_priv->cdclk_pll.vco = vco;
Ville Syrjälä2b730012016-05-13 23:41:34 +03005966}
5967
Imre Deak324513c2016-06-13 16:44:36 +03005968static void bxt_set_cdclk(struct drm_i915_private *dev_priv, int cdclk)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305969{
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005970 u32 val, divider;
5971 int vco, ret;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305972
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005973 vco = bxt_de_pll_vco(dev_priv, cdclk);
5974
5975 DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
5976
5977 /* cdclk = vco / 2 / div{1,1.5,2,4} */
5978 switch (DIV_ROUND_CLOSEST(vco, cdclk)) {
5979 case 8:
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305980 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305981 break;
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005982 case 4:
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305983 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305984 break;
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005985 case 3:
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305986 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305987 break;
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005988 case 2:
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305989 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305990 break;
5991 default:
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005992 WARN_ON(cdclk != dev_priv->cdclk_pll.ref);
5993 WARN_ON(vco != 0);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305994
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005995 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5996 break;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305997 }
5998
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305999 /* Inform power controller of upcoming frequency change */
Ville Syrjälä5f199df2016-05-13 23:41:38 +03006000 mutex_lock(&dev_priv->rps.hw_lock);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306001 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
6002 0x80000000);
6003 mutex_unlock(&dev_priv->rps.hw_lock);
6004
6005 if (ret) {
6006 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
Ville Syrjälä9ef56152016-05-11 22:44:49 +03006007 ret, cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306008 return;
6009 }
6010
Ville Syrjälä5f199df2016-05-13 23:41:38 +03006011 if (dev_priv->cdclk_pll.vco != 0 &&
6012 dev_priv->cdclk_pll.vco != vco)
Ville Syrjälä2b730012016-05-13 23:41:34 +03006013 bxt_de_pll_disable(dev_priv);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306014
Ville Syrjälä5f199df2016-05-13 23:41:38 +03006015 if (dev_priv->cdclk_pll.vco != vco)
6016 bxt_de_pll_enable(dev_priv, vco);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306017
Ville Syrjälä5f199df2016-05-13 23:41:38 +03006018 val = divider | skl_cdclk_decimal(cdclk);
6019 /*
6020 * FIXME if only the cd2x divider needs changing, it could be done
6021 * without shutting off the pipe (if only one pipe is active).
6022 */
6023 val |= BXT_CDCLK_CD2X_PIPE_NONE;
6024 /*
6025 * Disable SSA Precharge when CD clock frequency < 500 MHz,
6026 * enable otherwise.
6027 */
6028 if (cdclk >= 500000)
6029 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
6030 I915_WRITE(CDCLK_CTL, val);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306031
6032 mutex_lock(&dev_priv->rps.hw_lock);
6033 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
Ville Syrjälä9ef56152016-05-11 22:44:49 +03006034 DIV_ROUND_UP(cdclk, 25000));
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306035 mutex_unlock(&dev_priv->rps.hw_lock);
6036
6037 if (ret) {
6038 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
Ville Syrjälä9ef56152016-05-11 22:44:49 +03006039 ret, cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306040 return;
6041 }
6042
Ville Syrjälä4c75b942016-10-31 22:37:12 +02006043 intel_update_cdclk(dev_priv);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306044}
6045
Imre Deakd66a2192016-05-24 15:38:33 +03006046static void bxt_sanitize_cdclk(struct drm_i915_private *dev_priv)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306047{
Imre Deakd66a2192016-05-24 15:38:33 +03006048 u32 cdctl, expected;
6049
Ville Syrjälä4c75b942016-10-31 22:37:12 +02006050 intel_update_cdclk(dev_priv);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306051
Imre Deakd66a2192016-05-24 15:38:33 +03006052 if (dev_priv->cdclk_pll.vco == 0 ||
6053 dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
6054 goto sanitize;
6055
6056 /* DPLL okay; verify the cdclock
6057 *
6058 * Some BIOS versions leave an incorrect decimal frequency value and
6059 * set reserved MBZ bits in CDCLK_CTL at least during exiting from S4,
6060 * so sanitize this register.
6061 */
6062 cdctl = I915_READ(CDCLK_CTL);
6063 /*
6064 * Let's ignore the pipe field, since BIOS could have configured the
6065 * dividers both synching to an active pipe, or asynchronously
6066 * (PIPE_NONE).
6067 */
6068 cdctl &= ~BXT_CDCLK_CD2X_PIPE_NONE;
6069
6070 expected = (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) |
6071 skl_cdclk_decimal(dev_priv->cdclk_freq);
6072 /*
6073 * Disable SSA Precharge when CD clock frequency < 500 MHz,
6074 * enable otherwise.
6075 */
6076 if (dev_priv->cdclk_freq >= 500000)
6077 expected |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
6078
6079 if (cdctl == expected)
6080 /* All well; nothing to sanitize */
6081 return;
6082
6083sanitize:
6084 DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
6085
6086 /* force cdclk programming */
6087 dev_priv->cdclk_freq = 0;
6088
6089 /* force full PLL disable + enable */
6090 dev_priv->cdclk_pll.vco = -1;
6091}
6092
Imre Deak324513c2016-06-13 16:44:36 +03006093void bxt_init_cdclk(struct drm_i915_private *dev_priv)
Imre Deakd66a2192016-05-24 15:38:33 +03006094{
6095 bxt_sanitize_cdclk(dev_priv);
6096
6097 if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0)
Ville Syrjälä089c6fd2016-05-13 23:41:36 +03006098 return;
Imre Deakc2e001e2016-04-01 16:02:43 +03006099
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306100 /*
6101 * FIXME:
6102 * - The initial CDCLK needs to be read from VBT.
6103 * Need to make this change after VBT has changes for BXT.
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306104 */
Imre Deak324513c2016-06-13 16:44:36 +03006105 bxt_set_cdclk(dev_priv, bxt_calc_cdclk(0));
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306106}
6107
Imre Deak324513c2016-06-13 16:44:36 +03006108void bxt_uninit_cdclk(struct drm_i915_private *dev_priv)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306109{
Imre Deak324513c2016-06-13 16:44:36 +03006110 bxt_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306111}
6112
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03006113static int skl_calc_cdclk(int max_pixclk, int vco)
6114{
Ville Syrjälä63911d72016-05-13 23:41:32 +03006115 if (vco == 8640000) {
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03006116 if (max_pixclk > 540000)
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03006117 return 617143;
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03006118 else if (max_pixclk > 432000)
6119 return 540000;
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03006120 else if (max_pixclk > 308571)
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03006121 return 432000;
6122 else
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03006123 return 308571;
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03006124 } else {
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03006125 if (max_pixclk > 540000)
6126 return 675000;
6127 else if (max_pixclk > 450000)
6128 return 540000;
6129 else if (max_pixclk > 337500)
6130 return 450000;
6131 else
6132 return 337500;
6133 }
6134}
6135
Ville Syrjäläea617912016-05-13 23:41:24 +03006136static void
6137skl_dpll0_update(struct drm_i915_private *dev_priv)
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006138{
Ville Syrjäläea617912016-05-13 23:41:24 +03006139 u32 val;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006140
Ville Syrjälä709e05c2016-05-13 23:41:33 +03006141 dev_priv->cdclk_pll.ref = 24000;
Imre Deak1c3f7702016-05-24 15:38:32 +03006142 dev_priv->cdclk_pll.vco = 0;
Ville Syrjälä709e05c2016-05-13 23:41:33 +03006143
Ville Syrjäläea617912016-05-13 23:41:24 +03006144 val = I915_READ(LCPLL1_CTL);
Imre Deak1c3f7702016-05-24 15:38:32 +03006145 if ((val & LCPLL_PLL_ENABLE) == 0)
Ville Syrjäläea617912016-05-13 23:41:24 +03006146 return;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006147
Imre Deak1c3f7702016-05-24 15:38:32 +03006148 if (WARN_ON((val & LCPLL_PLL_LOCK) == 0))
6149 return;
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03006150
Ville Syrjäläea617912016-05-13 23:41:24 +03006151 val = I915_READ(DPLL_CTRL1);
6152
Imre Deak1c3f7702016-05-24 15:38:32 +03006153 if (WARN_ON((val & (DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) |
6154 DPLL_CTRL1_SSC(SKL_DPLL0) |
6155 DPLL_CTRL1_OVERRIDE(SKL_DPLL0))) !=
6156 DPLL_CTRL1_OVERRIDE(SKL_DPLL0)))
6157 return;
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03006158
Ville Syrjäläea617912016-05-13 23:41:24 +03006159 switch (val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) {
6160 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810, SKL_DPLL0):
6161 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1350, SKL_DPLL0):
6162 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1620, SKL_DPLL0):
6163 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2700, SKL_DPLL0):
Ville Syrjälä63911d72016-05-13 23:41:32 +03006164 dev_priv->cdclk_pll.vco = 8100000;
Ville Syrjäläea617912016-05-13 23:41:24 +03006165 break;
6166 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080, SKL_DPLL0):
6167 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2160, SKL_DPLL0):
Ville Syrjälä63911d72016-05-13 23:41:32 +03006168 dev_priv->cdclk_pll.vco = 8640000;
Ville Syrjäläea617912016-05-13 23:41:24 +03006169 break;
6170 default:
6171 MISSING_CASE(val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
Ville Syrjäläea617912016-05-13 23:41:24 +03006172 break;
6173 }
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006174}
6175
Ville Syrjäläb2045352016-05-13 23:41:27 +03006176void skl_set_preferred_cdclk_vco(struct drm_i915_private *dev_priv, int vco)
6177{
6178 bool changed = dev_priv->skl_preferred_vco_freq != vco;
6179
6180 dev_priv->skl_preferred_vco_freq = vco;
6181
6182 if (changed)
Ville Syrjälä4c75b942016-10-31 22:37:12 +02006183 intel_update_max_cdclk(dev_priv);
Ville Syrjäläb2045352016-05-13 23:41:27 +03006184}
6185
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006186static void
Ville Syrjälä3861fc62016-05-11 22:44:50 +03006187skl_dpll0_enable(struct drm_i915_private *dev_priv, int vco)
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006188{
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03006189 int min_cdclk = skl_calc_cdclk(0, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006190 u32 val;
6191
Ville Syrjälä63911d72016-05-13 23:41:32 +03006192 WARN_ON(vco != 8100000 && vco != 8640000);
Ville Syrjäläb2045352016-05-13 23:41:27 +03006193
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006194 /* select the minimum CDCLK before enabling DPLL 0 */
Ville Syrjälä9ef56152016-05-11 22:44:49 +03006195 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_cdclk);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006196 I915_WRITE(CDCLK_CTL, val);
6197 POSTING_READ(CDCLK_CTL);
6198
6199 /*
6200 * We always enable DPLL0 with the lowest link rate possible, but still
6201 * taking into account the VCO required to operate the eDP panel at the
6202 * desired frequency. The usual DP link rates operate with a VCO of
6203 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
6204 * The modeset code is responsible for the selection of the exact link
6205 * rate later on, with the constraint of choosing a frequency that
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03006206 * works with vco.
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006207 */
6208 val = I915_READ(DPLL_CTRL1);
6209
6210 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
6211 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
6212 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
Ville Syrjälä63911d72016-05-13 23:41:32 +03006213 if (vco == 8640000)
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006214 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
6215 SKL_DPLL0);
6216 else
6217 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
6218 SKL_DPLL0);
6219
6220 I915_WRITE(DPLL_CTRL1, val);
6221 POSTING_READ(DPLL_CTRL1);
6222
6223 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
6224
Chris Wilsone24ca052016-06-30 15:33:05 +01006225 if (intel_wait_for_register(dev_priv,
6226 LCPLL1_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
6227 5))
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006228 DRM_ERROR("DPLL0 not locked\n");
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03006229
Ville Syrjälä63911d72016-05-13 23:41:32 +03006230 dev_priv->cdclk_pll.vco = vco;
Ville Syrjäläb2045352016-05-13 23:41:27 +03006231
6232 /* We'll want to keep using the current vco from now on. */
6233 skl_set_preferred_cdclk_vco(dev_priv, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006234}
6235
Ville Syrjälä430e05d2016-05-11 22:44:47 +03006236static void
6237skl_dpll0_disable(struct drm_i915_private *dev_priv)
6238{
6239 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
Chris Wilson8ad32a052016-06-30 15:33:06 +01006240 if (intel_wait_for_register(dev_priv,
6241 LCPLL1_CTL, LCPLL_PLL_LOCK, 0,
6242 1))
Ville Syrjälä430e05d2016-05-11 22:44:47 +03006243 DRM_ERROR("Couldn't disable DPLL0\n");
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03006244
Ville Syrjälä63911d72016-05-13 23:41:32 +03006245 dev_priv->cdclk_pll.vco = 0;
Ville Syrjälä430e05d2016-05-11 22:44:47 +03006246}
6247
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006248static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
6249{
6250 int ret;
6251 u32 val;
6252
6253 /* inform PCU we want to change CDCLK */
6254 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
6255 mutex_lock(&dev_priv->rps.hw_lock);
6256 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
6257 mutex_unlock(&dev_priv->rps.hw_lock);
6258
6259 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
6260}
6261
6262static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
6263{
Ville Syrjälä848496e2016-07-13 16:32:03 +03006264 return _wait_for(skl_cdclk_pcu_ready(dev_priv), 3000, 10) == 0;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006265}
6266
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03006267static void skl_set_cdclk(struct drm_i915_private *dev_priv, int cdclk, int vco)
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006268{
6269 u32 freq_select, pcu_ack;
6270
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03006271 WARN_ON((cdclk == 24000) != (vco == 0));
6272
Ville Syrjälä63911d72016-05-13 23:41:32 +03006273 DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006274
6275 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
6276 DRM_ERROR("failed to inform PCU about cdclk change\n");
6277 return;
6278 }
6279
6280 /* set CDCLK_CTL */
Ville Syrjälä9ef56152016-05-11 22:44:49 +03006281 switch (cdclk) {
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006282 case 450000:
6283 case 432000:
6284 freq_select = CDCLK_FREQ_450_432;
6285 pcu_ack = 1;
6286 break;
6287 case 540000:
6288 freq_select = CDCLK_FREQ_540;
6289 pcu_ack = 2;
6290 break;
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03006291 case 308571:
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006292 case 337500:
6293 default:
6294 freq_select = CDCLK_FREQ_337_308;
6295 pcu_ack = 0;
6296 break;
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03006297 case 617143:
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006298 case 675000:
6299 freq_select = CDCLK_FREQ_675_617;
6300 pcu_ack = 3;
6301 break;
6302 }
6303
Ville Syrjälä63911d72016-05-13 23:41:32 +03006304 if (dev_priv->cdclk_pll.vco != 0 &&
6305 dev_priv->cdclk_pll.vco != vco)
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03006306 skl_dpll0_disable(dev_priv);
6307
Ville Syrjälä63911d72016-05-13 23:41:32 +03006308 if (dev_priv->cdclk_pll.vco != vco)
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03006309 skl_dpll0_enable(dev_priv, vco);
6310
Ville Syrjälä9ef56152016-05-11 22:44:49 +03006311 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(cdclk));
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006312 POSTING_READ(CDCLK_CTL);
6313
6314 /* inform PCU of the change */
6315 mutex_lock(&dev_priv->rps.hw_lock);
6316 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
6317 mutex_unlock(&dev_priv->rps.hw_lock);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01006318
Ville Syrjälä4c75b942016-10-31 22:37:12 +02006319 intel_update_cdclk(dev_priv);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006320}
6321
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03006322static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv);
6323
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006324void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
6325{
Ville Syrjälä709e05c2016-05-13 23:41:33 +03006326 skl_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref, 0);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006327}
6328
6329void skl_init_cdclk(struct drm_i915_private *dev_priv)
6330{
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03006331 int cdclk, vco;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006332
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03006333 skl_sanitize_cdclk(dev_priv);
6334
Ville Syrjälä63911d72016-05-13 23:41:32 +03006335 if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0) {
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03006336 /*
6337 * Use the current vco as our initial
6338 * guess as to what the preferred vco is.
6339 */
6340 if (dev_priv->skl_preferred_vco_freq == 0)
6341 skl_set_preferred_cdclk_vco(dev_priv,
Ville Syrjälä63911d72016-05-13 23:41:32 +03006342 dev_priv->cdclk_pll.vco);
Ville Syrjälä70c2c182016-05-13 23:41:30 +03006343 return;
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03006344 }
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006345
Ville Syrjälä70c2c182016-05-13 23:41:30 +03006346 vco = dev_priv->skl_preferred_vco_freq;
6347 if (vco == 0)
Ville Syrjälä63911d72016-05-13 23:41:32 +03006348 vco = 8100000;
Ville Syrjälä70c2c182016-05-13 23:41:30 +03006349 cdclk = skl_calc_cdclk(0, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006350
Ville Syrjälä70c2c182016-05-13 23:41:30 +03006351 skl_set_cdclk(dev_priv, cdclk, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006352}
6353
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03006354static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
Shobhit Kumarc73666f2015-10-20 18:13:12 +05306355{
Ville Syrjälä09492492016-05-13 23:41:28 +03006356 uint32_t cdctl, expected;
Shobhit Kumarc73666f2015-10-20 18:13:12 +05306357
Shobhit Kumarf1b391a2015-11-05 18:05:32 +05306358 /*
6359 * check if the pre-os intialized the display
6360 * There is SWF18 scratchpad register defined which is set by the
6361 * pre-os which can be used by the OS drivers to check the status
6362 */
6363 if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
6364 goto sanitize;
6365
Ville Syrjälä4c75b942016-10-31 22:37:12 +02006366 intel_update_cdclk(dev_priv);
Imre Deak1c3f7702016-05-24 15:38:32 +03006367 /* Is PLL enabled and locked ? */
6368 if (dev_priv->cdclk_pll.vco == 0 ||
6369 dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
6370 goto sanitize;
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03006371
Shobhit Kumarc73666f2015-10-20 18:13:12 +05306372 /* DPLL okay; verify the cdclock
6373 *
6374 * Noticed in some instances that the freq selection is correct but
6375 * decimal part is programmed wrong from BIOS where pre-os does not
6376 * enable display. Verify the same as well.
6377 */
Ville Syrjälä09492492016-05-13 23:41:28 +03006378 cdctl = I915_READ(CDCLK_CTL);
6379 expected = (cdctl & CDCLK_FREQ_SEL_MASK) |
6380 skl_cdclk_decimal(dev_priv->cdclk_freq);
6381 if (cdctl == expected)
Shobhit Kumarc73666f2015-10-20 18:13:12 +05306382 /* All well; nothing to sanitize */
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03006383 return;
6384
Shobhit Kumarc73666f2015-10-20 18:13:12 +05306385sanitize:
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03006386 DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
Clint Taylorc89e39f2016-05-13 23:41:21 +03006387
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03006388 /* force cdclk programming */
6389 dev_priv->cdclk_freq = 0;
6390 /* force full PLL disable + enable */
Ville Syrjälä63911d72016-05-13 23:41:32 +03006391 dev_priv->cdclk_pll.vco = -1;
Shobhit Kumarc73666f2015-10-20 18:13:12 +05306392}
6393
Jesse Barnes30a970c2013-11-04 13:48:12 -08006394/* Adjust CDclk dividers to allow high res or save power if possible */
6395static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
6396{
Chris Wilsonfac5e232016-07-04 11:34:36 +01006397 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006398 u32 val, cmd;
6399
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02006400 WARN_ON(dev_priv->display.get_display_clock_speed(dev_priv)
Vandana Kannan164dfd22014-11-24 13:37:41 +05306401 != dev_priv->cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02006402
Ville Syrjälädfcab172014-06-13 13:37:47 +03006403 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08006404 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03006405 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006406 cmd = 1;
6407 else
6408 cmd = 0;
6409
6410 mutex_lock(&dev_priv->rps.hw_lock);
6411 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
6412 val &= ~DSPFREQGUAR_MASK;
6413 val |= (cmd << DSPFREQGUAR_SHIFT);
6414 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
6415 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
6416 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
6417 50)) {
6418 DRM_ERROR("timed out waiting for CDclk change\n");
6419 }
6420 mutex_unlock(&dev_priv->rps.hw_lock);
6421
Ville Syrjälä54433e92015-05-26 20:42:31 +03006422 mutex_lock(&dev_priv->sb_lock);
6423
Ville Syrjälädfcab172014-06-13 13:37:47 +03006424 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006425 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006426
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006427 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006428
Jesse Barnes30a970c2013-11-04 13:48:12 -08006429 /* adjust cdclk divider */
6430 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Vandana Kannan87d5d252015-09-24 23:29:17 +03006431 val &= ~CCK_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006432 val |= divider;
6433 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03006434
6435 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
Vandana Kannan87d5d252015-09-24 23:29:17 +03006436 CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
Ville Syrjäläa877e802014-06-13 13:37:52 +03006437 50))
6438 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08006439 }
6440
Jesse Barnes30a970c2013-11-04 13:48:12 -08006441 /* adjust self-refresh exit latency value */
6442 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
6443 val &= ~0x7f;
6444
6445 /*
6446 * For high bandwidth configs, we set a higher latency in the bunit
6447 * so that the core display fetch happens in time to avoid underruns.
6448 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03006449 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006450 val |= 4500 / 250; /* 4.5 usec */
6451 else
6452 val |= 3000 / 250; /* 3.0 usec */
6453 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
Ville Syrjälä54433e92015-05-26 20:42:31 +03006454
Ville Syrjäläa5805162015-05-26 20:42:30 +03006455 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006456
Ville Syrjälä4c75b942016-10-31 22:37:12 +02006457 intel_update_cdclk(dev_priv);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006458}
6459
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006460static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
6461{
Chris Wilsonfac5e232016-07-04 11:34:36 +01006462 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006463 u32 val, cmd;
6464
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02006465 WARN_ON(dev_priv->display.get_display_clock_speed(dev_priv)
Vandana Kannan164dfd22014-11-24 13:37:41 +05306466 != dev_priv->cdclk_freq);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006467
6468 switch (cdclk) {
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006469 case 333333:
6470 case 320000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006471 case 266667:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006472 case 200000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006473 break;
6474 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01006475 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006476 return;
6477 }
6478
Ville Syrjälä9d0d3fd2015-03-02 20:07:17 +02006479 /*
6480 * Specs are full of misinformation, but testing on actual
6481 * hardware has shown that we just need to write the desired
6482 * CCK divider into the Punit register.
6483 */
6484 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
6485
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006486 mutex_lock(&dev_priv->rps.hw_lock);
6487 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
6488 val &= ~DSPFREQGUAR_MASK_CHV;
6489 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
6490 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
6491 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
6492 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
6493 50)) {
6494 DRM_ERROR("timed out waiting for CDclk change\n");
6495 }
6496 mutex_unlock(&dev_priv->rps.hw_lock);
6497
Ville Syrjälä4c75b942016-10-31 22:37:12 +02006498 intel_update_cdclk(dev_priv);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006499}
6500
Jesse Barnes30a970c2013-11-04 13:48:12 -08006501static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
6502 int max_pixclk)
6503{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006504 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02006505 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03006506
Jesse Barnes30a970c2013-11-04 13:48:12 -08006507 /*
6508 * Really only a few cases to deal with, as only 4 CDclks are supported:
6509 * 200MHz
6510 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03006511 * 320/333MHz (depends on HPLL freq)
Ville Syrjälä6cca3192015-03-02 20:07:16 +02006512 * 400MHz (VLV only)
6513 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
6514 * of the lower bin and adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03006515 *
6516 * We seem to get an unstable or solid color picture at 200MHz.
6517 * Not sure what's wrong. For now use 200MHz only when all pipes
6518 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08006519 */
Ville Syrjälä6cca3192015-03-02 20:07:16 +02006520 if (!IS_CHERRYVIEW(dev_priv) &&
6521 max_pixclk > freq_320*limit/100)
Ville Syrjälädfcab172014-06-13 13:37:47 +03006522 return 400000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02006523 else if (max_pixclk > 266667*limit/100)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03006524 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03006525 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03006526 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03006527 else
6528 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006529}
6530
Imre Deak324513c2016-06-13 16:44:36 +03006531static int bxt_calc_cdclk(int max_pixclk)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006532{
Ville Syrjälä760e1472016-05-11 22:44:46 +03006533 if (max_pixclk > 576000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306534 return 624000;
Ville Syrjälä760e1472016-05-11 22:44:46 +03006535 else if (max_pixclk > 384000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306536 return 576000;
Ville Syrjälä760e1472016-05-11 22:44:46 +03006537 else if (max_pixclk > 288000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306538 return 384000;
Ville Syrjälä760e1472016-05-11 22:44:46 +03006539 else if (max_pixclk > 144000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306540 return 288000;
6541 else
6542 return 144000;
6543}
6544
Maarten Lankhorste8788cb2016-02-16 10:25:11 +01006545/* Compute the max pixel clock for new configuration. */
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006546static int intel_mode_max_pixclk(struct drm_device *dev,
6547 struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006548{
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006549 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Chris Wilsonfac5e232016-07-04 11:34:36 +01006550 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006551 struct drm_crtc *crtc;
6552 struct drm_crtc_state *crtc_state;
6553 unsigned max_pixclk = 0, i;
6554 enum pipe pipe;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006555
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006556 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
6557 sizeof(intel_state->min_pixclk));
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006558
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006559 for_each_crtc_in_state(state, crtc, crtc_state, i) {
6560 int pixclk = 0;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006561
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006562 if (crtc_state->enable)
6563 pixclk = crtc_state->adjusted_mode.crtc_clock;
6564
6565 intel_state->min_pixclk[i] = pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006566 }
6567
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006568 for_each_pipe(dev_priv, pipe)
6569 max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
6570
Jesse Barnes30a970c2013-11-04 13:48:12 -08006571 return max_pixclk;
6572}
6573
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006574static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006575{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006576 struct drm_device *dev = state->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006577 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006578 int max_pixclk = intel_mode_max_pixclk(dev, state);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006579 struct intel_atomic_state *intel_state =
6580 to_intel_atomic_state(state);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006581
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006582 intel_state->cdclk = intel_state->dev_cdclk =
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006583 valleyview_calc_cdclk(dev_priv, max_pixclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306584
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006585 if (!intel_state->active_crtcs)
6586 intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
6587
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006588 return 0;
6589}
Jesse Barnes30a970c2013-11-04 13:48:12 -08006590
Imre Deak324513c2016-06-13 16:44:36 +03006591static int bxt_modeset_calc_cdclk(struct drm_atomic_state *state)
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006592{
Ville Syrjälä4e5ca602016-05-11 22:44:44 +03006593 int max_pixclk = ilk_max_pixel_rate(state);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006594 struct intel_atomic_state *intel_state =
6595 to_intel_atomic_state(state);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02006596
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006597 intel_state->cdclk = intel_state->dev_cdclk =
Imre Deak324513c2016-06-13 16:44:36 +03006598 bxt_calc_cdclk(max_pixclk);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02006599
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006600 if (!intel_state->active_crtcs)
Imre Deak324513c2016-06-13 16:44:36 +03006601 intel_state->dev_cdclk = bxt_calc_cdclk(0);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006602
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006603 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006604}
6605
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006606static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
6607{
6608 unsigned int credits, default_credits;
6609
6610 if (IS_CHERRYVIEW(dev_priv))
6611 default_credits = PFI_CREDIT(12);
6612 else
6613 default_credits = PFI_CREDIT(8);
6614
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03006615 if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006616 /* CHV suggested value is 31 or 63 */
6617 if (IS_CHERRYVIEW(dev_priv))
Ville Syrjäläfcc00082015-05-26 20:22:40 +03006618 credits = PFI_CREDIT_63;
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006619 else
6620 credits = PFI_CREDIT(15);
6621 } else {
6622 credits = default_credits;
6623 }
6624
6625 /*
6626 * WA - write default credits before re-programming
6627 * FIXME: should we also set the resend bit here?
6628 */
6629 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6630 default_credits);
6631
6632 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6633 credits | PFI_CREDIT_RESEND);
6634
6635 /*
6636 * FIXME is this guaranteed to clear
6637 * immediately or should we poll for it?
6638 */
6639 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
6640}
6641
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006642static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006643{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006644 struct drm_device *dev = old_state->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006645 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006646 struct intel_atomic_state *old_intel_state =
6647 to_intel_atomic_state(old_state);
6648 unsigned req_cdclk = old_intel_state->dev_cdclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006649
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006650 /*
6651 * FIXME: We can end up here with all power domains off, yet
6652 * with a CDCLK frequency other than the minimum. To account
6653 * for this take the PIPE-A power domain, which covers the HW
6654 * blocks needed for the following programming. This can be
6655 * removed once it's guaranteed that we get here either with
6656 * the minimum CDCLK set, or the required power domains
6657 * enabled.
6658 */
6659 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006660
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01006661 if (IS_CHERRYVIEW(dev_priv))
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006662 cherryview_set_cdclk(dev, req_cdclk);
6663 else
6664 valleyview_set_cdclk(dev, req_cdclk);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006665
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006666 vlv_program_pfi_credits(dev_priv);
Imre Deak738c05c2014-11-19 16:25:37 +02006667
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006668 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006669}
6670
Maarten Lankhorst4a806552016-08-09 17:04:01 +02006671static void valleyview_crtc_enable(struct intel_crtc_state *pipe_config,
6672 struct drm_atomic_state *old_state)
Jesse Barnes89b667f2013-04-18 14:51:36 -07006673{
Maarten Lankhorst4a806552016-08-09 17:04:01 +02006674 struct drm_crtc *crtc = pipe_config->base.crtc;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006675 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006676 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006677 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006678 int pipe = intel_crtc->pipe;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006679
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006680 if (WARN_ON(intel_crtc->active))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006681 return;
6682
Ville Syrjälä37a56502016-06-22 21:57:04 +03006683 if (intel_crtc_has_dp_encoder(intel_crtc->config))
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306684 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006685
6686 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02006687 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006688
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01006689 if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
Chris Wilsonfac5e232016-07-04 11:34:36 +01006690 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03006691
6692 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6693 I915_WRITE(CHV_CANVAS(pipe), 0);
6694 }
6695
Daniel Vetter5b18e572014-04-24 23:55:06 +02006696 i9xx_set_pipeconf(intel_crtc);
6697
Jesse Barnes89b667f2013-04-18 14:51:36 -07006698 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006699
Daniel Vettera72e4c92014-09-30 10:56:47 +02006700 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006701
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02006702 intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006703
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01006704 if (IS_CHERRYVIEW(dev_priv)) {
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006705 chv_prepare_pll(intel_crtc, intel_crtc->config);
6706 chv_enable_pll(intel_crtc, intel_crtc->config);
6707 } else {
6708 vlv_prepare_pll(intel_crtc, intel_crtc->config);
6709 vlv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006710 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07006711
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02006712 intel_encoders_pre_enable(crtc, pipe_config, old_state);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006713
Jesse Barnes2dd24552013-04-25 12:55:01 -07006714 i9xx_pfit_enable(intel_crtc);
6715
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006716 intel_color_load_luts(&pipe_config->base);
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006717
Ville Syrjälä432081b2016-10-31 22:37:03 +02006718 intel_update_watermarks(intel_crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006719 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006720
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006721 assert_vblank_disabled(crtc);
6722 drm_crtc_vblank_on(crtc);
6723
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02006724 intel_encoders_enable(crtc, pipe_config, old_state);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006725}
6726
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006727static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6728{
6729 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006730 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006731
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006732 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6733 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006734}
6735
Maarten Lankhorst4a806552016-08-09 17:04:01 +02006736static void i9xx_crtc_enable(struct intel_crtc_state *pipe_config,
6737 struct drm_atomic_state *old_state)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006738{
Maarten Lankhorst4a806552016-08-09 17:04:01 +02006739 struct drm_crtc *crtc = pipe_config->base.crtc;
Zhenyu Wang2c072452009-06-05 15:38:42 +08006740 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006741 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006742 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006743 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08006744
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006745 if (WARN_ON(intel_crtc->active))
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006746 return;
6747
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006748 i9xx_set_pll_dividers(intel_crtc);
6749
Ville Syrjälä37a56502016-06-22 21:57:04 +03006750 if (intel_crtc_has_dp_encoder(intel_crtc->config))
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306751 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006752
6753 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02006754 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006755
Daniel Vetter5b18e572014-04-24 23:55:06 +02006756 i9xx_set_pipeconf(intel_crtc);
6757
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006758 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01006759
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01006760 if (!IS_GEN2(dev_priv))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006761 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006762
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02006763 intel_encoders_pre_enable(crtc, pipe_config, old_state);
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02006764
Daniel Vetterf6736a12013-06-05 13:34:30 +02006765 i9xx_enable_pll(intel_crtc);
6766
Jesse Barnes2dd24552013-04-25 12:55:01 -07006767 i9xx_pfit_enable(intel_crtc);
6768
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006769 intel_color_load_luts(&pipe_config->base);
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006770
Ville Syrjälä432081b2016-10-31 22:37:03 +02006771 intel_update_watermarks(intel_crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006772 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006773
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006774 assert_vblank_disabled(crtc);
6775 drm_crtc_vblank_on(crtc);
6776
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02006777 intel_encoders_enable(crtc, pipe_config, old_state);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006778}
6779
Daniel Vetter87476d62013-04-11 16:29:06 +02006780static void i9xx_pfit_disable(struct intel_crtc *crtc)
6781{
6782 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006783 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter328d8e82013-05-08 10:36:31 +02006784
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006785 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02006786 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02006787
6788 assert_pipe_disabled(dev_priv, crtc->pipe);
6789
Daniel Vetter328d8e82013-05-08 10:36:31 +02006790 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6791 I915_READ(PFIT_CONTROL));
6792 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02006793}
6794
Maarten Lankhorst4a806552016-08-09 17:04:01 +02006795static void i9xx_crtc_disable(struct intel_crtc_state *old_crtc_state,
6796 struct drm_atomic_state *old_state)
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006797{
Maarten Lankhorst4a806552016-08-09 17:04:01 +02006798 struct drm_crtc *crtc = old_crtc_state->base.crtc;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006799 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006800 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006801 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6802 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006803
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006804 /*
6805 * On gen2 planes are double buffered but the pipe isn't, so we must
6806 * wait for planes to fully turn off before disabling the pipe.
6807 */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01006808 if (IS_GEN2(dev_priv))
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02006809 intel_wait_for_vblank(dev_priv, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006810
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02006811 intel_encoders_disable(crtc, old_crtc_state, old_state);
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006812
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006813 drm_crtc_vblank_off(crtc);
6814 assert_vblank_disabled(crtc);
6815
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03006816 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006817
Daniel Vetter87476d62013-04-11 16:29:06 +02006818 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006819
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02006820 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006821
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03006822 if (!intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DSI)) {
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01006823 if (IS_CHERRYVIEW(dev_priv))
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006824 chv_disable_pll(dev_priv, pipe);
Tvrtko Ursulin11a914c2016-10-13 11:03:08 +01006825 else if (IS_VALLEYVIEW(dev_priv))
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006826 vlv_disable_pll(dev_priv, pipe);
6827 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03006828 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006829 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006830
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02006831 intel_encoders_post_pll_disable(crtc, old_crtc_state, old_state);
Ville Syrjäläd6db9952015-07-08 23:45:49 +03006832
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01006833 if (!IS_GEN2(dev_priv))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006834 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006835}
6836
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006837static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006838{
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006839 struct intel_encoder *encoder;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006840 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006841 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006842 enum intel_display_power_domain domain;
6843 unsigned long domains;
Maarten Lankhorst4a806552016-08-09 17:04:01 +02006844 struct drm_atomic_state *state;
6845 struct intel_crtc_state *crtc_state;
6846 int ret;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006847
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006848 if (!intel_crtc->active)
6849 return;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006850
Ville Syrjälä936e71e2016-07-26 19:06:59 +03006851 if (to_intel_plane_state(crtc->primary->state)->base.visible) {
Daniel Vetter5a21b662016-05-24 17:13:53 +02006852 WARN_ON(intel_crtc->flip_work);
Maarten Lankhorstfc32b1f2015-10-19 17:09:23 +02006853
Ville Syrjälä2622a082016-03-09 19:07:26 +02006854 intel_pre_disable_primary_noatomic(crtc);
Maarten Lankhorst54a419612015-11-23 10:25:28 +01006855
6856 intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
Ville Syrjälä936e71e2016-07-26 19:06:59 +03006857 to_intel_plane_state(crtc->primary->state)->base.visible = false;
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006858 }
6859
Maarten Lankhorst4a806552016-08-09 17:04:01 +02006860 state = drm_atomic_state_alloc(crtc->dev);
6861 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
6862
6863 /* Everything's already locked, -EDEADLK can't happen. */
6864 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
6865 ret = drm_atomic_add_affected_connectors(state, crtc);
6866
6867 WARN_ON(IS_ERR(crtc_state) || ret);
6868
6869 dev_priv->display.crtc_disable(crtc_state, state);
6870
Chris Wilson08536952016-10-14 13:18:18 +01006871 drm_atomic_state_put(state);
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006872
Ville Syrjälä78108b72016-05-27 20:59:19 +03006873 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
6874 crtc->base.id, crtc->name);
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006875
6876 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
6877 crtc->state->active = false;
Matt Roper37d90782015-09-24 15:53:06 -07006878 intel_crtc->active = false;
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006879 crtc->enabled = false;
6880 crtc->state->connector_mask = 0;
6881 crtc->state->encoder_mask = 0;
6882
6883 for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
6884 encoder->base.crtc = NULL;
6885
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -02006886 intel_fbc_disable(intel_crtc);
Ville Syrjälä432081b2016-10-31 22:37:03 +02006887 intel_update_watermarks(intel_crtc);
Maarten Lankhorst1f7457b2015-07-13 11:55:05 +02006888 intel_disable_shared_dpll(intel_crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006889
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006890 domains = intel_crtc->enabled_power_domains;
6891 for_each_power_domain(domain, domains)
6892 intel_display_power_put(dev_priv, domain);
6893 intel_crtc->enabled_power_domains = 0;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006894
6895 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
6896 dev_priv->min_pixclk[intel_crtc->pipe] = 0;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006897}
6898
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006899/*
6900 * turn all crtc's off, but do not adjust state
6901 * This has to be paired with a call to intel_modeset_setup_hw_state.
6902 */
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006903int intel_display_suspend(struct drm_device *dev)
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006904{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006905 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006906 struct drm_atomic_state *state;
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006907 int ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006908
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006909 state = drm_atomic_helper_suspend(dev);
6910 ret = PTR_ERR_OR_ZERO(state);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006911 if (ret)
6912 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006913 else
6914 dev_priv->modeset_restore_state = state;
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006915 return ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006916}
6917
Chris Wilsonea5b2132010-08-04 13:50:23 +01006918void intel_encoder_destroy(struct drm_encoder *encoder)
6919{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006920 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01006921
Chris Wilsonea5b2132010-08-04 13:50:23 +01006922 drm_encoder_cleanup(encoder);
6923 kfree(intel_encoder);
6924}
6925
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006926/* Cross check the actual hw state with our own modeset state tracking (and it's
6927 * internal consistency). */
Daniel Vetter5a21b662016-05-24 17:13:53 +02006928static void intel_connector_verify_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006929{
Daniel Vetter5a21b662016-05-24 17:13:53 +02006930 struct drm_crtc *crtc = connector->base.state->crtc;
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006931
6932 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6933 connector->base.base.id,
6934 connector->base.name);
6935
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006936 if (connector->get_hw_state(connector)) {
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006937 struct intel_encoder *encoder = connector->encoder;
Daniel Vetter5a21b662016-05-24 17:13:53 +02006938 struct drm_connector_state *conn_state = connector->base.state;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006939
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006940 I915_STATE_WARN(!crtc,
6941 "connector enabled without attached crtc\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006942
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006943 if (!crtc)
6944 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006945
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006946 I915_STATE_WARN(!crtc->state->active,
6947 "connector is active, but attached crtc isn't\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006948
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006949 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006950 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006951
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006952 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006953 "atomic encoder doesn't match attached encoder\n");
Dave Airlie36cd7442014-05-02 13:44:18 +10006954
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006955 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006956 "attached encoder crtc differs from connector crtc\n");
6957 } else {
Maarten Lankhorst4d688a22015-08-05 12:37:06 +02006958 I915_STATE_WARN(crtc && crtc->state->active,
6959 "attached crtc is active, but connector isn't\n");
Daniel Vetter5a21b662016-05-24 17:13:53 +02006960 I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006961 "best encoder set without crtc!\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006962 }
6963}
6964
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006965int intel_connector_init(struct intel_connector *connector)
6966{
Maarten Lankhorst5350a032016-01-04 12:53:15 +01006967 drm_atomic_helper_connector_reset(&connector->base);
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006968
Maarten Lankhorst5350a032016-01-04 12:53:15 +01006969 if (!connector->base.state)
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006970 return -ENOMEM;
6971
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006972 return 0;
6973}
6974
6975struct intel_connector *intel_connector_alloc(void)
6976{
6977 struct intel_connector *connector;
6978
6979 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6980 if (!connector)
6981 return NULL;
6982
6983 if (intel_connector_init(connector) < 0) {
6984 kfree(connector);
6985 return NULL;
6986 }
6987
6988 return connector;
6989}
6990
Daniel Vetterf0947c32012-07-02 13:10:34 +02006991/* Simple connector->get_hw_state implementation for encoders that support only
6992 * one connector and no cloning and hence the encoder state determines the state
6993 * of the connector. */
6994bool intel_connector_get_hw_state(struct intel_connector *connector)
6995{
Daniel Vetter24929352012-07-02 20:28:59 +02006996 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02006997 struct intel_encoder *encoder = connector->encoder;
6998
6999 return encoder->get_hw_state(encoder, &pipe);
7000}
7001
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007002static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02007003{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007004 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
7005 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02007006
7007 return 0;
7008}
7009
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007010static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007011 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02007012{
Tvrtko Ursulin86527442016-10-13 11:03:00 +01007013 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007014 struct drm_atomic_state *state = pipe_config->base.state;
7015 struct intel_crtc *other_crtc;
7016 struct intel_crtc_state *other_crtc_state;
7017
Daniel Vetter1857e1d2013-04-29 19:34:16 +02007018 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
7019 pipe_name(pipe), pipe_config->fdi_lanes);
7020 if (pipe_config->fdi_lanes > 4) {
7021 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
7022 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007023 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02007024 }
7025
Tvrtko Ursulin86527442016-10-13 11:03:00 +01007026 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02007027 if (pipe_config->fdi_lanes > 2) {
7028 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
7029 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007030 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02007031 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007032 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02007033 }
7034 }
7035
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +00007036 if (INTEL_INFO(dev_priv)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007037 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02007038
7039 /* Ivybridge 3 pipe is really complicated */
7040 switch (pipe) {
7041 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007042 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02007043 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007044 if (pipe_config->fdi_lanes <= 2)
7045 return 0;
7046
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +02007047 other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_C);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007048 other_crtc_state =
7049 intel_atomic_get_crtc_state(state, other_crtc);
7050 if (IS_ERR(other_crtc_state))
7051 return PTR_ERR(other_crtc_state);
7052
7053 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02007054 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
7055 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007056 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02007057 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007058 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02007059 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02007060 if (pipe_config->fdi_lanes > 2) {
7061 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
7062 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007063 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02007064 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007065
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +02007066 other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_B);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007067 other_crtc_state =
7068 intel_atomic_get_crtc_state(state, other_crtc);
7069 if (IS_ERR(other_crtc_state))
7070 return PTR_ERR(other_crtc_state);
7071
7072 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02007073 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007074 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02007075 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007076 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02007077 default:
7078 BUG();
7079 }
7080}
7081
Daniel Vettere29c22c2013-02-21 00:00:16 +01007082#define RETRY 1
7083static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007084 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02007085{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02007086 struct drm_device *dev = intel_crtc->base.dev;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03007087 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007088 int lane, link_bw, fdi_dotclock, ret;
7089 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02007090
Daniel Vettere29c22c2013-02-21 00:00:16 +01007091retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02007092 /* FDI is a binary signal running at ~2.7GHz, encoding
7093 * each output octet as 10 bits. The actual frequency
7094 * is stored as a divider into a 100MHz clock, and the
7095 * mode pixel clock is stored in units of 1KHz.
7096 * Hence the bw of each lane in terms of the mode signal
7097 * is:
7098 */
Ville Syrjälä21a727b2016-02-17 21:41:10 +02007099 link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02007100
Damien Lespiau241bfc32013-09-25 16:45:37 +01007101 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02007102
Daniel Vetter2bd89a02013-06-01 17:16:19 +02007103 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02007104 pipe_config->pipe_bpp);
7105
7106 pipe_config->fdi_lanes = lane;
7107
Daniel Vetter2bd89a02013-06-01 17:16:19 +02007108 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02007109 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02007110
Ville Syrjäläe3b247d2016-02-17 21:41:09 +02007111 ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007112 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01007113 pipe_config->pipe_bpp -= 2*3;
7114 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
7115 pipe_config->pipe_bpp);
7116 needs_recompute = true;
7117 pipe_config->bw_constrained = true;
7118
7119 goto retry;
7120 }
7121
7122 if (needs_recompute)
7123 return RETRY;
7124
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007125 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02007126}
7127
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03007128static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
7129 struct intel_crtc_state *pipe_config)
7130{
7131 if (pipe_config->pipe_bpp > 24)
7132 return false;
7133
7134 /* HSW can handle pixel rate up to cdclk? */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03007135 if (IS_HASWELL(dev_priv))
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03007136 return true;
7137
7138 /*
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03007139 * We compare against max which means we must take
7140 * the increased cdclk requirement into account when
7141 * calculating the new cdclk.
7142 *
7143 * Should measure whether using a lower cdclk w/o IPS
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03007144 */
7145 return ilk_pipe_pixel_rate(pipe_config) <=
7146 dev_priv->max_cdclk_freq * 95 / 100;
7147}
7148
Paulo Zanoni42db64e2013-05-31 16:33:22 -03007149static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007150 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03007151{
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03007152 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007153 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03007154
Jani Nikulad330a952014-01-21 11:24:25 +02007155 pipe_config->ips_enabled = i915.enable_ips &&
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03007156 hsw_crtc_supports_ips(crtc) &&
7157 pipe_config_supports_ips(dev_priv, pipe_config);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03007158}
7159
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02007160static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
7161{
7162 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7163
7164 /* GDG double wide on either pipe, otherwise pipe A only */
7165 return INTEL_INFO(dev_priv)->gen < 4 &&
7166 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
7167}
7168
Daniel Vettera43f6e02013-06-07 23:10:32 +02007169static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007170 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08007171{
Daniel Vettera43f6e02013-06-07 23:10:32 +02007172 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007173 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03007174 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ville Syrjäläf3261152016-05-24 21:34:18 +03007175 int clock_limit = dev_priv->max_dotclk_freq;
Chris Wilson89749352010-09-12 18:25:19 +01007176
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00007177 if (INTEL_GEN(dev_priv) < 4) {
Ville Syrjäläf3261152016-05-24 21:34:18 +03007178 clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007179
7180 /*
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02007181 * Enable double wide mode when the dot clock
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007182 * is > 90% of the (display) core speed.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007183 */
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02007184 if (intel_crtc_supports_double_wide(crtc) &&
7185 adjusted_mode->crtc_clock > clock_limit) {
Ville Syrjäläf3261152016-05-24 21:34:18 +03007186 clock_limit = dev_priv->max_dotclk_freq;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007187 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03007188 }
Ville Syrjäläf3261152016-05-24 21:34:18 +03007189 }
Ville Syrjäläad3a4472013-09-04 18:30:04 +03007190
Ville Syrjäläf3261152016-05-24 21:34:18 +03007191 if (adjusted_mode->crtc_clock > clock_limit) {
7192 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
7193 adjusted_mode->crtc_clock, clock_limit,
7194 yesno(pipe_config->double_wide));
7195 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08007196 }
Chris Wilson89749352010-09-12 18:25:19 +01007197
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03007198 /*
7199 * Pipe horizontal size must be even in:
7200 * - DVO ganged mode
7201 * - LVDS dual channel mode
7202 * - Double wide pipe
7203 */
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007204 if ((intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03007205 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
7206 pipe_config->pipe_src_w &= ~1;
7207
Damien Lespiau8693a822013-05-03 18:48:11 +01007208 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
7209 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03007210 */
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +01007211 if ((INTEL_GEN(dev_priv) > 4 || IS_G4X(dev_priv)) &&
Ville Syrjäläaad941d2015-09-25 16:38:56 +03007212 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01007213 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03007214
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007215 if (HAS_IPS(dev_priv))
Daniel Vettera43f6e02013-06-07 23:10:32 +02007216 hsw_compute_ips_config(crtc, pipe_config);
7217
Daniel Vetter877d48d2013-04-19 11:24:43 +02007218 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02007219 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02007220
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +02007221 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007222}
7223
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02007224static int skylake_get_display_clock_speed(struct drm_i915_private *dev_priv)
Ville Syrjälä1652d192015-03-31 14:12:01 +03007225{
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02007226 u32 cdctl;
Ville Syrjälä1652d192015-03-31 14:12:01 +03007227
Ville Syrjäläea617912016-05-13 23:41:24 +03007228 skl_dpll0_update(dev_priv);
7229
Ville Syrjälä63911d72016-05-13 23:41:32 +03007230 if (dev_priv->cdclk_pll.vco == 0)
Ville Syrjälä709e05c2016-05-13 23:41:33 +03007231 return dev_priv->cdclk_pll.ref;
Ville Syrjälä1652d192015-03-31 14:12:01 +03007232
Ville Syrjäläea617912016-05-13 23:41:24 +03007233 cdctl = I915_READ(CDCLK_CTL);
Ville Syrjälä1652d192015-03-31 14:12:01 +03007234
Ville Syrjälä63911d72016-05-13 23:41:32 +03007235 if (dev_priv->cdclk_pll.vco == 8640000) {
Ville Syrjälä1652d192015-03-31 14:12:01 +03007236 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
7237 case CDCLK_FREQ_450_432:
7238 return 432000;
7239 case CDCLK_FREQ_337_308:
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03007240 return 308571;
Ville Syrjäläea617912016-05-13 23:41:24 +03007241 case CDCLK_FREQ_540:
7242 return 540000;
Ville Syrjälä1652d192015-03-31 14:12:01 +03007243 case CDCLK_FREQ_675_617:
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03007244 return 617143;
Ville Syrjälä1652d192015-03-31 14:12:01 +03007245 default:
Ville Syrjäläea617912016-05-13 23:41:24 +03007246 MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
Ville Syrjälä1652d192015-03-31 14:12:01 +03007247 }
7248 } else {
Ville Syrjälä1652d192015-03-31 14:12:01 +03007249 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
7250 case CDCLK_FREQ_450_432:
7251 return 450000;
7252 case CDCLK_FREQ_337_308:
7253 return 337500;
Ville Syrjäläea617912016-05-13 23:41:24 +03007254 case CDCLK_FREQ_540:
7255 return 540000;
Ville Syrjälä1652d192015-03-31 14:12:01 +03007256 case CDCLK_FREQ_675_617:
7257 return 675000;
7258 default:
Ville Syrjäläea617912016-05-13 23:41:24 +03007259 MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
Ville Syrjälä1652d192015-03-31 14:12:01 +03007260 }
7261 }
7262
Ville Syrjälä709e05c2016-05-13 23:41:33 +03007263 return dev_priv->cdclk_pll.ref;
Ville Syrjälä1652d192015-03-31 14:12:01 +03007264}
7265
Ville Syrjälä83d7c812016-05-13 23:41:35 +03007266static void bxt_de_pll_update(struct drm_i915_private *dev_priv)
7267{
7268 u32 val;
7269
7270 dev_priv->cdclk_pll.ref = 19200;
Imre Deak1c3f7702016-05-24 15:38:32 +03007271 dev_priv->cdclk_pll.vco = 0;
Ville Syrjälä83d7c812016-05-13 23:41:35 +03007272
7273 val = I915_READ(BXT_DE_PLL_ENABLE);
Imre Deak1c3f7702016-05-24 15:38:32 +03007274 if ((val & BXT_DE_PLL_PLL_ENABLE) == 0)
Ville Syrjälä83d7c812016-05-13 23:41:35 +03007275 return;
Ville Syrjälä83d7c812016-05-13 23:41:35 +03007276
Imre Deak1c3f7702016-05-24 15:38:32 +03007277 if (WARN_ON((val & BXT_DE_PLL_LOCK) == 0))
7278 return;
Ville Syrjälä83d7c812016-05-13 23:41:35 +03007279
7280 val = I915_READ(BXT_DE_PLL_CTL);
7281 dev_priv->cdclk_pll.vco = (val & BXT_DE_PLL_RATIO_MASK) *
7282 dev_priv->cdclk_pll.ref;
7283}
7284
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02007285static int broxton_get_display_clock_speed(struct drm_i915_private *dev_priv)
Bob Paauweacd3f3d2015-06-23 14:14:26 -07007286{
Ville Syrjäläf5986242016-05-13 23:41:37 +03007287 u32 divider;
7288 int div, vco;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07007289
Ville Syrjälä83d7c812016-05-13 23:41:35 +03007290 bxt_de_pll_update(dev_priv);
7291
Ville Syrjäläf5986242016-05-13 23:41:37 +03007292 vco = dev_priv->cdclk_pll.vco;
7293 if (vco == 0)
7294 return dev_priv->cdclk_pll.ref;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07007295
Ville Syrjäläf5986242016-05-13 23:41:37 +03007296 divider = I915_READ(CDCLK_CTL) & BXT_CDCLK_CD2X_DIV_SEL_MASK;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07007297
Ville Syrjäläf5986242016-05-13 23:41:37 +03007298 switch (divider) {
Bob Paauweacd3f3d2015-06-23 14:14:26 -07007299 case BXT_CDCLK_CD2X_DIV_SEL_1:
Ville Syrjäläf5986242016-05-13 23:41:37 +03007300 div = 2;
7301 break;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07007302 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
Ville Syrjäläf5986242016-05-13 23:41:37 +03007303 div = 3;
7304 break;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07007305 case BXT_CDCLK_CD2X_DIV_SEL_2:
Ville Syrjäläf5986242016-05-13 23:41:37 +03007306 div = 4;
7307 break;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07007308 case BXT_CDCLK_CD2X_DIV_SEL_4:
Ville Syrjäläf5986242016-05-13 23:41:37 +03007309 div = 8;
7310 break;
7311 default:
7312 MISSING_CASE(divider);
7313 return dev_priv->cdclk_pll.ref;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07007314 }
7315
Ville Syrjäläf5986242016-05-13 23:41:37 +03007316 return DIV_ROUND_CLOSEST(vco, div);
Bob Paauweacd3f3d2015-06-23 14:14:26 -07007317}
7318
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02007319static int broadwell_get_display_clock_speed(struct drm_i915_private *dev_priv)
Ville Syrjälä1652d192015-03-31 14:12:01 +03007320{
Ville Syrjälä1652d192015-03-31 14:12:01 +03007321 uint32_t lcpll = I915_READ(LCPLL_CTL);
7322 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
7323
7324 if (lcpll & LCPLL_CD_SOURCE_FCLK)
7325 return 800000;
7326 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
7327 return 450000;
7328 else if (freq == LCPLL_CLK_FREQ_450)
7329 return 450000;
7330 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
7331 return 540000;
7332 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
7333 return 337500;
7334 else
7335 return 675000;
7336}
7337
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02007338static int haswell_get_display_clock_speed(struct drm_i915_private *dev_priv)
Ville Syrjälä1652d192015-03-31 14:12:01 +03007339{
Ville Syrjälä1652d192015-03-31 14:12:01 +03007340 uint32_t lcpll = I915_READ(LCPLL_CTL);
7341 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
7342
7343 if (lcpll & LCPLL_CD_SOURCE_FCLK)
7344 return 800000;
7345 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
7346 return 450000;
7347 else if (freq == LCPLL_CLK_FREQ_450)
7348 return 450000;
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007349 else if (IS_HSW_ULT(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +03007350 return 337500;
7351 else
7352 return 540000;
Jesse Barnes79e53942008-11-07 14:24:08 -08007353}
7354
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02007355static int valleyview_get_display_clock_speed(struct drm_i915_private *dev_priv)
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07007356{
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02007357 return vlv_get_cck_clock_hpll(dev_priv, "cdclk",
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03007358 CCK_DISPLAY_CLOCK_CONTROL);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07007359}
7360
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02007361static int ilk_get_display_clock_speed(struct drm_i915_private *dev_priv)
Ville Syrjäläb37a6432015-03-31 14:11:54 +03007362{
7363 return 450000;
7364}
7365
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02007366static int i945_get_display_clock_speed(struct drm_i915_private *dev_priv)
Jesse Barnes79e53942008-11-07 14:24:08 -08007367{
Jesse Barnese70236a2009-09-21 10:42:27 -07007368 return 400000;
7369}
Jesse Barnes79e53942008-11-07 14:24:08 -08007370
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02007371static int i915_get_display_clock_speed(struct drm_i915_private *dev_priv)
Jesse Barnese70236a2009-09-21 10:42:27 -07007372{
Ville Syrjäläe907f172015-03-31 14:09:47 +03007373 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07007374}
Jesse Barnes79e53942008-11-07 14:24:08 -08007375
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02007376static int i9xx_misc_get_display_clock_speed(struct drm_i915_private *dev_priv)
Jesse Barnese70236a2009-09-21 10:42:27 -07007377{
7378 return 200000;
7379}
Jesse Barnes79e53942008-11-07 14:24:08 -08007380
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02007381static int pnv_get_display_clock_speed(struct drm_i915_private *dev_priv)
Daniel Vetter257a7ff2013-07-26 08:35:42 +02007382{
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02007383 struct pci_dev *pdev = dev_priv->drm.pdev;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02007384 u16 gcfgc = 0;
7385
David Weinehall52a05c32016-08-22 13:32:44 +03007386 pci_read_config_word(pdev, GCFGC, &gcfgc);
Daniel Vetter257a7ff2013-07-26 08:35:42 +02007387
7388 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
7389 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03007390 return 266667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02007391 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03007392 return 333333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02007393 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03007394 return 444444;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02007395 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
7396 return 200000;
7397 default:
7398 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
7399 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03007400 return 133333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02007401 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03007402 return 166667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02007403 }
7404}
7405
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02007406static int i915gm_get_display_clock_speed(struct drm_i915_private *dev_priv)
Jesse Barnese70236a2009-09-21 10:42:27 -07007407{
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02007408 struct pci_dev *pdev = dev_priv->drm.pdev;
Jesse Barnese70236a2009-09-21 10:42:27 -07007409 u16 gcfgc = 0;
7410
David Weinehall52a05c32016-08-22 13:32:44 +03007411 pci_read_config_word(pdev, GCFGC, &gcfgc);
Jesse Barnese70236a2009-09-21 10:42:27 -07007412
7413 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Ville Syrjäläe907f172015-03-31 14:09:47 +03007414 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07007415 else {
7416 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
7417 case GC_DISPLAY_CLOCK_333_MHZ:
Ville Syrjäläe907f172015-03-31 14:09:47 +03007418 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07007419 default:
7420 case GC_DISPLAY_CLOCK_190_200_MHZ:
7421 return 190000;
7422 }
7423 }
7424}
Jesse Barnes79e53942008-11-07 14:24:08 -08007425
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02007426static int i865_get_display_clock_speed(struct drm_i915_private *dev_priv)
Jesse Barnese70236a2009-09-21 10:42:27 -07007427{
Ville Syrjäläe907f172015-03-31 14:09:47 +03007428 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07007429}
7430
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02007431static int i85x_get_display_clock_speed(struct drm_i915_private *dev_priv)
Jesse Barnese70236a2009-09-21 10:42:27 -07007432{
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02007433 struct pci_dev *pdev = dev_priv->drm.pdev;
Jesse Barnese70236a2009-09-21 10:42:27 -07007434 u16 hpllcc = 0;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03007435
Ville Syrjälä65cd2b32015-05-22 11:22:32 +03007436 /*
7437 * 852GM/852GMV only supports 133 MHz and the HPLLCC
7438 * encoding is different :(
7439 * FIXME is this the right way to detect 852GM/852GMV?
7440 */
David Weinehall52a05c32016-08-22 13:32:44 +03007441 if (pdev->revision == 0x1)
Ville Syrjälä65cd2b32015-05-22 11:22:32 +03007442 return 133333;
7443
David Weinehall52a05c32016-08-22 13:32:44 +03007444 pci_bus_read_config_word(pdev->bus,
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03007445 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
7446
Jesse Barnese70236a2009-09-21 10:42:27 -07007447 /* Assume that the hardware is in the high speed state. This
7448 * should be the default.
7449 */
7450 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
7451 case GC_CLOCK_133_200:
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03007452 case GC_CLOCK_133_200_2:
Jesse Barnese70236a2009-09-21 10:42:27 -07007453 case GC_CLOCK_100_200:
7454 return 200000;
7455 case GC_CLOCK_166_250:
7456 return 250000;
7457 case GC_CLOCK_100_133:
Ville Syrjäläe907f172015-03-31 14:09:47 +03007458 return 133333;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03007459 case GC_CLOCK_133_266:
7460 case GC_CLOCK_133_266_2:
7461 case GC_CLOCK_166_266:
7462 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07007463 }
7464
7465 /* Shouldn't happen */
7466 return 0;
7467}
7468
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02007469static int i830_get_display_clock_speed(struct drm_i915_private *dev_priv)
Jesse Barnese70236a2009-09-21 10:42:27 -07007470{
Ville Syrjäläe907f172015-03-31 14:09:47 +03007471 return 133333;
Jesse Barnes79e53942008-11-07 14:24:08 -08007472}
7473
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02007474static unsigned int intel_hpll_vco(struct drm_i915_private *dev_priv)
Ville Syrjälä34edce22015-05-22 11:22:33 +03007475{
Ville Syrjälä34edce22015-05-22 11:22:33 +03007476 static const unsigned int blb_vco[8] = {
7477 [0] = 3200000,
7478 [1] = 4000000,
7479 [2] = 5333333,
7480 [3] = 4800000,
7481 [4] = 6400000,
7482 };
7483 static const unsigned int pnv_vco[8] = {
7484 [0] = 3200000,
7485 [1] = 4000000,
7486 [2] = 5333333,
7487 [3] = 4800000,
7488 [4] = 2666667,
7489 };
7490 static const unsigned int cl_vco[8] = {
7491 [0] = 3200000,
7492 [1] = 4000000,
7493 [2] = 5333333,
7494 [3] = 6400000,
7495 [4] = 3333333,
7496 [5] = 3566667,
7497 [6] = 4266667,
7498 };
7499 static const unsigned int elk_vco[8] = {
7500 [0] = 3200000,
7501 [1] = 4000000,
7502 [2] = 5333333,
7503 [3] = 4800000,
7504 };
7505 static const unsigned int ctg_vco[8] = {
7506 [0] = 3200000,
7507 [1] = 4000000,
7508 [2] = 5333333,
7509 [3] = 6400000,
7510 [4] = 2666667,
7511 [5] = 4266667,
7512 };
7513 const unsigned int *vco_table;
7514 unsigned int vco;
7515 uint8_t tmp = 0;
7516
7517 /* FIXME other chipsets? */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007518 if (IS_GM45(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +03007519 vco_table = ctg_vco;
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +01007520 else if (IS_G4X(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +03007521 vco_table = elk_vco;
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02007522 else if (IS_CRESTLINE(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +03007523 vco_table = cl_vco;
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02007524 else if (IS_PINEVIEW(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +03007525 vco_table = pnv_vco;
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02007526 else if (IS_G33(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +03007527 vco_table = blb_vco;
7528 else
7529 return 0;
7530
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02007531 tmp = I915_READ(IS_MOBILE(dev_priv) ? HPLLVCO_MOBILE : HPLLVCO);
Ville Syrjälä34edce22015-05-22 11:22:33 +03007532
7533 vco = vco_table[tmp & 0x7];
7534 if (vco == 0)
7535 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
7536 else
7537 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
7538
7539 return vco;
7540}
7541
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02007542static int gm45_get_display_clock_speed(struct drm_i915_private *dev_priv)
Ville Syrjälä34edce22015-05-22 11:22:33 +03007543{
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02007544 struct pci_dev *pdev = dev_priv->drm.pdev;
7545 unsigned int cdclk_sel, vco = intel_hpll_vco(dev_priv);
Ville Syrjälä34edce22015-05-22 11:22:33 +03007546 uint16_t tmp = 0;
7547
David Weinehall52a05c32016-08-22 13:32:44 +03007548 pci_read_config_word(pdev, GCFGC, &tmp);
Ville Syrjälä34edce22015-05-22 11:22:33 +03007549
7550 cdclk_sel = (tmp >> 12) & 0x1;
7551
7552 switch (vco) {
7553 case 2666667:
7554 case 4000000:
7555 case 5333333:
7556 return cdclk_sel ? 333333 : 222222;
7557 case 3200000:
7558 return cdclk_sel ? 320000 : 228571;
7559 default:
7560 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
7561 return 222222;
7562 }
7563}
7564
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02007565static int i965gm_get_display_clock_speed(struct drm_i915_private *dev_priv)
Ville Syrjälä34edce22015-05-22 11:22:33 +03007566{
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02007567 struct pci_dev *pdev = dev_priv->drm.pdev;
Ville Syrjälä34edce22015-05-22 11:22:33 +03007568 static const uint8_t div_3200[] = { 16, 10, 8 };
7569 static const uint8_t div_4000[] = { 20, 12, 10 };
7570 static const uint8_t div_5333[] = { 24, 16, 14 };
7571 const uint8_t *div_table;
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02007572 unsigned int cdclk_sel, vco = intel_hpll_vco(dev_priv);
Ville Syrjälä34edce22015-05-22 11:22:33 +03007573 uint16_t tmp = 0;
7574
David Weinehall52a05c32016-08-22 13:32:44 +03007575 pci_read_config_word(pdev, GCFGC, &tmp);
Ville Syrjälä34edce22015-05-22 11:22:33 +03007576
7577 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
7578
7579 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7580 goto fail;
7581
7582 switch (vco) {
7583 case 3200000:
7584 div_table = div_3200;
7585 break;
7586 case 4000000:
7587 div_table = div_4000;
7588 break;
7589 case 5333333:
7590 div_table = div_5333;
7591 break;
7592 default:
7593 goto fail;
7594 }
7595
7596 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7597
Damien Lespiaucaf4e252015-06-04 16:56:18 +01007598fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03007599 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
7600 return 200000;
7601}
7602
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02007603static int g33_get_display_clock_speed(struct drm_i915_private *dev_priv)
Ville Syrjälä34edce22015-05-22 11:22:33 +03007604{
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02007605 struct pci_dev *pdev = dev_priv->drm.pdev;
Ville Syrjälä34edce22015-05-22 11:22:33 +03007606 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
7607 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
7608 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
7609 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
7610 const uint8_t *div_table;
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02007611 unsigned int cdclk_sel, vco = intel_hpll_vco(dev_priv);
Ville Syrjälä34edce22015-05-22 11:22:33 +03007612 uint16_t tmp = 0;
7613
David Weinehall52a05c32016-08-22 13:32:44 +03007614 pci_read_config_word(pdev, GCFGC, &tmp);
Ville Syrjälä34edce22015-05-22 11:22:33 +03007615
7616 cdclk_sel = (tmp >> 4) & 0x7;
7617
7618 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7619 goto fail;
7620
7621 switch (vco) {
7622 case 3200000:
7623 div_table = div_3200;
7624 break;
7625 case 4000000:
7626 div_table = div_4000;
7627 break;
7628 case 4800000:
7629 div_table = div_4800;
7630 break;
7631 case 5333333:
7632 div_table = div_5333;
7633 break;
7634 default:
7635 goto fail;
7636 }
7637
7638 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7639
Damien Lespiaucaf4e252015-06-04 16:56:18 +01007640fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03007641 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
7642 return 190476;
7643}
7644
Zhenyu Wang2c072452009-06-05 15:38:42 +08007645static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007646intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007647{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007648 while (*num > DATA_LINK_M_N_MASK ||
7649 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08007650 *num >>= 1;
7651 *den >>= 1;
7652 }
7653}
7654
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007655static void compute_m_n(unsigned int m, unsigned int n,
7656 uint32_t *ret_m, uint32_t *ret_n)
7657{
7658 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7659 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7660 intel_reduce_m_n_ratio(ret_m, ret_n);
7661}
7662
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007663void
7664intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7665 int pixel_clock, int link_clock,
7666 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007667{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007668 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007669
7670 compute_m_n(bits_per_pixel * pixel_clock,
7671 link_clock * nlanes * 8,
7672 &m_n->gmch_m, &m_n->gmch_n);
7673
7674 compute_m_n(pixel_clock, link_clock,
7675 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08007676}
7677
Chris Wilsona7615032011-01-12 17:04:08 +00007678static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7679{
Jani Nikulad330a952014-01-21 11:24:25 +02007680 if (i915.panel_use_ssc >= 0)
7681 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007682 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07007683 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00007684}
7685
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007686static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007687{
Daniel Vetter7df00d72013-05-21 21:54:55 +02007688 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007689}
Daniel Vetterf47709a2013-03-28 10:42:02 +01007690
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007691static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7692{
7693 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007694}
7695
Daniel Vetterf47709a2013-03-28 10:42:02 +01007696static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007697 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03007698 struct dpll *reduced_clock)
Jesse Barnesa7516a02011-12-15 12:30:37 -08007699{
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02007700 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007701 u32 fp, fp2 = 0;
7702
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02007703 if (IS_PINEVIEW(dev_priv)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007704 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007705 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007706 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007707 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007708 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007709 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007710 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007711 }
7712
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007713 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007714
Daniel Vetterf47709a2013-03-28 10:42:02 +01007715 crtc->lowfreq_avail = false;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007716 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07007717 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007718 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007719 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007720 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007721 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007722 }
7723}
7724
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007725static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7726 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07007727{
7728 u32 reg_val;
7729
7730 /*
7731 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7732 * and set it to a reasonable value instead.
7733 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007734 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007735 reg_val &= 0xffffff00;
7736 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007737 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007738
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007739 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007740 reg_val &= 0x8cffffff;
7741 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007742 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007743
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007744 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007745 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007746 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007747
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007748 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007749 reg_val &= 0x00ffffff;
7750 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007751 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007752}
7753
Daniel Vetterb5518422013-05-03 11:49:48 +02007754static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7755 struct intel_link_m_n *m_n)
7756{
7757 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007758 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetterb5518422013-05-03 11:49:48 +02007759 int pipe = crtc->pipe;
7760
Daniel Vettere3b95f12013-05-03 11:49:49 +02007761 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7762 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7763 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7764 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007765}
7766
7767static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07007768 struct intel_link_m_n *m_n,
7769 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02007770{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00007771 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Daniel Vetterb5518422013-05-03 11:49:48 +02007772 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007773 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02007774
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00007775 if (INTEL_GEN(dev_priv) >= 5) {
Daniel Vetterb5518422013-05-03 11:49:48 +02007776 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7777 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7778 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7779 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07007780 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7781 * for gen < 8) and if DRRS is supported (to make sure the
7782 * registers are not unnecessarily accessed).
7783 */
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01007784 if (m2_n2 && (IS_CHERRYVIEW(dev_priv) ||
7785 INTEL_GEN(dev_priv) < 8) && crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07007786 I915_WRITE(PIPE_DATA_M2(transcoder),
7787 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7788 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7789 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7790 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7791 }
Daniel Vetterb5518422013-05-03 11:49:48 +02007792 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02007793 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7794 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7795 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7796 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007797 }
7798}
7799
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307800void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007801{
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307802 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7803
7804 if (m_n == M1_N1) {
7805 dp_m_n = &crtc->config->dp_m_n;
7806 dp_m2_n2 = &crtc->config->dp_m2_n2;
7807 } else if (m_n == M2_N2) {
7808
7809 /*
7810 * M2_N2 registers are not supported. Hence m2_n2 divider value
7811 * needs to be programmed into M1_N1.
7812 */
7813 dp_m_n = &crtc->config->dp_m2_n2;
7814 } else {
7815 DRM_ERROR("Unsupported divider value\n");
7816 return;
7817 }
7818
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007819 if (crtc->config->has_pch_encoder)
7820 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007821 else
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307822 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007823}
7824
Daniel Vetter251ac862015-06-18 10:30:24 +02007825static void vlv_compute_dpll(struct intel_crtc *crtc,
7826 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007827{
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007828 pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007829 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007830 if (crtc->pipe != PIPE_A)
7831 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007832
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007833 /* DPLL not used with DSI, but still need the rest set up */
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03007834 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007835 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
7836 DPLL_EXT_BUFFER_ENABLE_VLV;
7837
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007838 pipe_config->dpll_hw_state.dpll_md =
7839 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
7840}
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007841
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007842static void chv_compute_dpll(struct intel_crtc *crtc,
7843 struct intel_crtc_state *pipe_config)
7844{
7845 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007846 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007847 if (crtc->pipe != PIPE_A)
7848 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7849
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007850 /* DPLL not used with DSI, but still need the rest set up */
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03007851 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007852 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
7853
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007854 pipe_config->dpll_hw_state.dpll_md =
7855 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007856}
7857
Ville Syrjäläd288f652014-10-28 13:20:22 +02007858static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007859 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007860{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007861 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007862 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007863 enum pipe pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007864 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007865 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007866 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007867
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007868 /* Enable Refclk */
7869 I915_WRITE(DPLL(pipe),
7870 pipe_config->dpll_hw_state.dpll &
7871 ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
7872
7873 /* No need to actually set up the DPLL with DSI */
7874 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7875 return;
7876
Ville Syrjäläa5805162015-05-26 20:42:30 +03007877 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01007878
Ville Syrjäläd288f652014-10-28 13:20:22 +02007879 bestn = pipe_config->dpll.n;
7880 bestm1 = pipe_config->dpll.m1;
7881 bestm2 = pipe_config->dpll.m2;
7882 bestp1 = pipe_config->dpll.p1;
7883 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007884
Jesse Barnes89b667f2013-04-18 14:51:36 -07007885 /* See eDP HDMI DPIO driver vbios notes doc */
7886
7887 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007888 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007889 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007890
7891 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007892 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007893
7894 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007895 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007896 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007897 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007898
7899 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007900 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007901
7902 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007903 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7904 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7905 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007906 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07007907
7908 /*
7909 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7910 * but we don't support that).
7911 * Note: don't use the DAC post divider as it seems unstable.
7912 */
7913 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007914 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007915
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007916 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007917 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007918
Jesse Barnes89b667f2013-04-18 14:51:36 -07007919 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02007920 if (pipe_config->port_clock == 162000 ||
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007921 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
7922 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007923 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b01202013-07-05 19:21:38 +03007924 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007925 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007926 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007927 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007928
Ville Syrjälä37a56502016-06-22 21:57:04 +03007929 if (intel_crtc_has_dp_encoder(pipe_config)) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07007930 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007931 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007932 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007933 0x0df40000);
7934 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007935 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007936 0x0df70000);
7937 } else { /* HDMI or VGA */
7938 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007939 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007940 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007941 0x0df70000);
7942 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007943 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007944 0x0df40000);
7945 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007946
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007947 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007948 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ville Syrjälä2210ce72016-06-22 21:57:05 +03007949 if (intel_crtc_has_dp_encoder(crtc->config))
Jesse Barnes89b667f2013-04-18 14:51:36 -07007950 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007951 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007952
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007953 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Ville Syrjäläa5805162015-05-26 20:42:30 +03007954 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007955}
7956
Ville Syrjäläd288f652014-10-28 13:20:22 +02007957static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007958 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007959{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007960 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007961 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007962 enum pipe pipe = crtc->pipe;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007963 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307964 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007965 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307966 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307967 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007968
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007969 /* Enable Refclk and SSC */
7970 I915_WRITE(DPLL(pipe),
7971 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
7972
7973 /* No need to actually set up the DPLL with DSI */
7974 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7975 return;
7976
Ville Syrjäläd288f652014-10-28 13:20:22 +02007977 bestn = pipe_config->dpll.n;
7978 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7979 bestm1 = pipe_config->dpll.m1;
7980 bestm2 = pipe_config->dpll.m2 >> 22;
7981 bestp1 = pipe_config->dpll.p1;
7982 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307983 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307984 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307985 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007986
Ville Syrjäläa5805162015-05-26 20:42:30 +03007987 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007988
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007989 /* p1 and p2 divider */
7990 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7991 5 << DPIO_CHV_S1_DIV_SHIFT |
7992 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7993 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7994 1 << DPIO_CHV_K_DIV_SHIFT);
7995
7996 /* Feedback post-divider - m2 */
7997 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7998
7999 /* Feedback refclk divider - n and m1 */
8000 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
8001 DPIO_CHV_M1_DIV_BY_2 |
8002 1 << DPIO_CHV_N_DIV_SHIFT);
8003
8004 /* M2 fraction division */
Ville Syrjälä25a25df2015-07-08 23:45:47 +03008005 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03008006
8007 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05308008 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
8009 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
8010 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
8011 if (bestm2_frac)
8012 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
8013 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03008014
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05308015 /* Program digital lock detect threshold */
8016 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
8017 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
8018 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
8019 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
8020 if (!bestm2_frac)
8021 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
8022 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
8023
Chon Ming Lee9d556c92014-05-02 14:27:47 +03008024 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05308025 if (vco == 5400000) {
8026 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
8027 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
8028 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
8029 tribuf_calcntr = 0x9;
8030 } else if (vco <= 6200000) {
8031 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
8032 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
8033 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
8034 tribuf_calcntr = 0x9;
8035 } else if (vco <= 6480000) {
8036 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
8037 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
8038 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
8039 tribuf_calcntr = 0x8;
8040 } else {
8041 /* Not supported. Apply the same limits as in the max case */
8042 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
8043 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
8044 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
8045 tribuf_calcntr = 0;
8046 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03008047 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
8048
Ville Syrjälä968040b2015-03-11 22:52:08 +02008049 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05308050 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
8051 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
8052 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
8053
Chon Ming Lee9d556c92014-05-02 14:27:47 +03008054 /* AFC Recal */
8055 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
8056 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
8057 DPIO_AFC_RECAL);
8058
Ville Syrjäläa5805162015-05-26 20:42:30 +03008059 mutex_unlock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03008060}
8061
Ville Syrjäläd288f652014-10-28 13:20:22 +02008062/**
8063 * vlv_force_pll_on - forcibly enable just the PLL
8064 * @dev_priv: i915 private structure
8065 * @pipe: pipe PLL to enable
8066 * @dpll: PLL configuration
8067 *
8068 * Enable the PLL for @pipe using the supplied @dpll config. To be used
8069 * in cases where we need the PLL enabled even when @pipe is not going to
8070 * be enabled.
8071 */
Ville Syrjälä30ad9812016-10-31 22:37:07 +02008072int vlv_force_pll_on(struct drm_i915_private *dev_priv, enum pipe pipe,
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00008073 const struct dpll *dpll)
Ville Syrjäläd288f652014-10-28 13:20:22 +02008074{
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +02008075 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00008076 struct intel_crtc_state *pipe_config;
8077
8078 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
8079 if (!pipe_config)
8080 return -ENOMEM;
8081
8082 pipe_config->base.crtc = &crtc->base;
8083 pipe_config->pixel_multiplier = 1;
8084 pipe_config->dpll = *dpll;
Ville Syrjäläd288f652014-10-28 13:20:22 +02008085
Ville Syrjälä30ad9812016-10-31 22:37:07 +02008086 if (IS_CHERRYVIEW(dev_priv)) {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00008087 chv_compute_dpll(crtc, pipe_config);
8088 chv_prepare_pll(crtc, pipe_config);
8089 chv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02008090 } else {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00008091 vlv_compute_dpll(crtc, pipe_config);
8092 vlv_prepare_pll(crtc, pipe_config);
8093 vlv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02008094 }
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00008095
8096 kfree(pipe_config);
8097
8098 return 0;
Ville Syrjäläd288f652014-10-28 13:20:22 +02008099}
8100
8101/**
8102 * vlv_force_pll_off - forcibly disable just the PLL
8103 * @dev_priv: i915 private structure
8104 * @pipe: pipe PLL to disable
8105 *
8106 * Disable the PLL for @pipe. To be used in cases where we need
8107 * the PLL enabled even when @pipe is not going to be enabled.
8108 */
Ville Syrjälä30ad9812016-10-31 22:37:07 +02008109void vlv_force_pll_off(struct drm_i915_private *dev_priv, enum pipe pipe)
Ville Syrjäläd288f652014-10-28 13:20:22 +02008110{
Ville Syrjälä30ad9812016-10-31 22:37:07 +02008111 if (IS_CHERRYVIEW(dev_priv))
8112 chv_disable_pll(dev_priv, pipe);
Ville Syrjäläd288f652014-10-28 13:20:22 +02008113 else
Ville Syrjälä30ad9812016-10-31 22:37:07 +02008114 vlv_disable_pll(dev_priv, pipe);
Ville Syrjäläd288f652014-10-28 13:20:22 +02008115}
8116
Daniel Vetter251ac862015-06-18 10:30:24 +02008117static void i9xx_compute_dpll(struct intel_crtc *crtc,
8118 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03008119 struct dpll *reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008120{
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02008121 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008122 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008123 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008124
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008125 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05308126
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008127 dpll = DPLL_VGA_MODE_DIS;
8128
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008129 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008130 dpll |= DPLLB_MODE_LVDS;
8131 else
8132 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01008133
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008134 if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) || IS_G33(dev_priv)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008135 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02008136 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008137 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02008138
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03008139 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
8140 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
Daniel Vetter4a33e482013-07-06 12:52:05 +02008141 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008142
Ville Syrjälä37a56502016-06-22 21:57:04 +03008143 if (intel_crtc_has_dp_encoder(crtc_state))
Daniel Vetter4a33e482013-07-06 12:52:05 +02008144 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008145
8146 /* compute bitmask from p1 value */
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02008147 if (IS_PINEVIEW(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008148 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
8149 else {
8150 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +01008151 if (IS_G4X(dev_priv) && reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008152 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
8153 }
8154 switch (clock->p2) {
8155 case 5:
8156 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8157 break;
8158 case 7:
8159 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8160 break;
8161 case 10:
8162 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8163 break;
8164 case 14:
8165 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8166 break;
8167 }
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02008168 if (INTEL_GEN(dev_priv) >= 4)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008169 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
8170
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008171 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008172 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008173 else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02008174 intel_panel_use_ssc(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008175 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
8176 else
8177 dpll |= PLL_REF_INPUT_DREFCLK;
8178
8179 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008180 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008181
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02008182 if (INTEL_GEN(dev_priv) >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008183 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02008184 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008185 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008186 }
8187}
8188
Daniel Vetter251ac862015-06-18 10:30:24 +02008189static void i8xx_compute_dpll(struct intel_crtc *crtc,
8190 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03008191 struct dpll *reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008192{
Daniel Vetterf47709a2013-03-28 10:42:02 +01008193 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008194 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008195 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008196 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008197
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008198 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05308199
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008200 dpll = DPLL_VGA_MODE_DIS;
8201
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008202 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008203 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
8204 } else {
8205 if (clock->p1 == 2)
8206 dpll |= PLL_P1_DIVIDE_BY_TWO;
8207 else
8208 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
8209 if (clock->p2 == 4)
8210 dpll |= PLL_P2_DIVIDE_BY_4;
8211 }
8212
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008213 if (!IS_I830(dev_priv) &&
8214 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02008215 dpll |= DPLL_DVO_2X_MODE;
8216
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008217 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02008218 intel_panel_use_ssc(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008219 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
8220 else
8221 dpll |= PLL_REF_INPUT_DREFCLK;
8222
8223 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008224 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008225}
8226
Daniel Vetter8a654f32013-06-01 17:16:22 +02008227static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03008228{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00008229 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03008230 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008231 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03008232 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02008233 uint32_t crtc_vtotal, crtc_vblank_end;
8234 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02008235
8236 /* We need to be careful not to changed the adjusted mode, for otherwise
8237 * the hw state checker will get angry at the mismatch. */
8238 crtc_vtotal = adjusted_mode->crtc_vtotal;
8239 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03008240
Ville Syrjälä609aeac2014-03-28 23:29:30 +02008241 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03008242 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02008243 crtc_vtotal -= 1;
8244 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02008245
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008246 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02008247 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
8248 else
8249 vsyncshift = adjusted_mode->crtc_hsync_start -
8250 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02008251 if (vsyncshift < 0)
8252 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03008253 }
8254
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00008255 if (INTEL_GEN(dev_priv) > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02008256 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03008257
Paulo Zanonife2b8f92012-10-23 18:30:02 -02008258 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03008259 (adjusted_mode->crtc_hdisplay - 1) |
8260 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02008261 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03008262 (adjusted_mode->crtc_hblank_start - 1) |
8263 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02008264 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03008265 (adjusted_mode->crtc_hsync_start - 1) |
8266 ((adjusted_mode->crtc_hsync_end - 1) << 16));
8267
Paulo Zanonife2b8f92012-10-23 18:30:02 -02008268 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03008269 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02008270 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02008271 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03008272 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02008273 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02008274 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03008275 (adjusted_mode->crtc_vsync_start - 1) |
8276 ((adjusted_mode->crtc_vsync_end - 1) << 16));
8277
Paulo Zanonib5e508d2012-10-24 11:34:43 -02008278 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
8279 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
8280 * documented on the DDI_FUNC_CTL register description, EDP Input Select
8281 * bits. */
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01008282 if (IS_HASWELL(dev_priv) && cpu_transcoder == TRANSCODER_EDP &&
Paulo Zanonib5e508d2012-10-24 11:34:43 -02008283 (pipe == PIPE_B || pipe == PIPE_C))
8284 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
8285
Jani Nikulabc58be62016-03-18 17:05:39 +02008286}
8287
8288static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
8289{
8290 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008291 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikulabc58be62016-03-18 17:05:39 +02008292 enum pipe pipe = intel_crtc->pipe;
8293
Paulo Zanonib0e77b92012-10-01 18:10:53 -03008294 /* pipesrc controls the size that is scaled from, which should
8295 * always be the user's requested size.
8296 */
8297 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008298 ((intel_crtc->config->pipe_src_w - 1) << 16) |
8299 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03008300}
8301
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008302static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008303 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008304{
8305 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008306 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008307 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
8308 uint32_t tmp;
8309
8310 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02008311 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
8312 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008313 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02008314 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
8315 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008316 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02008317 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
8318 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008319
8320 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02008321 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
8322 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008323 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02008324 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
8325 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008326 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02008327 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
8328 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008329
8330 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02008331 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
8332 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
8333 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008334 }
Jani Nikulabc58be62016-03-18 17:05:39 +02008335}
8336
8337static void intel_get_pipe_src_size(struct intel_crtc *crtc,
8338 struct intel_crtc_state *pipe_config)
8339{
8340 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008341 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikulabc58be62016-03-18 17:05:39 +02008342 u32 tmp;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008343
8344 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03008345 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
8346 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
8347
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02008348 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
8349 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008350}
8351
Daniel Vetterf6a83282014-02-11 15:28:57 -08008352void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008353 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03008354{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02008355 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
8356 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
8357 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
8358 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03008359
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02008360 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
8361 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
8362 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
8363 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03008364
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02008365 mode->flags = pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02008366 mode->type = DRM_MODE_TYPE_DRIVER;
Jesse Barnesbabea612013-06-26 18:57:38 +03008367
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02008368 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
8369 mode->flags |= pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02008370
8371 mode->hsync = drm_mode_hsync(mode);
8372 mode->vrefresh = drm_mode_vrefresh(mode);
8373 drm_mode_set_name(mode);
Jesse Barnesbabea612013-06-26 18:57:38 +03008374}
8375
Daniel Vetter84b046f2013-02-19 18:48:54 +01008376static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
8377{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00008378 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
Daniel Vetter84b046f2013-02-19 18:48:54 +01008379 uint32_t pipeconf;
8380
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02008381 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01008382
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03008383 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
8384 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
8385 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02008386
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008387 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03008388 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01008389
Daniel Vetterff9ce462013-04-24 14:57:17 +02008390 /* only g4x and later have fancy bpc/dither controls */
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +01008391 if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
8392 IS_CHERRYVIEW(dev_priv)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02008393 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008394 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02008395 pipeconf |= PIPECONF_DITHER_EN |
8396 PIPECONF_DITHER_TYPE_SP;
8397
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008398 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02008399 case 18:
8400 pipeconf |= PIPECONF_6BPC;
8401 break;
8402 case 24:
8403 pipeconf |= PIPECONF_8BPC;
8404 break;
8405 case 30:
8406 pipeconf |= PIPECONF_10BPC;
8407 break;
8408 default:
8409 /* Case prevented by intel_choose_pipe_bpp_dither. */
8410 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01008411 }
8412 }
8413
Tvrtko Ursulin56b857a2016-11-07 09:29:20 +00008414 if (HAS_PIPE_CXSR(dev_priv)) {
Daniel Vetter84b046f2013-02-19 18:48:54 +01008415 if (intel_crtc->lowfreq_avail) {
8416 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
8417 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
8418 } else {
8419 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01008420 }
8421 }
8422
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008423 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00008424 if (INTEL_GEN(dev_priv) < 4 ||
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008425 intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02008426 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
8427 else
8428 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
8429 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01008430 pipeconf |= PIPECONF_PROGRESSIVE;
8431
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01008432 if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
Wayne Boyer666a4532015-12-09 12:29:35 -08008433 intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02008434 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03008435
Daniel Vetter84b046f2013-02-19 18:48:54 +01008436 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
8437 POSTING_READ(PIPECONF(intel_crtc->pipe));
8438}
8439
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02008440static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
8441 struct intel_crtc_state *crtc_state)
8442{
8443 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008444 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008445 const struct intel_limit *limit;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02008446 int refclk = 48000;
8447
8448 memset(&crtc_state->dpll_hw_state, 0,
8449 sizeof(crtc_state->dpll_hw_state));
8450
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008451 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02008452 if (intel_panel_use_ssc(dev_priv)) {
8453 refclk = dev_priv->vbt.lvds_ssc_freq;
8454 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
8455 }
8456
8457 limit = &intel_limits_i8xx_lvds;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008458 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02008459 limit = &intel_limits_i8xx_dvo;
8460 } else {
8461 limit = &intel_limits_i8xx_dac;
8462 }
8463
8464 if (!crtc_state->clock_set &&
8465 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8466 refclk, NULL, &crtc_state->dpll)) {
8467 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8468 return -EINVAL;
8469 }
8470
8471 i8xx_compute_dpll(crtc, crtc_state, NULL);
8472
8473 return 0;
8474}
8475
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02008476static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
8477 struct intel_crtc_state *crtc_state)
8478{
8479 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008480 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008481 const struct intel_limit *limit;
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02008482 int refclk = 96000;
8483
8484 memset(&crtc_state->dpll_hw_state, 0,
8485 sizeof(crtc_state->dpll_hw_state));
8486
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008487 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02008488 if (intel_panel_use_ssc(dev_priv)) {
8489 refclk = dev_priv->vbt.lvds_ssc_freq;
8490 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
8491 }
8492
8493 if (intel_is_dual_link_lvds(dev))
8494 limit = &intel_limits_g4x_dual_channel_lvds;
8495 else
8496 limit = &intel_limits_g4x_single_channel_lvds;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008497 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
8498 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02008499 limit = &intel_limits_g4x_hdmi;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008500 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02008501 limit = &intel_limits_g4x_sdvo;
8502 } else {
8503 /* The option is for other outputs */
8504 limit = &intel_limits_i9xx_sdvo;
8505 }
8506
8507 if (!crtc_state->clock_set &&
8508 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8509 refclk, NULL, &crtc_state->dpll)) {
8510 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8511 return -EINVAL;
8512 }
8513
8514 i9xx_compute_dpll(crtc, crtc_state, NULL);
8515
8516 return 0;
8517}
8518
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008519static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
8520 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008521{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008522 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008523 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008524 const struct intel_limit *limit;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02008525 int refclk = 96000;
Jesse Barnes79e53942008-11-07 14:24:08 -08008526
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03008527 memset(&crtc_state->dpll_hw_state, 0,
8528 sizeof(crtc_state->dpll_hw_state));
8529
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008530 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008531 if (intel_panel_use_ssc(dev_priv)) {
8532 refclk = dev_priv->vbt.lvds_ssc_freq;
8533 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
8534 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008535
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008536 limit = &intel_limits_pineview_lvds;
8537 } else {
8538 limit = &intel_limits_pineview_sdvo;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02008539 }
Jani Nikulaf2335332013-09-13 11:03:09 +03008540
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008541 if (!crtc_state->clock_set &&
8542 !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8543 refclk, NULL, &crtc_state->dpll)) {
8544 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8545 return -EINVAL;
8546 }
8547
8548 i9xx_compute_dpll(crtc, crtc_state, NULL);
8549
8550 return 0;
8551}
8552
8553static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
8554 struct intel_crtc_state *crtc_state)
8555{
8556 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008557 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008558 const struct intel_limit *limit;
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008559 int refclk = 96000;
8560
8561 memset(&crtc_state->dpll_hw_state, 0,
8562 sizeof(crtc_state->dpll_hw_state));
8563
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008564 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008565 if (intel_panel_use_ssc(dev_priv)) {
8566 refclk = dev_priv->vbt.lvds_ssc_freq;
8567 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03008568 }
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008569
8570 limit = &intel_limits_i9xx_lvds;
8571 } else {
8572 limit = &intel_limits_i9xx_sdvo;
8573 }
8574
8575 if (!crtc_state->clock_set &&
8576 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8577 refclk, NULL, &crtc_state->dpll)) {
8578 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8579 return -EINVAL;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008580 }
Eric Anholtf564048e2011-03-30 13:01:02 -07008581
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02008582 i9xx_compute_dpll(crtc, crtc_state, NULL);
Eric Anholtf564048e2011-03-30 13:01:02 -07008583
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008584 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07008585}
8586
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008587static int chv_crtc_compute_clock(struct intel_crtc *crtc,
8588 struct intel_crtc_state *crtc_state)
8589{
8590 int refclk = 100000;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008591 const struct intel_limit *limit = &intel_limits_chv;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008592
8593 memset(&crtc_state->dpll_hw_state, 0,
8594 sizeof(crtc_state->dpll_hw_state));
8595
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008596 if (!crtc_state->clock_set &&
8597 !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8598 refclk, NULL, &crtc_state->dpll)) {
8599 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8600 return -EINVAL;
8601 }
8602
8603 chv_compute_dpll(crtc, crtc_state);
8604
8605 return 0;
8606}
8607
8608static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
8609 struct intel_crtc_state *crtc_state)
8610{
8611 int refclk = 100000;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008612 const struct intel_limit *limit = &intel_limits_vlv;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008613
8614 memset(&crtc_state->dpll_hw_state, 0,
8615 sizeof(crtc_state->dpll_hw_state));
8616
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008617 if (!crtc_state->clock_set &&
8618 !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8619 refclk, NULL, &crtc_state->dpll)) {
8620 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8621 return -EINVAL;
8622 }
8623
8624 vlv_compute_dpll(crtc, crtc_state);
8625
8626 return 0;
8627}
8628
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008629static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008630 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008631{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00008632 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008633 uint32_t tmp;
8634
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008635 if (INTEL_GEN(dev_priv) <= 3 &&
8636 (IS_I830(dev_priv) || !IS_MOBILE(dev_priv)))
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02008637 return;
8638
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008639 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02008640 if (!(tmp & PFIT_ENABLE))
8641 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008642
Daniel Vetter06922822013-07-11 13:35:40 +02008643 /* Check whether the pfit is attached to our pipe. */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00008644 if (INTEL_GEN(dev_priv) < 4) {
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008645 if (crtc->pipe != PIPE_B)
8646 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008647 } else {
8648 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
8649 return;
8650 }
8651
Daniel Vetter06922822013-07-11 13:35:40 +02008652 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008653 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008654}
8655
Jesse Barnesacbec812013-09-20 11:29:32 -07008656static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008657 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07008658{
8659 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008660 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesacbec812013-09-20 11:29:32 -07008661 int pipe = pipe_config->cpu_transcoder;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03008662 struct dpll clock;
Jesse Barnesacbec812013-09-20 11:29:32 -07008663 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07008664 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07008665
Ville Syrjäläb5219732016-03-15 16:40:01 +02008666 /* In case of DSI, DPLL will not be used */
8667 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
Shobhit Kumarf573de52014-07-30 20:32:37 +05308668 return;
8669
Ville Syrjäläa5805162015-05-26 20:42:30 +03008670 mutex_lock(&dev_priv->sb_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08008671 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008672 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesacbec812013-09-20 11:29:32 -07008673
8674 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
8675 clock.m2 = mdiv & DPIO_M2DIV_MASK;
8676 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
8677 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
8678 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
8679
Imre Deakdccbea32015-06-22 23:35:51 +03008680 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07008681}
8682
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008683static void
8684i9xx_get_initial_plane_config(struct intel_crtc *crtc,
8685 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008686{
8687 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008688 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008689 u32 val, base, offset;
8690 int pipe = crtc->pipe, plane = crtc->plane;
8691 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008692 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008693 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008694 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008695
Damien Lespiau42a7b082015-02-05 19:35:13 +00008696 val = I915_READ(DSPCNTR(plane));
8697 if (!(val & DISPLAY_PLANE_ENABLE))
8698 return;
8699
Damien Lespiaud9806c92015-01-21 14:07:19 +00008700 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008701 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008702 DRM_DEBUG_KMS("failed to alloc fb\n");
8703 return;
8704 }
8705
Damien Lespiau1b842c82015-01-21 13:50:54 +00008706 fb = &intel_fb->base;
8707
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00008708 if (INTEL_GEN(dev_priv) >= 4) {
Daniel Vetter18c52472015-02-10 17:16:09 +00008709 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008710 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00008711 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8712 }
8713 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008714
8715 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00008716 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008717 fb->pixel_format = fourcc;
8718 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008719
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00008720 if (INTEL_GEN(dev_priv) >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008721 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008722 offset = I915_READ(DSPTILEOFF(plane));
8723 else
8724 offset = I915_READ(DSPLINOFF(plane));
8725 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8726 } else {
8727 base = I915_READ(DSPADDR(plane));
8728 }
8729 plane_config->base = base;
8730
8731 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008732 fb->width = ((val >> 16) & 0xfff) + 1;
8733 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008734
8735 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008736 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008737
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008738 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008739 fb->pixel_format,
8740 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008741
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008742 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008743
Damien Lespiau2844a922015-01-20 12:51:48 +00008744 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8745 pipe_name(pipe), plane, fb->width, fb->height,
8746 fb->bits_per_pixel, base, fb->pitches[0],
8747 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008748
Damien Lespiau2d140302015-02-05 17:22:18 +00008749 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008750}
8751
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008752static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008753 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008754{
8755 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008756 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008757 int pipe = pipe_config->cpu_transcoder;
8758 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03008759 struct dpll clock;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008760 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008761 int refclk = 100000;
8762
Ville Syrjäläb5219732016-03-15 16:40:01 +02008763 /* In case of DSI, DPLL will not be used */
8764 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
8765 return;
8766
Ville Syrjäläa5805162015-05-26 20:42:30 +03008767 mutex_lock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008768 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8769 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8770 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8771 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
Imre Deak0d7b6b12015-07-02 14:29:58 +03008772 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008773 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008774
8775 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008776 clock.m2 = (pll_dw0 & 0xff) << 22;
8777 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
8778 clock.m2 |= pll_dw2 & 0x3fffff;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008779 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8780 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8781 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8782
Imre Deakdccbea32015-06-22 23:35:51 +03008783 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008784}
8785
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008786static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008787 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008788{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00008789 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Imre Deak17290502016-02-12 18:55:11 +02008790 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008791 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02008792 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008793
Imre Deak17290502016-02-12 18:55:11 +02008794 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8795 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +02008796 return false;
8797
Daniel Vettere143a212013-07-04 12:01:15 +02008798 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008799 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02008800
Imre Deak17290502016-02-12 18:55:11 +02008801 ret = false;
8802
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008803 tmp = I915_READ(PIPECONF(crtc->pipe));
8804 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02008805 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008806
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +01008807 if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
8808 IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008809 switch (tmp & PIPECONF_BPC_MASK) {
8810 case PIPECONF_6BPC:
8811 pipe_config->pipe_bpp = 18;
8812 break;
8813 case PIPECONF_8BPC:
8814 pipe_config->pipe_bpp = 24;
8815 break;
8816 case PIPECONF_10BPC:
8817 pipe_config->pipe_bpp = 30;
8818 break;
8819 default:
8820 break;
8821 }
8822 }
8823
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01008824 if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
Wayne Boyer666a4532015-12-09 12:29:35 -08008825 (tmp & PIPECONF_COLOR_RANGE_SELECT))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008826 pipe_config->limited_color_range = true;
8827
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00008828 if (INTEL_GEN(dev_priv) < 4)
Ville Syrjälä282740f2013-09-04 18:30:03 +03008829 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8830
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008831 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +02008832 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008833
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008834 i9xx_get_pfit_config(crtc, pipe_config);
8835
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00008836 if (INTEL_GEN(dev_priv) >= 4) {
Ville Syrjäläc2317752016-03-15 16:39:56 +02008837 /* No way to read it out on pipes B and C */
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01008838 if (IS_CHERRYVIEW(dev_priv) && crtc->pipe != PIPE_A)
Ville Syrjäläc2317752016-03-15 16:39:56 +02008839 tmp = dev_priv->chv_dpll_md[crtc->pipe];
8840 else
8841 tmp = I915_READ(DPLL_MD(crtc->pipe));
Daniel Vetter6c49f242013-06-06 12:45:25 +02008842 pipe_config->pixel_multiplier =
8843 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8844 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008845 pipe_config->dpll_hw_state.dpll_md = tmp;
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008846 } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
8847 IS_G33(dev_priv)) {
Daniel Vetter6c49f242013-06-06 12:45:25 +02008848 tmp = I915_READ(DPLL(crtc->pipe));
8849 pipe_config->pixel_multiplier =
8850 ((tmp & SDVO_MULTIPLIER_MASK)
8851 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8852 } else {
8853 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8854 * port and will be fixed up in the encoder->get_config
8855 * function. */
8856 pipe_config->pixel_multiplier = 1;
8857 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008858 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01008859 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03008860 /*
8861 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8862 * on 830. Filter it out here so that we don't
8863 * report errors due to that.
8864 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008865 if (IS_I830(dev_priv))
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03008866 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8867
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008868 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8869 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03008870 } else {
8871 /* Mask out read-only status bits. */
8872 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8873 DPLL_PORTC_READY_MASK |
8874 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008875 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02008876
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01008877 if (IS_CHERRYVIEW(dev_priv))
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008878 chv_crtc_clock_get(crtc, pipe_config);
Tvrtko Ursulin11a914c2016-10-13 11:03:08 +01008879 else if (IS_VALLEYVIEW(dev_priv))
Jesse Barnesacbec812013-09-20 11:29:32 -07008880 vlv_crtc_clock_get(crtc, pipe_config);
8881 else
8882 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03008883
Ville Syrjälä0f646142015-08-26 19:39:18 +03008884 /*
8885 * Normally the dotclock is filled in by the encoder .get_config()
8886 * but in case the pipe is enabled w/o any ports we need a sane
8887 * default.
8888 */
8889 pipe_config->base.adjusted_mode.crtc_clock =
8890 pipe_config->port_clock / pipe_config->pixel_multiplier;
8891
Imre Deak17290502016-02-12 18:55:11 +02008892 ret = true;
8893
8894out:
8895 intel_display_power_put(dev_priv, power_domain);
8896
8897 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008898}
8899
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02008900static void ironlake_init_pch_refclk(struct drm_i915_private *dev_priv)
Jesse Barnes13d83a62011-08-03 12:59:20 -07008901{
Jesse Barnes13d83a62011-08-03 12:59:20 -07008902 struct intel_encoder *encoder;
Lyude1c1a24d2016-06-14 11:04:09 -04008903 int i;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008904 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008905 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008906 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008907 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07008908 bool has_ck505 = false;
8909 bool can_ssc = false;
Lyude1c1a24d2016-06-14 11:04:09 -04008910 bool using_ssc_source = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008911
8912 /* We need to take the global config into account */
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02008913 for_each_intel_encoder(&dev_priv->drm, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07008914 switch (encoder->type) {
8915 case INTEL_OUTPUT_LVDS:
8916 has_panel = true;
8917 has_lvds = true;
8918 break;
8919 case INTEL_OUTPUT_EDP:
8920 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03008921 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07008922 has_cpu_edp = true;
8923 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008924 default:
8925 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008926 }
8927 }
8928
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01008929 if (HAS_PCH_IBX(dev_priv)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008930 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07008931 can_ssc = has_ck505;
8932 } else {
8933 has_ck505 = false;
8934 can_ssc = true;
8935 }
8936
Lyude1c1a24d2016-06-14 11:04:09 -04008937 /* Check if any DPLLs are using the SSC source */
8938 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
8939 u32 temp = I915_READ(PCH_DPLL(i));
8940
8941 if (!(temp & DPLL_VCO_ENABLE))
8942 continue;
8943
8944 if ((temp & PLL_REF_INPUT_MASK) ==
8945 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
8946 using_ssc_source = true;
8947 break;
8948 }
8949 }
8950
8951 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
8952 has_panel, has_lvds, has_ck505, using_ssc_source);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008953
8954 /* Ironlake: try to setup display ref clock before DPLL
8955 * enabling. This is only under driver's control after
8956 * PCH B stepping, previous chipset stepping should be
8957 * ignoring this setting.
8958 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008959 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008960
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008961 /* As we must carefully and slowly disable/enable each source in turn,
8962 * compute the final state we want first and check if we need to
8963 * make any changes at all.
8964 */
8965 final = val;
8966 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07008967 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008968 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07008969 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008970 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8971
Daniel Vetter8c07eb62016-06-09 18:39:07 +02008972 final &= ~DREF_SSC_SOURCE_MASK;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008973 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Daniel Vetter8c07eb62016-06-09 18:39:07 +02008974 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008975
Keith Packard199e5d72011-09-22 12:01:57 -07008976 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008977 final |= DREF_SSC_SOURCE_ENABLE;
8978
8979 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8980 final |= DREF_SSC1_ENABLE;
8981
8982 if (has_cpu_edp) {
8983 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8984 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8985 else
8986 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8987 } else
8988 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Lyude1c1a24d2016-06-14 11:04:09 -04008989 } else if (using_ssc_source) {
8990 final |= DREF_SSC_SOURCE_ENABLE;
8991 final |= DREF_SSC1_ENABLE;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008992 }
8993
8994 if (final == val)
8995 return;
8996
8997 /* Always enable nonspread source */
8998 val &= ~DREF_NONSPREAD_SOURCE_MASK;
8999
9000 if (has_ck505)
9001 val |= DREF_NONSPREAD_CK505_ENABLE;
9002 else
9003 val |= DREF_NONSPREAD_SOURCE_ENABLE;
9004
9005 if (has_panel) {
9006 val &= ~DREF_SSC_SOURCE_MASK;
9007 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07009008
Keith Packard199e5d72011-09-22 12:01:57 -07009009 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07009010 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07009011 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009012 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02009013 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009014 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07009015
9016 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009017 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07009018 POSTING_READ(PCH_DREF_CONTROL);
9019 udelay(200);
9020
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009021 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07009022
9023 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07009024 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07009025 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07009026 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009027 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02009028 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009029 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07009030 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009031 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07009032
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009033 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07009034 POSTING_READ(PCH_DREF_CONTROL);
9035 udelay(200);
9036 } else {
Lyude1c1a24d2016-06-14 11:04:09 -04009037 DRM_DEBUG_KMS("Disabling CPU source output\n");
Keith Packard199e5d72011-09-22 12:01:57 -07009038
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009039 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07009040
9041 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009042 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07009043
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009044 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07009045 POSTING_READ(PCH_DREF_CONTROL);
9046 udelay(200);
9047
Lyude1c1a24d2016-06-14 11:04:09 -04009048 if (!using_ssc_source) {
9049 DRM_DEBUG_KMS("Disabling SSC source\n");
Keith Packard199e5d72011-09-22 12:01:57 -07009050
Lyude1c1a24d2016-06-14 11:04:09 -04009051 /* Turn off the SSC source */
9052 val &= ~DREF_SSC_SOURCE_MASK;
9053 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07009054
Lyude1c1a24d2016-06-14 11:04:09 -04009055 /* Turn off SSC1 */
9056 val &= ~DREF_SSC1_ENABLE;
9057
9058 I915_WRITE(PCH_DREF_CONTROL, val);
9059 POSTING_READ(PCH_DREF_CONTROL);
9060 udelay(200);
9061 }
Jesse Barnes13d83a62011-08-03 12:59:20 -07009062 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009063
9064 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07009065}
9066
Paulo Zanonif31f2d52013-07-18 18:51:11 -03009067static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02009068{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03009069 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02009070
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03009071 tmp = I915_READ(SOUTH_CHICKEN2);
9072 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
9073 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02009074
Imre Deakcf3598c2016-06-28 13:37:31 +03009075 if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
9076 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03009077 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02009078
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03009079 tmp = I915_READ(SOUTH_CHICKEN2);
9080 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
9081 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02009082
Imre Deakcf3598c2016-06-28 13:37:31 +03009083 if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
9084 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03009085 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03009086}
9087
9088/* WaMPhyProgramming:hsw */
9089static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
9090{
9091 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02009092
9093 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
9094 tmp &= ~(0xFF << 24);
9095 tmp |= (0x12 << 24);
9096 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
9097
Paulo Zanonidde86e22012-12-01 12:04:25 -02009098 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
9099 tmp |= (1 << 11);
9100 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
9101
9102 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
9103 tmp |= (1 << 11);
9104 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
9105
Paulo Zanonidde86e22012-12-01 12:04:25 -02009106 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
9107 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
9108 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
9109
9110 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
9111 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
9112 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
9113
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03009114 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
9115 tmp &= ~(7 << 13);
9116 tmp |= (5 << 13);
9117 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02009118
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03009119 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
9120 tmp &= ~(7 << 13);
9121 tmp |= (5 << 13);
9122 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02009123
9124 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
9125 tmp &= ~0xFF;
9126 tmp |= 0x1C;
9127 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
9128
9129 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
9130 tmp &= ~0xFF;
9131 tmp |= 0x1C;
9132 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
9133
9134 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
9135 tmp &= ~(0xFF << 16);
9136 tmp |= (0x1C << 16);
9137 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
9138
9139 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
9140 tmp &= ~(0xFF << 16);
9141 tmp |= (0x1C << 16);
9142 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
9143
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03009144 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
9145 tmp |= (1 << 27);
9146 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02009147
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03009148 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
9149 tmp |= (1 << 27);
9150 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02009151
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03009152 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
9153 tmp &= ~(0xF << 28);
9154 tmp |= (4 << 28);
9155 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02009156
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03009157 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
9158 tmp &= ~(0xF << 28);
9159 tmp |= (4 << 28);
9160 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03009161}
9162
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03009163/* Implements 3 different sequences from BSpec chapter "Display iCLK
9164 * Programming" based on the parameters passed:
9165 * - Sequence to enable CLKOUT_DP
9166 * - Sequence to enable CLKOUT_DP without spread
9167 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
9168 */
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02009169static void lpt_enable_clkout_dp(struct drm_i915_private *dev_priv,
9170 bool with_spread, bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03009171{
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03009172 uint32_t reg, tmp;
9173
9174 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
9175 with_spread = true;
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01009176 if (WARN(HAS_PCH_LPT_LP(dev_priv) &&
9177 with_fdi, "LP PCH doesn't have FDI\n"))
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03009178 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03009179
Ville Syrjäläa5805162015-05-26 20:42:30 +03009180 mutex_lock(&dev_priv->sb_lock);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03009181
9182 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
9183 tmp &= ~SBI_SSCCTL_DISABLE;
9184 tmp |= SBI_SSCCTL_PATHALT;
9185 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
9186
9187 udelay(24);
9188
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03009189 if (with_spread) {
9190 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
9191 tmp &= ~SBI_SSCCTL_PATHALT;
9192 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03009193
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03009194 if (with_fdi) {
9195 lpt_reset_fdi_mphy(dev_priv);
9196 lpt_program_fdi_mphy(dev_priv);
9197 }
9198 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02009199
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01009200 reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03009201 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
9202 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
9203 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01009204
Ville Syrjäläa5805162015-05-26 20:42:30 +03009205 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02009206}
9207
Paulo Zanoni47701c32013-07-23 11:19:25 -03009208/* Sequence to disable CLKOUT_DP */
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02009209static void lpt_disable_clkout_dp(struct drm_i915_private *dev_priv)
Paulo Zanoni47701c32013-07-23 11:19:25 -03009210{
Paulo Zanoni47701c32013-07-23 11:19:25 -03009211 uint32_t reg, tmp;
9212
Ville Syrjäläa5805162015-05-26 20:42:30 +03009213 mutex_lock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03009214
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01009215 reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni47701c32013-07-23 11:19:25 -03009216 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
9217 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
9218 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
9219
9220 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
9221 if (!(tmp & SBI_SSCCTL_DISABLE)) {
9222 if (!(tmp & SBI_SSCCTL_PATHALT)) {
9223 tmp |= SBI_SSCCTL_PATHALT;
9224 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
9225 udelay(32);
9226 }
9227 tmp |= SBI_SSCCTL_DISABLE;
9228 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
9229 }
9230
Ville Syrjäläa5805162015-05-26 20:42:30 +03009231 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03009232}
9233
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02009234#define BEND_IDX(steps) ((50 + (steps)) / 5)
9235
9236static const uint16_t sscdivintphase[] = {
9237 [BEND_IDX( 50)] = 0x3B23,
9238 [BEND_IDX( 45)] = 0x3B23,
9239 [BEND_IDX( 40)] = 0x3C23,
9240 [BEND_IDX( 35)] = 0x3C23,
9241 [BEND_IDX( 30)] = 0x3D23,
9242 [BEND_IDX( 25)] = 0x3D23,
9243 [BEND_IDX( 20)] = 0x3E23,
9244 [BEND_IDX( 15)] = 0x3E23,
9245 [BEND_IDX( 10)] = 0x3F23,
9246 [BEND_IDX( 5)] = 0x3F23,
9247 [BEND_IDX( 0)] = 0x0025,
9248 [BEND_IDX( -5)] = 0x0025,
9249 [BEND_IDX(-10)] = 0x0125,
9250 [BEND_IDX(-15)] = 0x0125,
9251 [BEND_IDX(-20)] = 0x0225,
9252 [BEND_IDX(-25)] = 0x0225,
9253 [BEND_IDX(-30)] = 0x0325,
9254 [BEND_IDX(-35)] = 0x0325,
9255 [BEND_IDX(-40)] = 0x0425,
9256 [BEND_IDX(-45)] = 0x0425,
9257 [BEND_IDX(-50)] = 0x0525,
9258};
9259
9260/*
9261 * Bend CLKOUT_DP
9262 * steps -50 to 50 inclusive, in steps of 5
9263 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
9264 * change in clock period = -(steps / 10) * 5.787 ps
9265 */
9266static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
9267{
9268 uint32_t tmp;
9269 int idx = BEND_IDX(steps);
9270
9271 if (WARN_ON(steps % 5 != 0))
9272 return;
9273
9274 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
9275 return;
9276
9277 mutex_lock(&dev_priv->sb_lock);
9278
9279 if (steps % 10 != 0)
9280 tmp = 0xAAAAAAAB;
9281 else
9282 tmp = 0x00000000;
9283 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
9284
9285 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
9286 tmp &= 0xffff0000;
9287 tmp |= sscdivintphase[idx];
9288 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
9289
9290 mutex_unlock(&dev_priv->sb_lock);
9291}
9292
9293#undef BEND_IDX
9294
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02009295static void lpt_init_pch_refclk(struct drm_i915_private *dev_priv)
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03009296{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03009297 struct intel_encoder *encoder;
9298 bool has_vga = false;
9299
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02009300 for_each_intel_encoder(&dev_priv->drm, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03009301 switch (encoder->type) {
9302 case INTEL_OUTPUT_ANALOG:
9303 has_vga = true;
9304 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02009305 default:
9306 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03009307 }
9308 }
9309
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02009310 if (has_vga) {
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02009311 lpt_bend_clkout_dp(dev_priv, 0);
9312 lpt_enable_clkout_dp(dev_priv, true, true);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02009313 } else {
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02009314 lpt_disable_clkout_dp(dev_priv);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02009315 }
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03009316}
9317
Paulo Zanonidde86e22012-12-01 12:04:25 -02009318/*
9319 * Initialize reference clocks when the driver loads
9320 */
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02009321void intel_init_pch_refclk(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02009322{
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01009323 if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02009324 ironlake_init_pch_refclk(dev_priv);
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01009325 else if (HAS_PCH_LPT(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02009326 lpt_init_pch_refclk(dev_priv);
Paulo Zanonidde86e22012-12-01 12:04:25 -02009327}
9328
Daniel Vetter6ff93602013-04-19 11:24:36 +02009329static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03009330{
Chris Wilsonfac5e232016-07-04 11:34:36 +01009331 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Paulo Zanonic8203562012-09-12 10:06:29 -03009332 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9333 int pipe = intel_crtc->pipe;
9334 uint32_t val;
9335
Daniel Vetter78114072013-06-13 00:54:57 +02009336 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03009337
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009338 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03009339 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01009340 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03009341 break;
9342 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01009343 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03009344 break;
9345 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01009346 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03009347 break;
9348 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01009349 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03009350 break;
9351 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03009352 /* Case prevented by intel_choose_pipe_bpp_dither. */
9353 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03009354 }
9355
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009356 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03009357 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
9358
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009359 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03009360 val |= PIPECONF_INTERLACED_ILK;
9361 else
9362 val |= PIPECONF_PROGRESSIVE;
9363
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009364 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02009365 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02009366
Paulo Zanonic8203562012-09-12 10:06:29 -03009367 I915_WRITE(PIPECONF(pipe), val);
9368 POSTING_READ(PIPECONF(pipe));
9369}
9370
Daniel Vetter6ff93602013-04-19 11:24:36 +02009371static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03009372{
Chris Wilsonfac5e232016-07-04 11:34:36 +01009373 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03009374 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009375 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jani Nikula391bf042016-03-18 17:05:40 +02009376 u32 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03009377
Jani Nikula391bf042016-03-18 17:05:40 +02009378 if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03009379 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
9380
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009381 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03009382 val |= PIPECONF_INTERLACED_ILK;
9383 else
9384 val |= PIPECONF_PROGRESSIVE;
9385
Paulo Zanoni702e7a52012-10-23 18:29:59 -02009386 I915_WRITE(PIPECONF(cpu_transcoder), val);
9387 POSTING_READ(PIPECONF(cpu_transcoder));
Jani Nikula391bf042016-03-18 17:05:40 +02009388}
9389
Jani Nikula391bf042016-03-18 17:05:40 +02009390static void haswell_set_pipemisc(struct drm_crtc *crtc)
9391{
Chris Wilsonfac5e232016-07-04 11:34:36 +01009392 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Jani Nikula391bf042016-03-18 17:05:40 +02009393 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9394
9395 if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
9396 u32 val = 0;
Paulo Zanoni756f85c2013-11-02 21:07:38 -07009397
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009398 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07009399 case 18:
9400 val |= PIPEMISC_DITHER_6_BPC;
9401 break;
9402 case 24:
9403 val |= PIPEMISC_DITHER_8_BPC;
9404 break;
9405 case 30:
9406 val |= PIPEMISC_DITHER_10_BPC;
9407 break;
9408 case 36:
9409 val |= PIPEMISC_DITHER_12_BPC;
9410 break;
9411 default:
9412 /* Case prevented by pipe_config_set_bpp. */
9413 BUG();
9414 }
9415
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009416 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07009417 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
9418
Jani Nikula391bf042016-03-18 17:05:40 +02009419 I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07009420 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03009421}
9422
Paulo Zanonid4b19312012-11-29 11:29:32 -02009423int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
9424{
9425 /*
9426 * Account for spread spectrum to avoid
9427 * oversubscribing the link. Max center spread
9428 * is 2.5%; use 5% for safety's sake.
9429 */
9430 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02009431 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02009432}
9433
Daniel Vetter7429e9d2013-04-20 17:19:46 +02009434static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02009435{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02009436 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03009437}
9438
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02009439static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
9440 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03009441 struct dpll *reduced_clock)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03009442{
9443 struct drm_crtc *crtc = &intel_crtc->base;
9444 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009445 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02009446 u32 dpll, fp, fp2;
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03009447 int factor;
Jesse Barnes79e53942008-11-07 14:24:08 -08009448
Chris Wilsonc1858122010-12-03 21:35:48 +00009449 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07009450 factor = 21;
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03009451 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Eric Anholt8febb292011-03-30 13:01:07 -07009452 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02009453 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01009454 (HAS_PCH_IBX(dev_priv) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07009455 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009456 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07009457 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00009458
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02009459 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Chris Wilsonc1858122010-12-03 21:35:48 +00009460
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02009461 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
9462 fp |= FP_CB_TUNE;
9463
9464 if (reduced_clock) {
9465 fp2 = i9xx_dpll_compute_fp(reduced_clock);
9466
9467 if (reduced_clock->m < factor * reduced_clock->n)
9468 fp2 |= FP_CB_TUNE;
9469 } else {
9470 fp2 = fp;
9471 }
Daniel Vetter9a7c7892013-04-04 22:20:34 +02009472
Chris Wilson5eddb702010-09-11 13:48:45 +01009473 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08009474
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03009475 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
Eric Anholta07d6782011-03-30 13:01:08 -07009476 dpll |= DPLLB_MODE_LVDS;
9477 else
9478 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02009479
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009480 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02009481 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02009482
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03009483 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
9484 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
Daniel Vetter4a33e482013-07-06 12:52:05 +02009485 dpll |= DPLL_SDVO_HIGH_SPEED;
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03009486
Ville Syrjälä37a56502016-06-22 21:57:04 +03009487 if (intel_crtc_has_dp_encoder(crtc_state))
Daniel Vetter4a33e482013-07-06 12:52:05 +02009488 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08009489
Ville Syrjälä7d7f8632016-09-26 11:30:46 +03009490 /*
9491 * The high speed IO clock is only really required for
9492 * SDVO/HDMI/DP, but we also enable it for CRT to make it
9493 * possible to share the DPLL between CRT and HDMI. Enabling
9494 * the clock needlessly does no real harm, except use up a
9495 * bit of power potentially.
9496 *
9497 * We'll limit this to IVB with 3 pipes, since it has only two
9498 * DPLLs and so DPLL sharing is the only way to get three pipes
9499 * driving PCH ports at the same time. On SNB we could do this,
9500 * and potentially avoid enabling the second DPLL, but it's not
9501 * clear if it''s a win or loss power wise. No point in doing
9502 * this on ILK at all since it has a fixed DPLL<->pipe mapping.
9503 */
9504 if (INTEL_INFO(dev_priv)->num_pipes == 3 &&
9505 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
9506 dpll |= DPLL_SDVO_HIGH_SPEED;
9507
Eric Anholta07d6782011-03-30 13:01:08 -07009508 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009509 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07009510 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009511 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07009512
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009513 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07009514 case 5:
9515 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
9516 break;
9517 case 7:
9518 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
9519 break;
9520 case 10:
9521 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
9522 break;
9523 case 14:
9524 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
9525 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08009526 }
9527
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03009528 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
9529 intel_panel_use_ssc(dev_priv))
Kristian Høgsberg43565a02009-02-13 20:56:52 -05009530 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08009531 else
9532 dpll |= PLL_REF_INPUT_DREFCLK;
9533
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02009534 dpll |= DPLL_VCO_ENABLE;
9535
9536 crtc_state->dpll_hw_state.dpll = dpll;
9537 crtc_state->dpll_hw_state.fp0 = fp;
9538 crtc_state->dpll_hw_state.fp1 = fp2;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03009539}
9540
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009541static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
9542 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08009543{
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02009544 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009545 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03009546 struct dpll reduced_clock;
Ander Conselvan de Oliveira7ed9f892016-03-21 18:00:07 +02009547 bool has_reduced_clock = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02009548 struct intel_shared_dpll *pll;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03009549 const struct intel_limit *limit;
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02009550 int refclk = 120000;
Jesse Barnes79e53942008-11-07 14:24:08 -08009551
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03009552 memset(&crtc_state->dpll_hw_state, 0,
9553 sizeof(crtc_state->dpll_hw_state));
9554
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02009555 crtc->lowfreq_avail = false;
9556
9557 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
9558 if (!crtc_state->has_pch_encoder)
9559 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009560
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03009561 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02009562 if (intel_panel_use_ssc(dev_priv)) {
9563 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
9564 dev_priv->vbt.lvds_ssc_freq);
9565 refclk = dev_priv->vbt.lvds_ssc_freq;
9566 }
9567
9568 if (intel_is_dual_link_lvds(dev)) {
9569 if (refclk == 100000)
9570 limit = &intel_limits_ironlake_dual_lvds_100m;
9571 else
9572 limit = &intel_limits_ironlake_dual_lvds;
9573 } else {
9574 if (refclk == 100000)
9575 limit = &intel_limits_ironlake_single_lvds_100m;
9576 else
9577 limit = &intel_limits_ironlake_single_lvds;
9578 }
9579 } else {
9580 limit = &intel_limits_ironlake_dac;
9581 }
9582
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02009583 if (!crtc_state->clock_set &&
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02009584 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
9585 refclk, NULL, &crtc_state->dpll)) {
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02009586 DRM_ERROR("Couldn't find PLL settings for mode!\n");
9587 return -EINVAL;
Daniel Vetterf47709a2013-03-28 10:42:02 +01009588 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009589
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02009590 ironlake_compute_dpll(crtc, crtc_state,
9591 has_reduced_clock ? &reduced_clock : NULL);
Daniel Vetter66e985c2013-06-05 13:34:20 +02009592
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02009593 pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
9594 if (pll == NULL) {
9595 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
9596 pipe_name(crtc->pipe));
9597 return -EINVAL;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02009598 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009599
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03009600 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02009601 has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009602 crtc->lowfreq_avail = true;
Daniel Vettere2b78262013-06-07 23:10:03 +02009603
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009604 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009605}
9606
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009607static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
9608 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02009609{
9610 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009611 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009612 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02009613
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009614 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
9615 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
9616 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
9617 & ~TU_SIZE_MASK;
9618 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
9619 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
9620 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9621}
9622
9623static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
9624 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009625 struct intel_link_m_n *m_n,
9626 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009627{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00009628 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009629 enum pipe pipe = crtc->pipe;
9630
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00009631 if (INTEL_GEN(dev_priv) >= 5) {
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009632 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
9633 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
9634 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
9635 & ~TU_SIZE_MASK;
9636 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
9637 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
9638 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009639 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
9640 * gen < 8) and if DRRS is supported (to make sure the
9641 * registers are not unnecessarily read).
9642 */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00009643 if (m2_n2 && INTEL_GEN(dev_priv) < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009644 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009645 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
9646 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
9647 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
9648 & ~TU_SIZE_MASK;
9649 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
9650 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
9651 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9652 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009653 } else {
9654 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
9655 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
9656 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
9657 & ~TU_SIZE_MASK;
9658 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
9659 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
9660 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9661 }
9662}
9663
9664void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009665 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009666{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02009667 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009668 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
9669 else
9670 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009671 &pipe_config->dp_m_n,
9672 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009673}
9674
Daniel Vetter72419202013-04-04 13:28:53 +02009675static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009676 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02009677{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009678 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009679 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02009680}
9681
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009682static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009683 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009684{
9685 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009686 struct drm_i915_private *dev_priv = to_i915(dev);
Chandra Kondurua1b22782015-04-07 15:28:45 -07009687 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
9688 uint32_t ps_ctrl = 0;
9689 int id = -1;
9690 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009691
Chandra Kondurua1b22782015-04-07 15:28:45 -07009692 /* find scaler attached to this pipe */
9693 for (i = 0; i < crtc->num_scalers; i++) {
9694 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
9695 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
9696 id = i;
9697 pipe_config->pch_pfit.enabled = true;
9698 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9699 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9700 break;
9701 }
9702 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009703
Chandra Kondurua1b22782015-04-07 15:28:45 -07009704 scaler_state->scaler_id = id;
9705 if (id >= 0) {
9706 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9707 } else {
9708 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009709 }
9710}
9711
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009712static void
9713skylake_get_initial_plane_config(struct intel_crtc *crtc,
9714 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009715{
9716 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009717 struct drm_i915_private *dev_priv = to_i915(dev);
Damien Lespiau40f46282015-02-27 11:15:21 +00009718 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009719 int pipe = crtc->pipe;
9720 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009721 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009722 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009723 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009724
Damien Lespiaud9806c92015-01-21 14:07:19 +00009725 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009726 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009727 DRM_DEBUG_KMS("failed to alloc fb\n");
9728 return;
9729 }
9730
Damien Lespiau1b842c82015-01-21 13:50:54 +00009731 fb = &intel_fb->base;
9732
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009733 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00009734 if (!(val & PLANE_CTL_ENABLE))
9735 goto error;
9736
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009737 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9738 fourcc = skl_format_to_fourcc(pixel_format,
9739 val & PLANE_CTL_ORDER_RGBX,
9740 val & PLANE_CTL_ALPHA_MASK);
9741 fb->pixel_format = fourcc;
9742 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9743
Damien Lespiau40f46282015-02-27 11:15:21 +00009744 tiling = val & PLANE_CTL_TILED_MASK;
9745 switch (tiling) {
9746 case PLANE_CTL_TILED_LINEAR:
9747 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9748 break;
9749 case PLANE_CTL_TILED_X:
9750 plane_config->tiling = I915_TILING_X;
9751 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9752 break;
9753 case PLANE_CTL_TILED_Y:
9754 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9755 break;
9756 case PLANE_CTL_TILED_YF:
9757 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9758 break;
9759 default:
9760 MISSING_CASE(tiling);
9761 goto error;
9762 }
9763
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009764 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9765 plane_config->base = base;
9766
9767 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9768
9769 val = I915_READ(PLANE_SIZE(pipe, 0));
9770 fb->height = ((val >> 16) & 0xfff) + 1;
9771 fb->width = ((val >> 0) & 0x1fff) + 1;
9772
9773 val = I915_READ(PLANE_STRIDE(pipe, 0));
Ville Syrjälä7b49f942016-01-12 21:08:32 +02009774 stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
Damien Lespiau40f46282015-02-27 11:15:21 +00009775 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009776 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9777
9778 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009779 fb->pixel_format,
9780 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009781
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009782 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009783
9784 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9785 pipe_name(pipe), fb->width, fb->height,
9786 fb->bits_per_pixel, base, fb->pitches[0],
9787 plane_config->size);
9788
Damien Lespiau2d140302015-02-05 17:22:18 +00009789 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009790 return;
9791
9792error:
Matthew Auldd1a3a032016-08-23 16:00:44 +01009793 kfree(intel_fb);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009794}
9795
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009796static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009797 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009798{
9799 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009800 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009801 uint32_t tmp;
9802
9803 tmp = I915_READ(PF_CTL(crtc->pipe));
9804
9805 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009806 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009807 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9808 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02009809
9810 /* We currently do not free assignements of panel fitters on
9811 * ivb/hsw (since we don't use the higher upscaling modes which
9812 * differentiates them) so just WARN about this case for now. */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01009813 if (IS_GEN7(dev_priv)) {
Daniel Vettercb8b2a32013-06-01 17:16:23 +02009814 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9815 PF_PIPE_SEL_IVB(crtc->pipe));
9816 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009817 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009818}
9819
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009820static void
9821ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9822 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009823{
9824 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009825 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009826 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009827 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009828 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009829 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009830 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009831 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009832
Damien Lespiau42a7b082015-02-05 19:35:13 +00009833 val = I915_READ(DSPCNTR(pipe));
9834 if (!(val & DISPLAY_PLANE_ENABLE))
9835 return;
9836
Damien Lespiaud9806c92015-01-21 14:07:19 +00009837 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009838 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009839 DRM_DEBUG_KMS("failed to alloc fb\n");
9840 return;
9841 }
9842
Damien Lespiau1b842c82015-01-21 13:50:54 +00009843 fb = &intel_fb->base;
9844
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00009845 if (INTEL_GEN(dev_priv) >= 4) {
Daniel Vetter18c52472015-02-10 17:16:09 +00009846 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00009847 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00009848 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9849 }
9850 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009851
9852 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00009853 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009854 fb->pixel_format = fourcc;
9855 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009856
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009857 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01009858 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009859 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009860 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00009861 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009862 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009863 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009864 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009865 }
9866 plane_config->base = base;
9867
9868 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009869 fb->width = ((val >> 16) & 0xfff) + 1;
9870 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009871
9872 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009873 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009874
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009875 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009876 fb->pixel_format,
9877 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009878
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009879 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009880
Damien Lespiau2844a922015-01-20 12:51:48 +00009881 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9882 pipe_name(pipe), fb->width, fb->height,
9883 fb->bits_per_pixel, base, fb->pitches[0],
9884 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009885
Damien Lespiau2d140302015-02-05 17:22:18 +00009886 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009887}
9888
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009889static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009890 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009891{
9892 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009893 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak17290502016-02-12 18:55:11 +02009894 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009895 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02009896 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009897
Imre Deak17290502016-02-12 18:55:11 +02009898 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9899 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03009900 return false;
9901
Daniel Vettere143a212013-07-04 12:01:15 +02009902 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009903 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02009904
Imre Deak17290502016-02-12 18:55:11 +02009905 ret = false;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009906 tmp = I915_READ(PIPECONF(crtc->pipe));
9907 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02009908 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009909
Ville Syrjälä42571ae2013-09-06 23:29:00 +03009910 switch (tmp & PIPECONF_BPC_MASK) {
9911 case PIPECONF_6BPC:
9912 pipe_config->pipe_bpp = 18;
9913 break;
9914 case PIPECONF_8BPC:
9915 pipe_config->pipe_bpp = 24;
9916 break;
9917 case PIPECONF_10BPC:
9918 pipe_config->pipe_bpp = 30;
9919 break;
9920 case PIPECONF_12BPC:
9921 pipe_config->pipe_bpp = 36;
9922 break;
9923 default:
9924 break;
9925 }
9926
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02009927 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9928 pipe_config->limited_color_range = true;
9929
Daniel Vetterab9412b2013-05-03 11:49:46 +02009930 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02009931 struct intel_shared_dpll *pll;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009932 enum intel_dpll_id pll_id;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009933
Daniel Vetter88adfff2013-03-28 10:42:01 +01009934 pipe_config->has_pch_encoder = true;
9935
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009936 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9937 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9938 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02009939
9940 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009941
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009942 if (HAS_PCH_IBX(dev_priv)) {
Imre Deakd9a7bc62016-05-12 16:18:50 +03009943 /*
9944 * The pipe->pch transcoder and pch transcoder->pll
9945 * mapping is fixed.
9946 */
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009947 pll_id = (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009948 } else {
9949 tmp = I915_READ(PCH_DPLL_SEL);
9950 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009951 pll_id = DPLL_ID_PCH_PLL_B;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009952 else
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009953 pll_id= DPLL_ID_PCH_PLL_A;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009954 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02009955
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009956 pipe_config->shared_dpll =
9957 intel_get_shared_dpll_by_id(dev_priv, pll_id);
9958 pll = pipe_config->shared_dpll;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009959
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +02009960 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9961 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02009962
9963 tmp = pipe_config->dpll_hw_state.dpll;
9964 pipe_config->pixel_multiplier =
9965 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9966 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03009967
9968 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009969 } else {
9970 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009971 }
9972
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009973 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +02009974 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009975
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009976 ironlake_get_pfit_config(crtc, pipe_config);
9977
Imre Deak17290502016-02-12 18:55:11 +02009978 ret = true;
9979
9980out:
9981 intel_display_power_put(dev_priv, power_domain);
9982
9983 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009984}
9985
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009986static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9987{
Chris Wilson91c8a322016-07-05 10:40:23 +01009988 struct drm_device *dev = &dev_priv->drm;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009989 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009990
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009991 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05009992 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009993 pipe_name(crtc->pipe));
9994
Rob Clarke2c719b2014-12-15 13:56:32 -05009995 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9996 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
Ville Syrjälä01403de2015-09-18 20:03:33 +03009997 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9998 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
Imre Deak44cb7342016-08-10 14:07:29 +03009999 I915_STATE_WARN(I915_READ(PP_STATUS(0)) & PP_ON, "Panel power on\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050010000 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010001 "CPU PWM1 enabled\n");
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +010010002 if (IS_HASWELL(dev_priv))
Rob Clarke2c719b2014-12-15 13:56:32 -050010003 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -030010004 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050010005 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010006 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050010007 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010008 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050010009 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010010
Paulo Zanoni9926ada2014-04-01 19:39:47 -030010011 /*
10012 * In theory we can still leave IRQs enabled, as long as only the HPD
10013 * interrupts remain enabled. We used to check for that, but since it's
10014 * gen-specific and since we only disable LCPLL after we fully disable
10015 * the interrupts, the check below should be enough.
10016 */
Rob Clarke2c719b2014-12-15 13:56:32 -050010017 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010018}
10019
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -030010020static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
10021{
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +010010022 if (IS_HASWELL(dev_priv))
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -030010023 return I915_READ(D_COMP_HSW);
10024 else
10025 return I915_READ(D_COMP_BDW);
10026}
10027
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -030010028static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
10029{
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +010010030 if (IS_HASWELL(dev_priv)) {
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -030010031 mutex_lock(&dev_priv->rps.hw_lock);
10032 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
10033 val))
Chris Wilson79cf2192016-08-24 11:16:07 +010010034 DRM_DEBUG_KMS("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -030010035 mutex_unlock(&dev_priv->rps.hw_lock);
10036 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -030010037 I915_WRITE(D_COMP_BDW, val);
10038 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -030010039 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010040}
10041
10042/*
10043 * This function implements pieces of two sequences from BSpec:
10044 * - Sequence for display software to disable LCPLL
10045 * - Sequence for display software to allow package C8+
10046 * The steps implemented here are just the steps that actually touch the LCPLL
10047 * register. Callers should take care of disabling all the display engine
10048 * functions, doing the mode unset, fixing interrupts, etc.
10049 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -030010050static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
10051 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010052{
10053 uint32_t val;
10054
10055 assert_can_disable_lcpll(dev_priv);
10056
10057 val = I915_READ(LCPLL_CTL);
10058
10059 if (switch_to_fclk) {
10060 val |= LCPLL_CD_SOURCE_FCLK;
10061 I915_WRITE(LCPLL_CTL, val);
10062
Imre Deakf53dd632016-06-28 13:37:32 +030010063 if (wait_for_us(I915_READ(LCPLL_CTL) &
10064 LCPLL_CD_SOURCE_FCLK_DONE, 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010065 DRM_ERROR("Switching to FCLK failed\n");
10066
10067 val = I915_READ(LCPLL_CTL);
10068 }
10069
10070 val |= LCPLL_PLL_DISABLE;
10071 I915_WRITE(LCPLL_CTL, val);
10072 POSTING_READ(LCPLL_CTL);
10073
Chris Wilson24d84412016-06-30 15:33:07 +010010074 if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010075 DRM_ERROR("LCPLL still locked\n");
10076
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -030010077 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010078 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -030010079 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010080 ndelay(100);
10081
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -030010082 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
10083 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010084 DRM_ERROR("D_COMP RCOMP still in progress\n");
10085
10086 if (allow_power_down) {
10087 val = I915_READ(LCPLL_CTL);
10088 val |= LCPLL_POWER_DOWN_ALLOW;
10089 I915_WRITE(LCPLL_CTL, val);
10090 POSTING_READ(LCPLL_CTL);
10091 }
10092}
10093
10094/*
10095 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
10096 * source.
10097 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -030010098static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010099{
10100 uint32_t val;
10101
10102 val = I915_READ(LCPLL_CTL);
10103
10104 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
10105 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
10106 return;
10107
Paulo Zanonia8a8bd52014-03-07 20:08:05 -030010108 /*
10109 * Make sure we're not on PC8 state before disabling PC8, otherwise
10110 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -030010111 */
Mika Kuoppala59bad942015-01-16 11:34:40 +020010112 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -030010113
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010114 if (val & LCPLL_POWER_DOWN_ALLOW) {
10115 val &= ~LCPLL_POWER_DOWN_ALLOW;
10116 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +020010117 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010118 }
10119
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -030010120 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010121 val |= D_COMP_COMP_FORCE;
10122 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -030010123 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010124
10125 val = I915_READ(LCPLL_CTL);
10126 val &= ~LCPLL_PLL_DISABLE;
10127 I915_WRITE(LCPLL_CTL, val);
10128
Chris Wilson93220c02016-06-30 15:33:08 +010010129 if (intel_wait_for_register(dev_priv,
10130 LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
10131 5))
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010132 DRM_ERROR("LCPLL not locked yet\n");
10133
10134 if (val & LCPLL_CD_SOURCE_FCLK) {
10135 val = I915_READ(LCPLL_CTL);
10136 val &= ~LCPLL_CD_SOURCE_FCLK;
10137 I915_WRITE(LCPLL_CTL, val);
10138
Imre Deakf53dd632016-06-28 13:37:32 +030010139 if (wait_for_us((I915_READ(LCPLL_CTL) &
10140 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010141 DRM_ERROR("Switching back to LCPLL failed\n");
10142 }
Paulo Zanoni215733f2013-08-19 13:18:07 -030010143
Mika Kuoppala59bad942015-01-16 11:34:40 +020010144 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ville Syrjälä4c75b942016-10-31 22:37:12 +020010145 intel_update_cdclk(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010146}
10147
Paulo Zanoni765dab672014-03-07 20:08:18 -030010148/*
10149 * Package states C8 and deeper are really deep PC states that can only be
10150 * reached when all the devices on the system allow it, so even if the graphics
10151 * device allows PC8+, it doesn't mean the system will actually get to these
10152 * states. Our driver only allows PC8+ when going into runtime PM.
10153 *
10154 * The requirements for PC8+ are that all the outputs are disabled, the power
10155 * well is disabled and most interrupts are disabled, and these are also
10156 * requirements for runtime PM. When these conditions are met, we manually do
10157 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
10158 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
10159 * hang the machine.
10160 *
10161 * When we really reach PC8 or deeper states (not just when we allow it) we lose
10162 * the state of some registers, so when we come back from PC8+ we need to
10163 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
10164 * need to take care of the registers kept by RC6. Notice that this happens even
10165 * if we don't put the device in PCI D3 state (which is what currently happens
10166 * because of the runtime PM support).
10167 *
10168 * For more, read "Display Sequences for Package C8" on the hardware
10169 * documentation.
10170 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -030010171void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -030010172{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010173 uint32_t val;
10174
Paulo Zanonic67a4702013-08-19 13:18:09 -030010175 DRM_DEBUG_KMS("Enabling package C8+\n");
10176
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010010177 if (HAS_PCH_LPT_LP(dev_priv)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -030010178 val = I915_READ(SOUTH_DSPCLK_GATE_D);
10179 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
10180 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
10181 }
10182
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020010183 lpt_disable_clkout_dp(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -030010184 hsw_disable_lcpll(dev_priv, true, true);
10185}
10186
Paulo Zanonia14cb6f2014-03-07 20:08:17 -030010187void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -030010188{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010189 uint32_t val;
10190
Paulo Zanonic67a4702013-08-19 13:18:09 -030010191 DRM_DEBUG_KMS("Disabling package C8+\n");
10192
10193 hsw_restore_lcpll(dev_priv);
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020010194 lpt_init_pch_refclk(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -030010195
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010010196 if (HAS_PCH_LPT_LP(dev_priv)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -030010197 val = I915_READ(SOUTH_DSPCLK_GATE_D);
10198 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
10199 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
10200 }
Paulo Zanonic67a4702013-08-19 13:18:09 -030010201}
10202
Imre Deak324513c2016-06-13 16:44:36 +030010203static void bxt_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Vandana Kannanf8437dd12014-11-24 13:37:39 +053010204{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030010205 struct drm_device *dev = old_state->dev;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010010206 struct intel_atomic_state *old_intel_state =
10207 to_intel_atomic_state(old_state);
10208 unsigned int req_cdclk = old_intel_state->dev_cdclk;
Vandana Kannanf8437dd12014-11-24 13:37:39 +053010209
Imre Deak324513c2016-06-13 16:44:36 +030010210 bxt_set_cdclk(to_i915(dev), req_cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +053010211}
10212
Dhinakaran Pandiyanb30ce9e2016-11-01 11:47:59 -070010213static int bdw_adjust_min_pipe_pixel_rate(struct intel_crtc_state *crtc_state,
10214 int pixel_rate)
10215{
Dhinakaran Pandiyan9c754022016-11-02 13:13:21 -070010216 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
10217
Dhinakaran Pandiyanb30ce9e2016-11-01 11:47:59 -070010218 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
Dhinakaran Pandiyan9c754022016-11-02 13:13:21 -070010219 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
Dhinakaran Pandiyanb30ce9e2016-11-01 11:47:59 -070010220 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
10221
10222 /* BSpec says "Do not use DisplayPort with CDCLK less than
10223 * 432 MHz, audio enabled, port width x4, and link rate
10224 * HBR2 (5.4 GHz), or else there may be audio corruption or
10225 * screen corruption."
10226 */
10227 if (intel_crtc_has_dp_encoder(crtc_state) &&
10228 crtc_state->has_audio &&
10229 crtc_state->port_clock >= 540000 &&
10230 crtc_state->lane_count == 4)
10231 pixel_rate = max(432000, pixel_rate);
10232
10233 return pixel_rate;
10234}
10235
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010236/* compute the max rate for new configuration */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020010237static int ilk_max_pixel_rate(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010238{
Maarten Lankhorst565602d2015-12-10 12:33:57 +010010239 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Chris Wilsonfac5e232016-07-04 11:34:36 +010010240 struct drm_i915_private *dev_priv = to_i915(state->dev);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010010241 struct drm_crtc *crtc;
10242 struct drm_crtc_state *cstate;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020010243 struct intel_crtc_state *crtc_state;
Maarten Lankhorst565602d2015-12-10 12:33:57 +010010244 unsigned max_pixel_rate = 0, i;
10245 enum pipe pipe;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010246
Maarten Lankhorst565602d2015-12-10 12:33:57 +010010247 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
10248 sizeof(intel_state->min_pixclk));
10249
10250 for_each_crtc_in_state(state, crtc, cstate, i) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020010251 int pixel_rate;
10252
Maarten Lankhorst565602d2015-12-10 12:33:57 +010010253 crtc_state = to_intel_crtc_state(cstate);
10254 if (!crtc_state->base.enable) {
10255 intel_state->min_pixclk[i] = 0;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010256 continue;
Maarten Lankhorst565602d2015-12-10 12:33:57 +010010257 }
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010258
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020010259 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010260
Dhinakaran Pandiyan9c754022016-11-02 13:13:21 -070010261 if (IS_BROADWELL(dev_priv) || IS_GEN9(dev_priv))
Dhinakaran Pandiyanb30ce9e2016-11-01 11:47:59 -070010262 pixel_rate = bdw_adjust_min_pipe_pixel_rate(crtc_state,
10263 pixel_rate);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010264
Maarten Lankhorst565602d2015-12-10 12:33:57 +010010265 intel_state->min_pixclk[i] = pixel_rate;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010266 }
10267
Maarten Lankhorst565602d2015-12-10 12:33:57 +010010268 for_each_pipe(dev_priv, pipe)
10269 max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
10270
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010271 return max_pixel_rate;
10272}
10273
10274static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
10275{
Chris Wilsonfac5e232016-07-04 11:34:36 +010010276 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010277 uint32_t val, data;
10278 int ret;
10279
10280 if (WARN((I915_READ(LCPLL_CTL) &
10281 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
10282 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
10283 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
10284 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
10285 "trying to change cdclk frequency with cdclk not enabled\n"))
10286 return;
10287
10288 mutex_lock(&dev_priv->rps.hw_lock);
10289 ret = sandybridge_pcode_write(dev_priv,
10290 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
10291 mutex_unlock(&dev_priv->rps.hw_lock);
10292 if (ret) {
10293 DRM_ERROR("failed to inform pcode about cdclk change\n");
10294 return;
10295 }
10296
10297 val = I915_READ(LCPLL_CTL);
10298 val |= LCPLL_CD_SOURCE_FCLK;
10299 I915_WRITE(LCPLL_CTL, val);
10300
Tvrtko Ursulin5ba00172016-03-03 14:36:45 +000010301 if (wait_for_us(I915_READ(LCPLL_CTL) &
10302 LCPLL_CD_SOURCE_FCLK_DONE, 1))
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010303 DRM_ERROR("Switching to FCLK failed\n");
10304
10305 val = I915_READ(LCPLL_CTL);
10306 val &= ~LCPLL_CLK_FREQ_MASK;
10307
10308 switch (cdclk) {
10309 case 450000:
10310 val |= LCPLL_CLK_FREQ_450;
10311 data = 0;
10312 break;
10313 case 540000:
10314 val |= LCPLL_CLK_FREQ_54O_BDW;
10315 data = 1;
10316 break;
10317 case 337500:
10318 val |= LCPLL_CLK_FREQ_337_5_BDW;
10319 data = 2;
10320 break;
10321 case 675000:
10322 val |= LCPLL_CLK_FREQ_675_BDW;
10323 data = 3;
10324 break;
10325 default:
10326 WARN(1, "invalid cdclk frequency\n");
10327 return;
10328 }
10329
10330 I915_WRITE(LCPLL_CTL, val);
10331
10332 val = I915_READ(LCPLL_CTL);
10333 val &= ~LCPLL_CD_SOURCE_FCLK;
10334 I915_WRITE(LCPLL_CTL, val);
10335
Tvrtko Ursulin5ba00172016-03-03 14:36:45 +000010336 if (wait_for_us((I915_READ(LCPLL_CTL) &
10337 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010338 DRM_ERROR("Switching back to LCPLL failed\n");
10339
10340 mutex_lock(&dev_priv->rps.hw_lock);
10341 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
10342 mutex_unlock(&dev_priv->rps.hw_lock);
10343
Ville Syrjälä7f1052a2016-04-26 19:46:32 +030010344 I915_WRITE(CDCLK_FREQ, DIV_ROUND_CLOSEST(cdclk, 1000) - 1);
10345
Ville Syrjälä4c75b942016-10-31 22:37:12 +020010346 intel_update_cdclk(dev_priv);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010347
10348 WARN(cdclk != dev_priv->cdclk_freq,
10349 "cdclk requested %d kHz but got %d kHz\n",
10350 cdclk, dev_priv->cdclk_freq);
10351}
10352
Ville Syrjälä587c7912016-05-11 22:44:41 +030010353static int broadwell_calc_cdclk(int max_pixclk)
10354{
10355 if (max_pixclk > 540000)
10356 return 675000;
10357 else if (max_pixclk > 450000)
10358 return 540000;
10359 else if (max_pixclk > 337500)
10360 return 450000;
10361 else
10362 return 337500;
10363}
10364
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020010365static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010366{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020010367 struct drm_i915_private *dev_priv = to_i915(state->dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010010368 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020010369 int max_pixclk = ilk_max_pixel_rate(state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010370 int cdclk;
10371
10372 /*
10373 * FIXME should also account for plane ratio
10374 * once 64bpp pixel formats are supported.
10375 */
Ville Syrjälä587c7912016-05-11 22:44:41 +030010376 cdclk = broadwell_calc_cdclk(max_pixclk);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010377
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010378 if (cdclk > dev_priv->max_cdclk_freq) {
Maarten Lankhorst63ba5342015-11-24 11:29:03 +010010379 DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
10380 cdclk, dev_priv->max_cdclk_freq);
10381 return -EINVAL;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010382 }
10383
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010010384 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
10385 if (!intel_state->active_crtcs)
Ville Syrjälä587c7912016-05-11 22:44:41 +030010386 intel_state->dev_cdclk = broadwell_calc_cdclk(0);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010387
10388 return 0;
10389}
10390
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020010391static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010392{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020010393 struct drm_device *dev = old_state->dev;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010010394 struct intel_atomic_state *old_intel_state =
10395 to_intel_atomic_state(old_state);
10396 unsigned req_cdclk = old_intel_state->dev_cdclk;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010397
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020010398 broadwell_set_cdclk(dev, req_cdclk);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010399}
10400
Clint Taylorc89e39f2016-05-13 23:41:21 +030010401static int skl_modeset_calc_cdclk(struct drm_atomic_state *state)
10402{
10403 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
10404 struct drm_i915_private *dev_priv = to_i915(state->dev);
10405 const int max_pixclk = ilk_max_pixel_rate(state);
Ville Syrjäläa8ca4932016-05-13 23:41:23 +030010406 int vco = intel_state->cdclk_pll_vco;
Clint Taylorc89e39f2016-05-13 23:41:21 +030010407 int cdclk;
10408
10409 /*
10410 * FIXME should also account for plane ratio
10411 * once 64bpp pixel formats are supported.
10412 */
Ville Syrjäläa8ca4932016-05-13 23:41:23 +030010413 cdclk = skl_calc_cdclk(max_pixclk, vco);
Clint Taylorc89e39f2016-05-13 23:41:21 +030010414
10415 /*
10416 * FIXME move the cdclk caclulation to
10417 * compute_config() so we can fail gracegully.
10418 */
10419 if (cdclk > dev_priv->max_cdclk_freq) {
10420 DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
10421 cdclk, dev_priv->max_cdclk_freq);
10422 cdclk = dev_priv->max_cdclk_freq;
10423 }
10424
10425 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
10426 if (!intel_state->active_crtcs)
Ville Syrjäläa8ca4932016-05-13 23:41:23 +030010427 intel_state->dev_cdclk = skl_calc_cdclk(0, vco);
Clint Taylorc89e39f2016-05-13 23:41:21 +030010428
10429 return 0;
10430}
10431
10432static void skl_modeset_commit_cdclk(struct drm_atomic_state *old_state)
10433{
Ville Syrjälä1cd593e2016-05-13 23:41:26 +030010434 struct drm_i915_private *dev_priv = to_i915(old_state->dev);
10435 struct intel_atomic_state *intel_state = to_intel_atomic_state(old_state);
10436 unsigned int req_cdclk = intel_state->dev_cdclk;
10437 unsigned int req_vco = intel_state->cdclk_pll_vco;
Clint Taylorc89e39f2016-05-13 23:41:21 +030010438
Ville Syrjälä1cd593e2016-05-13 23:41:26 +030010439 skl_set_cdclk(dev_priv, req_cdclk, req_vco);
Clint Taylorc89e39f2016-05-13 23:41:21 +030010440}
10441
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +020010442static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
10443 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030010444{
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +030010445 if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
Mika Kaholaaf3997b2016-02-05 13:29:28 +020010446 if (!intel_ddi_pll_select(crtc, crtc_state))
10447 return -EINVAL;
10448 }
Daniel Vetter716c2e52014-06-25 22:02:02 +030010449
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +030010450 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +020010451
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +020010452 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010453}
10454
Satheeshakrishna M3760b592014-08-22 09:49:11 +053010455static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
10456 enum port port,
10457 struct intel_crtc_state *pipe_config)
10458{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010459 enum intel_dpll_id id;
10460
Satheeshakrishna M3760b592014-08-22 09:49:11 +053010461 switch (port) {
10462 case PORT_A:
Imre Deak08250c42016-03-14 19:55:34 +020010463 id = DPLL_ID_SKL_DPLL0;
Satheeshakrishna M3760b592014-08-22 09:49:11 +053010464 break;
10465 case PORT_B:
Imre Deak08250c42016-03-14 19:55:34 +020010466 id = DPLL_ID_SKL_DPLL1;
Satheeshakrishna M3760b592014-08-22 09:49:11 +053010467 break;
10468 case PORT_C:
Imre Deak08250c42016-03-14 19:55:34 +020010469 id = DPLL_ID_SKL_DPLL2;
Satheeshakrishna M3760b592014-08-22 09:49:11 +053010470 break;
10471 default:
10472 DRM_ERROR("Incorrect port type\n");
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010473 return;
Satheeshakrishna M3760b592014-08-22 09:49:11 +053010474 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010475
10476 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Satheeshakrishna M3760b592014-08-22 09:49:11 +053010477}
10478
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +000010479static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
10480 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010481 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +000010482{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010483 enum intel_dpll_id id;
Ander Conselvan de Oliveiraa3c988e2016-03-08 17:46:27 +020010484 u32 temp;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +000010485
10486 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
Ander Conselvan de Oliveirac8560522016-09-01 15:08:07 -070010487 id = temp >> (port * 3 + 1);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +000010488
Ander Conselvan de Oliveirac8560522016-09-01 15:08:07 -070010489 if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL3))
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010490 return;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010491
10492 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +000010493}
10494
Damien Lespiau7d2c8172014-07-29 18:06:18 +010010495static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
10496 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010497 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +010010498{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010499 enum intel_dpll_id id;
Ander Conselvan de Oliveirac8560522016-09-01 15:08:07 -070010500 uint32_t ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010501
Ander Conselvan de Oliveirac8560522016-09-01 15:08:07 -070010502 switch (ddi_pll_sel) {
Damien Lespiau7d2c8172014-07-29 18:06:18 +010010503 case PORT_CLK_SEL_WRPLL1:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010504 id = DPLL_ID_WRPLL1;
Damien Lespiau7d2c8172014-07-29 18:06:18 +010010505 break;
10506 case PORT_CLK_SEL_WRPLL2:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010507 id = DPLL_ID_WRPLL2;
Damien Lespiau7d2c8172014-07-29 18:06:18 +010010508 break;
Maarten Lankhorst00490c22015-11-16 14:42:12 +010010509 case PORT_CLK_SEL_SPLL:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010510 id = DPLL_ID_SPLL;
Ville Syrjälä79bd23d2015-12-01 23:32:07 +020010511 break;
Ander Conselvan de Oliveira9d16da62016-03-08 17:46:26 +020010512 case PORT_CLK_SEL_LCPLL_810:
10513 id = DPLL_ID_LCPLL_810;
10514 break;
10515 case PORT_CLK_SEL_LCPLL_1350:
10516 id = DPLL_ID_LCPLL_1350;
10517 break;
10518 case PORT_CLK_SEL_LCPLL_2700:
10519 id = DPLL_ID_LCPLL_2700;
10520 break;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010521 default:
Ander Conselvan de Oliveirac8560522016-09-01 15:08:07 -070010522 MISSING_CASE(ddi_pll_sel);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010523 /* fall through */
10524 case PORT_CLK_SEL_NONE:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010525 return;
Damien Lespiau7d2c8172014-07-29 18:06:18 +010010526 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010527
10528 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Damien Lespiau7d2c8172014-07-29 18:06:18 +010010529}
10530
Jani Nikulacf304292016-03-18 17:05:41 +020010531static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
10532 struct intel_crtc_state *pipe_config,
10533 unsigned long *power_domain_mask)
10534{
10535 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010536 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikulacf304292016-03-18 17:05:41 +020010537 enum intel_display_power_domain power_domain;
10538 u32 tmp;
10539
Imre Deakd9a7bc62016-05-12 16:18:50 +030010540 /*
10541 * The pipe->transcoder mapping is fixed with the exception of the eDP
10542 * transcoder handled below.
10543 */
Jani Nikulacf304292016-03-18 17:05:41 +020010544 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
10545
10546 /*
10547 * XXX: Do intel_display_power_get_if_enabled before reading this (for
10548 * consistency and less surprising code; it's in always on power).
10549 */
10550 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
10551 if (tmp & TRANS_DDI_FUNC_ENABLE) {
10552 enum pipe trans_edp_pipe;
10553 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
10554 default:
10555 WARN(1, "unknown pipe linked to edp transcoder\n");
10556 case TRANS_DDI_EDP_INPUT_A_ONOFF:
10557 case TRANS_DDI_EDP_INPUT_A_ON:
10558 trans_edp_pipe = PIPE_A;
10559 break;
10560 case TRANS_DDI_EDP_INPUT_B_ONOFF:
10561 trans_edp_pipe = PIPE_B;
10562 break;
10563 case TRANS_DDI_EDP_INPUT_C_ONOFF:
10564 trans_edp_pipe = PIPE_C;
10565 break;
10566 }
10567
10568 if (trans_edp_pipe == crtc->pipe)
10569 pipe_config->cpu_transcoder = TRANSCODER_EDP;
10570 }
10571
10572 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
10573 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
10574 return false;
10575 *power_domain_mask |= BIT(power_domain);
10576
10577 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
10578
10579 return tmp & PIPECONF_ENABLE;
10580}
10581
Jani Nikula4d1de972016-03-18 17:05:42 +020010582static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
10583 struct intel_crtc_state *pipe_config,
10584 unsigned long *power_domain_mask)
10585{
10586 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010587 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikula4d1de972016-03-18 17:05:42 +020010588 enum intel_display_power_domain power_domain;
10589 enum port port;
10590 enum transcoder cpu_transcoder;
10591 u32 tmp;
10592
Jani Nikula4d1de972016-03-18 17:05:42 +020010593 for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
10594 if (port == PORT_A)
10595 cpu_transcoder = TRANSCODER_DSI_A;
10596 else
10597 cpu_transcoder = TRANSCODER_DSI_C;
10598
10599 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
10600 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
10601 continue;
10602 *power_domain_mask |= BIT(power_domain);
10603
Imre Deakdb18b6a2016-03-24 12:41:40 +020010604 /*
10605 * The PLL needs to be enabled with a valid divider
10606 * configuration, otherwise accessing DSI registers will hang
10607 * the machine. See BSpec North Display Engine
10608 * registers/MIPI[BXT]. We can break out here early, since we
10609 * need the same DSI PLL to be enabled for both DSI ports.
10610 */
10611 if (!intel_dsi_pll_is_enabled(dev_priv))
10612 break;
10613
Jani Nikula4d1de972016-03-18 17:05:42 +020010614 /* XXX: this works for video mode only */
10615 tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
10616 if (!(tmp & DPI_ENABLE))
10617 continue;
10618
10619 tmp = I915_READ(MIPI_CTRL(port));
10620 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
10621 continue;
10622
10623 pipe_config->cpu_transcoder = cpu_transcoder;
Jani Nikula4d1de972016-03-18 17:05:42 +020010624 break;
10625 }
10626
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +030010627 return transcoder_is_dsi(pipe_config->cpu_transcoder);
Jani Nikula4d1de972016-03-18 17:05:42 +020010628}
10629
Daniel Vetter26804af2014-06-25 22:01:55 +030010630static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010631 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +030010632{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000010633 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030010634 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +030010635 enum port port;
10636 uint32_t tmp;
10637
10638 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
10639
10640 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
10641
Tvrtko Ursulin08537232016-10-13 11:03:02 +010010642 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +000010643 skylake_get_ddi_pll(dev_priv, port, pipe_config);
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +010010644 else if (IS_BROXTON(dev_priv))
Satheeshakrishna M3760b592014-08-22 09:49:11 +053010645 bxt_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +000010646 else
10647 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +030010648
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010649 pll = pipe_config->shared_dpll;
10650 if (pll) {
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020010651 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
10652 &pipe_config->dpll_hw_state));
Daniel Vetterd452c5b2014-07-04 11:27:39 -030010653 }
10654
Daniel Vetter26804af2014-06-25 22:01:55 +030010655 /*
10656 * Haswell has only FDI/PCH transcoder A. It is which is connected to
10657 * DDI E. So just check whether this pipe is wired to DDI E and whether
10658 * the PCH transcoder is on.
10659 */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000010660 if (INTEL_GEN(dev_priv) < 9 &&
Damien Lespiauca370452013-12-03 13:56:24 +000010661 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +030010662 pipe_config->has_pch_encoder = true;
10663
10664 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
10665 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
10666 FDI_DP_PORT_WIDTH_SHIFT) + 1;
10667
10668 ironlake_get_fdi_m_n_config(crtc, pipe_config);
10669 }
10670}
10671
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010672static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010673 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010674{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000010675 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Imre Deak17290502016-02-12 18:55:11 +020010676 enum intel_display_power_domain power_domain;
10677 unsigned long power_domain_mask;
Jani Nikulacf304292016-03-18 17:05:41 +020010678 bool active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010679
Imre Deak17290502016-02-12 18:55:11 +020010680 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
10681 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +020010682 return false;
Imre Deak17290502016-02-12 18:55:11 +020010683 power_domain_mask = BIT(power_domain);
10684
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010685 pipe_config->shared_dpll = NULL;
Daniel Vetterc0d43d62013-06-07 23:11:08 +020010686
Jani Nikulacf304292016-03-18 17:05:41 +020010687 active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
Daniel Vettereccb1402013-05-22 00:50:22 +020010688
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +030010689 if (IS_BROXTON(dev_priv) &&
10690 bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
10691 WARN_ON(active);
10692 active = true;
Jani Nikula4d1de972016-03-18 17:05:42 +020010693 }
10694
Jani Nikulacf304292016-03-18 17:05:41 +020010695 if (!active)
Imre Deak17290502016-02-12 18:55:11 +020010696 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010697
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +030010698 if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
Jani Nikula4d1de972016-03-18 17:05:42 +020010699 haswell_get_ddi_port_state(crtc, pipe_config);
10700 intel_get_pipe_timings(crtc, pipe_config);
10701 }
Daniel Vetter627eb5a2013-04-29 19:33:42 +020010702
Jani Nikulabc58be62016-03-18 17:05:39 +020010703 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010704
Lionel Landwerlin05dc6982016-03-16 10:57:15 +000010705 pipe_config->gamma_mode =
10706 I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
10707
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000010708 if (INTEL_GEN(dev_priv) >= 9) {
Ville Syrjälä65edccc2016-10-31 22:37:01 +020010709 skl_init_scalers(dev_priv, crtc, pipe_config);
Chandra Kondurua1b22782015-04-07 15:28:45 -070010710
Chandra Konduruaf99ceda2015-05-11 14:35:47 -070010711 pipe_config->scaler_state.scaler_id = -1;
10712 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
10713 }
10714
Imre Deak17290502016-02-12 18:55:11 +020010715 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
10716 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
10717 power_domain_mask |= BIT(power_domain);
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000010718 if (INTEL_GEN(dev_priv) >= 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +000010719 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -080010720 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -070010721 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesbd2e2442014-11-13 17:51:47 +000010722 }
Daniel Vetter88adfff2013-03-28 10:42:01 +010010723
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +010010724 if (IS_HASWELL(dev_priv))
Jesse Barnese59150d2014-01-07 13:30:45 -080010725 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
10726 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030010727
Jani Nikula4d1de972016-03-18 17:05:42 +020010728 if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
10729 !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
Clint Taylorebb69c92014-09-30 10:30:22 -070010730 pipe_config->pixel_multiplier =
10731 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
10732 } else {
10733 pipe_config->pixel_multiplier = 1;
10734 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020010735
Imre Deak17290502016-02-12 18:55:11 +020010736out:
10737 for_each_power_domain(power_domain, power_domain_mask)
10738 intel_display_power_put(dev_priv, power_domain);
10739
Jani Nikulacf304292016-03-18 17:05:41 +020010740 return active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010741}
10742
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010743static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
10744 const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +010010745{
10746 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010747 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson560b85b2010-08-07 11:01:38 +010010748 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +030010749 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +010010750
Ville Syrjälä936e71e2016-07-26 19:06:59 +030010751 if (plane_state && plane_state->base.visible) {
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010752 unsigned int width = plane_state->base.crtc_w;
10753 unsigned int height = plane_state->base.crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +030010754 unsigned int stride = roundup_pow_of_two(width) * 4;
10755
10756 switch (stride) {
10757 default:
10758 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
10759 width, stride);
10760 stride = 256;
10761 /* fallthrough */
10762 case 256:
10763 case 512:
10764 case 1024:
10765 case 2048:
10766 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010767 }
10768
Ville Syrjälädc41c152014-08-13 11:57:05 +030010769 cntl |= CURSOR_ENABLE |
10770 CURSOR_GAMMA_ENABLE |
10771 CURSOR_FORMAT_ARGB |
10772 CURSOR_STRIDE(stride);
10773
10774 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010775 }
Chris Wilson560b85b2010-08-07 11:01:38 +010010776
Ville Syrjälädc41c152014-08-13 11:57:05 +030010777 if (intel_crtc->cursor_cntl != 0 &&
10778 (intel_crtc->cursor_base != base ||
10779 intel_crtc->cursor_size != size ||
10780 intel_crtc->cursor_cntl != cntl)) {
10781 /* On these chipsets we can only modify the base/size/stride
10782 * whilst the cursor is disabled.
10783 */
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010784 I915_WRITE(CURCNTR(PIPE_A), 0);
10785 POSTING_READ(CURCNTR(PIPE_A));
Ville Syrjälädc41c152014-08-13 11:57:05 +030010786 intel_crtc->cursor_cntl = 0;
10787 }
10788
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010789 if (intel_crtc->cursor_base != base) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010790 I915_WRITE(CURBASE(PIPE_A), base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010791 intel_crtc->cursor_base = base;
10792 }
Ville Syrjälädc41c152014-08-13 11:57:05 +030010793
10794 if (intel_crtc->cursor_size != size) {
10795 I915_WRITE(CURSIZE, size);
10796 intel_crtc->cursor_size = size;
10797 }
10798
Chris Wilson4b0e3332014-05-30 16:35:26 +030010799 if (intel_crtc->cursor_cntl != cntl) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010800 I915_WRITE(CURCNTR(PIPE_A), cntl);
10801 POSTING_READ(CURCNTR(PIPE_A));
Chris Wilson4b0e3332014-05-30 16:35:26 +030010802 intel_crtc->cursor_cntl = cntl;
10803 }
Chris Wilson560b85b2010-08-07 11:01:38 +010010804}
10805
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010806static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
10807 const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +010010808{
10809 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010810 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson560b85b2010-08-07 11:01:38 +010010811 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10812 int pipe = intel_crtc->pipe;
Ville Syrjälä663f3122015-12-14 13:16:48 +020010813 uint32_t cntl = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +010010814
Ville Syrjälä936e71e2016-07-26 19:06:59 +030010815 if (plane_state && plane_state->base.visible) {
Chris Wilson4b0e3332014-05-30 16:35:26 +030010816 cntl = MCURSOR_GAMMA_ENABLE;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010817 switch (plane_state->base.crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010818 case 64:
10819 cntl |= CURSOR_MODE_64_ARGB_AX;
10820 break;
10821 case 128:
10822 cntl |= CURSOR_MODE_128_ARGB_AX;
10823 break;
10824 case 256:
10825 cntl |= CURSOR_MODE_256_ARGB_AX;
10826 break;
10827 default:
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010828 MISSING_CASE(plane_state->base.crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010829 return;
Chris Wilson560b85b2010-08-07 11:01:38 +010010830 }
Chris Wilson4b0e3332014-05-30 16:35:26 +030010831 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010832
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010010833 if (HAS_DDI(dev_priv))
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010834 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010835
Ville Syrjäläf22aa142016-11-14 18:53:58 +020010836 if (plane_state->base.rotation & DRM_ROTATE_180)
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010837 cntl |= CURSOR_ROTATE_180;
10838 }
Ville Syrjälä4398ad42014-10-23 07:41:34 -070010839
Chris Wilson4b0e3332014-05-30 16:35:26 +030010840 if (intel_crtc->cursor_cntl != cntl) {
10841 I915_WRITE(CURCNTR(pipe), cntl);
10842 POSTING_READ(CURCNTR(pipe));
10843 intel_crtc->cursor_cntl = cntl;
10844 }
10845
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010846 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010847 I915_WRITE(CURBASE(pipe), base);
10848 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010849
10850 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010851}
10852
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010853/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +010010854static void intel_crtc_update_cursor(struct drm_crtc *crtc,
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010855 const struct intel_plane_state *plane_state)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010856{
10857 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010858 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010859 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10860 int pipe = intel_crtc->pipe;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010861 u32 base = intel_crtc->cursor_addr;
10862 u32 pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010863
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010864 if (plane_state) {
10865 int x = plane_state->base.crtc_x;
10866 int y = plane_state->base.crtc_y;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010867
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010868 if (x < 0) {
10869 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10870 x = -x;
10871 }
10872 pos |= x << CURSOR_X_SHIFT;
10873
10874 if (y < 0) {
10875 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10876 y = -y;
10877 }
10878 pos |= y << CURSOR_Y_SHIFT;
10879
10880 /* ILK+ do this automagically */
Tvrtko Ursulin49cff962016-10-13 11:02:54 +010010881 if (HAS_GMCH_DISPLAY(dev_priv) &&
Ville Syrjäläf22aa142016-11-14 18:53:58 +020010882 plane_state->base.rotation & DRM_ROTATE_180) {
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010883 base += (plane_state->base.crtc_h *
10884 plane_state->base.crtc_w - 1) * 4;
10885 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010886 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010887
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010888 I915_WRITE(CURPOS(pipe), pos);
10889
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +010010890 if (IS_845G(dev_priv) || IS_I865G(dev_priv))
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010891 i845_update_cursor(crtc, base, plane_state);
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010892 else
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010893 i9xx_update_cursor(crtc, base, plane_state);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010894}
10895
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +010010896static bool cursor_size_ok(struct drm_i915_private *dev_priv,
Ville Syrjälädc41c152014-08-13 11:57:05 +030010897 uint32_t width, uint32_t height)
10898{
10899 if (width == 0 || height == 0)
10900 return false;
10901
10902 /*
10903 * 845g/865g are special in that they are only limited by
10904 * the width of their cursors, the height is arbitrary up to
10905 * the precision of the register. Everything else requires
10906 * square cursors, limited to a few power-of-two sizes.
10907 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +010010908 if (IS_845G(dev_priv) || IS_I865G(dev_priv)) {
Ville Syrjälädc41c152014-08-13 11:57:05 +030010909 if ((width & 63) != 0)
10910 return false;
10911
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +010010912 if (width > (IS_845G(dev_priv) ? 64 : 512))
Ville Syrjälädc41c152014-08-13 11:57:05 +030010913 return false;
10914
10915 if (height > 1023)
10916 return false;
10917 } else {
10918 switch (width | height) {
10919 case 256:
10920 case 128:
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +010010921 if (IS_GEN2(dev_priv))
Ville Syrjälädc41c152014-08-13 11:57:05 +030010922 return false;
10923 case 64:
10924 break;
10925 default:
10926 return false;
10927 }
10928 }
10929
10930 return true;
10931}
10932
Jesse Barnes79e53942008-11-07 14:24:08 -080010933/* VESA 640x480x72Hz mode to set on the pipe */
10934static struct drm_display_mode load_detect_mode = {
10935 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10936 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10937};
10938
Daniel Vettera8bb6812014-02-10 18:00:39 +010010939struct drm_framebuffer *
10940__intel_framebuffer_create(struct drm_device *dev,
10941 struct drm_mode_fb_cmd2 *mode_cmd,
10942 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +010010943{
10944 struct intel_framebuffer *intel_fb;
10945 int ret;
10946
10947 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010948 if (!intel_fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010949 return ERR_PTR(-ENOMEM);
Chris Wilsond2dff872011-04-19 08:36:26 +010010950
10951 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010952 if (ret)
10953 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +010010954
10955 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010956
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010957err:
10958 kfree(intel_fb);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010959 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +010010960}
10961
Daniel Vetterb5ea6422014-03-02 21:18:00 +010010962static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +010010963intel_framebuffer_create(struct drm_device *dev,
10964 struct drm_mode_fb_cmd2 *mode_cmd,
10965 struct drm_i915_gem_object *obj)
10966{
10967 struct drm_framebuffer *fb;
10968 int ret;
10969
10970 ret = i915_mutex_lock_interruptible(dev);
10971 if (ret)
10972 return ERR_PTR(ret);
10973 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10974 mutex_unlock(&dev->struct_mutex);
10975
10976 return fb;
10977}
10978
Chris Wilsond2dff872011-04-19 08:36:26 +010010979static u32
10980intel_framebuffer_pitch_for_width(int width, int bpp)
10981{
10982 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10983 return ALIGN(pitch, 64);
10984}
10985
10986static u32
10987intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10988{
10989 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +020010990 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +010010991}
10992
10993static struct drm_framebuffer *
10994intel_framebuffer_create_for_mode(struct drm_device *dev,
10995 struct drm_display_mode *mode,
10996 int depth, int bpp)
10997{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010998 struct drm_framebuffer *fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010999 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +000011000 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +010011001
Dave Gordond37cd8a2016-04-22 19:14:32 +010011002 obj = i915_gem_object_create(dev,
Chris Wilsond2dff872011-04-19 08:36:26 +010011003 intel_framebuffer_size_for_mode(mode, bpp));
Chris Wilsonfe3db792016-04-25 13:32:13 +010011004 if (IS_ERR(obj))
11005 return ERR_CAST(obj);
Chris Wilsond2dff872011-04-19 08:36:26 +010011006
11007 mode_cmd.width = mode->hdisplay;
11008 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -080011009 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
11010 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +000011011 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +010011012
Lukas Wunnerdcb13942015-07-04 11:50:58 +020011013 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
11014 if (IS_ERR(fb))
Chris Wilsonf0cd5182016-10-28 13:58:43 +010011015 i915_gem_object_put(obj);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020011016
11017 return fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010011018}
11019
11020static struct drm_framebuffer *
11021mode_fits_in_fbdev(struct drm_device *dev,
11022 struct drm_display_mode *mode)
11023{
Daniel Vetter06957262015-08-10 13:34:08 +020011024#ifdef CONFIG_DRM_FBDEV_EMULATION
Chris Wilsonfac5e232016-07-04 11:34:36 +010011025 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsond2dff872011-04-19 08:36:26 +010011026 struct drm_i915_gem_object *obj;
11027 struct drm_framebuffer *fb;
11028
Daniel Vetter4c0e5522014-02-14 16:35:54 +010011029 if (!dev_priv->fbdev)
11030 return NULL;
11031
11032 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010011033 return NULL;
11034
Jesse Barnes8bcd4552014-02-07 12:10:38 -080011035 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +010011036 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +010011037
Jesse Barnes8bcd4552014-02-07 12:10:38 -080011038 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +020011039 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
11040 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +010011041 return NULL;
11042
Ville Syrjälä01f2c772011-12-20 00:06:49 +020011043 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +010011044 return NULL;
11045
Maarten Lankhorstedde3612016-02-17 09:18:35 +010011046 drm_framebuffer_reference(fb);
Chris Wilsond2dff872011-04-19 08:36:26 +010011047 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +020011048#else
11049 return NULL;
11050#endif
Chris Wilsond2dff872011-04-19 08:36:26 +010011051}
11052
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030011053static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
11054 struct drm_crtc *crtc,
11055 struct drm_display_mode *mode,
11056 struct drm_framebuffer *fb,
11057 int x, int y)
11058{
11059 struct drm_plane_state *plane_state;
11060 int hdisplay, vdisplay;
11061 int ret;
11062
11063 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
11064 if (IS_ERR(plane_state))
11065 return PTR_ERR(plane_state);
11066
11067 if (mode)
11068 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
11069 else
11070 hdisplay = vdisplay = 0;
11071
11072 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
11073 if (ret)
11074 return ret;
11075 drm_atomic_set_fb_for_plane(plane_state, fb);
11076 plane_state->crtc_x = 0;
11077 plane_state->crtc_y = 0;
11078 plane_state->crtc_w = hdisplay;
11079 plane_state->crtc_h = vdisplay;
11080 plane_state->src_x = x << 16;
11081 plane_state->src_y = y << 16;
11082 plane_state->src_w = hdisplay << 16;
11083 plane_state->src_h = vdisplay << 16;
11084
11085 return 0;
11086}
11087
Daniel Vetterd2434ab2012-08-12 21:20:10 +020011088bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +010011089 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -050011090 struct intel_load_detect_pipe *old,
11091 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080011092{
11093 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020011094 struct intel_encoder *intel_encoder =
11095 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -080011096 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +010011097 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -080011098 struct drm_crtc *crtc = NULL;
11099 struct drm_device *dev = encoder->dev;
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +020011100 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter94352cf2012-07-05 22:51:56 +020011101 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -050011102 struct drm_mode_config *config = &dev->mode_config;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010011103 struct drm_atomic_state *state = NULL, *restore_state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020011104 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030011105 struct intel_crtc_state *crtc_state;
Rob Clark51fd3712013-11-19 12:10:12 -050011106 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -080011107
Chris Wilsond2dff872011-04-19 08:36:26 +010011108 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030011109 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030011110 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010011111
Maarten Lankhorstedde3612016-02-17 09:18:35 +010011112 old->restore_state = NULL;
11113
Rob Clark51fd3712013-11-19 12:10:12 -050011114retry:
11115 ret = drm_modeset_lock(&config->connection_mutex, ctx);
11116 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020011117 goto fail;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020011118
Jesse Barnes79e53942008-11-07 14:24:08 -080011119 /*
11120 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +010011121 *
Jesse Barnes79e53942008-11-07 14:24:08 -080011122 * - if the connector already has an assigned crtc, use it (but make
11123 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +010011124 *
Jesse Barnes79e53942008-11-07 14:24:08 -080011125 * - try to find the first unused crtc that can drive this connector,
11126 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -080011127 */
11128
11129 /* See if we already have a CRTC for this connector */
Maarten Lankhorstedde3612016-02-17 09:18:35 +010011130 if (connector->state->crtc) {
11131 crtc = connector->state->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +010011132
Rob Clark51fd3712013-11-19 12:10:12 -050011133 ret = drm_modeset_lock(&crtc->mutex, ctx);
11134 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020011135 goto fail;
Chris Wilson8261b192011-04-19 23:18:09 +010011136
11137 /* Make sure the crtc and connector are running */
Maarten Lankhorstedde3612016-02-17 09:18:35 +010011138 goto found;
Jesse Barnes79e53942008-11-07 14:24:08 -080011139 }
11140
11141 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010011142 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -080011143 i++;
11144 if (!(encoder->possible_crtcs & (1 << i)))
11145 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010011146
11147 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
11148 if (ret)
11149 goto fail;
11150
11151 if (possible_crtc->state->enable) {
11152 drm_modeset_unlock(&possible_crtc->mutex);
Ville Syrjäläa4592492014-08-11 13:15:36 +030011153 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010011154 }
Ville Syrjäläa4592492014-08-11 13:15:36 +030011155
11156 crtc = possible_crtc;
11157 break;
Jesse Barnes79e53942008-11-07 14:24:08 -080011158 }
11159
11160 /*
11161 * If we didn't find an unused CRTC, don't use any.
11162 */
11163 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +010011164 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020011165 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080011166 }
11167
Maarten Lankhorstedde3612016-02-17 09:18:35 +010011168found:
11169 intel_crtc = to_intel_crtc(crtc);
11170
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010011171 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
11172 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020011173 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080011174
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011175 state = drm_atomic_state_alloc(dev);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010011176 restore_state = drm_atomic_state_alloc(dev);
11177 if (!state || !restore_state) {
11178 ret = -ENOMEM;
11179 goto fail;
11180 }
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011181
11182 state->acquire_ctx = ctx;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010011183 restore_state->acquire_ctx = ctx;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011184
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020011185 connector_state = drm_atomic_get_connector_state(state, connector);
11186 if (IS_ERR(connector_state)) {
11187 ret = PTR_ERR(connector_state);
11188 goto fail;
11189 }
11190
Maarten Lankhorstedde3612016-02-17 09:18:35 +010011191 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
11192 if (ret)
11193 goto fail;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020011194
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030011195 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
11196 if (IS_ERR(crtc_state)) {
11197 ret = PTR_ERR(crtc_state);
11198 goto fail;
11199 }
11200
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020011201 crtc_state->base.active = crtc_state->base.enable = true;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030011202
Chris Wilson64927112011-04-20 07:25:26 +010011203 if (!mode)
11204 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -080011205
Chris Wilsond2dff872011-04-19 08:36:26 +010011206 /* We need a framebuffer large enough to accommodate all accesses
11207 * that the plane may generate whilst we perform load detection.
11208 * We can not rely on the fbcon either being present (we get called
11209 * during its initialisation to detect all boot displays, or it may
11210 * not even exist) or that it is large enough to satisfy the
11211 * requested mode.
11212 */
Daniel Vetter94352cf2012-07-05 22:51:56 +020011213 fb = mode_fits_in_fbdev(dev, mode);
11214 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +010011215 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020011216 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
Chris Wilsond2dff872011-04-19 08:36:26 +010011217 } else
11218 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020011219 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +010011220 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020011221 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080011222 }
Chris Wilsond2dff872011-04-19 08:36:26 +010011223
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030011224 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
11225 if (ret)
11226 goto fail;
11227
Maarten Lankhorstedde3612016-02-17 09:18:35 +010011228 drm_framebuffer_unreference(fb);
11229
11230 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
11231 if (ret)
11232 goto fail;
11233
11234 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
11235 if (!ret)
11236 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
11237 if (!ret)
11238 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
11239 if (ret) {
11240 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
11241 goto fail;
11242 }
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030011243
Maarten Lankhorst3ba86072016-02-29 09:18:57 +010011244 ret = drm_atomic_commit(state);
11245 if (ret) {
Chris Wilson64927112011-04-20 07:25:26 +010011246 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020011247 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080011248 }
Maarten Lankhorstedde3612016-02-17 09:18:35 +010011249
11250 old->restore_state = restore_state;
Chris Wilson71731882011-04-19 23:10:58 +010011251
Jesse Barnes79e53942008-11-07 14:24:08 -080011252 /* let the connector get through one full cycle before testing */
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +020011253 intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +010011254 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020011255
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020011256fail:
Chris Wilson7fb71c82016-10-19 12:37:43 +010011257 if (state) {
11258 drm_atomic_state_put(state);
11259 state = NULL;
11260 }
11261 if (restore_state) {
11262 drm_atomic_state_put(restore_state);
11263 restore_state = NULL;
11264 }
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011265
Rob Clark51fd3712013-11-19 12:10:12 -050011266 if (ret == -EDEADLK) {
11267 drm_modeset_backoff(ctx);
11268 goto retry;
11269 }
11270
Ville Syrjälä412b61d2014-01-17 15:59:39 +020011271 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -080011272}
11273
Daniel Vetterd2434ab2012-08-12 21:20:10 +020011274void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020011275 struct intel_load_detect_pipe *old,
11276 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080011277{
Daniel Vetterd2434ab2012-08-12 21:20:10 +020011278 struct intel_encoder *intel_encoder =
11279 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +010011280 struct drm_encoder *encoder = &intel_encoder->base;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010011281 struct drm_atomic_state *state = old->restore_state;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030011282 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080011283
Chris Wilsond2dff872011-04-19 08:36:26 +010011284 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030011285 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030011286 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010011287
Maarten Lankhorstedde3612016-02-17 09:18:35 +010011288 if (!state)
Chris Wilson0622a532011-04-21 09:32:11 +010011289 return;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010011290
11291 ret = drm_atomic_commit(state);
Chris Wilson08536952016-10-14 13:18:18 +010011292 if (ret)
Maarten Lankhorstedde3612016-02-17 09:18:35 +010011293 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
Chris Wilson08536952016-10-14 13:18:18 +010011294 drm_atomic_state_put(state);
Jesse Barnes79e53942008-11-07 14:24:08 -080011295}
11296
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030011297static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011298 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030011299{
Chris Wilsonfac5e232016-07-04 11:34:36 +010011300 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030011301 u32 dpll = pipe_config->dpll_hw_state.dpll;
11302
11303 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +020011304 return dev_priv->vbt.lvds_ssc_freq;
Tvrtko Ursulin6e266952016-10-13 11:02:53 +010011305 else if (HAS_PCH_SPLIT(dev_priv))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030011306 return 120000;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010011307 else if (!IS_GEN2(dev_priv))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030011308 return 96000;
11309 else
11310 return 48000;
11311}
11312
Jesse Barnes79e53942008-11-07 14:24:08 -080011313/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011314static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011315 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -080011316{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011317 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010011318 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011319 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +030011320 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -080011321 u32 fp;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +030011322 struct dpll clock;
Imre Deakdccbea32015-06-22 23:35:51 +030011323 int port_clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030011324 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -080011325
11326 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +030011327 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -080011328 else
Ville Syrjälä293623f2013-09-13 16:18:46 +030011329 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -080011330
11331 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +020011332 if (IS_PINEVIEW(dev_priv)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -050011333 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
11334 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +080011335 } else {
11336 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
11337 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
11338 }
11339
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010011340 if (!IS_GEN2(dev_priv)) {
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +020011341 if (IS_PINEVIEW(dev_priv))
Adam Jacksonf2b115e2009-12-03 17:14:42 -050011342 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
11343 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +080011344 else
11345 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -080011346 DPLL_FPA01_P1_POST_DIV_SHIFT);
11347
11348 switch (dpll & DPLL_MODE_MASK) {
11349 case DPLLB_MODE_DAC_SERIAL:
11350 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
11351 5 : 10;
11352 break;
11353 case DPLLB_MODE_LVDS:
11354 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
11355 7 : 14;
11356 break;
11357 default:
Zhao Yakui28c97732009-10-09 11:39:41 +080011358 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -080011359 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011360 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080011361 }
11362
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +020011363 if (IS_PINEVIEW(dev_priv))
Imre Deakdccbea32015-06-22 23:35:51 +030011364 port_clock = pnv_calc_dpll_params(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +020011365 else
Imre Deakdccbea32015-06-22 23:35:51 +030011366 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080011367 } else {
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +010011368 u32 lvds = IS_I830(dev_priv) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020011369 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -080011370
11371 if (is_lvds) {
11372 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
11373 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020011374
11375 if (lvds & LVDS_CLKB_POWER_UP)
11376 clock.p2 = 7;
11377 else
11378 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -080011379 } else {
11380 if (dpll & PLL_P1_DIVIDE_BY_TWO)
11381 clock.p1 = 2;
11382 else {
11383 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
11384 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
11385 }
11386 if (dpll & PLL_P2_DIVIDE_BY_4)
11387 clock.p2 = 4;
11388 else
11389 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -080011390 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030011391
Imre Deakdccbea32015-06-22 23:35:51 +030011392 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080011393 }
11394
Ville Syrjälä18442d02013-09-13 16:00:08 +030011395 /*
11396 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +010011397 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +030011398 * encoder's get_config() function.
11399 */
Imre Deakdccbea32015-06-22 23:35:51 +030011400 pipe_config->port_clock = port_clock;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011401}
11402
Ville Syrjälä6878da02013-09-13 15:59:11 +030011403int intel_dotclock_calculate(int link_freq,
11404 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011405{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011406 /*
11407 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +030011408 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011409 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +030011410 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011411 *
11412 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +030011413 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -080011414 */
11415
Ville Syrjälä6878da02013-09-13 15:59:11 +030011416 if (!m_n->link_n)
11417 return 0;
11418
11419 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
11420}
11421
Ville Syrjälä18442d02013-09-13 16:00:08 +030011422static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011423 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +030011424{
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020011425 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä18442d02013-09-13 16:00:08 +030011426
11427 /* read out port_clock from the DPLL */
11428 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +030011429
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011430 /*
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020011431 * In case there is an active pipe without active ports,
11432 * we may need some idea for the dotclock anyway.
11433 * Calculate one based on the FDI configuration.
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011434 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011435 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä21a727b2016-02-17 21:41:10 +020011436 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjälä18442d02013-09-13 16:00:08 +030011437 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -080011438}
11439
11440/** Returns the currently programmed mode of the given pipe. */
11441struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
11442 struct drm_crtc *crtc)
11443{
Chris Wilsonfac5e232016-07-04 11:34:36 +010011444 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080011445 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020011446 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080011447 struct drm_display_mode *mode;
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000011448 struct intel_crtc_state *pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -020011449 int htot = I915_READ(HTOTAL(cpu_transcoder));
11450 int hsync = I915_READ(HSYNC(cpu_transcoder));
11451 int vtot = I915_READ(VTOTAL(cpu_transcoder));
11452 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +030011453 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -080011454
11455 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
11456 if (!mode)
11457 return NULL;
11458
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000011459 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
11460 if (!pipe_config) {
11461 kfree(mode);
11462 return NULL;
11463 }
11464
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011465 /*
11466 * Construct a pipe_config sufficient for getting the clock info
11467 * back out of crtc_clock_get.
11468 *
11469 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
11470 * to use a real value here instead.
11471 */
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000011472 pipe_config->cpu_transcoder = (enum transcoder) pipe;
11473 pipe_config->pixel_multiplier = 1;
11474 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
11475 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
11476 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
11477 i9xx_crtc_clock_get(intel_crtc, pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011478
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000011479 mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -080011480 mode->hdisplay = (htot & 0xffff) + 1;
11481 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
11482 mode->hsync_start = (hsync & 0xffff) + 1;
11483 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
11484 mode->vdisplay = (vtot & 0xffff) + 1;
11485 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
11486 mode->vsync_start = (vsync & 0xffff) + 1;
11487 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
11488
11489 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -080011490
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000011491 kfree(pipe_config);
11492
Jesse Barnes79e53942008-11-07 14:24:08 -080011493 return mode;
11494}
11495
11496static void intel_crtc_destroy(struct drm_crtc *crtc)
11497{
11498 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020011499 struct drm_device *dev = crtc->dev;
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011500 struct intel_flip_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +020011501
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011502 spin_lock_irq(&dev->event_lock);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011503 work = intel_crtc->flip_work;
11504 intel_crtc->flip_work = NULL;
11505 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020011506
Daniel Vetter5a21b662016-05-24 17:13:53 +020011507 if (work) {
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011508 cancel_work_sync(&work->mmio_work);
11509 cancel_work_sync(&work->unpin_work);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011510 kfree(work);
Daniel Vetter67e77c52010-08-20 22:26:30 +020011511 }
Jesse Barnes79e53942008-11-07 14:24:08 -080011512
11513 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020011514
Jesse Barnes79e53942008-11-07 14:24:08 -080011515 kfree(intel_crtc);
11516}
11517
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011518static void intel_unpin_work_fn(struct work_struct *__work)
11519{
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011520 struct intel_flip_work *work =
11521 container_of(__work, struct intel_flip_work, unpin_work);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011522 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
11523 struct drm_device *dev = crtc->base.dev;
11524 struct drm_plane *primary = crtc->base.primary;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011525
Daniel Vetter5a21b662016-05-24 17:13:53 +020011526 if (is_mmio_work(work))
11527 flush_work(&work->mmio_work);
11528
11529 mutex_lock(&dev->struct_mutex);
11530 intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
Chris Wilsonf8c417c2016-07-20 13:31:53 +010011531 i915_gem_object_put(work->pending_flip_obj);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011532 mutex_unlock(&dev->struct_mutex);
11533
Chris Wilsone8a261e2016-07-20 13:31:49 +010011534 i915_gem_request_put(work->flip_queued_req);
11535
Chris Wilson5748b6a2016-08-04 16:32:38 +010011536 intel_frontbuffer_flip_complete(to_i915(dev),
11537 to_intel_plane(primary)->frontbuffer_bit);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011538 intel_fbc_post_update(crtc);
11539 drm_framebuffer_unreference(work->old_fb);
11540
11541 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
11542 atomic_dec(&crtc->unpin_work_count);
11543
11544 kfree(work);
11545}
11546
11547/* Is 'a' after or equal to 'b'? */
11548static bool g4x_flip_count_after_eq(u32 a, u32 b)
11549{
11550 return !((a - b) & 0x80000000);
11551}
11552
11553static bool __pageflip_finished_cs(struct intel_crtc *crtc,
11554 struct intel_flip_work *work)
11555{
11556 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010011557 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011558
Chris Wilson8af29b02016-09-09 14:11:47 +010011559 if (abort_flip_on_reset(crtc))
Daniel Vetter5a21b662016-05-24 17:13:53 +020011560 return true;
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011561
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011562 /*
Daniel Vetter5a21b662016-05-24 17:13:53 +020011563 * The relevant registers doen't exist on pre-ctg.
11564 * As the flip done interrupt doesn't trigger for mmio
11565 * flips on gmch platforms, a flip count check isn't
11566 * really needed there. But since ctg has the registers,
11567 * include it in the check anyway.
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011568 */
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010011569 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
Daniel Vetter5a21b662016-05-24 17:13:53 +020011570 return true;
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011571
Daniel Vetter5a21b662016-05-24 17:13:53 +020011572 /*
11573 * BDW signals flip done immediately if the plane
11574 * is disabled, even if the plane enable is already
11575 * armed to occur at the next vblank :(
11576 */
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020011577
Daniel Vetter5a21b662016-05-24 17:13:53 +020011578 /*
11579 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
11580 * used the same base address. In that case the mmio flip might
11581 * have completed, but the CS hasn't even executed the flip yet.
11582 *
11583 * A flip count check isn't enough as the CS might have updated
11584 * the base address just after start of vblank, but before we
11585 * managed to process the interrupt. This means we'd complete the
11586 * CS flip too soon.
11587 *
11588 * Combining both checks should get us a good enough result. It may
11589 * still happen that the CS flip has been executed, but has not
11590 * yet actually completed. But in case the base address is the same
11591 * anyway, we don't really care.
11592 */
11593 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
11594 crtc->flip_work->gtt_offset &&
11595 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
11596 crtc->flip_work->flip_count);
11597}
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011598
Daniel Vetter5a21b662016-05-24 17:13:53 +020011599static bool
11600__pageflip_finished_mmio(struct intel_crtc *crtc,
11601 struct intel_flip_work *work)
11602{
11603 /*
11604 * MMIO work completes when vblank is different from
11605 * flip_queued_vblank.
11606 *
11607 * Reset counter value doesn't matter, this is handled by
11608 * i915_wait_request finishing early, so no need to handle
11609 * reset here.
11610 */
11611 return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011612}
11613
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011614
11615static bool pageflip_finished(struct intel_crtc *crtc,
11616 struct intel_flip_work *work)
11617{
11618 if (!atomic_read(&work->pending))
11619 return false;
11620
11621 smp_rmb();
11622
Daniel Vetter5a21b662016-05-24 17:13:53 +020011623 if (is_mmio_work(work))
11624 return __pageflip_finished_mmio(crtc, work);
11625 else
11626 return __pageflip_finished_cs(crtc, work);
11627}
11628
11629void intel_finish_page_flip_cs(struct drm_i915_private *dev_priv, int pipe)
11630{
Chris Wilson91c8a322016-07-05 10:40:23 +010011631 struct drm_device *dev = &dev_priv->drm;
Ville Syrjälä98187832016-10-31 22:37:10 +020011632 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011633 struct intel_flip_work *work;
11634 unsigned long flags;
11635
11636 /* Ignore early vblank irqs */
11637 if (!crtc)
11638 return;
11639
Daniel Vetterf3260382014-09-15 14:55:23 +020011640 /*
Daniel Vetter5a21b662016-05-24 17:13:53 +020011641 * This is called both by irq handlers and the reset code (to complete
11642 * lost pageflips) so needs the full irqsave spinlocks.
Chris Wilsone7d841c2012-12-03 11:36:30 +000011643 */
Daniel Vetter5a21b662016-05-24 17:13:53 +020011644 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020011645 work = crtc->flip_work;
Daniel Vetter5a21b662016-05-24 17:13:53 +020011646
11647 if (work != NULL &&
11648 !is_mmio_work(work) &&
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020011649 pageflip_finished(crtc, work))
11650 page_flip_completed(crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011651
11652 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011653}
11654
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011655void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
Chris Wilsone7d841c2012-12-03 11:36:30 +000011656{
Chris Wilson91c8a322016-07-05 10:40:23 +010011657 struct drm_device *dev = &dev_priv->drm;
Ville Syrjälä98187832016-10-31 22:37:10 +020011658 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011659 struct intel_flip_work *work;
11660 unsigned long flags;
11661
11662 /* Ignore early vblank irqs */
11663 if (!crtc)
11664 return;
11665
11666 /*
11667 * This is called both by irq handlers and the reset code (to complete
11668 * lost pageflips) so needs the full irqsave spinlocks.
11669 */
11670 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020011671 work = crtc->flip_work;
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011672
Daniel Vetter5a21b662016-05-24 17:13:53 +020011673 if (work != NULL &&
11674 is_mmio_work(work) &&
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020011675 pageflip_finished(crtc, work))
11676 page_flip_completed(crtc);
Maarten Lankhorst68858432016-05-17 15:07:52 +020011677
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011678 spin_unlock_irqrestore(&dev->event_lock, flags);
11679}
11680
Daniel Vetter5a21b662016-05-24 17:13:53 +020011681static inline void intel_mark_page_flip_active(struct intel_crtc *crtc,
11682 struct intel_flip_work *work)
11683{
11684 work->flip_queued_vblank = intel_crtc_get_vblank_counter(crtc);
11685
11686 /* Ensure that the work item is consistent when activating it ... */
11687 smp_mb__before_atomic();
11688 atomic_set(&work->pending, 1);
11689}
11690
11691static int intel_gen2_queue_flip(struct drm_device *dev,
11692 struct drm_crtc *crtc,
11693 struct drm_framebuffer *fb,
11694 struct drm_i915_gem_object *obj,
11695 struct drm_i915_gem_request *req,
11696 uint32_t flags)
11697{
Chris Wilson7e37f882016-08-02 22:50:21 +010011698 struct intel_ring *ring = req->ring;
Daniel Vetter5a21b662016-05-24 17:13:53 +020011699 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11700 u32 flip_mask;
11701 int ret;
11702
11703 ret = intel_ring_begin(req, 6);
11704 if (ret)
11705 return ret;
11706
11707 /* Can't queue multiple flips, so wait for the previous
11708 * one to finish before executing the next.
11709 */
11710 if (intel_crtc->plane)
11711 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11712 else
11713 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Chris Wilsonb5321f32016-08-02 22:50:18 +010011714 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
11715 intel_ring_emit(ring, MI_NOOP);
11716 intel_ring_emit(ring, MI_DISPLAY_FLIP |
Daniel Vetter5a21b662016-05-24 17:13:53 +020011717 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Chris Wilsonb5321f32016-08-02 22:50:18 +010011718 intel_ring_emit(ring, fb->pitches[0]);
11719 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
11720 intel_ring_emit(ring, 0); /* aux display base address, unused */
Daniel Vetter5a21b662016-05-24 17:13:53 +020011721
11722 return 0;
11723}
11724
11725static int intel_gen3_queue_flip(struct drm_device *dev,
11726 struct drm_crtc *crtc,
11727 struct drm_framebuffer *fb,
11728 struct drm_i915_gem_object *obj,
11729 struct drm_i915_gem_request *req,
11730 uint32_t flags)
11731{
Chris Wilson7e37f882016-08-02 22:50:21 +010011732 struct intel_ring *ring = req->ring;
Daniel Vetter5a21b662016-05-24 17:13:53 +020011733 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11734 u32 flip_mask;
11735 int ret;
11736
11737 ret = intel_ring_begin(req, 6);
11738 if (ret)
11739 return ret;
11740
11741 if (intel_crtc->plane)
11742 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11743 else
11744 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Chris Wilsonb5321f32016-08-02 22:50:18 +010011745 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
11746 intel_ring_emit(ring, MI_NOOP);
11747 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
Daniel Vetter5a21b662016-05-24 17:13:53 +020011748 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Chris Wilsonb5321f32016-08-02 22:50:18 +010011749 intel_ring_emit(ring, fb->pitches[0]);
11750 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
11751 intel_ring_emit(ring, MI_NOOP);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011752
11753 return 0;
11754}
11755
11756static int intel_gen4_queue_flip(struct drm_device *dev,
11757 struct drm_crtc *crtc,
11758 struct drm_framebuffer *fb,
11759 struct drm_i915_gem_object *obj,
11760 struct drm_i915_gem_request *req,
11761 uint32_t flags)
11762{
Chris Wilson7e37f882016-08-02 22:50:21 +010011763 struct intel_ring *ring = req->ring;
Chris Wilsonfac5e232016-07-04 11:34:36 +010011764 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011765 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11766 uint32_t pf, pipesrc;
11767 int ret;
11768
11769 ret = intel_ring_begin(req, 4);
11770 if (ret)
11771 return ret;
11772
11773 /* i965+ uses the linear or tiled offsets from the
11774 * Display Registers (which do not change across a page-flip)
11775 * so we need only reprogram the base address.
11776 */
Chris Wilsonb5321f32016-08-02 22:50:18 +010011777 intel_ring_emit(ring, MI_DISPLAY_FLIP |
Daniel Vetter5a21b662016-05-24 17:13:53 +020011778 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Chris Wilsonb5321f32016-08-02 22:50:18 +010011779 intel_ring_emit(ring, fb->pitches[0]);
11780 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset |
Ville Syrjälä72618eb2016-02-04 20:38:20 +020011781 intel_fb_modifier_to_tiling(fb->modifier[0]));
Daniel Vetter5a21b662016-05-24 17:13:53 +020011782
11783 /* XXX Enabling the panel-fitter across page-flip is so far
11784 * untested on non-native modes, so ignore it for now.
11785 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
11786 */
11787 pf = 0;
11788 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Chris Wilsonb5321f32016-08-02 22:50:18 +010011789 intel_ring_emit(ring, pf | pipesrc);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011790
11791 return 0;
11792}
11793
11794static int intel_gen6_queue_flip(struct drm_device *dev,
11795 struct drm_crtc *crtc,
11796 struct drm_framebuffer *fb,
11797 struct drm_i915_gem_object *obj,
11798 struct drm_i915_gem_request *req,
11799 uint32_t flags)
11800{
Chris Wilson7e37f882016-08-02 22:50:21 +010011801 struct intel_ring *ring = req->ring;
Chris Wilsonfac5e232016-07-04 11:34:36 +010011802 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011803 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11804 uint32_t pf, pipesrc;
11805 int ret;
11806
11807 ret = intel_ring_begin(req, 4);
11808 if (ret)
11809 return ret;
11810
Chris Wilsonb5321f32016-08-02 22:50:18 +010011811 intel_ring_emit(ring, MI_DISPLAY_FLIP |
Daniel Vetter5a21b662016-05-24 17:13:53 +020011812 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Ville Syrjälä72618eb2016-02-04 20:38:20 +020011813 intel_ring_emit(ring, fb->pitches[0] |
11814 intel_fb_modifier_to_tiling(fb->modifier[0]));
Chris Wilsonb5321f32016-08-02 22:50:18 +010011815 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011816
11817 /* Contrary to the suggestions in the documentation,
11818 * "Enable Panel Fitter" does not seem to be required when page
11819 * flipping with a non-native mode, and worse causes a normal
11820 * modeset to fail.
11821 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11822 */
11823 pf = 0;
11824 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Chris Wilsonb5321f32016-08-02 22:50:18 +010011825 intel_ring_emit(ring, pf | pipesrc);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011826
11827 return 0;
11828}
11829
11830static int intel_gen7_queue_flip(struct drm_device *dev,
11831 struct drm_crtc *crtc,
11832 struct drm_framebuffer *fb,
11833 struct drm_i915_gem_object *obj,
11834 struct drm_i915_gem_request *req,
11835 uint32_t flags)
11836{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010011837 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson7e37f882016-08-02 22:50:21 +010011838 struct intel_ring *ring = req->ring;
Daniel Vetter5a21b662016-05-24 17:13:53 +020011839 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11840 uint32_t plane_bit = 0;
11841 int len, ret;
11842
11843 switch (intel_crtc->plane) {
11844 case PLANE_A:
11845 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11846 break;
11847 case PLANE_B:
11848 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11849 break;
11850 case PLANE_C:
11851 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11852 break;
11853 default:
11854 WARN_ONCE(1, "unknown plane in flip command\n");
11855 return -ENODEV;
11856 }
11857
11858 len = 4;
Chris Wilsonb5321f32016-08-02 22:50:18 +010011859 if (req->engine->id == RCS) {
Daniel Vetter5a21b662016-05-24 17:13:53 +020011860 len += 6;
11861 /*
11862 * On Gen 8, SRM is now taking an extra dword to accommodate
11863 * 48bits addresses, and we need a NOOP for the batch size to
11864 * stay even.
11865 */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010011866 if (IS_GEN8(dev_priv))
Daniel Vetter5a21b662016-05-24 17:13:53 +020011867 len += 2;
11868 }
11869
11870 /*
11871 * BSpec MI_DISPLAY_FLIP for IVB:
11872 * "The full packet must be contained within the same cache line."
11873 *
11874 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11875 * cacheline, if we ever start emitting more commands before
11876 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11877 * then do the cacheline alignment, and finally emit the
11878 * MI_DISPLAY_FLIP.
11879 */
11880 ret = intel_ring_cacheline_align(req);
11881 if (ret)
11882 return ret;
11883
11884 ret = intel_ring_begin(req, len);
11885 if (ret)
11886 return ret;
11887
11888 /* Unmask the flip-done completion message. Note that the bspec says that
11889 * we should do this for both the BCS and RCS, and that we must not unmask
11890 * more than one flip event at any time (or ensure that one flip message
11891 * can be sent by waiting for flip-done prior to queueing new flips).
11892 * Experimentation says that BCS works despite DERRMR masking all
11893 * flip-done completion events and that unmasking all planes at once
11894 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11895 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11896 */
Chris Wilsonb5321f32016-08-02 22:50:18 +010011897 if (req->engine->id == RCS) {
11898 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
11899 intel_ring_emit_reg(ring, DERRMR);
11900 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
Daniel Vetter5a21b662016-05-24 17:13:53 +020011901 DERRMR_PIPEB_PRI_FLIP_DONE |
11902 DERRMR_PIPEC_PRI_FLIP_DONE));
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010011903 if (IS_GEN8(dev_priv))
Chris Wilsonb5321f32016-08-02 22:50:18 +010011904 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8 |
Daniel Vetter5a21b662016-05-24 17:13:53 +020011905 MI_SRM_LRM_GLOBAL_GTT);
11906 else
Chris Wilsonb5321f32016-08-02 22:50:18 +010011907 intel_ring_emit(ring, MI_STORE_REGISTER_MEM |
Daniel Vetter5a21b662016-05-24 17:13:53 +020011908 MI_SRM_LRM_GLOBAL_GTT);
Chris Wilsonb5321f32016-08-02 22:50:18 +010011909 intel_ring_emit_reg(ring, DERRMR);
Chris Wilsonbde13eb2016-08-15 10:49:07 +010011910 intel_ring_emit(ring,
11911 i915_ggtt_offset(req->engine->scratch) + 256);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010011912 if (IS_GEN8(dev_priv)) {
Chris Wilsonb5321f32016-08-02 22:50:18 +010011913 intel_ring_emit(ring, 0);
11914 intel_ring_emit(ring, MI_NOOP);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011915 }
11916 }
11917
Chris Wilsonb5321f32016-08-02 22:50:18 +010011918 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä72618eb2016-02-04 20:38:20 +020011919 intel_ring_emit(ring, fb->pitches[0] |
11920 intel_fb_modifier_to_tiling(fb->modifier[0]));
Chris Wilsonb5321f32016-08-02 22:50:18 +010011921 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
11922 intel_ring_emit(ring, (MI_NOOP));
Daniel Vetter5a21b662016-05-24 17:13:53 +020011923
11924 return 0;
11925}
11926
11927static bool use_mmio_flip(struct intel_engine_cs *engine,
11928 struct drm_i915_gem_object *obj)
11929{
11930 /*
11931 * This is not being used for older platforms, because
11932 * non-availability of flip done interrupt forces us to use
11933 * CS flips. Older platforms derive flip done using some clever
11934 * tricks involving the flip_pending status bits and vblank irqs.
11935 * So using MMIO flips there would disrupt this mechanism.
11936 */
11937
11938 if (engine == NULL)
11939 return true;
11940
11941 if (INTEL_GEN(engine->i915) < 5)
11942 return false;
11943
11944 if (i915.use_mmio_flip < 0)
11945 return false;
11946 else if (i915.use_mmio_flip > 0)
11947 return true;
11948 else if (i915.enable_execlists)
11949 return true;
Chris Wilsonc37efb92016-06-17 08:28:47 +010011950
Chris Wilsond07f0e52016-10-28 13:58:44 +010011951 return engine != i915_gem_object_last_write_engine(obj);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011952}
11953
11954static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
11955 unsigned int rotation,
11956 struct intel_flip_work *work)
11957{
11958 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010011959 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011960 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
11961 const enum pipe pipe = intel_crtc->pipe;
Ville Syrjäläd2196772016-01-28 18:33:11 +020011962 u32 ctl, stride = skl_plane_stride(fb, 0, rotation);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011963
11964 ctl = I915_READ(PLANE_CTL(pipe, 0));
11965 ctl &= ~PLANE_CTL_TILED_MASK;
11966 switch (fb->modifier[0]) {
11967 case DRM_FORMAT_MOD_NONE:
11968 break;
11969 case I915_FORMAT_MOD_X_TILED:
11970 ctl |= PLANE_CTL_TILED_X;
11971 break;
11972 case I915_FORMAT_MOD_Y_TILED:
11973 ctl |= PLANE_CTL_TILED_Y;
11974 break;
11975 case I915_FORMAT_MOD_Yf_TILED:
11976 ctl |= PLANE_CTL_TILED_YF;
11977 break;
11978 default:
11979 MISSING_CASE(fb->modifier[0]);
11980 }
11981
11982 /*
Daniel Vetter5a21b662016-05-24 17:13:53 +020011983 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11984 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11985 */
11986 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11987 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11988
11989 I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
11990 POSTING_READ(PLANE_SURF(pipe, 0));
11991}
11992
11993static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
11994 struct intel_flip_work *work)
11995{
11996 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010011997 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä72618eb2016-02-04 20:38:20 +020011998 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
Daniel Vetter5a21b662016-05-24 17:13:53 +020011999 i915_reg_t reg = DSPCNTR(intel_crtc->plane);
12000 u32 dspcntr;
12001
12002 dspcntr = I915_READ(reg);
12003
Ville Syrjälä72618eb2016-02-04 20:38:20 +020012004 if (fb->modifier[0] == I915_FORMAT_MOD_X_TILED)
Daniel Vetter5a21b662016-05-24 17:13:53 +020012005 dspcntr |= DISPPLANE_TILED;
12006 else
12007 dspcntr &= ~DISPPLANE_TILED;
12008
12009 I915_WRITE(reg, dspcntr);
12010
12011 I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
12012 POSTING_READ(DSPSURF(intel_crtc->plane));
12013}
12014
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020012015static void intel_mmio_flip_work_func(struct work_struct *w)
Damien Lespiauff944562014-11-20 14:58:16 +000012016{
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020012017 struct intel_flip_work *work =
12018 container_of(w, struct intel_flip_work, mmio_work);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012019 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
12020 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
12021 struct intel_framebuffer *intel_fb =
12022 to_intel_framebuffer(crtc->base.primary->fb);
12023 struct drm_i915_gem_object *obj = intel_fb->obj;
12024
Chris Wilsond07f0e52016-10-28 13:58:44 +010012025 WARN_ON(i915_gem_object_wait(obj, 0, MAX_SCHEDULE_TIMEOUT, NULL) < 0);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012026
12027 intel_pipe_update_start(crtc);
12028
12029 if (INTEL_GEN(dev_priv) >= 9)
12030 skl_do_mmio_flip(crtc, work->rotation, work);
12031 else
12032 /* use_mmio_flip() retricts MMIO flips to ilk+ */
12033 ilk_do_mmio_flip(crtc, work);
12034
12035 intel_pipe_update_end(crtc, work);
12036}
12037
12038static int intel_default_queue_flip(struct drm_device *dev,
12039 struct drm_crtc *crtc,
12040 struct drm_framebuffer *fb,
12041 struct drm_i915_gem_object *obj,
12042 struct drm_i915_gem_request *req,
12043 uint32_t flags)
12044{
12045 return -ENODEV;
12046}
12047
12048static bool __pageflip_stall_check_cs(struct drm_i915_private *dev_priv,
12049 struct intel_crtc *intel_crtc,
12050 struct intel_flip_work *work)
12051{
12052 u32 addr, vblank;
12053
12054 if (!atomic_read(&work->pending))
12055 return false;
12056
12057 smp_rmb();
12058
12059 vblank = intel_crtc_get_vblank_counter(intel_crtc);
12060 if (work->flip_ready_vblank == 0) {
12061 if (work->flip_queued_req &&
Chris Wilsonf69a02c2016-07-01 17:23:16 +010012062 !i915_gem_request_completed(work->flip_queued_req))
Daniel Vetter5a21b662016-05-24 17:13:53 +020012063 return false;
12064
12065 work->flip_ready_vblank = vblank;
12066 }
12067
12068 if (vblank - work->flip_ready_vblank < 3)
12069 return false;
12070
12071 /* Potential stall - if we see that the flip has happened,
12072 * assume a missed interrupt. */
12073 if (INTEL_GEN(dev_priv) >= 4)
12074 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
12075 else
12076 addr = I915_READ(DSPADDR(intel_crtc->plane));
12077
12078 /* There is a potential issue here with a false positive after a flip
12079 * to the same address. We could address this by checking for a
12080 * non-incrementing frame counter.
12081 */
12082 return addr == work->gtt_offset;
12083}
12084
12085void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
12086{
Chris Wilson91c8a322016-07-05 10:40:23 +010012087 struct drm_device *dev = &dev_priv->drm;
Ville Syrjälä98187832016-10-31 22:37:10 +020012088 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012089 struct intel_flip_work *work;
12090
12091 WARN_ON(!in_interrupt());
12092
12093 if (crtc == NULL)
12094 return;
12095
12096 spin_lock(&dev->event_lock);
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020012097 work = crtc->flip_work;
Daniel Vetter5a21b662016-05-24 17:13:53 +020012098
12099 if (work != NULL && !is_mmio_work(work) &&
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020012100 __pageflip_stall_check_cs(dev_priv, crtc, work)) {
Daniel Vetter5a21b662016-05-24 17:13:53 +020012101 WARN_ONCE(1,
12102 "Kicking stuck page flip: queued at %d, now %d\n",
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020012103 work->flip_queued_vblank, intel_crtc_get_vblank_counter(crtc));
12104 page_flip_completed(crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012105 work = NULL;
12106 }
12107
12108 if (work != NULL && !is_mmio_work(work) &&
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020012109 intel_crtc_get_vblank_counter(crtc) - work->flip_queued_vblank > 1)
Daniel Vetter5a21b662016-05-24 17:13:53 +020012110 intel_queue_rps_boost_for_request(work->flip_queued_req);
12111 spin_unlock(&dev->event_lock);
12112}
12113
12114static int intel_crtc_page_flip(struct drm_crtc *crtc,
12115 struct drm_framebuffer *fb,
12116 struct drm_pending_vblank_event *event,
12117 uint32_t page_flip_flags)
12118{
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020012119 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010012120 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012121 struct drm_framebuffer *old_fb = crtc->primary->fb;
12122 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
12123 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12124 struct drm_plane *primary = crtc->primary;
12125 enum pipe pipe = intel_crtc->pipe;
12126 struct intel_flip_work *work;
12127 struct intel_engine_cs *engine;
12128 bool mmio_flip;
Chris Wilson8e637172016-08-02 22:50:26 +010012129 struct drm_i915_gem_request *request;
Chris Wilson058d88c2016-08-15 10:49:06 +010012130 struct i915_vma *vma;
Daniel Vetter5a21b662016-05-24 17:13:53 +020012131 int ret;
Sourab Gupta84c33a62014-06-02 16:47:17 +053012132
Daniel Vetter5a21b662016-05-24 17:13:53 +020012133 /*
12134 * drm_mode_page_flip_ioctl() should already catch this, but double
12135 * check to be safe. In the future we may enable pageflipping from
12136 * a disabled primary plane.
12137 */
12138 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
12139 return -EBUSY;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020012140
Daniel Vetter5a21b662016-05-24 17:13:53 +020012141 /* Can't change pixel format via MI display flips. */
12142 if (fb->pixel_format != crtc->primary->fb->pixel_format)
12143 return -EINVAL;
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020012144
Daniel Vetter5a21b662016-05-24 17:13:53 +020012145 /*
12146 * TILEOFF/LINOFF registers can't be changed via MI display flips.
12147 * Note that pitch changes could also affect these register.
12148 */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000012149 if (INTEL_GEN(dev_priv) > 3 &&
Daniel Vetter5a21b662016-05-24 17:13:53 +020012150 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
12151 fb->pitches[0] != crtc->primary->fb->pitches[0]))
12152 return -EINVAL;
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020012153
Daniel Vetter5a21b662016-05-24 17:13:53 +020012154 if (i915_terminally_wedged(&dev_priv->gpu_error))
12155 goto out_hang;
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020012156
Daniel Vetter5a21b662016-05-24 17:13:53 +020012157 work = kzalloc(sizeof(*work), GFP_KERNEL);
12158 if (work == NULL)
12159 return -ENOMEM;
12160
12161 work->event = event;
12162 work->crtc = crtc;
12163 work->old_fb = old_fb;
12164 INIT_WORK(&work->unpin_work, intel_unpin_work_fn);
Sourab Gupta84c33a62014-06-02 16:47:17 +053012165
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020012166 ret = drm_crtc_vblank_get(crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012167 if (ret)
12168 goto free_work;
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020012169
Daniel Vetter5a21b662016-05-24 17:13:53 +020012170 /* We borrow the event spin lock for protecting flip_work */
12171 spin_lock_irq(&dev->event_lock);
12172 if (intel_crtc->flip_work) {
12173 /* Before declaring the flip queue wedged, check if
12174 * the hardware completed the operation behind our backs.
12175 */
12176 if (pageflip_finished(intel_crtc, intel_crtc->flip_work)) {
12177 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
12178 page_flip_completed(intel_crtc);
12179 } else {
12180 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
12181 spin_unlock_irq(&dev->event_lock);
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020012182
Daniel Vetter5a21b662016-05-24 17:13:53 +020012183 drm_crtc_vblank_put(crtc);
12184 kfree(work);
12185 return -EBUSY;
12186 }
12187 }
12188 intel_crtc->flip_work = work;
12189 spin_unlock_irq(&dev->event_lock);
Alex Goinsfd8e0582015-11-25 18:43:38 -080012190
Daniel Vetter5a21b662016-05-24 17:13:53 +020012191 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
12192 flush_workqueue(dev_priv->wq);
12193
12194 /* Reference the objects for the scheduled work. */
12195 drm_framebuffer_reference(work->old_fb);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012196
12197 crtc->primary->fb = fb;
12198 update_state_fb(crtc->primary);
Maarten Lankhorstfaf68d92016-06-14 14:24:20 +020012199
Chris Wilson25dc5562016-07-20 13:31:52 +010012200 work->pending_flip_obj = i915_gem_object_get(obj);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012201
12202 ret = i915_mutex_lock_interruptible(dev);
12203 if (ret)
12204 goto cleanup;
12205
Chris Wilson8af29b02016-09-09 14:11:47 +010012206 intel_crtc->reset_count = i915_reset_count(&dev_priv->gpu_error);
12207 if (i915_reset_in_progress_or_wedged(&dev_priv->gpu_error)) {
Daniel Vetter5a21b662016-05-24 17:13:53 +020012208 ret = -EIO;
Matthew Auldddbb2712016-11-28 10:36:48 +000012209 goto unlock;
Daniel Vetter5a21b662016-05-24 17:13:53 +020012210 }
12211
12212 atomic_inc(&intel_crtc->unpin_work_count);
12213
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010012214 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
Daniel Vetter5a21b662016-05-24 17:13:53 +020012215 work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
12216
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010012217 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Akash Goel3b3f1652016-10-13 22:44:48 +053012218 engine = dev_priv->engine[BCS];
Ville Syrjälä72618eb2016-02-04 20:38:20 +020012219 if (fb->modifier[0] != old_fb->modifier[0])
Daniel Vetter5a21b662016-05-24 17:13:53 +020012220 /* vlv: DISPLAY_FLIP fails to change tiling */
12221 engine = NULL;
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +010012222 } else if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv)) {
Akash Goel3b3f1652016-10-13 22:44:48 +053012223 engine = dev_priv->engine[BCS];
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000012224 } else if (INTEL_GEN(dev_priv) >= 7) {
Chris Wilsond07f0e52016-10-28 13:58:44 +010012225 engine = i915_gem_object_last_write_engine(obj);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012226 if (engine == NULL || engine->id != RCS)
Akash Goel3b3f1652016-10-13 22:44:48 +053012227 engine = dev_priv->engine[BCS];
Daniel Vetter5a21b662016-05-24 17:13:53 +020012228 } else {
Akash Goel3b3f1652016-10-13 22:44:48 +053012229 engine = dev_priv->engine[RCS];
Daniel Vetter5a21b662016-05-24 17:13:53 +020012230 }
12231
12232 mmio_flip = use_mmio_flip(engine, obj);
12233
Chris Wilson058d88c2016-08-15 10:49:06 +010012234 vma = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
12235 if (IS_ERR(vma)) {
12236 ret = PTR_ERR(vma);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012237 goto cleanup_pending;
Chris Wilson058d88c2016-08-15 10:49:06 +010012238 }
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020012239
Ville Syrjälä6687c902015-09-15 13:16:41 +030012240 work->gtt_offset = intel_fb_gtt_offset(fb, primary->state->rotation);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012241 work->gtt_offset += intel_crtc->dspaddr_offset;
12242 work->rotation = crtc->primary->state->rotation;
12243
Paulo Zanoni1f0613162016-08-17 16:41:44 -030012244 /*
12245 * There's the potential that the next frame will not be compatible with
12246 * FBC, so we want to call pre_update() before the actual page flip.
12247 * The problem is that pre_update() caches some information about the fb
12248 * object, so we want to do this only after the object is pinned. Let's
12249 * be on the safe side and do this immediately before scheduling the
12250 * flip.
12251 */
12252 intel_fbc_pre_update(intel_crtc, intel_crtc->config,
12253 to_intel_plane_state(primary->state));
12254
Daniel Vetter5a21b662016-05-24 17:13:53 +020012255 if (mmio_flip) {
12256 INIT_WORK(&work->mmio_work, intel_mmio_flip_work_func);
Imre Deak6277c8d2016-09-20 14:58:19 +030012257 queue_work(system_unbound_wq, &work->mmio_work);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012258 } else {
Chris Wilson8e637172016-08-02 22:50:26 +010012259 request = i915_gem_request_alloc(engine, engine->last_context);
12260 if (IS_ERR(request)) {
12261 ret = PTR_ERR(request);
12262 goto cleanup_unpin;
12263 }
12264
Chris Wilsona2bc4692016-09-09 14:11:56 +010012265 ret = i915_gem_request_await_object(request, obj, false);
Chris Wilson8e637172016-08-02 22:50:26 +010012266 if (ret)
12267 goto cleanup_request;
12268
Daniel Vetter5a21b662016-05-24 17:13:53 +020012269 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
12270 page_flip_flags);
12271 if (ret)
Chris Wilson8e637172016-08-02 22:50:26 +010012272 goto cleanup_request;
Daniel Vetter5a21b662016-05-24 17:13:53 +020012273
12274 intel_mark_page_flip_active(intel_crtc, work);
12275
Chris Wilson8e637172016-08-02 22:50:26 +010012276 work->flip_queued_req = i915_gem_request_get(request);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012277 i915_add_request_no_flush(request);
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020012278 }
12279
Chris Wilson92117f02016-11-28 14:36:48 +000012280 i915_gem_object_wait_priority(obj, 0, I915_PRIORITY_DISPLAY);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012281 i915_gem_track_fb(intel_fb_obj(old_fb), obj,
12282 to_intel_plane(primary)->frontbuffer_bit);
12283 mutex_unlock(&dev->struct_mutex);
12284
Chris Wilson5748b6a2016-08-04 16:32:38 +010012285 intel_frontbuffer_flip_prepare(to_i915(dev),
Daniel Vetter5a21b662016-05-24 17:13:53 +020012286 to_intel_plane(primary)->frontbuffer_bit);
12287
12288 trace_i915_flip_request(intel_crtc->plane, obj);
12289
12290 return 0;
12291
Chris Wilson8e637172016-08-02 22:50:26 +010012292cleanup_request:
12293 i915_add_request_no_flush(request);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012294cleanup_unpin:
12295 intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
12296cleanup_pending:
Daniel Vetter5a21b662016-05-24 17:13:53 +020012297 atomic_dec(&intel_crtc->unpin_work_count);
Matthew Auldddbb2712016-11-28 10:36:48 +000012298unlock:
Daniel Vetter5a21b662016-05-24 17:13:53 +020012299 mutex_unlock(&dev->struct_mutex);
12300cleanup:
12301 crtc->primary->fb = old_fb;
12302 update_state_fb(crtc->primary);
12303
Chris Wilsonf0cd5182016-10-28 13:58:43 +010012304 i915_gem_object_put(obj);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012305 drm_framebuffer_unreference(work->old_fb);
12306
12307 spin_lock_irq(&dev->event_lock);
12308 intel_crtc->flip_work = NULL;
12309 spin_unlock_irq(&dev->event_lock);
12310
12311 drm_crtc_vblank_put(crtc);
12312free_work:
12313 kfree(work);
12314
12315 if (ret == -EIO) {
12316 struct drm_atomic_state *state;
12317 struct drm_plane_state *plane_state;
12318
12319out_hang:
12320 state = drm_atomic_state_alloc(dev);
12321 if (!state)
12322 return -ENOMEM;
12323 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
12324
12325retry:
12326 plane_state = drm_atomic_get_plane_state(state, primary);
12327 ret = PTR_ERR_OR_ZERO(plane_state);
12328 if (!ret) {
12329 drm_atomic_set_fb_for_plane(plane_state, fb);
12330
12331 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
12332 if (!ret)
12333 ret = drm_atomic_commit(state);
12334 }
12335
12336 if (ret == -EDEADLK) {
12337 drm_modeset_backoff(state->acquire_ctx);
12338 drm_atomic_state_clear(state);
12339 goto retry;
12340 }
12341
Chris Wilson08536952016-10-14 13:18:18 +010012342 drm_atomic_state_put(state);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012343
12344 if (ret == 0 && event) {
12345 spin_lock_irq(&dev->event_lock);
12346 drm_crtc_send_vblank_event(crtc, event);
12347 spin_unlock_irq(&dev->event_lock);
12348 }
12349 }
12350 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070012351}
12352
Daniel Vetter5a21b662016-05-24 17:13:53 +020012353
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012354/**
12355 * intel_wm_need_update - Check whether watermarks need updating
12356 * @plane: drm plane
12357 * @state: new plane state
12358 *
12359 * Check current plane state versus the new one to determine whether
12360 * watermarks need to be recalculated.
12361 *
12362 * Returns true or false.
12363 */
12364static bool intel_wm_need_update(struct drm_plane *plane,
12365 struct drm_plane_state *state)
12366{
Matt Roperd21fbe82015-09-24 15:53:12 -070012367 struct intel_plane_state *new = to_intel_plane_state(state);
12368 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
12369
12370 /* Update watermarks on tiling or size changes. */
Ville Syrjälä936e71e2016-07-26 19:06:59 +030012371 if (new->base.visible != cur->base.visible)
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010012372 return true;
12373
12374 if (!cur->base.fb || !new->base.fb)
12375 return false;
12376
12377 if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
12378 cur->base.rotation != new->base.rotation ||
Ville Syrjälä936e71e2016-07-26 19:06:59 +030012379 drm_rect_width(&new->base.src) != drm_rect_width(&cur->base.src) ||
12380 drm_rect_height(&new->base.src) != drm_rect_height(&cur->base.src) ||
12381 drm_rect_width(&new->base.dst) != drm_rect_width(&cur->base.dst) ||
12382 drm_rect_height(&new->base.dst) != drm_rect_height(&cur->base.dst))
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012383 return true;
12384
12385 return false;
12386}
12387
Matt Roperd21fbe82015-09-24 15:53:12 -070012388static bool needs_scaling(struct intel_plane_state *state)
12389{
Ville Syrjälä936e71e2016-07-26 19:06:59 +030012390 int src_w = drm_rect_width(&state->base.src) >> 16;
12391 int src_h = drm_rect_height(&state->base.src) >> 16;
12392 int dst_w = drm_rect_width(&state->base.dst);
12393 int dst_h = drm_rect_height(&state->base.dst);
Matt Roperd21fbe82015-09-24 15:53:12 -070012394
12395 return (src_w != dst_w || src_h != dst_h);
12396}
12397
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012398int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
12399 struct drm_plane_state *plane_state)
12400{
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010012401 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012402 struct drm_crtc *crtc = crtc_state->crtc;
12403 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12404 struct drm_plane *plane = plane_state->plane;
12405 struct drm_device *dev = crtc->dev;
Matt Ropered4a6a72016-02-23 17:20:13 -080012406 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012407 struct intel_plane_state *old_plane_state =
12408 to_intel_plane_state(plane->state);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012409 bool mode_changed = needs_modeset(crtc_state);
12410 bool was_crtc_enabled = crtc->state->active;
12411 bool is_crtc_enabled = crtc_state->active;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012412 bool turn_off, turn_on, visible, was_visible;
12413 struct drm_framebuffer *fb = plane_state->fb;
Ville Syrjälä78108b72016-05-27 20:59:19 +030012414 int ret;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012415
Tvrtko Ursulin55b8f2a2016-10-14 09:17:22 +010012416 if (INTEL_GEN(dev_priv) >= 9 && plane->type != DRM_PLANE_TYPE_CURSOR) {
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012417 ret = skl_update_scaler_plane(
12418 to_intel_crtc_state(crtc_state),
12419 to_intel_plane_state(plane_state));
12420 if (ret)
12421 return ret;
12422 }
12423
Ville Syrjälä936e71e2016-07-26 19:06:59 +030012424 was_visible = old_plane_state->base.visible;
12425 visible = to_intel_plane_state(plane_state)->base.visible;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012426
12427 if (!was_crtc_enabled && WARN_ON(was_visible))
12428 was_visible = false;
12429
Maarten Lankhorst35c08f42015-12-03 14:31:07 +010012430 /*
12431 * Visibility is calculated as if the crtc was on, but
12432 * after scaler setup everything depends on it being off
12433 * when the crtc isn't active.
Ville Syrjäläf818ffe2016-04-29 17:31:18 +030012434 *
12435 * FIXME this is wrong for watermarks. Watermarks should also
12436 * be computed as if the pipe would be active. Perhaps move
12437 * per-plane wm computation to the .check_plane() hook, and
12438 * only combine the results from all planes in the current place?
Maarten Lankhorst35c08f42015-12-03 14:31:07 +010012439 */
12440 if (!is_crtc_enabled)
Ville Syrjälä936e71e2016-07-26 19:06:59 +030012441 to_intel_plane_state(plane_state)->base.visible = visible = false;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012442
12443 if (!was_visible && !visible)
12444 return 0;
12445
Maarten Lankhorste8861672016-02-24 11:24:26 +010012446 if (fb != old_plane_state->base.fb)
12447 pipe_config->fb_changed = true;
12448
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012449 turn_off = was_visible && (!visible || mode_changed);
12450 turn_on = visible && (!was_visible || mode_changed);
12451
Ville Syrjälä72660ce2016-05-27 20:59:20 +030012452 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
Ville Syrjälä78108b72016-05-27 20:59:19 +030012453 intel_crtc->base.base.id,
12454 intel_crtc->base.name,
Ville Syrjälä72660ce2016-05-27 20:59:20 +030012455 plane->base.id, plane->name,
12456 fb ? fb->base.id : -1);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012457
Ville Syrjälä72660ce2016-05-27 20:59:20 +030012458 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
12459 plane->base.id, plane->name,
12460 was_visible, visible,
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012461 turn_off, turn_on, mode_changed);
12462
Ville Syrjäläcaed3612016-03-09 19:07:25 +020012463 if (turn_on) {
12464 pipe_config->update_wm_pre = true;
12465
12466 /* must disable cxsr around plane enable/disable */
12467 if (plane->type != DRM_PLANE_TYPE_CURSOR)
12468 pipe_config->disable_cxsr = true;
12469 } else if (turn_off) {
12470 pipe_config->update_wm_post = true;
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010012471
Ville Syrjälä852eb002015-06-24 22:00:07 +030012472 /* must disable cxsr around plane enable/disable */
Maarten Lankhorste8861672016-02-24 11:24:26 +010012473 if (plane->type != DRM_PLANE_TYPE_CURSOR)
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010012474 pipe_config->disable_cxsr = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030012475 } else if (intel_wm_need_update(plane, plane_state)) {
Ville Syrjäläcaed3612016-03-09 19:07:25 +020012476 /* FIXME bollocks */
12477 pipe_config->update_wm_pre = true;
12478 pipe_config->update_wm_post = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030012479 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012480
Matt Ropered4a6a72016-02-23 17:20:13 -080012481 /* Pre-gen9 platforms need two-step watermark updates */
Ville Syrjäläcaed3612016-03-09 19:07:25 +020012482 if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000012483 INTEL_GEN(dev_priv) < 9 && dev_priv->display.optimize_watermarks)
Matt Ropered4a6a72016-02-23 17:20:13 -080012484 to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
12485
Rodrigo Vivi8be6ca82015-08-24 16:38:23 -070012486 if (visible || was_visible)
Maarten Lankhorstcd202f62016-03-09 10:35:44 +010012487 pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030012488
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +010012489 /*
12490 * WaCxSRDisabledForSpriteScaling:ivb
12491 *
12492 * cstate->update_wm was already set above, so this flag will
12493 * take effect when we commit and program watermarks.
12494 */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +010012495 if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev_priv) &&
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +010012496 needs_scaling(to_intel_plane_state(plane_state)) &&
12497 !needs_scaling(old_plane_state))
12498 pipe_config->disable_lp_wm = true;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012499
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012500 return 0;
12501}
12502
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012503static bool encoders_cloneable(const struct intel_encoder *a,
12504 const struct intel_encoder *b)
12505{
12506 /* masks could be asymmetric, so check both ways */
12507 return a == b || (a->cloneable & (1 << b->type) &&
12508 b->cloneable & (1 << a->type));
12509}
12510
12511static bool check_single_encoder_cloning(struct drm_atomic_state *state,
12512 struct intel_crtc *crtc,
12513 struct intel_encoder *encoder)
12514{
12515 struct intel_encoder *source_encoder;
12516 struct drm_connector *connector;
12517 struct drm_connector_state *connector_state;
12518 int i;
12519
12520 for_each_connector_in_state(state, connector, connector_state, i) {
12521 if (connector_state->crtc != &crtc->base)
12522 continue;
12523
12524 source_encoder =
12525 to_intel_encoder(connector_state->best_encoder);
12526 if (!encoders_cloneable(encoder, source_encoder))
12527 return false;
12528 }
12529
12530 return true;
12531}
12532
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012533static int intel_crtc_atomic_check(struct drm_crtc *crtc,
12534 struct drm_crtc_state *crtc_state)
12535{
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020012536 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010012537 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012538 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020012539 struct intel_crtc_state *pipe_config =
12540 to_intel_crtc_state(crtc_state);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012541 struct drm_atomic_state *state = crtc_state->state;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012542 int ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012543 bool mode_changed = needs_modeset(crtc_state);
12544
Ville Syrjälä852eb002015-06-24 22:00:07 +030012545 if (mode_changed && !crtc_state->active)
Ville Syrjäläcaed3612016-03-09 19:07:25 +020012546 pipe_config->update_wm_post = true;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020012547
Maarten Lankhorstad421372015-06-15 12:33:42 +020012548 if (mode_changed && crtc_state->enable &&
12549 dev_priv->display.crtc_compute_clock &&
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012550 !WARN_ON(pipe_config->shared_dpll)) {
Maarten Lankhorstad421372015-06-15 12:33:42 +020012551 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
12552 pipe_config);
12553 if (ret)
12554 return ret;
12555 }
12556
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000012557 if (crtc_state->color_mgmt_changed) {
12558 ret = intel_color_check(crtc, crtc_state);
12559 if (ret)
12560 return ret;
Lionel Landwerline7852a42016-05-25 14:30:41 +010012561
12562 /*
12563 * Changing color management on Intel hardware is
12564 * handled as part of planes update.
12565 */
12566 crtc_state->planes_changed = true;
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000012567 }
12568
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020012569 ret = 0;
Matt Roper86c8bbb2015-09-24 15:53:16 -070012570 if (dev_priv->display.compute_pipe_wm) {
Maarten Lankhorste3bddde2016-03-01 11:07:22 +010012571 ret = dev_priv->display.compute_pipe_wm(pipe_config);
Matt Ropered4a6a72016-02-23 17:20:13 -080012572 if (ret) {
12573 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
Matt Roper86c8bbb2015-09-24 15:53:16 -070012574 return ret;
Matt Ropered4a6a72016-02-23 17:20:13 -080012575 }
12576 }
12577
12578 if (dev_priv->display.compute_intermediate_wm &&
12579 !to_intel_atomic_state(state)->skip_intermediate_wm) {
12580 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
12581 return 0;
12582
12583 /*
12584 * Calculate 'intermediate' watermarks that satisfy both the
12585 * old state and the new state. We can program these
12586 * immediately.
12587 */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000012588 ret = dev_priv->display.compute_intermediate_wm(dev,
Matt Ropered4a6a72016-02-23 17:20:13 -080012589 intel_crtc,
12590 pipe_config);
12591 if (ret) {
12592 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
12593 return ret;
12594 }
Ville Syrjäläe3d54572016-05-13 10:10:42 -070012595 } else if (dev_priv->display.compute_intermediate_wm) {
12596 if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
12597 pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
Matt Roper86c8bbb2015-09-24 15:53:16 -070012598 }
12599
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000012600 if (INTEL_GEN(dev_priv) >= 9) {
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020012601 if (mode_changed)
12602 ret = skl_update_scaler_crtc(pipe_config);
12603
12604 if (!ret)
12605 ret = intel_atomic_setup_scalers(dev, intel_crtc,
12606 pipe_config);
12607 }
12608
12609 return ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012610}
12611
Jani Nikula65b38e02015-04-13 11:26:56 +030012612static const struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012613 .mode_set_base_atomic = intel_pipe_set_base_atomic,
Daniel Vetter5a21b662016-05-24 17:13:53 +020012614 .atomic_begin = intel_begin_crtc_commit,
12615 .atomic_flush = intel_finish_crtc_commit,
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012616 .atomic_check = intel_crtc_atomic_check,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012617};
12618
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020012619static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
12620{
12621 struct intel_connector *connector;
12622
12623 for_each_intel_connector(dev, connector) {
Daniel Vetter8863dc72016-05-06 15:39:03 +020012624 if (connector->base.state->crtc)
12625 drm_connector_unreference(&connector->base);
12626
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020012627 if (connector->base.encoder) {
12628 connector->base.state->best_encoder =
12629 connector->base.encoder;
12630 connector->base.state->crtc =
12631 connector->base.encoder->crtc;
Daniel Vetter8863dc72016-05-06 15:39:03 +020012632
12633 drm_connector_reference(&connector->base);
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020012634 } else {
12635 connector->base.state->best_encoder = NULL;
12636 connector->base.state->crtc = NULL;
12637 }
12638 }
12639}
12640
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012641static void
Robin Schroereba905b2014-05-18 02:24:50 +020012642connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012643 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012644{
Ville Syrjälä6a2a5c52016-09-28 16:51:42 +030012645 const struct drm_display_info *info = &connector->base.display_info;
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012646 int bpp = pipe_config->pipe_bpp;
12647
12648 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
Ville Syrjälä6a2a5c52016-09-28 16:51:42 +030012649 connector->base.base.id,
12650 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012651
12652 /* Don't use an invalid EDID bpc value */
Ville Syrjälä6a2a5c52016-09-28 16:51:42 +030012653 if (info->bpc != 0 && info->bpc * 3 < bpp) {
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012654 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
Ville Syrjälä6a2a5c52016-09-28 16:51:42 +030012655 bpp, info->bpc * 3);
12656 pipe_config->pipe_bpp = info->bpc * 3;
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012657 }
12658
Mario Kleiner196f9542016-07-06 12:05:45 +020012659 /* Clamp bpp to 8 on screens without EDID 1.4 */
Ville Syrjälä6a2a5c52016-09-28 16:51:42 +030012660 if (info->bpc == 0 && bpp > 24) {
Mario Kleiner196f9542016-07-06 12:05:45 +020012661 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
12662 bpp);
12663 pipe_config->pipe_bpp = 24;
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012664 }
12665}
12666
12667static int
12668compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012669 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012670{
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010012671 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012672 struct drm_atomic_state *state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012673 struct drm_connector *connector;
12674 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012675 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012676
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010012677 if ((IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
12678 IS_CHERRYVIEW(dev_priv)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012679 bpp = 10*3;
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010012680 else if (INTEL_GEN(dev_priv) >= 5)
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012681 bpp = 12*3;
12682 else
12683 bpp = 8*3;
12684
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012685
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012686 pipe_config->pipe_bpp = bpp;
12687
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012688 state = pipe_config->base.state;
12689
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012690 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012691 for_each_connector_in_state(state, connector, connector_state, i) {
12692 if (connector_state->crtc != &crtc->base)
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012693 continue;
12694
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012695 connected_sink_compute_bpp(to_intel_connector(connector),
12696 pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012697 }
12698
12699 return bpp;
12700}
12701
Daniel Vetter644db712013-09-19 14:53:58 +020012702static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
12703{
12704 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
12705 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010012706 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020012707 mode->crtc_hdisplay, mode->crtc_hsync_start,
12708 mode->crtc_hsync_end, mode->crtc_htotal,
12709 mode->crtc_vdisplay, mode->crtc_vsync_start,
12710 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
12711}
12712
Tvrtko Ursulinf6982332016-11-17 12:30:08 +000012713static inline void
12714intel_dump_m_n_config(struct intel_crtc_state *pipe_config, char *id,
Tvrtko Ursulina4309652016-11-17 12:30:09 +000012715 unsigned int lane_count, struct intel_link_m_n *m_n)
Tvrtko Ursulinf6982332016-11-17 12:30:08 +000012716{
Tvrtko Ursulina4309652016-11-17 12:30:09 +000012717 DRM_DEBUG_KMS("%s: lanes: %i; gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
12718 id, lane_count,
Tvrtko Ursulinf6982332016-11-17 12:30:08 +000012719 m_n->gmch_m, m_n->gmch_n,
12720 m_n->link_m, m_n->link_n, m_n->tu);
12721}
12722
Daniel Vetterc0b03412013-05-28 12:05:54 +020012723static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012724 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012725 const char *context)
12726{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012727 struct drm_device *dev = crtc->base.dev;
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010012728 struct drm_i915_private *dev_priv = to_i915(dev);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012729 struct drm_plane *plane;
12730 struct intel_plane *intel_plane;
12731 struct intel_plane_state *state;
12732 struct drm_framebuffer *fb;
12733
Tvrtko Ursulin66766e42016-11-17 12:30:10 +000012734 DRM_DEBUG_KMS("[CRTC:%d:%s]%s\n",
12735 crtc->base.base.id, crtc->base.name, context);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012736
Tvrtko Ursulin2c894292016-11-17 12:30:11 +000012737 DRM_DEBUG_KMS("cpu_transcoder: %s, pipe bpp: %i, dithering: %i\n",
12738 transcoder_name(pipe_config->cpu_transcoder),
Daniel Vetterc0b03412013-05-28 12:05:54 +020012739 pipe_config->pipe_bpp, pipe_config->dither);
Tvrtko Ursulina4309652016-11-17 12:30:09 +000012740
12741 if (pipe_config->has_pch_encoder)
12742 intel_dump_m_n_config(pipe_config, "fdi",
12743 pipe_config->fdi_lanes,
12744 &pipe_config->fdi_m_n);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012745
Tvrtko Ursulinf6982332016-11-17 12:30:08 +000012746 if (intel_crtc_has_dp_encoder(pipe_config)) {
Tvrtko Ursulina4309652016-11-17 12:30:09 +000012747 intel_dump_m_n_config(pipe_config, "dp m_n",
12748 pipe_config->lane_count, &pipe_config->dp_m_n);
Tvrtko Ursulind806e682016-11-17 15:44:09 +000012749 if (pipe_config->has_drrs)
12750 intel_dump_m_n_config(pipe_config, "dp m2_n2",
12751 pipe_config->lane_count,
12752 &pipe_config->dp_m2_n2);
Tvrtko Ursulinf6982332016-11-17 12:30:08 +000012753 }
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012754
Daniel Vetter55072d12014-11-20 16:10:28 +010012755 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
Tvrtko Ursulin2c894292016-11-17 12:30:11 +000012756 pipe_config->has_audio, pipe_config->has_infoframe);
Daniel Vetter55072d12014-11-20 16:10:28 +010012757
Daniel Vetterc0b03412013-05-28 12:05:54 +020012758 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012759 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012760 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012761 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
12762 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Tvrtko Ursulin2c894292016-11-17 12:30:11 +000012763 DRM_DEBUG_KMS("port clock: %d, pipe src size: %dx%d\n",
12764 pipe_config->port_clock,
Ville Syrjälä37327ab2013-09-04 18:25:28 +030012765 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Tvrtko Ursulindd2f6162016-11-17 12:30:12 +000012766
12767 if (INTEL_GEN(dev_priv) >= 9)
12768 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
12769 crtc->num_scalers,
12770 pipe_config->scaler_state.scaler_users,
12771 pipe_config->scaler_state.scaler_id);
Tvrtko Ursulina74f8372016-11-17 12:30:13 +000012772
12773 if (HAS_GMCH_DISPLAY(dev_priv))
12774 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
12775 pipe_config->gmch_pfit.control,
12776 pipe_config->gmch_pfit.pgm_ratios,
12777 pipe_config->gmch_pfit.lvds_border_bits);
12778 else
12779 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
12780 pipe_config->pch_pfit.pos,
12781 pipe_config->pch_pfit.size,
Tvrtko Ursulin08c4d7f2016-11-17 12:30:14 +000012782 enableddisabled(pipe_config->pch_pfit.enabled));
Tvrtko Ursulina74f8372016-11-17 12:30:13 +000012783
Tvrtko Ursulin2c894292016-11-17 12:30:11 +000012784 DRM_DEBUG_KMS("ips: %i, double wide: %i\n",
12785 pipe_config->ips_enabled, pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012786
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +010012787 if (IS_BROXTON(dev_priv)) {
Ander Conselvan de Oliveirac8560522016-09-01 15:08:07 -070012788 DRM_DEBUG_KMS("dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012789 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
Imre Deakc8453332015-06-18 17:25:55 +030012790 "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012791 pipe_config->dpll_hw_state.ebb0,
Imre Deak05712c12015-06-18 17:25:54 +030012792 pipe_config->dpll_hw_state.ebb4,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012793 pipe_config->dpll_hw_state.pll0,
12794 pipe_config->dpll_hw_state.pll1,
12795 pipe_config->dpll_hw_state.pll2,
12796 pipe_config->dpll_hw_state.pll3,
12797 pipe_config->dpll_hw_state.pll6,
12798 pipe_config->dpll_hw_state.pll8,
Imre Deak05712c12015-06-18 17:25:54 +030012799 pipe_config->dpll_hw_state.pll9,
Imre Deakc8453332015-06-18 17:25:55 +030012800 pipe_config->dpll_hw_state.pll10,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012801 pipe_config->dpll_hw_state.pcsdw12);
Tvrtko Ursulin08537232016-10-13 11:03:02 +010012802 } else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
Ander Conselvan de Oliveirac8560522016-09-01 15:08:07 -070012803 DRM_DEBUG_KMS("dpll_hw_state: "
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012804 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012805 pipe_config->dpll_hw_state.ctrl1,
12806 pipe_config->dpll_hw_state.cfgcr1,
12807 pipe_config->dpll_hw_state.cfgcr2);
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010012808 } else if (HAS_DDI(dev_priv)) {
Ander Conselvan de Oliveirac8560522016-09-01 15:08:07 -070012809 DRM_DEBUG_KMS("dpll_hw_state: wrpll: 0x%x spll: 0x%x\n",
Maarten Lankhorst00490c22015-11-16 14:42:12 +010012810 pipe_config->dpll_hw_state.wrpll,
12811 pipe_config->dpll_hw_state.spll);
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012812 } else {
12813 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
12814 "fp0: 0x%x, fp1: 0x%x\n",
12815 pipe_config->dpll_hw_state.dpll,
12816 pipe_config->dpll_hw_state.dpll_md,
12817 pipe_config->dpll_hw_state.fp0,
12818 pipe_config->dpll_hw_state.fp1);
12819 }
12820
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012821 DRM_DEBUG_KMS("planes on this crtc\n");
12822 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
Eric Engestromb3c11ac2016-11-12 01:12:56 +000012823 struct drm_format_name_buf format_name;
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012824 intel_plane = to_intel_plane(plane);
12825 if (intel_plane->pipe != crtc->pipe)
12826 continue;
12827
12828 state = to_intel_plane_state(plane->state);
12829 fb = state->base.fb;
12830 if (!fb) {
Ville Syrjälä1d577e02016-05-27 20:59:25 +030012831 DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
12832 plane->base.id, plane->name, state->scaler_id);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012833 continue;
12834 }
12835
Tvrtko Ursulindd2f6162016-11-17 12:30:12 +000012836 DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d, fb = %ux%u format = %s\n",
12837 plane->base.id, plane->name,
Eric Engestromb3c11ac2016-11-12 01:12:56 +000012838 fb->base.id, fb->width, fb->height,
12839 drm_get_format_name(fb->pixel_format, &format_name));
Tvrtko Ursulindd2f6162016-11-17 12:30:12 +000012840 if (INTEL_GEN(dev_priv) >= 9)
12841 DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
12842 state->scaler_id,
12843 state->base.src.x1 >> 16,
12844 state->base.src.y1 >> 16,
12845 drm_rect_width(&state->base.src) >> 16,
12846 drm_rect_height(&state->base.src) >> 16,
12847 state->base.dst.x1, state->base.dst.y1,
12848 drm_rect_width(&state->base.dst),
12849 drm_rect_height(&state->base.dst));
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012850 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020012851}
12852
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012853static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012854{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012855 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012856 struct drm_connector *connector;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012857 unsigned int used_ports = 0;
Ville Syrjälä477321e2016-07-28 17:50:40 +030012858 unsigned int used_mst_ports = 0;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012859
12860 /*
12861 * Walk the connector list instead of the encoder
12862 * list to detect the problem on ddi platforms
12863 * where there's just one encoder per digital port.
12864 */
Ville Syrjälä0bff4852015-12-10 18:22:31 +020012865 drm_for_each_connector(connector, dev) {
12866 struct drm_connector_state *connector_state;
12867 struct intel_encoder *encoder;
12868
12869 connector_state = drm_atomic_get_existing_connector_state(state, connector);
12870 if (!connector_state)
12871 connector_state = connector->state;
12872
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012873 if (!connector_state->best_encoder)
12874 continue;
12875
12876 encoder = to_intel_encoder(connector_state->best_encoder);
12877
12878 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012879
12880 switch (encoder->type) {
12881 unsigned int port_mask;
12882 case INTEL_OUTPUT_UNKNOWN:
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010012883 if (WARN_ON(!HAS_DDI(to_i915(dev))))
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012884 break;
Ville Syrjäläcca05022016-06-22 21:57:06 +030012885 case INTEL_OUTPUT_DP:
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012886 case INTEL_OUTPUT_HDMI:
12887 case INTEL_OUTPUT_EDP:
12888 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12889
12890 /* the same port mustn't appear more than once */
12891 if (used_ports & port_mask)
12892 return false;
12893
12894 used_ports |= port_mask;
Ville Syrjälä477321e2016-07-28 17:50:40 +030012895 break;
12896 case INTEL_OUTPUT_DP_MST:
12897 used_mst_ports |=
12898 1 << enc_to_mst(&encoder->base)->primary->port;
12899 break;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012900 default:
12901 break;
12902 }
12903 }
12904
Ville Syrjälä477321e2016-07-28 17:50:40 +030012905 /* can't mix MST and SST/HDMI on the same port */
12906 if (used_ports & used_mst_ports)
12907 return false;
12908
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012909 return true;
12910}
12911
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012912static void
12913clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12914{
12915 struct drm_crtc_state tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012916 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012917 struct intel_dpll_hw_state dpll_hw_state;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012918 struct intel_shared_dpll *shared_dpll;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012919 bool force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012920
Ander Conselvan de Oliveira7546a382015-05-20 09:03:27 +030012921 /* FIXME: before the switch to atomic started, a new pipe_config was
12922 * kzalloc'd. Code that depends on any field being zero should be
12923 * fixed, so that the crtc_state can be safely duplicated. For now,
12924 * only fields that are know to not cause problems are preserved. */
12925
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012926 tmp_state = crtc_state->base;
Chandra Konduru663a3642015-04-07 15:28:41 -070012927 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012928 shared_dpll = crtc_state->shared_dpll;
12929 dpll_hw_state = crtc_state->dpll_hw_state;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012930 force_thru = crtc_state->pch_pfit.force_thru;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012931
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012932 memset(crtc_state, 0, sizeof *crtc_state);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012933
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012934 crtc_state->base = tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012935 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012936 crtc_state->shared_dpll = shared_dpll;
12937 crtc_state->dpll_hw_state = dpll_hw_state;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012938 crtc_state->pch_pfit.force_thru = force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012939}
12940
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012941static int
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012942intel_modeset_pipe_config(struct drm_crtc *crtc,
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012943 struct intel_crtc_state *pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +020012944{
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012945 struct drm_atomic_state *state = pipe_config->base.state;
Daniel Vetter7758a112012-07-08 19:40:39 +020012946 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012947 struct drm_connector *connector;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012948 struct drm_connector_state *connector_state;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012949 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012950 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010012951 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020012952
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012953 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020012954
Daniel Vettere143a212013-07-04 12:01:15 +020012955 pipe_config->cpu_transcoder =
12956 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012957
Imre Deak2960bc92013-07-30 13:36:32 +030012958 /*
12959 * Sanitize sync polarity flags based on requested ones. If neither
12960 * positive or negative polarity is requested, treat this as meaning
12961 * negative polarity.
12962 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012963 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012964 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012965 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012966
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012967 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012968 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012969 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012970
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012971 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12972 pipe_config);
12973 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012974 goto fail;
12975
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012976 /*
12977 * Determine the real pipe dimensions. Note that stereo modes can
12978 * increase the actual pipe size due to the frame doubling and
12979 * insertion of additional space for blanks between the frame. This
12980 * is stored in the crtc timings. We use the requested mode to do this
12981 * computation to clearly distinguish it from the adjusted mode, which
12982 * can be changed by the connectors in the below retry loop.
12983 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012984 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080012985 &pipe_config->pipe_src_w,
12986 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012987
Ville Syrjälä253c84c2016-06-22 21:57:01 +030012988 for_each_connector_in_state(state, connector, connector_state, i) {
12989 if (connector_state->crtc != crtc)
12990 continue;
12991
12992 encoder = to_intel_encoder(connector_state->best_encoder);
12993
Ville Syrjäläe25148d2016-06-22 21:57:09 +030012994 if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
12995 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
12996 goto fail;
12997 }
12998
Ville Syrjälä253c84c2016-06-22 21:57:01 +030012999 /*
13000 * Determine output_types before calling the .compute_config()
13001 * hooks so that the hooks can use this information safely.
13002 */
13003 pipe_config->output_types |= 1 << encoder->type;
13004 }
13005
Daniel Vettere29c22c2013-02-21 00:00:16 +010013006encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020013007 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020013008 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020013009 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020013010
Daniel Vetter135c81b2013-07-21 21:37:09 +020013011 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020013012 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
13013 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020013014
Daniel Vetter7758a112012-07-08 19:40:39 +020013015 /* Pass our mode to the connectors and the CRTC to give them a chance to
13016 * adjust it according to limitations or connector properties, and also
13017 * a chance to reject the mode entirely.
13018 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030013019 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020013020 if (connector_state->crtc != crtc)
13021 continue;
13022
13023 encoder = to_intel_encoder(connector_state->best_encoder);
13024
Maarten Lankhorst0a478c22016-08-09 17:04:05 +020013025 if (!(encoder->compute_config(encoder, pipe_config, connector_state))) {
Daniel Vetterefea6e82013-07-21 21:36:59 +020013026 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020013027 goto fail;
13028 }
13029 }
13030
Daniel Vetterff9a6752013-06-01 17:16:21 +020013031 /* Set default port clock if not overwritten by the encoder. Needs to be
13032 * done afterwards in case the encoder adjusts the mode. */
13033 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020013034 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010013035 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020013036
Daniel Vettera43f6e02013-06-07 23:10:32 +020013037 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010013038 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020013039 DRM_DEBUG_KMS("CRTC fixup failed\n");
13040 goto fail;
13041 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010013042
13043 if (ret == RETRY) {
13044 if (WARN(!retry, "loop in pipe configuration computation\n")) {
13045 ret = -EINVAL;
13046 goto fail;
13047 }
13048
13049 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
13050 retry = false;
13051 goto encoder_retry;
13052 }
13053
Daniel Vettere8fa4272015-08-12 11:43:34 +020013054 /* Dithering seems to not pass-through bits correctly when it should, so
13055 * only enable it on 6bpc panels. */
13056 pipe_config->dither = pipe_config->pipe_bpp == 6*3;
Daniel Vetter62f0ace2015-08-26 18:57:26 +020013057 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020013058 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010013059
Daniel Vetter7758a112012-07-08 19:40:39 +020013060fail:
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030013061 return ret;
Daniel Vetter7758a112012-07-08 19:40:39 +020013062}
13063
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013064static void
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013065intel_modeset_update_crtc_state(struct drm_atomic_state *state)
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013066{
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013067 struct drm_crtc *crtc;
13068 struct drm_crtc_state *crtc_state;
Maarten Lankhorst8a75d157c2015-07-13 16:30:14 +020013069 int i;
Daniel Vetterea9d7582012-07-10 10:42:52 +020013070
Ville Syrjälä76688512014-01-10 11:28:06 +020013071 /* Double check state. */
Maarten Lankhorst8a75d157c2015-07-13 16:30:14 +020013072 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst3cb480b2015-06-01 12:49:49 +020013073 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
Maarten Lankhorstfc467a222015-06-01 12:50:07 +020013074
13075 /* Update hwmode for vblank functions */
13076 if (crtc->state->active)
13077 crtc->hwmode = crtc->state->adjusted_mode;
13078 else
13079 crtc->hwmode.crtc_clock = 0;
Maarten Lankhorst61067a52015-09-23 16:29:36 +020013080
13081 /*
13082 * Update legacy state to satisfy fbc code. This can
13083 * be removed when fbc uses the atomic state.
13084 */
13085 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
13086 struct drm_plane_state *plane_state = crtc->primary->state;
13087
13088 crtc->primary->fb = plane_state->fb;
13089 crtc->x = plane_state->src_x >> 16;
13090 crtc->y = plane_state->src_y >> 16;
13091 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020013092 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020013093}
13094
Ville Syrjälä3bd26262013-09-06 23:29:02 +030013095static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030013096{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030013097 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030013098
13099 if (clock1 == clock2)
13100 return true;
13101
13102 if (!clock1 || !clock2)
13103 return false;
13104
13105 diff = abs(clock1 - clock2);
13106
13107 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
13108 return true;
13109
13110 return false;
13111}
13112
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013113static bool
13114intel_compare_m_n(unsigned int m, unsigned int n,
13115 unsigned int m2, unsigned int n2,
13116 bool exact)
13117{
13118 if (m == m2 && n == n2)
13119 return true;
13120
13121 if (exact || !m || !n || !m2 || !n2)
13122 return false;
13123
13124 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
13125
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010013126 if (n > n2) {
13127 while (n > n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013128 m2 <<= 1;
13129 n2 <<= 1;
13130 }
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010013131 } else if (n < n2) {
13132 while (n < n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013133 m <<= 1;
13134 n <<= 1;
13135 }
13136 }
13137
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010013138 if (n != n2)
13139 return false;
13140
13141 return intel_fuzzy_clock_check(m, m2);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013142}
13143
13144static bool
13145intel_compare_link_m_n(const struct intel_link_m_n *m_n,
13146 struct intel_link_m_n *m2_n2,
13147 bool adjust)
13148{
13149 if (m_n->tu == m2_n2->tu &&
13150 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
13151 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
13152 intel_compare_m_n(m_n->link_m, m_n->link_n,
13153 m2_n2->link_m, m2_n2->link_n, !adjust)) {
13154 if (adjust)
13155 *m2_n2 = *m_n;
13156
13157 return true;
13158 }
13159
13160 return false;
13161}
13162
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010013163static bool
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000013164intel_pipe_config_compare(struct drm_i915_private *dev_priv,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020013165 struct intel_crtc_state *current_config,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013166 struct intel_crtc_state *pipe_config,
13167 bool adjust)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010013168{
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013169 bool ret = true;
13170
13171#define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
13172 do { \
13173 if (!adjust) \
13174 DRM_ERROR(fmt, ##__VA_ARGS__); \
13175 else \
13176 DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
13177 } while (0)
13178
Daniel Vetter66e985c2013-06-05 13:34:20 +020013179#define PIPE_CONF_CHECK_X(name) \
13180 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013181 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter66e985c2013-06-05 13:34:20 +020013182 "(expected 0x%08x, found 0x%08x)\n", \
13183 current_config->name, \
13184 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013185 ret = false; \
Daniel Vetter66e985c2013-06-05 13:34:20 +020013186 }
13187
Daniel Vetter08a24032013-04-19 11:25:34 +020013188#define PIPE_CONF_CHECK_I(name) \
13189 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013190 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter08a24032013-04-19 11:25:34 +020013191 "(expected %i, found %i)\n", \
13192 current_config->name, \
13193 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013194 ret = false; \
13195 }
13196
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013197#define PIPE_CONF_CHECK_P(name) \
13198 if (current_config->name != pipe_config->name) { \
13199 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
13200 "(expected %p, found %p)\n", \
13201 current_config->name, \
13202 pipe_config->name); \
13203 ret = false; \
13204 }
13205
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013206#define PIPE_CONF_CHECK_M_N(name) \
13207 if (!intel_compare_link_m_n(&current_config->name, \
13208 &pipe_config->name,\
13209 adjust)) { \
13210 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
13211 "(expected tu %i gmch %i/%i link %i/%i, " \
13212 "found tu %i, gmch %i/%i link %i/%i)\n", \
13213 current_config->name.tu, \
13214 current_config->name.gmch_m, \
13215 current_config->name.gmch_n, \
13216 current_config->name.link_m, \
13217 current_config->name.link_n, \
13218 pipe_config->name.tu, \
13219 pipe_config->name.gmch_m, \
13220 pipe_config->name.gmch_n, \
13221 pipe_config->name.link_m, \
13222 pipe_config->name.link_n); \
13223 ret = false; \
13224 }
13225
Daniel Vetter55c561a2016-03-30 11:34:36 +020013226/* This is required for BDW+ where there is only one set of registers for
13227 * switching between high and low RR.
13228 * This macro can be used whenever a comparison has to be made between one
13229 * hw state and multiple sw state variables.
13230 */
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013231#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
13232 if (!intel_compare_link_m_n(&current_config->name, \
13233 &pipe_config->name, adjust) && \
13234 !intel_compare_link_m_n(&current_config->alt_name, \
13235 &pipe_config->name, adjust)) { \
13236 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
13237 "(expected tu %i gmch %i/%i link %i/%i, " \
13238 "or tu %i gmch %i/%i link %i/%i, " \
13239 "found tu %i, gmch %i/%i link %i/%i)\n", \
13240 current_config->name.tu, \
13241 current_config->name.gmch_m, \
13242 current_config->name.gmch_n, \
13243 current_config->name.link_m, \
13244 current_config->name.link_n, \
13245 current_config->alt_name.tu, \
13246 current_config->alt_name.gmch_m, \
13247 current_config->alt_name.gmch_n, \
13248 current_config->alt_name.link_m, \
13249 current_config->alt_name.link_n, \
13250 pipe_config->name.tu, \
13251 pipe_config->name.gmch_m, \
13252 pipe_config->name.gmch_n, \
13253 pipe_config->name.link_m, \
13254 pipe_config->name.link_n); \
13255 ret = false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010013256 }
13257
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020013258#define PIPE_CONF_CHECK_FLAGS(name, mask) \
13259 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013260 INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020013261 "(expected %i, found %i)\n", \
13262 current_config->name & (mask), \
13263 pipe_config->name & (mask)); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013264 ret = false; \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020013265 }
13266
Ville Syrjälä5e550652013-09-06 23:29:07 +030013267#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
13268 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013269 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Ville Syrjälä5e550652013-09-06 23:29:07 +030013270 "(expected %i, found %i)\n", \
13271 current_config->name, \
13272 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013273 ret = false; \
Ville Syrjälä5e550652013-09-06 23:29:07 +030013274 }
13275
Daniel Vetterbb760062013-06-06 14:55:52 +020013276#define PIPE_CONF_QUIRK(quirk) \
13277 ((current_config->quirks | pipe_config->quirks) & (quirk))
13278
Daniel Vettereccb1402013-05-22 00:50:22 +020013279 PIPE_CONF_CHECK_I(cpu_transcoder);
13280
Daniel Vetter08a24032013-04-19 11:25:34 +020013281 PIPE_CONF_CHECK_I(has_pch_encoder);
13282 PIPE_CONF_CHECK_I(fdi_lanes);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013283 PIPE_CONF_CHECK_M_N(fdi_m_n);
Daniel Vetter08a24032013-04-19 11:25:34 +020013284
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030013285 PIPE_CONF_CHECK_I(lane_count);
Imre Deak95a7a2a2016-06-13 16:44:35 +030013286 PIPE_CONF_CHECK_X(lane_lat_optim_mask);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070013287
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000013288 if (INTEL_GEN(dev_priv) < 8) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013289 PIPE_CONF_CHECK_M_N(dp_m_n);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070013290
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013291 if (current_config->has_drrs)
13292 PIPE_CONF_CHECK_M_N(dp_m2_n2);
13293 } else
13294 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030013295
Ville Syrjälä253c84c2016-06-22 21:57:01 +030013296 PIPE_CONF_CHECK_X(output_types);
Jani Nikulaa65347b2015-11-27 12:21:46 +020013297
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020013298 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
13299 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
13300 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
13301 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
13302 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
13303 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020013304
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020013305 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
13306 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
13307 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
13308 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
13309 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
13310 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020013311
Daniel Vetterc93f54c2013-06-27 19:47:19 +020013312 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b52014-04-24 23:54:47 +020013313 PIPE_CONF_CHECK_I(has_hdmi_sink);
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +010013314 if ((INTEL_GEN(dev_priv) < 8 && !IS_HASWELL(dev_priv)) ||
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010013315 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020013316 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080013317 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020013318
Daniel Vetter9ed109a2014-04-24 23:54:52 +020013319 PIPE_CONF_CHECK_I(has_audio);
13320
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020013321 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020013322 DRM_MODE_FLAG_INTERLACE);
13323
Daniel Vetterbb760062013-06-06 14:55:52 +020013324 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020013325 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020013326 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020013327 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020013328 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020013329 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020013330 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020013331 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020013332 DRM_MODE_FLAG_NVSYNC);
13333 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070013334
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030013335 PIPE_CONF_CHECK_X(gmch_pfit.control);
Daniel Vettere2ff2d42015-07-15 14:15:50 +020013336 /* pfit ratios are autocomputed by the hw on gen4+ */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000013337 if (INTEL_GEN(dev_priv) < 4)
Ville Syrjälä7f7d8dd2016-03-15 16:40:07 +020013338 PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030013339 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
Daniel Vetter99535992014-04-13 12:00:33 +020013340
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013341 if (!adjust) {
13342 PIPE_CONF_CHECK_I(pipe_src_w);
13343 PIPE_CONF_CHECK_I(pipe_src_h);
13344
13345 PIPE_CONF_CHECK_I(pch_pfit.enabled);
13346 if (current_config->pch_pfit.enabled) {
13347 PIPE_CONF_CHECK_X(pch_pfit.pos);
13348 PIPE_CONF_CHECK_X(pch_pfit.size);
13349 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020013350
Maarten Lankhorst7aefe2b2015-09-14 11:30:10 +020013351 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
13352 }
Chandra Kondurua1b22782015-04-07 15:28:45 -070013353
Jesse Barnese59150d2014-01-07 13:30:45 -080013354 /* BDW+ don't expose a synchronous way to read the state */
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +010013355 if (IS_HASWELL(dev_priv))
Jesse Barnese59150d2014-01-07 13:30:45 -080013356 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030013357
Ville Syrjälä282740f2013-09-04 18:30:03 +030013358 PIPE_CONF_CHECK_I(double_wide);
13359
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013360 PIPE_CONF_CHECK_P(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020013361 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020013362 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020013363 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
13364 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030013365 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Maarten Lankhorst00490c22015-11-16 14:42:12 +010013366 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000013367 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
13368 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
13369 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020013370
Ville Syrjälä47eacba2016-04-12 22:14:35 +030013371 PIPE_CONF_CHECK_X(dsi_pll.ctrl);
13372 PIPE_CONF_CHECK_X(dsi_pll.div);
13373
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010013374 if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5)
Ville Syrjälä42571ae2013-09-06 23:29:00 +030013375 PIPE_CONF_CHECK_I(pipe_bpp);
13376
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020013377 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080013378 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030013379
Daniel Vetter66e985c2013-06-05 13:34:20 +020013380#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020013381#undef PIPE_CONF_CHECK_I
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013382#undef PIPE_CONF_CHECK_P
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020013383#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030013384#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020013385#undef PIPE_CONF_QUIRK
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013386#undef INTEL_ERR_OR_DBG_KMS
Daniel Vetter627eb5a2013-04-29 19:33:42 +020013387
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013388 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010013389}
13390
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020013391static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
13392 const struct intel_crtc_state *pipe_config)
13393{
13394 if (pipe_config->has_pch_encoder) {
Ville Syrjälä21a727b2016-02-17 21:41:10 +020013395 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020013396 &pipe_config->fdi_m_n);
13397 int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
13398
13399 /*
13400 * FDI already provided one idea for the dotclock.
13401 * Yell if the encoder disagrees.
13402 */
13403 WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
13404 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
13405 fdi_dotclock, dotclock);
13406 }
13407}
13408
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013409static void verify_wm_state(struct drm_crtc *crtc,
13410 struct drm_crtc_state *new_state)
Damien Lespiau08db6652014-11-04 17:06:52 +000013411{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000013412 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Damien Lespiau08db6652014-11-04 17:06:52 +000013413 struct skl_ddb_allocation hw_ddb, *sw_ddb;
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040013414 struct skl_pipe_wm hw_wm, *sw_wm;
13415 struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
13416 struct skl_ddb_entry *hw_ddb_entry, *sw_ddb_entry;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013417 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13418 const enum pipe pipe = intel_crtc->pipe;
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040013419 int plane, level, max_level = ilk_wm_max_level(dev_priv);
Damien Lespiau08db6652014-11-04 17:06:52 +000013420
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000013421 if (INTEL_GEN(dev_priv) < 9 || !new_state->active)
Damien Lespiau08db6652014-11-04 17:06:52 +000013422 return;
13423
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040013424 skl_pipe_wm_get_hw_state(crtc, &hw_wm);
Maarten Lankhorst03af79e2016-10-26 15:41:36 +020013425 sw_wm = &to_intel_crtc_state(new_state)->wm.skl.optimal;
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040013426
Damien Lespiau08db6652014-11-04 17:06:52 +000013427 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
13428 sw_ddb = &dev_priv->wm.skl_hw.ddb;
13429
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013430 /* planes */
Matt Roper8b364b42016-10-26 15:51:28 -070013431 for_each_universal_plane(dev_priv, pipe, plane) {
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040013432 hw_plane_wm = &hw_wm.planes[plane];
13433 sw_plane_wm = &sw_wm->planes[plane];
Damien Lespiau08db6652014-11-04 17:06:52 +000013434
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040013435 /* Watermarks */
13436 for (level = 0; level <= max_level; level++) {
13437 if (skl_wm_level_equals(&hw_plane_wm->wm[level],
13438 &sw_plane_wm->wm[level]))
13439 continue;
Damien Lespiau08db6652014-11-04 17:06:52 +000013440
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040013441 DRM_ERROR("mismatch in WM pipe %c plane %d level %d (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
13442 pipe_name(pipe), plane + 1, level,
13443 sw_plane_wm->wm[level].plane_en,
13444 sw_plane_wm->wm[level].plane_res_b,
13445 sw_plane_wm->wm[level].plane_res_l,
13446 hw_plane_wm->wm[level].plane_en,
13447 hw_plane_wm->wm[level].plane_res_b,
13448 hw_plane_wm->wm[level].plane_res_l);
13449 }
13450
13451 if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
13452 &sw_plane_wm->trans_wm)) {
13453 DRM_ERROR("mismatch in trans WM pipe %c plane %d (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
13454 pipe_name(pipe), plane + 1,
13455 sw_plane_wm->trans_wm.plane_en,
13456 sw_plane_wm->trans_wm.plane_res_b,
13457 sw_plane_wm->trans_wm.plane_res_l,
13458 hw_plane_wm->trans_wm.plane_en,
13459 hw_plane_wm->trans_wm.plane_res_b,
13460 hw_plane_wm->trans_wm.plane_res_l);
13461 }
13462
13463 /* DDB */
13464 hw_ddb_entry = &hw_ddb.plane[pipe][plane];
13465 sw_ddb_entry = &sw_ddb->plane[pipe][plane];
13466
13467 if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
cpaul@redhat.comfaccd992016-10-14 17:31:58 -040013468 DRM_ERROR("mismatch in DDB state pipe %c plane %d (expected (%u,%u), found (%u,%u))\n",
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040013469 pipe_name(pipe), plane + 1,
13470 sw_ddb_entry->start, sw_ddb_entry->end,
13471 hw_ddb_entry->start, hw_ddb_entry->end);
13472 }
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013473 }
13474
Lyude27082492016-08-24 07:48:10 +020013475 /*
13476 * cursor
13477 * If the cursor plane isn't active, we may not have updated it's ddb
13478 * allocation. In that case since the ddb allocation will be updated
13479 * once the plane becomes visible, we can skip this check
13480 */
13481 if (intel_crtc->cursor_addr) {
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040013482 hw_plane_wm = &hw_wm.planes[PLANE_CURSOR];
13483 sw_plane_wm = &sw_wm->planes[PLANE_CURSOR];
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013484
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040013485 /* Watermarks */
13486 for (level = 0; level <= max_level; level++) {
13487 if (skl_wm_level_equals(&hw_plane_wm->wm[level],
13488 &sw_plane_wm->wm[level]))
13489 continue;
13490
13491 DRM_ERROR("mismatch in WM pipe %c cursor level %d (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
13492 pipe_name(pipe), level,
13493 sw_plane_wm->wm[level].plane_en,
13494 sw_plane_wm->wm[level].plane_res_b,
13495 sw_plane_wm->wm[level].plane_res_l,
13496 hw_plane_wm->wm[level].plane_en,
13497 hw_plane_wm->wm[level].plane_res_b,
13498 hw_plane_wm->wm[level].plane_res_l);
13499 }
13500
13501 if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
13502 &sw_plane_wm->trans_wm)) {
13503 DRM_ERROR("mismatch in trans WM pipe %c cursor (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
13504 pipe_name(pipe),
13505 sw_plane_wm->trans_wm.plane_en,
13506 sw_plane_wm->trans_wm.plane_res_b,
13507 sw_plane_wm->trans_wm.plane_res_l,
13508 hw_plane_wm->trans_wm.plane_en,
13509 hw_plane_wm->trans_wm.plane_res_b,
13510 hw_plane_wm->trans_wm.plane_res_l);
13511 }
13512
13513 /* DDB */
13514 hw_ddb_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
13515 sw_ddb_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
13516
13517 if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
cpaul@redhat.comfaccd992016-10-14 17:31:58 -040013518 DRM_ERROR("mismatch in DDB state pipe %c cursor (expected (%u,%u), found (%u,%u))\n",
Lyude27082492016-08-24 07:48:10 +020013519 pipe_name(pipe),
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040013520 sw_ddb_entry->start, sw_ddb_entry->end,
13521 hw_ddb_entry->start, hw_ddb_entry->end);
Lyude27082492016-08-24 07:48:10 +020013522 }
Damien Lespiau08db6652014-11-04 17:06:52 +000013523 }
13524}
13525
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013526static void
Maarten Lankhorst677100c2016-11-08 13:55:41 +010013527verify_connector_state(struct drm_device *dev,
13528 struct drm_atomic_state *state,
13529 struct drm_crtc *crtc)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013530{
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020013531 struct drm_connector *connector;
Maarten Lankhorst677100c2016-11-08 13:55:41 +010013532 struct drm_connector_state *old_conn_state;
13533 int i;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013534
Maarten Lankhorst677100c2016-11-08 13:55:41 +010013535 for_each_connector_in_state(state, connector, old_conn_state, i) {
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020013536 struct drm_encoder *encoder = connector->encoder;
13537 struct drm_connector_state *state = connector->state;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020013538
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013539 if (state->crtc != crtc)
13540 continue;
13541
Daniel Vetter5a21b662016-05-24 17:13:53 +020013542 intel_connector_verify_state(to_intel_connector(connector));
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013543
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020013544 I915_STATE_WARN(state->best_encoder != encoder,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020013545 "connector's atomic encoder doesn't match legacy encoder\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013546 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013547}
13548
13549static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013550verify_encoder_state(struct drm_device *dev)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013551{
13552 struct intel_encoder *encoder;
13553 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013554
Damien Lespiaub2784e12014-08-05 11:29:37 +010013555 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013556 bool enabled = false;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013557 enum pipe pipe;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013558
13559 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
13560 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030013561 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013562
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020013563 for_each_intel_connector(dev, connector) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013564 if (connector->base.state->best_encoder != &encoder->base)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013565 continue;
13566 enabled = true;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020013567
13568 I915_STATE_WARN(connector->base.state->crtc !=
13569 encoder->base.crtc,
13570 "connector's crtc doesn't match encoder crtc\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013571 }
Dave Airlie0e32b392014-05-02 14:02:48 +100013572
Rob Clarke2c719b2014-12-15 13:56:32 -050013573 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013574 "encoder's enabled state mismatch "
13575 "(expected %i, found %i)\n",
13576 !!encoder->base.crtc, enabled);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020013577
13578 if (!encoder->base.crtc) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013579 bool active;
13580
13581 active = encoder->get_hw_state(encoder, &pipe);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020013582 I915_STATE_WARN(active,
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013583 "encoder detached but still enabled on pipe %c.\n",
13584 pipe_name(pipe));
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020013585 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013586 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013587}
13588
13589static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013590verify_crtc_state(struct drm_crtc *crtc,
13591 struct drm_crtc_state *old_crtc_state,
13592 struct drm_crtc_state *new_crtc_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013593{
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013594 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010013595 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013596 struct intel_encoder *encoder;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013597 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13598 struct intel_crtc_state *pipe_config, *sw_config;
13599 struct drm_atomic_state *old_state;
13600 bool active;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013601
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013602 old_state = old_crtc_state->state;
Daniel Vetterec2dc6a2016-05-09 16:34:09 +020013603 __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013604 pipe_config = to_intel_crtc_state(old_crtc_state);
13605 memset(pipe_config, 0, sizeof(*pipe_config));
13606 pipe_config->base.crtc = crtc;
13607 pipe_config->base.state = old_state;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013608
Ville Syrjälä78108b72016-05-27 20:59:19 +030013609 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013610
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013611 active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013612
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013613 /* hw state is inconsistent with the pipe quirk */
13614 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
13615 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
13616 active = new_crtc_state->active;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013617
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013618 I915_STATE_WARN(new_crtc_state->active != active,
13619 "crtc active state doesn't match with hw state "
13620 "(expected %i, found %i)\n", new_crtc_state->active, active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013621
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013622 I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
13623 "transitional active state does not match atomic hw state "
13624 "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013625
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013626 for_each_encoder_on_crtc(dev, crtc, encoder) {
13627 enum pipe pipe;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013628
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013629 active = encoder->get_hw_state(encoder, &pipe);
13630 I915_STATE_WARN(active != new_crtc_state->active,
13631 "[ENCODER:%i] active %i with crtc active %i\n",
13632 encoder->base.base.id, active, new_crtc_state->active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013633
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013634 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
13635 "Encoder connected to wrong pipe %c\n",
13636 pipe_name(pipe));
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013637
Ville Syrjälä253c84c2016-06-22 21:57:01 +030013638 if (active) {
13639 pipe_config->output_types |= 1 << encoder->type;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013640 encoder->get_config(encoder, pipe_config);
Ville Syrjälä253c84c2016-06-22 21:57:01 +030013641 }
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013642 }
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013643
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013644 if (!new_crtc_state->active)
13645 return;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013646
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013647 intel_pipe_config_sanity_check(dev_priv, pipe_config);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013648
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013649 sw_config = to_intel_crtc_state(crtc->state);
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000013650 if (!intel_pipe_config_compare(dev_priv, sw_config,
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013651 pipe_config, false)) {
13652 I915_STATE_WARN(1, "pipe state doesn't match!\n");
13653 intel_dump_pipe_config(intel_crtc, pipe_config,
13654 "[hw state]");
13655 intel_dump_pipe_config(intel_crtc, sw_config,
13656 "[sw state]");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013657 }
13658}
13659
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013660static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013661verify_single_dpll_state(struct drm_i915_private *dev_priv,
13662 struct intel_shared_dpll *pll,
13663 struct drm_crtc *crtc,
13664 struct drm_crtc_state *new_state)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013665{
13666 struct intel_dpll_hw_state dpll_hw_state;
13667 unsigned crtc_mask;
13668 bool active;
13669
13670 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
13671
13672 DRM_DEBUG_KMS("%s\n", pll->name);
13673
13674 active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
13675
13676 if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
13677 I915_STATE_WARN(!pll->on && pll->active_mask,
13678 "pll in active use but not on in sw tracking\n");
13679 I915_STATE_WARN(pll->on && !pll->active_mask,
13680 "pll is on but not used by any active crtc\n");
13681 I915_STATE_WARN(pll->on != active,
13682 "pll on state mismatch (expected %i, found %i)\n",
13683 pll->on, active);
13684 }
13685
13686 if (!crtc) {
13687 I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
13688 "more active pll users than references: %x vs %x\n",
13689 pll->active_mask, pll->config.crtc_mask);
13690
13691 return;
13692 }
13693
13694 crtc_mask = 1 << drm_crtc_index(crtc);
13695
13696 if (new_state->active)
13697 I915_STATE_WARN(!(pll->active_mask & crtc_mask),
13698 "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
13699 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
13700 else
13701 I915_STATE_WARN(pll->active_mask & crtc_mask,
13702 "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
13703 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
13704
13705 I915_STATE_WARN(!(pll->config.crtc_mask & crtc_mask),
13706 "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
13707 crtc_mask, pll->config.crtc_mask);
13708
13709 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state,
13710 &dpll_hw_state,
13711 sizeof(dpll_hw_state)),
13712 "pll hw state mismatch\n");
13713}
13714
13715static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013716verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
13717 struct drm_crtc_state *old_crtc_state,
13718 struct drm_crtc_state *new_crtc_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013719{
Chris Wilsonfac5e232016-07-04 11:34:36 +010013720 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013721 struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
13722 struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
13723
13724 if (new_state->shared_dpll)
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013725 verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013726
13727 if (old_state->shared_dpll &&
13728 old_state->shared_dpll != new_state->shared_dpll) {
13729 unsigned crtc_mask = 1 << drm_crtc_index(crtc);
13730 struct intel_shared_dpll *pll = old_state->shared_dpll;
13731
13732 I915_STATE_WARN(pll->active_mask & crtc_mask,
13733 "pll active mismatch (didn't expect pipe %c in active mask)\n",
13734 pipe_name(drm_crtc_index(crtc)));
13735 I915_STATE_WARN(pll->config.crtc_mask & crtc_mask,
13736 "pll enabled crtcs mismatch (found %x in enabled mask)\n",
13737 pipe_name(drm_crtc_index(crtc)));
13738 }
13739}
13740
13741static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013742intel_modeset_verify_crtc(struct drm_crtc *crtc,
Maarten Lankhorst677100c2016-11-08 13:55:41 +010013743 struct drm_atomic_state *state,
13744 struct drm_crtc_state *old_state,
13745 struct drm_crtc_state *new_state)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013746{
Daniel Vetter5a21b662016-05-24 17:13:53 +020013747 if (!needs_modeset(new_state) &&
13748 !to_intel_crtc_state(new_state)->update_pipe)
13749 return;
13750
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013751 verify_wm_state(crtc, new_state);
Maarten Lankhorst677100c2016-11-08 13:55:41 +010013752 verify_connector_state(crtc->dev, state, crtc);
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013753 verify_crtc_state(crtc, old_state, new_state);
13754 verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013755}
13756
13757static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013758verify_disabled_dpll_state(struct drm_device *dev)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013759{
Chris Wilsonfac5e232016-07-04 11:34:36 +010013760 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013761 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020013762
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013763 for (i = 0; i < dev_priv->num_shared_dpll; i++)
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013764 verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013765}
Daniel Vetter53589012013-06-05 13:34:16 +020013766
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013767static void
Maarten Lankhorst677100c2016-11-08 13:55:41 +010013768intel_modeset_verify_disabled(struct drm_device *dev,
13769 struct drm_atomic_state *state)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013770{
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013771 verify_encoder_state(dev);
Maarten Lankhorst677100c2016-11-08 13:55:41 +010013772 verify_connector_state(dev, state, NULL);
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013773 verify_disabled_dpll_state(dev);
Daniel Vetter25c5b262012-07-08 22:08:04 +020013774}
13775
Ville Syrjälä80715b22014-05-15 20:23:23 +030013776static void update_scanline_offset(struct intel_crtc *crtc)
13777{
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010013778 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä80715b22014-05-15 20:23:23 +030013779
13780 /*
13781 * The scanline counter increments at the leading edge of hsync.
13782 *
13783 * On most platforms it starts counting from vtotal-1 on the
13784 * first active line. That means the scanline counter value is
13785 * always one less than what we would expect. Ie. just after
13786 * start of vblank, which also occurs at start of hsync (on the
13787 * last active line), the scanline counter will read vblank_start-1.
13788 *
13789 * On gen2 the scanline counter starts counting from 1 instead
13790 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
13791 * to keep the value positive), instead of adding one.
13792 *
13793 * On HSW+ the behaviour of the scanline counter depends on the output
13794 * type. For DP ports it behaves like most other platforms, but on HDMI
13795 * there's an extra 1 line difference. So we need to add two instead of
13796 * one to the value.
13797 */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010013798 if (IS_GEN2(dev_priv)) {
Ville Syrjälä124abe02015-09-08 13:40:45 +030013799 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030013800 int vtotal;
13801
Ville Syrjälä124abe02015-09-08 13:40:45 +030013802 vtotal = adjusted_mode->crtc_vtotal;
13803 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
Ville Syrjälä80715b22014-05-15 20:23:23 +030013804 vtotal /= 2;
13805
13806 crtc->scanline_offset = vtotal - 1;
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010013807 } else if (HAS_DDI(dev_priv) &&
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +030013808 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030013809 crtc->scanline_offset = 2;
13810 } else
13811 crtc->scanline_offset = 1;
13812}
13813
Maarten Lankhorstad421372015-06-15 12:33:42 +020013814static void intel_modeset_clear_plls(struct drm_atomic_state *state)
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013815{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030013816 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013817 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstad421372015-06-15 12:33:42 +020013818 struct intel_shared_dpll_config *shared_dpll = NULL;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013819 struct drm_crtc *crtc;
13820 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013821 int i;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013822
13823 if (!dev_priv->display.crtc_compute_clock)
Maarten Lankhorstad421372015-06-15 12:33:42 +020013824 return;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013825
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013826 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013827 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013828 struct intel_shared_dpll *old_dpll =
13829 to_intel_crtc_state(crtc->state)->shared_dpll;
Maarten Lankhorstad421372015-06-15 12:33:42 +020013830
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013831 if (!needs_modeset(crtc_state))
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030013832 continue;
13833
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013834 to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013835
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013836 if (!old_dpll)
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013837 continue;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013838
Maarten Lankhorstad421372015-06-15 12:33:42 +020013839 if (!shared_dpll)
13840 shared_dpll = intel_atomic_get_shared_dpll_state(state);
13841
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013842 intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013843 }
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013844}
13845
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013846/*
13847 * This implements the workaround described in the "notes" section of the mode
13848 * set sequence documentation. When going from no pipes or single pipe to
13849 * multiple pipes, and planes are enabled after the pipe, we need to wait at
13850 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
13851 */
13852static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
13853{
13854 struct drm_crtc_state *crtc_state;
13855 struct intel_crtc *intel_crtc;
13856 struct drm_crtc *crtc;
13857 struct intel_crtc_state *first_crtc_state = NULL;
13858 struct intel_crtc_state *other_crtc_state = NULL;
13859 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
13860 int i;
13861
13862 /* look at all crtc's that are going to be enabled in during modeset */
13863 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13864 intel_crtc = to_intel_crtc(crtc);
13865
13866 if (!crtc_state->active || !needs_modeset(crtc_state))
13867 continue;
13868
13869 if (first_crtc_state) {
13870 other_crtc_state = to_intel_crtc_state(crtc_state);
13871 break;
13872 } else {
13873 first_crtc_state = to_intel_crtc_state(crtc_state);
13874 first_pipe = intel_crtc->pipe;
13875 }
13876 }
13877
13878 /* No workaround needed? */
13879 if (!first_crtc_state)
13880 return 0;
13881
13882 /* w/a possibly needed, check how many crtc's are already enabled. */
13883 for_each_intel_crtc(state->dev, intel_crtc) {
13884 struct intel_crtc_state *pipe_config;
13885
13886 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
13887 if (IS_ERR(pipe_config))
13888 return PTR_ERR(pipe_config);
13889
13890 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
13891
13892 if (!pipe_config->base.active ||
13893 needs_modeset(&pipe_config->base))
13894 continue;
13895
13896 /* 2 or more enabled crtcs means no need for w/a */
13897 if (enabled_pipe != INVALID_PIPE)
13898 return 0;
13899
13900 enabled_pipe = intel_crtc->pipe;
13901 }
13902
13903 if (enabled_pipe != INVALID_PIPE)
13904 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
13905 else if (other_crtc_state)
13906 other_crtc_state->hsw_workaround_pipe = first_pipe;
13907
13908 return 0;
13909}
13910
Ville Syrjälä8d965612016-11-14 18:35:10 +020013911static int intel_lock_all_pipes(struct drm_atomic_state *state)
13912{
13913 struct drm_crtc *crtc;
13914
13915 /* Add all pipes to the state */
13916 for_each_crtc(state->dev, crtc) {
13917 struct drm_crtc_state *crtc_state;
13918
13919 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13920 if (IS_ERR(crtc_state))
13921 return PTR_ERR(crtc_state);
13922 }
13923
13924 return 0;
13925}
13926
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013927static int intel_modeset_all_pipes(struct drm_atomic_state *state)
13928{
13929 struct drm_crtc *crtc;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013930
Ville Syrjälä8d965612016-11-14 18:35:10 +020013931 /*
13932 * Add all pipes to the state, and force
13933 * a modeset on all the active ones.
13934 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013935 for_each_crtc(state->dev, crtc) {
Ville Syrjälä9780aad2016-11-14 18:35:11 +020013936 struct drm_crtc_state *crtc_state;
13937 int ret;
13938
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013939 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13940 if (IS_ERR(crtc_state))
13941 return PTR_ERR(crtc_state);
13942
13943 if (!crtc_state->active || needs_modeset(crtc_state))
13944 continue;
13945
13946 crtc_state->mode_changed = true;
13947
13948 ret = drm_atomic_add_affected_connectors(state, crtc);
13949 if (ret)
Ville Syrjälä9780aad2016-11-14 18:35:11 +020013950 return ret;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013951
13952 ret = drm_atomic_add_affected_planes(state, crtc);
13953 if (ret)
Ville Syrjälä9780aad2016-11-14 18:35:11 +020013954 return ret;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013955 }
13956
Ville Syrjälä9780aad2016-11-14 18:35:11 +020013957 return 0;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013958}
13959
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013960static int intel_modeset_checks(struct drm_atomic_state *state)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013961{
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013962 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Chris Wilsonfac5e232016-07-04 11:34:36 +010013963 struct drm_i915_private *dev_priv = to_i915(state->dev);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013964 struct drm_crtc *crtc;
13965 struct drm_crtc_state *crtc_state;
13966 int ret = 0, i;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013967
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013968 if (!check_digital_port_conflicts(state)) {
13969 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
13970 return -EINVAL;
13971 }
13972
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013973 intel_state->modeset = true;
13974 intel_state->active_crtcs = dev_priv->active_crtcs;
13975
13976 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13977 if (crtc_state->active)
13978 intel_state->active_crtcs |= 1 << i;
13979 else
13980 intel_state->active_crtcs &= ~(1 << i);
Matt Roper8b4a7d02016-05-12 07:06:00 -070013981
13982 if (crtc_state->active != crtc->state->active)
13983 intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013984 }
13985
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013986 /*
13987 * See if the config requires any additional preparation, e.g.
13988 * to adjust global state with pipes off. We need to do this
13989 * here so we can get the modeset_pipe updated config for the new
13990 * mode set on this crtc. For other crtcs we need to use the
13991 * adjusted_mode bits in the crtc directly.
13992 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013993 if (dev_priv->display.modeset_calc_cdclk) {
Clint Taylorc89e39f2016-05-13 23:41:21 +030013994 if (!intel_state->cdclk_pll_vco)
Ville Syrjälä63911d72016-05-13 23:41:32 +030013995 intel_state->cdclk_pll_vco = dev_priv->cdclk_pll.vco;
Ville Syrjäläb2045352016-05-13 23:41:27 +030013996 if (!intel_state->cdclk_pll_vco)
13997 intel_state->cdclk_pll_vco = dev_priv->skl_preferred_vco_freq;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013998
Clint Taylorc89e39f2016-05-13 23:41:21 +030013999 ret = dev_priv->display.modeset_calc_cdclk(state);
14000 if (ret < 0)
14001 return ret;
14002
Ville Syrjälä8d965612016-11-14 18:35:10 +020014003 /*
14004 * Writes to dev_priv->atomic_cdclk_freq must protected by
14005 * holding all the crtc locks, even if we don't end up
14006 * touching the hardware
14007 */
14008 if (intel_state->cdclk != dev_priv->atomic_cdclk_freq) {
14009 ret = intel_lock_all_pipes(state);
14010 if (ret < 0)
14011 return ret;
14012 }
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014013
Ville Syrjälä8d965612016-11-14 18:35:10 +020014014 /* All pipes must be switched off while we change the cdclk. */
14015 if (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
14016 intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco) {
14017 ret = intel_modeset_all_pipes(state);
14018 if (ret < 0)
14019 return ret;
14020 }
Maarten Lankhorste8788cb2016-02-16 10:25:11 +010014021
14022 DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
14023 intel_state->cdclk, intel_state->dev_cdclk);
Ville Syrjäläe0ca7a62016-11-14 18:35:09 +020014024 } else {
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010014025 to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
Ville Syrjäläe0ca7a62016-11-14 18:35:09 +020014026 }
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030014027
Maarten Lankhorstad421372015-06-15 12:33:42 +020014028 intel_modeset_clear_plls(state);
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030014029
Maarten Lankhorst565602d2015-12-10 12:33:57 +010014030 if (IS_HASWELL(dev_priv))
Maarten Lankhorstad421372015-06-15 12:33:42 +020014031 return haswell_mode_set_planes_workaround(state);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020014032
Maarten Lankhorstad421372015-06-15 12:33:42 +020014033 return 0;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030014034}
14035
Matt Roperaa363132015-09-24 15:53:18 -070014036/*
14037 * Handle calculation of various watermark data at the end of the atomic check
14038 * phase. The code here should be run after the per-crtc and per-plane 'check'
14039 * handlers to ensure that all derived state has been updated.
14040 */
Matt Roper55994c22016-05-12 07:06:08 -070014041static int calc_watermark_data(struct drm_atomic_state *state)
Matt Roperaa363132015-09-24 15:53:18 -070014042{
14043 struct drm_device *dev = state->dev;
Matt Roper98d39492016-05-12 07:06:03 -070014044 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roper98d39492016-05-12 07:06:03 -070014045
14046 /* Is there platform-specific watermark information to calculate? */
14047 if (dev_priv->display.compute_global_watermarks)
Matt Roper55994c22016-05-12 07:06:08 -070014048 return dev_priv->display.compute_global_watermarks(state);
14049
14050 return 0;
Matt Roperaa363132015-09-24 15:53:18 -070014051}
14052
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020014053/**
14054 * intel_atomic_check - validate state object
14055 * @dev: drm device
14056 * @state: state to validate
14057 */
14058static int intel_atomic_check(struct drm_device *dev,
14059 struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020014060{
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020014061 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roperaa363132015-09-24 15:53:18 -070014062 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020014063 struct drm_crtc *crtc;
14064 struct drm_crtc_state *crtc_state;
14065 int ret, i;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020014066 bool any_ms = false;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020014067
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020014068 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020014069 if (ret)
14070 return ret;
14071
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020014072 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020014073 struct intel_crtc_state *pipe_config =
14074 to_intel_crtc_state(crtc_state);
Daniel Vetter1ed51de2015-07-15 14:15:51 +020014075
14076 /* Catch I915_MODE_FLAG_INHERITED */
14077 if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
14078 crtc_state->mode_changed = true;
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020014079
Daniel Vetter26495482015-07-15 14:15:52 +020014080 if (!needs_modeset(crtc_state))
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020014081 continue;
14082
Daniel Vetteraf4a8792016-05-09 09:31:25 +020014083 if (!crtc_state->enable) {
14084 any_ms = true;
14085 continue;
14086 }
14087
Daniel Vetter26495482015-07-15 14:15:52 +020014088 /* FIXME: For only active_changed we shouldn't need to do any
14089 * state recomputation at all. */
14090
Daniel Vetter1ed51de2015-07-15 14:15:51 +020014091 ret = drm_atomic_add_affected_connectors(state, crtc);
14092 if (ret)
14093 return ret;
Maarten Lankhorstb3592832015-06-15 12:33:38 +020014094
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020014095 ret = intel_modeset_pipe_config(crtc, pipe_config);
Maarten Lankhorst25aa1c32016-05-03 10:30:38 +020014096 if (ret) {
14097 intel_dump_pipe_config(to_intel_crtc(crtc),
14098 pipe_config, "[failed]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020014099 return ret;
Maarten Lankhorst25aa1c32016-05-03 10:30:38 +020014100 }
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020014101
Jani Nikula73831232015-11-19 10:26:30 +020014102 if (i915.fastboot &&
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000014103 intel_pipe_config_compare(dev_priv,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020014104 to_intel_crtc_state(crtc->state),
Daniel Vetter1ed51de2015-07-15 14:15:51 +020014105 pipe_config, true)) {
Daniel Vetter26495482015-07-15 14:15:52 +020014106 crtc_state->mode_changed = false;
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020014107 to_intel_crtc_state(crtc_state)->update_pipe = true;
Daniel Vetter26495482015-07-15 14:15:52 +020014108 }
14109
Daniel Vetteraf4a8792016-05-09 09:31:25 +020014110 if (needs_modeset(crtc_state))
Daniel Vetter26495482015-07-15 14:15:52 +020014111 any_ms = true;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020014112
Daniel Vetteraf4a8792016-05-09 09:31:25 +020014113 ret = drm_atomic_add_affected_planes(state, crtc);
14114 if (ret)
14115 return ret;
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020014116
Daniel Vetter26495482015-07-15 14:15:52 +020014117 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
14118 needs_modeset(crtc_state) ?
14119 "[modeset]" : "[fastset]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020014120 }
14121
Maarten Lankhorst61333b62015-06-15 12:33:50 +020014122 if (any_ms) {
14123 ret = intel_modeset_checks(state);
14124
14125 if (ret)
14126 return ret;
Ville Syrjäläe0ca7a62016-11-14 18:35:09 +020014127 } else {
14128 intel_state->cdclk = dev_priv->atomic_cdclk_freq;
14129 }
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020014130
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020014131 ret = drm_atomic_helper_check_planes(dev, state);
Matt Roperaa363132015-09-24 15:53:18 -070014132 if (ret)
14133 return ret;
14134
Paulo Zanonif51be2e2016-01-19 11:35:50 -020014135 intel_fbc_choose_crtc(dev_priv, state);
Matt Roper55994c22016-05-12 07:06:08 -070014136 return calc_watermark_data(state);
Daniel Vettera6778b32012-07-02 09:56:42 +020014137}
14138
Maarten Lankhorst5008e872015-08-18 13:40:05 +020014139static int intel_atomic_prepare_commit(struct drm_device *dev,
Chris Wilsond07f0e52016-10-28 13:58:44 +010014140 struct drm_atomic_state *state)
Maarten Lankhorst5008e872015-08-18 13:40:05 +020014141{
Chris Wilsonfac5e232016-07-04 11:34:36 +010014142 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst5008e872015-08-18 13:40:05 +020014143 struct drm_crtc_state *crtc_state;
14144 struct drm_crtc *crtc;
14145 int i, ret;
14146
Daniel Vetter5a21b662016-05-24 17:13:53 +020014147 for_each_crtc_in_state(state, crtc, crtc_state, i) {
14148 if (state->legacy_cursor_update)
14149 continue;
14150
14151 ret = intel_crtc_wait_for_pending_flips(crtc);
14152 if (ret)
14153 return ret;
14154
14155 if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
14156 flush_workqueue(dev_priv->wq);
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020014157 }
14158
Maarten Lankhorstf9356752015-08-18 13:40:05 +020014159 ret = mutex_lock_interruptible(&dev->struct_mutex);
14160 if (ret)
14161 return ret;
14162
Maarten Lankhorst5008e872015-08-18 13:40:05 +020014163 ret = drm_atomic_helper_prepare_planes(dev, state);
Chris Wilsonf7e58382016-04-13 17:35:07 +010014164 mutex_unlock(&dev->struct_mutex);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020014165
Maarten Lankhorst5008e872015-08-18 13:40:05 +020014166 return ret;
14167}
14168
Maarten Lankhorsta2991412016-05-17 15:07:48 +020014169u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
14170{
14171 struct drm_device *dev = crtc->base.dev;
14172
14173 if (!dev->max_vblank_count)
14174 return drm_accurate_vblank_count(&crtc->base);
14175
14176 return dev->driver->get_vblank_counter(dev, crtc->pipe);
14177}
14178
Daniel Vetter5a21b662016-05-24 17:13:53 +020014179static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
14180 struct drm_i915_private *dev_priv,
14181 unsigned crtc_mask)
Maarten Lankhorste8861672016-02-24 11:24:26 +010014182{
Daniel Vetter5a21b662016-05-24 17:13:53 +020014183 unsigned last_vblank_count[I915_MAX_PIPES];
14184 enum pipe pipe;
14185 int ret;
Maarten Lankhorste8861672016-02-24 11:24:26 +010014186
Daniel Vetter5a21b662016-05-24 17:13:53 +020014187 if (!crtc_mask)
14188 return;
Maarten Lankhorste8861672016-02-24 11:24:26 +010014189
Daniel Vetter5a21b662016-05-24 17:13:53 +020014190 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä98187832016-10-31 22:37:10 +020014191 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
14192 pipe);
Maarten Lankhorste8861672016-02-24 11:24:26 +010014193
Daniel Vetter5a21b662016-05-24 17:13:53 +020014194 if (!((1 << pipe) & crtc_mask))
Maarten Lankhorste8861672016-02-24 11:24:26 +010014195 continue;
14196
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020014197 ret = drm_crtc_vblank_get(&crtc->base);
Daniel Vetter5a21b662016-05-24 17:13:53 +020014198 if (WARN_ON(ret != 0)) {
14199 crtc_mask &= ~(1 << pipe);
14200 continue;
14201 }
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020014202
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020014203 last_vblank_count[pipe] = drm_crtc_vblank_count(&crtc->base);
Daniel Vetter5a21b662016-05-24 17:13:53 +020014204 }
14205
14206 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä98187832016-10-31 22:37:10 +020014207 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
14208 pipe);
Daniel Vetter5a21b662016-05-24 17:13:53 +020014209 long lret;
14210
14211 if (!((1 << pipe) & crtc_mask))
14212 continue;
14213
14214 lret = wait_event_timeout(dev->vblank[pipe].queue,
14215 last_vblank_count[pipe] !=
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020014216 drm_crtc_vblank_count(&crtc->base),
Daniel Vetter5a21b662016-05-24 17:13:53 +020014217 msecs_to_jiffies(50));
14218
14219 WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
14220
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020014221 drm_crtc_vblank_put(&crtc->base);
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020014222 }
14223}
14224
Daniel Vetter5a21b662016-05-24 17:13:53 +020014225static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020014226{
Daniel Vetter5a21b662016-05-24 17:13:53 +020014227 /* fb updated, need to unpin old fb */
14228 if (crtc_state->fb_changed)
14229 return true;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020014230
Daniel Vetter5a21b662016-05-24 17:13:53 +020014231 /* wm changes, need vblank before final wm's */
14232 if (crtc_state->update_wm_post)
14233 return true;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020014234
Daniel Vetter5a21b662016-05-24 17:13:53 +020014235 /*
14236 * cxsr is re-enabled after vblank.
14237 * This is already handled by crtc_state->update_wm_post,
14238 * but added for clarity.
14239 */
14240 if (crtc_state->disable_cxsr)
14241 return true;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020014242
Daniel Vetter5a21b662016-05-24 17:13:53 +020014243 return false;
Maarten Lankhorste8861672016-02-24 11:24:26 +010014244}
14245
Lyude896e5bb2016-08-24 07:48:09 +020014246static void intel_update_crtc(struct drm_crtc *crtc,
14247 struct drm_atomic_state *state,
14248 struct drm_crtc_state *old_crtc_state,
14249 unsigned int *crtc_vblank_mask)
14250{
14251 struct drm_device *dev = crtc->dev;
14252 struct drm_i915_private *dev_priv = to_i915(dev);
14253 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14254 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc->state);
14255 bool modeset = needs_modeset(crtc->state);
14256
14257 if (modeset) {
14258 update_scanline_offset(intel_crtc);
14259 dev_priv->display.crtc_enable(pipe_config, state);
14260 } else {
14261 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
14262 }
14263
14264 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
14265 intel_fbc_enable(
14266 intel_crtc, pipe_config,
14267 to_intel_plane_state(crtc->primary->state));
14268 }
14269
14270 drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
14271
14272 if (needs_vblank_wait(pipe_config))
14273 *crtc_vblank_mask |= drm_crtc_mask(crtc);
14274}
14275
14276static void intel_update_crtcs(struct drm_atomic_state *state,
14277 unsigned int *crtc_vblank_mask)
14278{
14279 struct drm_crtc *crtc;
14280 struct drm_crtc_state *old_crtc_state;
14281 int i;
14282
14283 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
14284 if (!crtc->state->active)
14285 continue;
14286
14287 intel_update_crtc(crtc, state, old_crtc_state,
14288 crtc_vblank_mask);
14289 }
14290}
14291
Lyude27082492016-08-24 07:48:10 +020014292static void skl_update_crtcs(struct drm_atomic_state *state,
14293 unsigned int *crtc_vblank_mask)
14294{
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +020014295 struct drm_i915_private *dev_priv = to_i915(state->dev);
Lyude27082492016-08-24 07:48:10 +020014296 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
14297 struct drm_crtc *crtc;
Lyudece0ba282016-09-15 10:46:35 -040014298 struct intel_crtc *intel_crtc;
Lyude27082492016-08-24 07:48:10 +020014299 struct drm_crtc_state *old_crtc_state;
Lyudece0ba282016-09-15 10:46:35 -040014300 struct intel_crtc_state *cstate;
Lyude27082492016-08-24 07:48:10 +020014301 unsigned int updated = 0;
14302 bool progress;
14303 enum pipe pipe;
Maarten Lankhorst5eff5032016-11-08 13:55:35 +010014304 int i;
14305
14306 const struct skl_ddb_entry *entries[I915_MAX_PIPES] = {};
14307
14308 for_each_crtc_in_state(state, crtc, old_crtc_state, i)
14309 /* ignore allocations for crtc's that have been turned off. */
14310 if (crtc->state->active)
14311 entries[i] = &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb;
Lyude27082492016-08-24 07:48:10 +020014312
14313 /*
14314 * Whenever the number of active pipes changes, we need to make sure we
14315 * update the pipes in the right order so that their ddb allocations
14316 * never overlap with eachother inbetween CRTC updates. Otherwise we'll
14317 * cause pipe underruns and other bad stuff.
14318 */
14319 do {
Lyude27082492016-08-24 07:48:10 +020014320 progress = false;
14321
14322 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
14323 bool vbl_wait = false;
14324 unsigned int cmask = drm_crtc_mask(crtc);
Lyudece0ba282016-09-15 10:46:35 -040014325
14326 intel_crtc = to_intel_crtc(crtc);
14327 cstate = to_intel_crtc_state(crtc->state);
14328 pipe = intel_crtc->pipe;
Lyude27082492016-08-24 07:48:10 +020014329
Maarten Lankhorst5eff5032016-11-08 13:55:35 +010014330 if (updated & cmask || !cstate->base.active)
Lyude27082492016-08-24 07:48:10 +020014331 continue;
Maarten Lankhorst5eff5032016-11-08 13:55:35 +010014332
14333 if (skl_ddb_allocation_overlaps(entries, &cstate->wm.skl.ddb, i))
Lyude27082492016-08-24 07:48:10 +020014334 continue;
14335
14336 updated |= cmask;
Maarten Lankhorst5eff5032016-11-08 13:55:35 +010014337 entries[i] = &cstate->wm.skl.ddb;
Lyude27082492016-08-24 07:48:10 +020014338
14339 /*
14340 * If this is an already active pipe, it's DDB changed,
14341 * and this isn't the last pipe that needs updating
14342 * then we need to wait for a vblank to pass for the
14343 * new ddb allocation to take effect.
14344 */
Lyudece0ba282016-09-15 10:46:35 -040014345 if (!skl_ddb_entry_equal(&cstate->wm.skl.ddb,
Maarten Lankhorst512b5522016-11-08 13:55:34 +010014346 &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb) &&
Lyude27082492016-08-24 07:48:10 +020014347 !crtc->state->active_changed &&
14348 intel_state->wm_results.dirty_pipes != updated)
14349 vbl_wait = true;
14350
14351 intel_update_crtc(crtc, state, old_crtc_state,
14352 crtc_vblank_mask);
14353
14354 if (vbl_wait)
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +020014355 intel_wait_for_vblank(dev_priv, pipe);
Lyude27082492016-08-24 07:48:10 +020014356
14357 progress = true;
14358 }
14359 } while (progress);
14360}
14361
Daniel Vetter94f05022016-06-14 18:01:00 +020014362static void intel_atomic_commit_tail(struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020014363{
Daniel Vetter94f05022016-06-14 18:01:00 +020014364 struct drm_device *dev = state->dev;
Maarten Lankhorst565602d2015-12-10 12:33:57 +010014365 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Chris Wilsonfac5e232016-07-04 11:34:36 +010014366 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020014367 struct drm_crtc_state *old_crtc_state;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020014368 struct drm_crtc *crtc;
Daniel Vetter5a21b662016-05-24 17:13:53 +020014369 struct intel_crtc_state *intel_cstate;
Daniel Vetter5a21b662016-05-24 17:13:53 +020014370 bool hw_check = intel_state->modeset;
14371 unsigned long put_domains[I915_MAX_PIPES] = {};
14372 unsigned crtc_vblank_mask = 0;
Chris Wilsone95433c2016-10-28 13:58:27 +010014373 int i;
Daniel Vettera6778b32012-07-02 09:56:42 +020014374
Daniel Vetterea0000f2016-06-13 16:13:46 +020014375 drm_atomic_helper_wait_for_dependencies(state);
14376
Maarten Lankhorstc3b32652016-11-08 13:55:40 +010014377 if (intel_state->modeset)
Daniel Vetter5a21b662016-05-24 17:13:53 +020014378 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010014379
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020014380 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020014381 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14382
Daniel Vetter5a21b662016-05-24 17:13:53 +020014383 if (needs_modeset(crtc->state) ||
14384 to_intel_crtc_state(crtc->state)->update_pipe) {
14385 hw_check = true;
14386
14387 put_domains[to_intel_crtc(crtc)->pipe] =
14388 modeset_get_crtc_power_domains(crtc,
14389 to_intel_crtc_state(crtc->state));
14390 }
14391
Maarten Lankhorst61333b62015-06-15 12:33:50 +020014392 if (!needs_modeset(crtc->state))
14393 continue;
14394
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020014395 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
Daniel Vetter460da9162013-03-27 00:44:51 +010014396
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020014397 if (old_crtc_state->active) {
14398 intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
Maarten Lankhorst4a806552016-08-09 17:04:01 +020014399 dev_priv->display.crtc_disable(to_intel_crtc_state(old_crtc_state), state);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020014400 intel_crtc->active = false;
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -020014401 intel_fbc_disable(intel_crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020014402 intel_disable_shared_dpll(intel_crtc);
Ville Syrjälä9bbc8258a2015-11-20 22:09:20 +020014403
14404 /*
14405 * Underruns don't always raise
14406 * interrupts, so check manually.
14407 */
14408 intel_check_cpu_fifo_underruns(dev_priv);
14409 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorstb9001112015-11-19 16:07:16 +010014410
Maarten Lankhorste62929b2016-11-08 13:55:33 +010014411 if (!crtc->state->active) {
14412 /*
14413 * Make sure we don't call initial_watermarks
14414 * for ILK-style watermark updates.
14415 */
14416 if (dev_priv->display.atomic_update_watermarks)
14417 dev_priv->display.initial_watermarks(intel_state,
14418 to_intel_crtc_state(crtc->state));
14419 else
14420 intel_update_watermarks(intel_crtc);
14421 }
Maarten Lankhorsta5392052015-06-15 12:33:52 +020014422 }
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010014423 }
Daniel Vetter7758a112012-07-08 19:40:39 +020014424
Daniel Vetterea9d7582012-07-10 10:42:52 +020014425 /* Only after disabling all output pipelines that will be changed can we
14426 * update the the output configuration. */
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020014427 intel_modeset_update_crtc_state(state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020014428
Maarten Lankhorst565602d2015-12-10 12:33:57 +010014429 if (intel_state->modeset) {
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020014430 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010014431
14432 if (dev_priv->display.modeset_commit_cdclk &&
Clint Taylorc89e39f2016-05-13 23:41:21 +030014433 (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
Ville Syrjälä63911d72016-05-13 23:41:32 +030014434 intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco))
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010014435 dev_priv->display.modeset_commit_cdclk(state);
Maarten Lankhorstf6d19732016-03-23 14:58:07 +010014436
Lyude656d1b82016-08-17 15:55:54 -040014437 /*
14438 * SKL workaround: bspec recommends we disable the SAGV when we
14439 * have more then one pipe enabled
14440 */
Paulo Zanoni56feca92016-09-22 18:00:28 -030014441 if (!intel_can_enable_sagv(state))
Paulo Zanoni16dcdc42016-09-22 18:00:27 -030014442 intel_disable_sagv(dev_priv);
Lyude656d1b82016-08-17 15:55:54 -040014443
Maarten Lankhorst677100c2016-11-08 13:55:41 +010014444 intel_modeset_verify_disabled(dev, state);
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020014445 }
Daniel Vetter47fab732012-10-26 10:58:18 +020014446
Lyude896e5bb2016-08-24 07:48:09 +020014447 /* Complete the events for pipes that have now been disabled */
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020014448 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020014449 bool modeset = needs_modeset(crtc->state);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020014450
Daniel Vetter1f7528c2016-06-13 16:13:45 +020014451 /* Complete events for now disable pipes here. */
14452 if (modeset && !crtc->state->active && crtc->state->event) {
14453 spin_lock_irq(&dev->event_lock);
14454 drm_crtc_send_vblank_event(crtc, crtc->state->event);
14455 spin_unlock_irq(&dev->event_lock);
14456
14457 crtc->state->event = NULL;
14458 }
Matt Ropered4a6a72016-02-23 17:20:13 -080014459 }
14460
Lyude896e5bb2016-08-24 07:48:09 +020014461 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
14462 dev_priv->display.update_crtcs(state, &crtc_vblank_mask);
14463
Daniel Vetter94f05022016-06-14 18:01:00 +020014464 /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
14465 * already, but still need the state for the delayed optimization. To
14466 * fix this:
14467 * - wrap the optimization/post_plane_update stuff into a per-crtc work.
14468 * - schedule that vblank worker _before_ calling hw_done
14469 * - at the start of commit_tail, cancel it _synchrously
14470 * - switch over to the vblank wait helper in the core after that since
14471 * we don't need out special handling any more.
14472 */
Daniel Vetter5a21b662016-05-24 17:13:53 +020014473 if (!state->legacy_cursor_update)
14474 intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
14475
14476 /*
14477 * Now that the vblank has passed, we can go ahead and program the
14478 * optimal watermarks on platforms that need two-step watermark
14479 * programming.
14480 *
14481 * TODO: Move this (and other cleanup) to an async worker eventually.
14482 */
14483 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
14484 intel_cstate = to_intel_crtc_state(crtc->state);
14485
14486 if (dev_priv->display.optimize_watermarks)
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010014487 dev_priv->display.optimize_watermarks(intel_state,
14488 intel_cstate);
Daniel Vetter5a21b662016-05-24 17:13:53 +020014489 }
14490
14491 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
14492 intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
14493
14494 if (put_domains[i])
14495 modeset_put_power_domains(dev_priv, put_domains[i]);
14496
Maarten Lankhorst677100c2016-11-08 13:55:41 +010014497 intel_modeset_verify_crtc(crtc, state, old_crtc_state, crtc->state);
Daniel Vetter5a21b662016-05-24 17:13:53 +020014498 }
14499
Paulo Zanoni56feca92016-09-22 18:00:28 -030014500 if (intel_state->modeset && intel_can_enable_sagv(state))
Paulo Zanoni16dcdc42016-09-22 18:00:27 -030014501 intel_enable_sagv(dev_priv);
Lyude656d1b82016-08-17 15:55:54 -040014502
Daniel Vetter94f05022016-06-14 18:01:00 +020014503 drm_atomic_helper_commit_hw_done(state);
14504
Daniel Vetter5a21b662016-05-24 17:13:53 +020014505 if (intel_state->modeset)
14506 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
14507
14508 mutex_lock(&dev->struct_mutex);
14509 drm_atomic_helper_cleanup_planes(dev, state);
14510 mutex_unlock(&dev->struct_mutex);
14511
Daniel Vetterea0000f2016-06-13 16:13:46 +020014512 drm_atomic_helper_commit_cleanup_done(state);
14513
Chris Wilson08536952016-10-14 13:18:18 +010014514 drm_atomic_state_put(state);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080014515
Mika Kuoppala75714942015-12-16 09:26:48 +020014516 /* As one of the primary mmio accessors, KMS has a high likelihood
14517 * of triggering bugs in unclaimed access. After we finish
14518 * modesetting, see if an error has been flagged, and if so
14519 * enable debugging for the next modeset - and hope we catch
14520 * the culprit.
14521 *
14522 * XXX note that we assume display power is on at this point.
14523 * This might hold true now but we need to add pm helper to check
14524 * unclaimed only when the hardware is on, as atomic commits
14525 * can happen also when the device is completely off.
14526 */
14527 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
Daniel Vetter94f05022016-06-14 18:01:00 +020014528}
14529
14530static void intel_atomic_commit_work(struct work_struct *work)
14531{
Chris Wilsonc004a902016-10-28 13:58:45 +010014532 struct drm_atomic_state *state =
14533 container_of(work, struct drm_atomic_state, commit_work);
14534
Daniel Vetter94f05022016-06-14 18:01:00 +020014535 intel_atomic_commit_tail(state);
14536}
14537
Chris Wilsonc004a902016-10-28 13:58:45 +010014538static int __i915_sw_fence_call
14539intel_atomic_commit_ready(struct i915_sw_fence *fence,
14540 enum i915_sw_fence_notify notify)
14541{
14542 struct intel_atomic_state *state =
14543 container_of(fence, struct intel_atomic_state, commit_ready);
14544
14545 switch (notify) {
14546 case FENCE_COMPLETE:
14547 if (state->base.commit_work.func)
14548 queue_work(system_unbound_wq, &state->base.commit_work);
14549 break;
14550
14551 case FENCE_FREE:
14552 drm_atomic_state_put(&state->base);
14553 break;
14554 }
14555
14556 return NOTIFY_DONE;
14557}
14558
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020014559static void intel_atomic_track_fbs(struct drm_atomic_state *state)
14560{
14561 struct drm_plane_state *old_plane_state;
14562 struct drm_plane *plane;
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020014563 int i;
14564
Chris Wilsonfaf5bf02016-08-04 16:32:37 +010014565 for_each_plane_in_state(state, plane, old_plane_state, i)
14566 i915_gem_track_fb(intel_fb_obj(old_plane_state->fb),
14567 intel_fb_obj(plane->state->fb),
14568 to_intel_plane(plane)->frontbuffer_bit);
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020014569}
14570
Daniel Vetter94f05022016-06-14 18:01:00 +020014571/**
14572 * intel_atomic_commit - commit validated state object
14573 * @dev: DRM device
14574 * @state: the top-level driver state object
14575 * @nonblock: nonblocking commit
14576 *
14577 * This function commits a top-level state object that has been validated
14578 * with drm_atomic_helper_check().
14579 *
Daniel Vetter94f05022016-06-14 18:01:00 +020014580 * RETURNS
14581 * Zero for success or -errno.
14582 */
14583static int intel_atomic_commit(struct drm_device *dev,
14584 struct drm_atomic_state *state,
14585 bool nonblock)
14586{
14587 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Chris Wilsonfac5e232016-07-04 11:34:36 +010014588 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter94f05022016-06-14 18:01:00 +020014589 int ret = 0;
14590
Daniel Vetter94f05022016-06-14 18:01:00 +020014591 ret = drm_atomic_helper_setup_commit(state, nonblock);
14592 if (ret)
14593 return ret;
14594
Chris Wilsonc004a902016-10-28 13:58:45 +010014595 drm_atomic_state_get(state);
14596 i915_sw_fence_init(&intel_state->commit_ready,
14597 intel_atomic_commit_ready);
Daniel Vetter94f05022016-06-14 18:01:00 +020014598
Chris Wilsond07f0e52016-10-28 13:58:44 +010014599 ret = intel_atomic_prepare_commit(dev, state);
Daniel Vetter94f05022016-06-14 18:01:00 +020014600 if (ret) {
14601 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
Chris Wilsonc004a902016-10-28 13:58:45 +010014602 i915_sw_fence_commit(&intel_state->commit_ready);
Daniel Vetter94f05022016-06-14 18:01:00 +020014603 return ret;
14604 }
14605
14606 drm_atomic_helper_swap_state(state, true);
14607 dev_priv->wm.distrust_bios_wm = false;
Daniel Vetter94f05022016-06-14 18:01:00 +020014608 intel_shared_dpll_commit(state);
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020014609 intel_atomic_track_fbs(state);
Daniel Vetter94f05022016-06-14 18:01:00 +020014610
Maarten Lankhorstc3b32652016-11-08 13:55:40 +010014611 if (intel_state->modeset) {
14612 memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
14613 sizeof(intel_state->min_pixclk));
14614 dev_priv->active_crtcs = intel_state->active_crtcs;
14615 dev_priv->atomic_cdclk_freq = intel_state->cdclk;
14616 }
14617
Chris Wilson08536952016-10-14 13:18:18 +010014618 drm_atomic_state_get(state);
Chris Wilsonc004a902016-10-28 13:58:45 +010014619 INIT_WORK(&state->commit_work,
14620 nonblock ? intel_atomic_commit_work : NULL);
14621
14622 i915_sw_fence_commit(&intel_state->commit_ready);
14623 if (!nonblock) {
14624 i915_sw_fence_wait(&intel_state->commit_ready);
Daniel Vetter94f05022016-06-14 18:01:00 +020014625 intel_atomic_commit_tail(state);
Chris Wilsonc004a902016-10-28 13:58:45 +010014626 }
Mika Kuoppala75714942015-12-16 09:26:48 +020014627
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020014628 return 0;
Daniel Vetterf30da182013-04-11 20:22:50 +020014629}
14630
Chris Wilsonc0c36b942012-12-19 16:08:43 +000014631void intel_crtc_restore_mode(struct drm_crtc *crtc)
14632{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020014633 struct drm_device *dev = crtc->dev;
14634 struct drm_atomic_state *state;
Maarten Lankhorste694eb02015-07-14 16:19:12 +020014635 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030014636 int ret;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020014637
14638 state = drm_atomic_state_alloc(dev);
14639 if (!state) {
Ville Syrjälä78108b72016-05-27 20:59:19 +030014640 DRM_DEBUG_KMS("[CRTC:%d:%s] crtc restore failed, out of memory",
14641 crtc->base.id, crtc->name);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020014642 return;
14643 }
14644
Maarten Lankhorste694eb02015-07-14 16:19:12 +020014645 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020014646
Maarten Lankhorste694eb02015-07-14 16:19:12 +020014647retry:
14648 crtc_state = drm_atomic_get_crtc_state(state, crtc);
14649 ret = PTR_ERR_OR_ZERO(crtc_state);
14650 if (!ret) {
14651 if (!crtc_state->active)
14652 goto out;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020014653
Maarten Lankhorste694eb02015-07-14 16:19:12 +020014654 crtc_state->mode_changed = true;
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020014655 ret = drm_atomic_commit(state);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020014656 }
14657
Maarten Lankhorste694eb02015-07-14 16:19:12 +020014658 if (ret == -EDEADLK) {
14659 drm_atomic_state_clear(state);
14660 drm_modeset_backoff(state->acquire_ctx);
14661 goto retry;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030014662 }
14663
Maarten Lankhorste694eb02015-07-14 16:19:12 +020014664out:
Chris Wilson08536952016-10-14 13:18:18 +010014665 drm_atomic_state_put(state);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000014666}
14667
Bob Paauwea8784872016-07-15 14:59:02 +010014668/*
14669 * FIXME: Remove this once i915 is fully DRIVER_ATOMIC by calling
14670 * drm_atomic_helper_legacy_gamma_set() directly.
14671 */
14672static int intel_atomic_legacy_gamma_set(struct drm_crtc *crtc,
14673 u16 *red, u16 *green, u16 *blue,
14674 uint32_t size)
14675{
14676 struct drm_device *dev = crtc->dev;
14677 struct drm_mode_config *config = &dev->mode_config;
14678 struct drm_crtc_state *state;
14679 int ret;
14680
14681 ret = drm_atomic_helper_legacy_gamma_set(crtc, red, green, blue, size);
14682 if (ret)
14683 return ret;
14684
14685 /*
14686 * Make sure we update the legacy properties so this works when
14687 * atomic is not enabled.
14688 */
14689
14690 state = crtc->state;
14691
14692 drm_object_property_set_value(&crtc->base,
14693 config->degamma_lut_property,
14694 (state->degamma_lut) ?
14695 state->degamma_lut->base.id : 0);
14696
14697 drm_object_property_set_value(&crtc->base,
14698 config->ctm_property,
14699 (state->ctm) ?
14700 state->ctm->base.id : 0);
14701
14702 drm_object_property_set_value(&crtc->base,
14703 config->gamma_lut_property,
14704 (state->gamma_lut) ?
14705 state->gamma_lut->base.id : 0);
14706
14707 return 0;
14708}
14709
Chris Wilsonf6e5b162011-04-12 18:06:51 +010014710static const struct drm_crtc_funcs intel_crtc_funcs = {
Bob Paauwea8784872016-07-15 14:59:02 +010014711 .gamma_set = intel_atomic_legacy_gamma_set,
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020014712 .set_config = drm_atomic_helper_set_config,
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000014713 .set_property = drm_atomic_helper_crtc_set_property,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010014714 .destroy = intel_crtc_destroy,
Chris Wilson527b6ab2016-06-24 13:44:03 +010014715 .page_flip = intel_crtc_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080014716 .atomic_duplicate_state = intel_crtc_duplicate_state,
14717 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010014718};
14719
Matt Roper6beb8c232014-12-01 15:40:14 -080014720/**
14721 * intel_prepare_plane_fb - Prepare fb for usage on plane
14722 * @plane: drm plane to prepare for
14723 * @fb: framebuffer to prepare for presentation
14724 *
14725 * Prepares a framebuffer for usage on a display plane. Generally this
14726 * involves pinning the underlying object and updating the frontbuffer tracking
14727 * bits. Some older platforms need special physical address handling for
14728 * cursor planes.
14729 *
Maarten Lankhorstf9356752015-08-18 13:40:05 +020014730 * Must be called with struct_mutex held.
14731 *
Matt Roper6beb8c232014-12-01 15:40:14 -080014732 * Returns 0 on success, negative error code on failure.
14733 */
14734int
14735intel_prepare_plane_fb(struct drm_plane *plane,
Chris Wilson18320402016-08-18 19:00:16 +010014736 struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070014737{
Chris Wilsonc004a902016-10-28 13:58:45 +010014738 struct intel_atomic_state *intel_state =
14739 to_intel_atomic_state(new_state->state);
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +000014740 struct drm_i915_private *dev_priv = to_i915(plane->dev);
Maarten Lankhorst844f9112015-09-02 10:42:40 +020014741 struct drm_framebuffer *fb = new_state->fb;
Matt Roper6beb8c232014-12-01 15:40:14 -080014742 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014743 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
Chris Wilsonc004a902016-10-28 13:58:45 +010014744 int ret;
Matt Roper465c1202014-05-29 08:06:54 -070014745
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014746 if (!obj && !old_obj)
Matt Roper465c1202014-05-29 08:06:54 -070014747 return 0;
14748
Maarten Lankhorst5008e872015-08-18 13:40:05 +020014749 if (old_obj) {
14750 struct drm_crtc_state *crtc_state =
Chris Wilsonc004a902016-10-28 13:58:45 +010014751 drm_atomic_get_existing_crtc_state(new_state->state,
14752 plane->state->crtc);
Maarten Lankhorst5008e872015-08-18 13:40:05 +020014753
14754 /* Big Hammer, we also need to ensure that any pending
14755 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
14756 * current scanout is retired before unpinning the old
14757 * framebuffer. Note that we rely on userspace rendering
14758 * into the buffer attached to the pipe they are waiting
14759 * on. If not, userspace generates a GPU hang with IPEHR
14760 * point to the MI_WAIT_FOR_EVENT.
14761 *
14762 * This should only fail upon a hung GPU, in which case we
14763 * can safely continue.
14764 */
Chris Wilsonc004a902016-10-28 13:58:45 +010014765 if (needs_modeset(crtc_state)) {
14766 ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
14767 old_obj->resv, NULL,
14768 false, 0,
14769 GFP_KERNEL);
14770 if (ret < 0)
14771 return ret;
Chris Wilsonf4457ae2016-04-13 17:35:08 +010014772 }
Maarten Lankhorst5008e872015-08-18 13:40:05 +020014773 }
14774
Chris Wilsonc004a902016-10-28 13:58:45 +010014775 if (new_state->fence) { /* explicit fencing */
14776 ret = i915_sw_fence_await_dma_fence(&intel_state->commit_ready,
14777 new_state->fence,
14778 I915_FENCE_TIMEOUT,
14779 GFP_KERNEL);
14780 if (ret < 0)
14781 return ret;
14782 }
14783
Chris Wilsonc37efb92016-06-17 08:28:47 +010014784 if (!obj)
14785 return 0;
14786
Chris Wilsonc004a902016-10-28 13:58:45 +010014787 if (!new_state->fence) { /* implicit fencing */
14788 ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
14789 obj->resv, NULL,
14790 false, I915_FENCE_TIMEOUT,
14791 GFP_KERNEL);
14792 if (ret < 0)
14793 return ret;
Chris Wilson6b5e90f2016-11-14 20:41:05 +000014794
14795 i915_gem_object_wait_priority(obj, 0, I915_PRIORITY_DISPLAY);
Chris Wilsonc004a902016-10-28 13:58:45 +010014796 }
Daniel Vetter5a21b662016-05-24 17:13:53 +020014797
Chris Wilsonc37efb92016-06-17 08:28:47 +010014798 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +000014799 INTEL_INFO(dev_priv)->cursor_needs_physical) {
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +010014800 int align = IS_I830(dev_priv) ? 16 * 1024 : 256;
Matt Roper6beb8c232014-12-01 15:40:14 -080014801 ret = i915_gem_object_attach_phys(obj, align);
Chris Wilsond07f0e52016-10-28 13:58:44 +010014802 if (ret) {
Matt Roper6beb8c232014-12-01 15:40:14 -080014803 DRM_DEBUG_KMS("failed to attach phys object\n");
Chris Wilsond07f0e52016-10-28 13:58:44 +010014804 return ret;
14805 }
Matt Roper6beb8c232014-12-01 15:40:14 -080014806 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +010014807 struct i915_vma *vma;
14808
14809 vma = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
Chris Wilsond07f0e52016-10-28 13:58:44 +010014810 if (IS_ERR(vma)) {
14811 DRM_DEBUG_KMS("failed to pin object\n");
14812 return PTR_ERR(vma);
14813 }
Matt Roper6beb8c232014-12-01 15:40:14 -080014814 }
14815
Chris Wilsond07f0e52016-10-28 13:58:44 +010014816 return 0;
Matt Roper6beb8c232014-12-01 15:40:14 -080014817}
14818
Matt Roper38f3ce32014-12-02 07:45:25 -080014819/**
14820 * intel_cleanup_plane_fb - Cleans up an fb after plane use
14821 * @plane: drm plane to clean up for
14822 * @fb: old framebuffer that was on plane
14823 *
14824 * Cleans up a framebuffer that has just been removed from a plane.
Maarten Lankhorstf9356752015-08-18 13:40:05 +020014825 *
14826 * Must be called with struct_mutex held.
Matt Roper38f3ce32014-12-02 07:45:25 -080014827 */
14828void
14829intel_cleanup_plane_fb(struct drm_plane *plane,
Chris Wilson18320402016-08-18 19:00:16 +010014830 struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080014831{
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +000014832 struct drm_i915_private *dev_priv = to_i915(plane->dev);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020014833 struct intel_plane_state *old_intel_state;
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014834 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
14835 struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
Matt Roper38f3ce32014-12-02 07:45:25 -080014836
Maarten Lankhorst7580d772015-08-18 13:40:06 +020014837 old_intel_state = to_intel_plane_state(old_state);
14838
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014839 if (!obj && !old_obj)
Matt Roper38f3ce32014-12-02 07:45:25 -080014840 return;
14841
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014842 if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +000014843 !INTEL_INFO(dev_priv)->cursor_needs_physical))
Ville Syrjälä3465c582016-02-15 22:54:43 +020014844 intel_unpin_fb_obj(old_state->fb, old_state->rotation);
Matt Roper465c1202014-05-29 08:06:54 -070014845}
14846
Chandra Konduru6156a452015-04-27 13:48:39 -070014847int
14848skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
14849{
14850 int max_scale;
Chandra Konduru6156a452015-04-27 13:48:39 -070014851 int crtc_clock, cdclk;
14852
Maarten Lankhorstbf8a0af2015-11-24 11:29:02 +010014853 if (!intel_crtc || !crtc_state->base.enable)
Chandra Konduru6156a452015-04-27 13:48:39 -070014854 return DRM_PLANE_HELPER_NO_SCALING;
14855
Chandra Konduru6156a452015-04-27 13:48:39 -070014856 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014857 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
Chandra Konduru6156a452015-04-27 13:48:39 -070014858
Tvrtko Ursulin54bf1ce2015-10-20 17:17:07 +010014859 if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
Chandra Konduru6156a452015-04-27 13:48:39 -070014860 return DRM_PLANE_HELPER_NO_SCALING;
14861
14862 /*
14863 * skl max scale is lower of:
14864 * close to 3 but not 3, -1 is for that purpose
14865 * or
14866 * cdclk/crtc_clock
14867 */
14868 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
14869
14870 return max_scale;
14871}
14872
Matt Roper465c1202014-05-29 08:06:54 -070014873static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030014874intel_check_primary_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014875 struct intel_crtc_state *crtc_state,
Gustavo Padovan3c692a42014-09-05 17:04:49 -030014876 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070014877{
Ville Syrjäläb63a16f2016-01-28 16:53:54 +020014878 struct drm_i915_private *dev_priv = to_i915(plane->dev);
Matt Roper2b875c22014-12-01 15:40:13 -080014879 struct drm_crtc *crtc = state->base.crtc;
Chandra Konduru6156a452015-04-27 13:48:39 -070014880 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014881 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
14882 bool can_position = false;
Ville Syrjäläb63a16f2016-01-28 16:53:54 +020014883 int ret;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030014884
Ville Syrjäläb63a16f2016-01-28 16:53:54 +020014885 if (INTEL_GEN(dev_priv) >= 9) {
Ville Syrjälä693bdc22016-01-15 20:46:53 +020014886 /* use scaler when colorkey is not required */
14887 if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
14888 min_scale = 1;
14889 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
14890 }
Sonika Jindald8106362015-04-10 14:37:28 +053014891 can_position = true;
Chandra Konduru6156a452015-04-27 13:48:39 -070014892 }
Sonika Jindald8106362015-04-10 14:37:28 +053014893
Daniel Vettercc926382016-08-15 10:41:47 +020014894 ret = drm_plane_helper_check_state(&state->base,
14895 &state->clip,
14896 min_scale, max_scale,
14897 can_position, true);
Ville Syrjäläb63a16f2016-01-28 16:53:54 +020014898 if (ret)
14899 return ret;
14900
Daniel Vettercc926382016-08-15 10:41:47 +020014901 if (!state->base.fb)
Ville Syrjäläb63a16f2016-01-28 16:53:54 +020014902 return 0;
14903
14904 if (INTEL_GEN(dev_priv) >= 9) {
14905 ret = skl_check_plane_surface(state);
14906 if (ret)
14907 return ret;
14908 }
14909
14910 return 0;
Matt Roper465c1202014-05-29 08:06:54 -070014911}
14912
Daniel Vetter5a21b662016-05-24 17:13:53 +020014913static void intel_begin_crtc_commit(struct drm_crtc *crtc,
14914 struct drm_crtc_state *old_crtc_state)
14915{
14916 struct drm_device *dev = crtc->dev;
Lyude62e0fb82016-08-22 12:50:08 -040014917 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020014918 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Lyudeb707aa52016-09-15 10:56:06 -040014919 struct intel_crtc_state *intel_cstate =
14920 to_intel_crtc_state(crtc->state);
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010014921 struct intel_crtc_state *old_intel_cstate =
Daniel Vetter5a21b662016-05-24 17:13:53 +020014922 to_intel_crtc_state(old_crtc_state);
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010014923 struct intel_atomic_state *old_intel_state =
14924 to_intel_atomic_state(old_crtc_state->state);
Daniel Vetter5a21b662016-05-24 17:13:53 +020014925 bool modeset = needs_modeset(crtc->state);
14926
14927 /* Perform vblank evasion around commit operation */
14928 intel_pipe_update_start(intel_crtc);
14929
14930 if (modeset)
Maarten Lankhorste62929b2016-11-08 13:55:33 +010014931 goto out;
Daniel Vetter5a21b662016-05-24 17:13:53 +020014932
14933 if (crtc->state->color_mgmt_changed || to_intel_crtc_state(crtc->state)->update_pipe) {
14934 intel_color_set_csc(crtc->state);
14935 intel_color_load_luts(crtc->state);
14936 }
14937
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010014938 if (intel_cstate->update_pipe)
14939 intel_update_pipe_config(intel_crtc, old_intel_cstate);
14940 else if (INTEL_GEN(dev_priv) >= 9)
Daniel Vetter5a21b662016-05-24 17:13:53 +020014941 skl_detach_scalers(intel_crtc);
Lyude62e0fb82016-08-22 12:50:08 -040014942
Maarten Lankhorste62929b2016-11-08 13:55:33 +010014943out:
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010014944 if (dev_priv->display.atomic_update_watermarks)
14945 dev_priv->display.atomic_update_watermarks(old_intel_state,
14946 intel_cstate);
Daniel Vetter5a21b662016-05-24 17:13:53 +020014947}
14948
14949static void intel_finish_crtc_commit(struct drm_crtc *crtc,
14950 struct drm_crtc_state *old_crtc_state)
14951{
14952 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14953
14954 intel_pipe_update_end(intel_crtc, NULL);
14955}
14956
Matt Ropercf4c7c12014-12-04 10:27:42 -080014957/**
Matt Roper4a3b8762014-12-23 10:41:51 -080014958 * intel_plane_destroy - destroy a plane
14959 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080014960 *
Matt Roper4a3b8762014-12-23 10:41:51 -080014961 * Common destruction function for all types of planes (primary, cursor,
14962 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080014963 */
Matt Roper4a3b8762014-12-23 10:41:51 -080014964void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070014965{
Matt Roper465c1202014-05-29 08:06:54 -070014966 drm_plane_cleanup(plane);
Ville Syrjälä69ae5612016-05-27 20:59:22 +030014967 kfree(to_intel_plane(plane));
Matt Roper465c1202014-05-29 08:06:54 -070014968}
14969
Matt Roper65a3fea2015-01-21 16:35:42 -080014970const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070014971 .update_plane = drm_atomic_helper_update_plane,
14972 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070014973 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080014974 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080014975 .atomic_get_property = intel_plane_atomic_get_property,
14976 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080014977 .atomic_duplicate_state = intel_plane_duplicate_state,
14978 .atomic_destroy_state = intel_plane_destroy_state,
Matt Roper465c1202014-05-29 08:06:54 -070014979};
14980
Ville Syrjäläb079bd172016-10-25 18:58:02 +030014981static struct intel_plane *
Ville Syrjälä580503c2016-10-31 22:37:00 +020014982intel_primary_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
Matt Roper465c1202014-05-29 08:06:54 -070014983{
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014984 struct intel_plane *primary = NULL;
14985 struct intel_plane_state *state = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070014986 const uint32_t *intel_primary_formats;
Ville Syrjälä93ca7e02016-09-26 19:30:56 +030014987 unsigned int supported_rotations;
Thierry Reding45e37432015-08-12 16:54:28 +020014988 unsigned int num_formats;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014989 int ret;
Matt Roper465c1202014-05-29 08:06:54 -070014990
14991 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030014992 if (!primary) {
14993 ret = -ENOMEM;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014994 goto fail;
Ville Syrjäläb079bd172016-10-25 18:58:02 +030014995 }
Matt Roper465c1202014-05-29 08:06:54 -070014996
Matt Roper8e7d6882015-01-21 16:35:41 -080014997 state = intel_create_plane_state(&primary->base);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030014998 if (!state) {
14999 ret = -ENOMEM;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000015000 goto fail;
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015001 }
15002
Matt Roper8e7d6882015-01-21 16:35:41 -080015003 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080015004
Matt Roper465c1202014-05-29 08:06:54 -070015005 primary->can_scale = false;
15006 primary->max_downscale = 1;
Ville Syrjälä580503c2016-10-31 22:37:00 +020015007 if (INTEL_GEN(dev_priv) >= 9) {
Chandra Konduru6156a452015-04-27 13:48:39 -070015008 primary->can_scale = true;
Chandra Konduruaf99ceda2015-05-11 14:35:47 -070015009 state->scaler_id = -1;
Chandra Konduru6156a452015-04-27 13:48:39 -070015010 }
Matt Roper465c1202014-05-29 08:06:54 -070015011 primary->pipe = pipe;
Ville Syrjäläe3c566d2016-11-08 16:47:11 +020015012 /*
15013 * On gen2/3 only plane A can do FBC, but the panel fitter and LVDS
15014 * port is hooked to pipe B. Hence we want plane A feeding pipe B.
15015 */
15016 if (HAS_FBC(dev_priv) && INTEL_GEN(dev_priv) < 4)
15017 primary->plane = (enum plane) !pipe;
15018 else
15019 primary->plane = (enum plane) pipe;
Ville Syrjäläb14e5842016-11-22 18:01:56 +020015020 primary->id = PLANE_PRIMARY;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030015021 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080015022 primary->check_plane = intel_check_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070015023
Ville Syrjälä580503c2016-10-31 22:37:00 +020015024 if (INTEL_GEN(dev_priv) >= 9) {
Damien Lespiau6c0fd452015-05-19 12:29:16 +010015025 intel_primary_formats = skl_primary_formats;
15026 num_formats = ARRAY_SIZE(skl_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010015027
15028 primary->update_plane = skylake_update_primary_plane;
15029 primary->disable_plane = skylake_disable_primary_plane;
Tvrtko Ursulin6e266952016-10-13 11:02:53 +010015030 } else if (HAS_PCH_SPLIT(dev_priv)) {
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010015031 intel_primary_formats = i965_primary_formats;
15032 num_formats = ARRAY_SIZE(i965_primary_formats);
15033
15034 primary->update_plane = ironlake_update_primary_plane;
15035 primary->disable_plane = i9xx_disable_primary_plane;
Ville Syrjälä580503c2016-10-31 22:37:00 +020015036 } else if (INTEL_GEN(dev_priv) >= 4) {
Damien Lespiau568db4f2015-05-12 16:13:18 +010015037 intel_primary_formats = i965_primary_formats;
15038 num_formats = ARRAY_SIZE(i965_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010015039
15040 primary->update_plane = i9xx_update_primary_plane;
15041 primary->disable_plane = i9xx_disable_primary_plane;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010015042 } else {
15043 intel_primary_formats = i8xx_primary_formats;
15044 num_formats = ARRAY_SIZE(i8xx_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010015045
15046 primary->update_plane = i9xx_update_primary_plane;
15047 primary->disable_plane = i9xx_disable_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070015048 }
15049
Ville Syrjälä580503c2016-10-31 22:37:00 +020015050 if (INTEL_GEN(dev_priv) >= 9)
15051 ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
15052 0, &intel_plane_funcs,
Ville Syrjälä38573dc2016-05-27 20:59:23 +030015053 intel_primary_formats, num_formats,
15054 DRM_PLANE_TYPE_PRIMARY,
15055 "plane 1%c", pipe_name(pipe));
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010015056 else if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
Ville Syrjälä580503c2016-10-31 22:37:00 +020015057 ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
15058 0, &intel_plane_funcs,
Ville Syrjälä38573dc2016-05-27 20:59:23 +030015059 intel_primary_formats, num_formats,
15060 DRM_PLANE_TYPE_PRIMARY,
15061 "primary %c", pipe_name(pipe));
15062 else
Ville Syrjälä580503c2016-10-31 22:37:00 +020015063 ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
15064 0, &intel_plane_funcs,
Ville Syrjälä38573dc2016-05-27 20:59:23 +030015065 intel_primary_formats, num_formats,
15066 DRM_PLANE_TYPE_PRIMARY,
15067 "plane %c", plane_name(primary->plane));
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000015068 if (ret)
15069 goto fail;
Sonika Jindal48404c12014-08-22 14:06:04 +053015070
Dave Airlie5481e272016-10-25 16:36:13 +100015071 if (INTEL_GEN(dev_priv) >= 9) {
Ville Syrjälä93ca7e02016-09-26 19:30:56 +030015072 supported_rotations =
15073 DRM_ROTATE_0 | DRM_ROTATE_90 |
15074 DRM_ROTATE_180 | DRM_ROTATE_270;
Ville Syrjälä4ea7be22016-11-14 18:54:00 +020015075 } else if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
15076 supported_rotations =
15077 DRM_ROTATE_0 | DRM_ROTATE_180 |
15078 DRM_REFLECT_X;
Dave Airlie5481e272016-10-25 16:36:13 +100015079 } else if (INTEL_GEN(dev_priv) >= 4) {
Ville Syrjälä93ca7e02016-09-26 19:30:56 +030015080 supported_rotations =
15081 DRM_ROTATE_0 | DRM_ROTATE_180;
15082 } else {
15083 supported_rotations = DRM_ROTATE_0;
15084 }
15085
Dave Airlie5481e272016-10-25 16:36:13 +100015086 if (INTEL_GEN(dev_priv) >= 4)
Ville Syrjälä93ca7e02016-09-26 19:30:56 +030015087 drm_plane_create_rotation_property(&primary->base,
15088 DRM_ROTATE_0,
15089 supported_rotations);
Sonika Jindal48404c12014-08-22 14:06:04 +053015090
Matt Roperea2c67b2014-12-23 10:41:52 -080015091 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
15092
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015093 return primary;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000015094
15095fail:
15096 kfree(state);
15097 kfree(primary);
15098
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015099 return ERR_PTR(ret);
Matt Roper465c1202014-05-29 08:06:54 -070015100}
15101
Matt Roper3d7d6512014-06-10 08:28:13 -070015102static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030015103intel_check_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020015104 struct intel_crtc_state *crtc_state,
Gustavo Padovan852e7872014-09-05 17:22:31 -030015105 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070015106{
Matt Roper2b875c22014-12-01 15:40:13 -080015107 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030015108 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Ville Syrjäläb29ec922015-12-18 19:24:39 +020015109 enum pipe pipe = to_intel_plane(plane)->pipe;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030015110 unsigned stride;
15111 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030015112
Ville Syrjäläf8856a42016-07-26 19:07:00 +030015113 ret = drm_plane_helper_check_state(&state->base,
15114 &state->clip,
15115 DRM_PLANE_HELPER_NO_SCALING,
15116 DRM_PLANE_HELPER_NO_SCALING,
15117 true, true);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030015118 if (ret)
15119 return ret;
15120
Gustavo Padovan757f9a32014-09-24 14:20:24 -030015121 /* if we want to turn off the cursor ignore width and height */
15122 if (!obj)
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020015123 return 0;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030015124
Gustavo Padovan757f9a32014-09-24 14:20:24 -030015125 /* Check for which cursor types we support */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +010015126 if (!cursor_size_ok(to_i915(plane->dev), state->base.crtc_w,
15127 state->base.crtc_h)) {
Matt Roperea2c67b2014-12-23 10:41:52 -080015128 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
15129 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030015130 return -EINVAL;
15131 }
15132
Matt Roperea2c67b2014-12-23 10:41:52 -080015133 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
15134 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030015135 DRM_DEBUG_KMS("buffer is too small\n");
15136 return -ENOMEM;
15137 }
15138
Ville Syrjälä3a656b52015-03-09 21:08:37 +020015139 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030015140 DRM_DEBUG_KMS("cursor cannot be tiled\n");
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020015141 return -EINVAL;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030015142 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030015143
Ville Syrjäläb29ec922015-12-18 19:24:39 +020015144 /*
15145 * There's something wrong with the cursor on CHV pipe C.
15146 * If it straddles the left edge of the screen then
15147 * moving it away from the edge or disabling it often
15148 * results in a pipe underrun, and often that can lead to
15149 * dead pipe (constant underrun reported, and it scans
15150 * out just a solid color). To recover from that, the
15151 * display power well must be turned off and on again.
15152 * Refuse the put the cursor into that compromised position.
15153 */
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010015154 if (IS_CHERRYVIEW(to_i915(plane->dev)) && pipe == PIPE_C &&
Ville Syrjälä936e71e2016-07-26 19:06:59 +030015155 state->base.visible && state->base.crtc_x < 0) {
Ville Syrjäläb29ec922015-12-18 19:24:39 +020015156 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
15157 return -EINVAL;
15158 }
15159
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020015160 return 0;
Gustavo Padovan852e7872014-09-05 17:22:31 -030015161}
15162
Matt Roperf4a2cf22014-12-01 15:40:12 -080015163static void
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030015164intel_disable_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst7fabf5e2015-06-15 12:33:47 +020015165 struct drm_crtc *crtc)
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030015166{
Maarten Lankhorstf2858022016-01-07 11:54:09 +010015167 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
15168
15169 intel_crtc->cursor_addr = 0;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010015170 intel_crtc_update_cursor(crtc, NULL);
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030015171}
15172
15173static void
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010015174intel_update_cursor_plane(struct drm_plane *plane,
15175 const struct intel_crtc_state *crtc_state,
15176 const struct intel_plane_state *state)
Gustavo Padovan852e7872014-09-05 17:22:31 -030015177{
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010015178 struct drm_crtc *crtc = crtc_state->base.crtc;
15179 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +000015180 struct drm_i915_private *dev_priv = to_i915(plane->dev);
Matt Roper2b875c22014-12-01 15:40:13 -080015181 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080015182 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070015183
Matt Roperf4a2cf22014-12-01 15:40:12 -080015184 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080015185 addr = 0;
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +000015186 else if (!INTEL_INFO(dev_priv)->cursor_needs_physical)
Chris Wilson058d88c2016-08-15 10:49:06 +010015187 addr = i915_gem_object_ggtt_offset(obj, NULL);
Matt Roperf4a2cf22014-12-01 15:40:12 -080015188 else
Gustavo Padovana912f122014-12-01 15:40:10 -080015189 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080015190
Gustavo Padovana912f122014-12-01 15:40:10 -080015191 intel_crtc->cursor_addr = addr;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010015192 intel_crtc_update_cursor(crtc, state);
Matt Roper3d7d6512014-06-10 08:28:13 -070015193}
Gustavo Padovan852e7872014-09-05 17:22:31 -030015194
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015195static struct intel_plane *
Ville Syrjälä580503c2016-10-31 22:37:00 +020015196intel_cursor_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
Matt Roper3d7d6512014-06-10 08:28:13 -070015197{
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000015198 struct intel_plane *cursor = NULL;
15199 struct intel_plane_state *state = NULL;
15200 int ret;
Matt Roper3d7d6512014-06-10 08:28:13 -070015201
15202 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015203 if (!cursor) {
15204 ret = -ENOMEM;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000015205 goto fail;
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015206 }
Matt Roper3d7d6512014-06-10 08:28:13 -070015207
Matt Roper8e7d6882015-01-21 16:35:41 -080015208 state = intel_create_plane_state(&cursor->base);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015209 if (!state) {
15210 ret = -ENOMEM;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000015211 goto fail;
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015212 }
15213
Matt Roper8e7d6882015-01-21 16:35:41 -080015214 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080015215
Matt Roper3d7d6512014-06-10 08:28:13 -070015216 cursor->can_scale = false;
15217 cursor->max_downscale = 1;
15218 cursor->pipe = pipe;
15219 cursor->plane = pipe;
Ville Syrjäläb14e5842016-11-22 18:01:56 +020015220 cursor->id = PLANE_CURSOR;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030015221 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080015222 cursor->check_plane = intel_check_cursor_plane;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010015223 cursor->update_plane = intel_update_cursor_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030015224 cursor->disable_plane = intel_disable_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070015225
Ville Syrjälä580503c2016-10-31 22:37:00 +020015226 ret = drm_universal_plane_init(&dev_priv->drm, &cursor->base,
15227 0, &intel_plane_funcs,
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000015228 intel_cursor_formats,
15229 ARRAY_SIZE(intel_cursor_formats),
Ville Syrjälä38573dc2016-05-27 20:59:23 +030015230 DRM_PLANE_TYPE_CURSOR,
15231 "cursor %c", pipe_name(pipe));
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000015232 if (ret)
15233 goto fail;
Ville Syrjälä4398ad42014-10-23 07:41:34 -070015234
Dave Airlie5481e272016-10-25 16:36:13 +100015235 if (INTEL_GEN(dev_priv) >= 4)
Ville Syrjälä93ca7e02016-09-26 19:30:56 +030015236 drm_plane_create_rotation_property(&cursor->base,
15237 DRM_ROTATE_0,
15238 DRM_ROTATE_0 |
15239 DRM_ROTATE_180);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070015240
Ville Syrjälä580503c2016-10-31 22:37:00 +020015241 if (INTEL_GEN(dev_priv) >= 9)
Chandra Konduruaf99ceda2015-05-11 14:35:47 -070015242 state->scaler_id = -1;
15243
Matt Roperea2c67b2014-12-23 10:41:52 -080015244 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
15245
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015246 return cursor;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000015247
15248fail:
15249 kfree(state);
15250 kfree(cursor);
15251
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015252 return ERR_PTR(ret);
Matt Roper3d7d6512014-06-10 08:28:13 -070015253}
15254
Ville Syrjälä65edccc2016-10-31 22:37:01 +020015255static void skl_init_scalers(struct drm_i915_private *dev_priv,
15256 struct intel_crtc *crtc,
15257 struct intel_crtc_state *crtc_state)
Chandra Konduru549e2bf2015-04-07 15:28:38 -070015258{
Ville Syrjälä65edccc2016-10-31 22:37:01 +020015259 struct intel_crtc_scaler_state *scaler_state =
15260 &crtc_state->scaler_state;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070015261 int i;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070015262
Ville Syrjälä65edccc2016-10-31 22:37:01 +020015263 for (i = 0; i < crtc->num_scalers; i++) {
15264 struct intel_scaler *scaler = &scaler_state->scalers[i];
15265
15266 scaler->in_use = 0;
15267 scaler->mode = PS_SCALER_MODE_DYN;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070015268 }
15269
15270 scaler_state->scaler_id = -1;
15271}
15272
Ville Syrjälä5ab0d852016-10-31 22:37:11 +020015273static int intel_crtc_init(struct drm_i915_private *dev_priv, enum pipe pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080015274{
15275 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020015276 struct intel_crtc_state *crtc_state = NULL;
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015277 struct intel_plane *primary = NULL;
15278 struct intel_plane *cursor = NULL;
Ville Syrjäläa81d6fa2016-10-25 18:58:01 +030015279 int sprite, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080015280
Daniel Vetter955382f2013-09-19 14:05:45 +020015281 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015282 if (!intel_crtc)
15283 return -ENOMEM;
Jesse Barnes79e53942008-11-07 14:24:08 -080015284
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020015285 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015286 if (!crtc_state) {
15287 ret = -ENOMEM;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020015288 goto fail;
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015289 }
Ander Conselvan de Oliveira550acef2015-04-21 17:13:24 +030015290 intel_crtc->config = crtc_state;
15291 intel_crtc->base.state = &crtc_state->base;
Matt Roper07878242015-02-25 11:43:26 -080015292 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020015293
Chandra Konduru549e2bf2015-04-07 15:28:38 -070015294 /* initialize shared scalers */
Ville Syrjälä5ab0d852016-10-31 22:37:11 +020015295 if (INTEL_GEN(dev_priv) >= 9) {
Chandra Konduru549e2bf2015-04-07 15:28:38 -070015296 if (pipe == PIPE_C)
15297 intel_crtc->num_scalers = 1;
15298 else
15299 intel_crtc->num_scalers = SKL_NUM_SCALERS;
15300
Ville Syrjälä65edccc2016-10-31 22:37:01 +020015301 skl_init_scalers(dev_priv, intel_crtc, crtc_state);
Chandra Konduru549e2bf2015-04-07 15:28:38 -070015302 }
15303
Ville Syrjälä580503c2016-10-31 22:37:00 +020015304 primary = intel_primary_plane_create(dev_priv, pipe);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015305 if (IS_ERR(primary)) {
15306 ret = PTR_ERR(primary);
Matt Roper3d7d6512014-06-10 08:28:13 -070015307 goto fail;
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015308 }
Ville Syrjäläd97d7b42016-11-22 18:01:57 +020015309 intel_crtc->plane_ids_mask |= BIT(primary->id);
Matt Roper3d7d6512014-06-10 08:28:13 -070015310
Ville Syrjäläa81d6fa2016-10-25 18:58:01 +030015311 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015312 struct intel_plane *plane;
15313
Ville Syrjälä580503c2016-10-31 22:37:00 +020015314 plane = intel_sprite_plane_create(dev_priv, pipe, sprite);
Ville Syrjäläd2b2cbc2016-11-07 22:20:56 +020015315 if (IS_ERR(plane)) {
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015316 ret = PTR_ERR(plane);
15317 goto fail;
15318 }
Ville Syrjäläd97d7b42016-11-22 18:01:57 +020015319 intel_crtc->plane_ids_mask |= BIT(plane->id);
Ville Syrjäläa81d6fa2016-10-25 18:58:01 +030015320 }
15321
Ville Syrjälä580503c2016-10-31 22:37:00 +020015322 cursor = intel_cursor_plane_create(dev_priv, pipe);
Ville Syrjäläd2b2cbc2016-11-07 22:20:56 +020015323 if (IS_ERR(cursor)) {
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015324 ret = PTR_ERR(cursor);
Matt Roper3d7d6512014-06-10 08:28:13 -070015325 goto fail;
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015326 }
Ville Syrjäläd97d7b42016-11-22 18:01:57 +020015327 intel_crtc->plane_ids_mask |= BIT(cursor->id);
Matt Roper3d7d6512014-06-10 08:28:13 -070015328
Ville Syrjälä5ab0d852016-10-31 22:37:11 +020015329 ret = drm_crtc_init_with_planes(&dev_priv->drm, &intel_crtc->base,
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015330 &primary->base, &cursor->base,
15331 &intel_crtc_funcs,
Ville Syrjälä4d5d72b72016-05-27 20:59:21 +030015332 "pipe %c", pipe_name(pipe));
Matt Roper3d7d6512014-06-10 08:28:13 -070015333 if (ret)
15334 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080015335
Jesse Barnes80824002009-09-10 15:28:06 -070015336 intel_crtc->pipe = pipe;
Ville Syrjäläe3c566d2016-11-08 16:47:11 +020015337 intel_crtc->plane = primary->plane;
Jesse Barnes80824002009-09-10 15:28:06 -070015338
Chris Wilson4b0e3332014-05-30 16:35:26 +030015339 intel_crtc->cursor_base = ~0;
15340 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030015341 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030015342
Ville Syrjälä852eb002015-06-24 22:00:07 +030015343 intel_crtc->wm.cxsr_allowed = true;
15344
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080015345 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
15346 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020015347 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = intel_crtc;
15348 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = intel_crtc;
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080015349
Jesse Barnes79e53942008-11-07 14:24:08 -080015350 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020015351
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +000015352 intel_color_init(&intel_crtc->base);
15353
Daniel Vetter87b6b102014-05-15 15:33:46 +020015354 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015355
15356 return 0;
Matt Roper3d7d6512014-06-10 08:28:13 -070015357
15358fail:
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015359 /*
15360 * drm_mode_config_cleanup() will free up any
15361 * crtcs/planes already initialized.
15362 */
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020015363 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070015364 kfree(intel_crtc);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015365
15366 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080015367}
15368
Jesse Barnes752aa882013-10-31 18:55:49 +020015369enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
15370{
15371 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015372 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020015373
Rob Clark51fd3712013-11-19 12:10:12 -050015374 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020015375
Ville Syrjäläd3babd32014-11-07 11:16:01 +020015376 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020015377 return INVALID_PIPE;
15378
15379 return to_intel_crtc(encoder->crtc)->pipe;
15380}
15381
Carl Worth08d7b3d2009-04-29 14:43:54 -070015382int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000015383 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070015384{
Carl Worth08d7b3d2009-04-29 14:43:54 -070015385 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040015386 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020015387 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070015388
Rob Clark7707e652014-07-17 23:30:04 -040015389 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Chris Wilson71240ed2016-06-24 14:00:24 +010015390 if (!drmmode_crtc)
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030015391 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070015392
Rob Clark7707e652014-07-17 23:30:04 -040015393 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020015394 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070015395
Daniel Vetterc05422d2009-08-11 16:05:30 +020015396 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070015397}
15398
Daniel Vetter66a92782012-07-12 20:08:18 +020015399static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080015400{
Daniel Vetter66a92782012-07-12 20:08:18 +020015401 struct drm_device *dev = encoder->base.dev;
15402 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080015403 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080015404 int entry = 0;
15405
Damien Lespiaub2784e12014-08-05 11:29:37 +010015406 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020015407 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020015408 index_mask |= (1 << entry);
15409
Jesse Barnes79e53942008-11-07 14:24:08 -080015410 entry++;
15411 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010015412
Jesse Barnes79e53942008-11-07 14:24:08 -080015413 return index_mask;
15414}
15415
Ville Syrjälä646d5772016-10-31 22:37:14 +020015416static bool has_edp_a(struct drm_i915_private *dev_priv)
Chris Wilson4d302442010-12-14 19:21:29 +000015417{
Ville Syrjälä646d5772016-10-31 22:37:14 +020015418 if (!IS_MOBILE(dev_priv))
Chris Wilson4d302442010-12-14 19:21:29 +000015419 return false;
15420
15421 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
15422 return false;
15423
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010015424 if (IS_GEN5(dev_priv) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000015425 return false;
15426
15427 return true;
15428}
15429
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000015430static bool intel_crt_present(struct drm_i915_private *dev_priv)
Jesse Barnes84b4e042014-06-25 08:24:29 -070015431{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000015432 if (INTEL_GEN(dev_priv) >= 9)
Damien Lespiau884497e2013-12-03 13:56:23 +000015433 return false;
15434
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +010015435 if (IS_HSW_ULT(dev_priv) || IS_BDW_ULT(dev_priv))
Jesse Barnes84b4e042014-06-25 08:24:29 -070015436 return false;
15437
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010015438 if (IS_CHERRYVIEW(dev_priv))
Jesse Barnes84b4e042014-06-25 08:24:29 -070015439 return false;
15440
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010015441 if (HAS_PCH_LPT_H(dev_priv) &&
15442 I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
Ville Syrjälä65e472e2015-12-01 23:28:55 +020015443 return false;
15444
Ville Syrjälä70ac54d2015-12-01 23:29:56 +020015445 /* DDI E can't be used if DDI A requires 4 lanes */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010015446 if (HAS_DDI(dev_priv) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
Ville Syrjälä70ac54d2015-12-01 23:29:56 +020015447 return false;
15448
Ville Syrjäläe4abb732015-12-01 23:31:33 +020015449 if (!dev_priv->vbt.int_crt_support)
Jesse Barnes84b4e042014-06-25 08:24:29 -070015450 return false;
15451
15452 return true;
15453}
15454
Imre Deak8090ba82016-08-10 14:07:33 +030015455void intel_pps_unlock_regs_wa(struct drm_i915_private *dev_priv)
15456{
15457 int pps_num;
15458 int pps_idx;
15459
15460 if (HAS_DDI(dev_priv))
15461 return;
15462 /*
15463 * This w/a is needed at least on CPT/PPT, but to be sure apply it
15464 * everywhere where registers can be write protected.
15465 */
15466 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
15467 pps_num = 2;
15468 else
15469 pps_num = 1;
15470
15471 for (pps_idx = 0; pps_idx < pps_num; pps_idx++) {
15472 u32 val = I915_READ(PP_CONTROL(pps_idx));
15473
15474 val = (val & ~PANEL_UNLOCK_MASK) | PANEL_UNLOCK_REGS;
15475 I915_WRITE(PP_CONTROL(pps_idx), val);
15476 }
15477}
15478
Imre Deak44cb7342016-08-10 14:07:29 +030015479static void intel_pps_init(struct drm_i915_private *dev_priv)
15480{
15481 if (HAS_PCH_SPLIT(dev_priv) || IS_BROXTON(dev_priv))
15482 dev_priv->pps_mmio_base = PCH_PPS_BASE;
15483 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
15484 dev_priv->pps_mmio_base = VLV_PPS_BASE;
15485 else
15486 dev_priv->pps_mmio_base = PPS_BASE;
Imre Deak8090ba82016-08-10 14:07:33 +030015487
15488 intel_pps_unlock_regs_wa(dev_priv);
Imre Deak44cb7342016-08-10 14:07:29 +030015489}
15490
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015491static void intel_setup_outputs(struct drm_i915_private *dev_priv)
Jesse Barnes79e53942008-11-07 14:24:08 -080015492{
Chris Wilson4ef69c72010-09-09 15:14:28 +010015493 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040015494 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080015495
Imre Deak44cb7342016-08-10 14:07:29 +030015496 intel_pps_init(dev_priv);
15497
Imre Deak97a824e12016-06-21 11:51:47 +030015498 /*
15499 * intel_edp_init_connector() depends on this completing first, to
15500 * prevent the registeration of both eDP and LVDS and the incorrect
15501 * sharing of the PPS.
15502 */
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015503 intel_lvds_init(dev_priv);
Jesse Barnes79e53942008-11-07 14:24:08 -080015504
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000015505 if (intel_crt_present(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015506 intel_crt_init(dev_priv);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040015507
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +010015508 if (IS_BROXTON(dev_priv)) {
Vandana Kannanc776eb22014-08-19 12:05:01 +053015509 /*
15510 * FIXME: Broxton doesn't support port detection via the
15511 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
15512 * detect the ports.
15513 */
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015514 intel_ddi_init(dev_priv, PORT_A);
15515 intel_ddi_init(dev_priv, PORT_B);
15516 intel_ddi_init(dev_priv, PORT_C);
Shashank Sharmac6c794a2016-03-22 12:01:50 +020015517
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015518 intel_dsi_init(dev_priv);
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010015519 } else if (HAS_DDI(dev_priv)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030015520 int found;
15521
Jesse Barnesde31fac2015-03-06 15:53:32 -080015522 /*
15523 * Haswell uses DDI functions to detect digital outputs.
15524 * On SKL pre-D0 the strap isn't connected, so we assume
15525 * it's there.
15526 */
Ville Syrjälä77179402015-09-18 20:03:35 +030015527 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080015528 /* WaIgnoreDDIAStrap: skl */
Tvrtko Ursulin08537232016-10-13 11:03:02 +010015529 if (found || IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015530 intel_ddi_init(dev_priv, PORT_A);
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030015531
15532 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
15533 * register */
15534 found = I915_READ(SFUSE_STRAP);
15535
15536 if (found & SFUSE_STRAP_DDIB_DETECTED)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015537 intel_ddi_init(dev_priv, PORT_B);
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030015538 if (found & SFUSE_STRAP_DDIC_DETECTED)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015539 intel_ddi_init(dev_priv, PORT_C);
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030015540 if (found & SFUSE_STRAP_DDID_DETECTED)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015541 intel_ddi_init(dev_priv, PORT_D);
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070015542 /*
15543 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
15544 */
Tvrtko Ursulin08537232016-10-13 11:03:02 +010015545 if ((IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) &&
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070015546 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
15547 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
15548 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015549 intel_ddi_init(dev_priv, PORT_E);
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070015550
Tvrtko Ursulin6e266952016-10-13 11:02:53 +010015551 } else if (HAS_PCH_SPLIT(dev_priv)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040015552 int found;
Tvrtko Ursulindd11bc12016-11-16 08:55:41 +000015553 dpd_is_edp = intel_dp_is_edp(dev_priv, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020015554
Ville Syrjälä646d5772016-10-31 22:37:14 +020015555 if (has_edp_a(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015556 intel_dp_init(dev_priv, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040015557
Paulo Zanonidc0fa712013-02-19 16:21:46 -030015558 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080015559 /* PCH SDVOB multiplex with HDMIB */
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015560 found = intel_sdvo_init(dev_priv, PCH_SDVOB, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080015561 if (!found)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015562 intel_hdmi_init(dev_priv, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080015563 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015564 intel_dp_init(dev_priv, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080015565 }
15566
Paulo Zanonidc0fa712013-02-19 16:21:46 -030015567 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015568 intel_hdmi_init(dev_priv, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080015569
Paulo Zanonidc0fa712013-02-19 16:21:46 -030015570 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015571 intel_hdmi_init(dev_priv, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080015572
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080015573 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015574 intel_dp_init(dev_priv, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080015575
Daniel Vetter270b3042012-10-27 15:52:05 +020015576 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015577 intel_dp_init(dev_priv, PCH_DP_D, PORT_D);
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010015578 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä22f350422016-06-03 12:17:43 +030015579 bool has_edp, has_port;
Chris Wilson457c52d2016-06-01 08:27:50 +010015580
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030015581 /*
15582 * The DP_DETECTED bit is the latched state of the DDC
15583 * SDA pin at boot. However since eDP doesn't require DDC
15584 * (no way to plug in a DP->HDMI dongle) the DDC pins for
15585 * eDP ports may have been muxed to an alternate function.
15586 * Thus we can't rely on the DP_DETECTED bit alone to detect
15587 * eDP ports. Consult the VBT as well as DP_DETECTED to
15588 * detect eDP ports.
Ville Syrjälä22f350422016-06-03 12:17:43 +030015589 *
15590 * Sadly the straps seem to be missing sometimes even for HDMI
15591 * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
15592 * and VBT for the presence of the port. Additionally we can't
15593 * trust the port type the VBT declares as we've seen at least
15594 * HDMI ports that the VBT claim are DP or eDP.
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030015595 */
Tvrtko Ursulindd11bc12016-11-16 08:55:41 +000015596 has_edp = intel_dp_is_edp(dev_priv, PORT_B);
Ville Syrjälä22f350422016-06-03 12:17:43 +030015597 has_port = intel_bios_is_port_present(dev_priv, PORT_B);
15598 if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015599 has_edp &= intel_dp_init(dev_priv, VLV_DP_B, PORT_B);
Ville Syrjälä22f350422016-06-03 12:17:43 +030015600 if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015601 intel_hdmi_init(dev_priv, VLV_HDMIB, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030015602
Tvrtko Ursulindd11bc12016-11-16 08:55:41 +000015603 has_edp = intel_dp_is_edp(dev_priv, PORT_C);
Ville Syrjälä22f350422016-06-03 12:17:43 +030015604 has_port = intel_bios_is_port_present(dev_priv, PORT_C);
15605 if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015606 has_edp &= intel_dp_init(dev_priv, VLV_DP_C, PORT_C);
Ville Syrjälä22f350422016-06-03 12:17:43 +030015607 if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015608 intel_hdmi_init(dev_priv, VLV_HDMIC, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053015609
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010015610 if (IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä22f350422016-06-03 12:17:43 +030015611 /*
15612 * eDP not supported on port D,
15613 * so no need to worry about it
15614 */
15615 has_port = intel_bios_is_port_present(dev_priv, PORT_D);
15616 if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015617 intel_dp_init(dev_priv, CHV_DP_D, PORT_D);
Ville Syrjälä22f350422016-06-03 12:17:43 +030015618 if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015619 intel_hdmi_init(dev_priv, CHV_HDMID, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030015620 }
15621
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015622 intel_dsi_init(dev_priv);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010015623 } else if (!IS_GEN2(dev_priv) && !IS_PINEVIEW(dev_priv)) {
Ma Ling27185ae2009-08-24 13:50:23 +080015624 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080015625
Paulo Zanonie2debe92013-02-18 19:00:27 -030015626 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080015627 DRM_DEBUG_KMS("probing SDVOB\n");
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015628 found = intel_sdvo_init(dev_priv, GEN3_SDVOB, PORT_B);
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010015629 if (!found && IS_G4X(dev_priv)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080015630 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015631 intel_hdmi_init(dev_priv, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080015632 }
Ma Ling27185ae2009-08-24 13:50:23 +080015633
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010015634 if (!found && IS_G4X(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015635 intel_dp_init(dev_priv, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080015636 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040015637
15638 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040015639
Paulo Zanonie2debe92013-02-18 19:00:27 -030015640 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080015641 DRM_DEBUG_KMS("probing SDVOC\n");
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015642 found = intel_sdvo_init(dev_priv, GEN3_SDVOC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080015643 }
Ma Ling27185ae2009-08-24 13:50:23 +080015644
Paulo Zanonie2debe92013-02-18 19:00:27 -030015645 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080015646
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010015647 if (IS_G4X(dev_priv)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080015648 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015649 intel_hdmi_init(dev_priv, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080015650 }
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010015651 if (IS_G4X(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015652 intel_dp_init(dev_priv, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080015653 }
Ma Ling27185ae2009-08-24 13:50:23 +080015654
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010015655 if (IS_G4X(dev_priv) && (I915_READ(DP_D) & DP_DETECTED))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015656 intel_dp_init(dev_priv, DP_D, PORT_D);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010015657 } else if (IS_GEN2(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015658 intel_dvo_init(dev_priv);
Jesse Barnes79e53942008-11-07 14:24:08 -080015659
Tvrtko Ursulin56b857a2016-11-07 09:29:20 +000015660 if (SUPPORTS_TV(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015661 intel_tv_init(dev_priv);
Jesse Barnes79e53942008-11-07 14:24:08 -080015662
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015663 intel_psr_init(dev_priv);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070015664
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015665 for_each_intel_encoder(&dev_priv->drm, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010015666 encoder->base.possible_crtcs = encoder->crtc_mask;
15667 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020015668 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080015669 }
Chris Wilson47356eb2011-01-11 17:06:04 +000015670
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015671 intel_init_pch_refclk(dev_priv);
Daniel Vetter270b3042012-10-27 15:52:05 +020015672
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015673 drm_helper_move_panel_connectors_to_head(&dev_priv->drm);
Jesse Barnes79e53942008-11-07 14:24:08 -080015674}
15675
15676static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
15677{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030015678 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080015679 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080015680
Daniel Vetteref2d6332014-02-10 18:00:38 +010015681 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030015682 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010015683 WARN_ON(!intel_fb->obj->framebuffer_references--);
Chris Wilsonf8c417c2016-07-20 13:31:53 +010015684 i915_gem_object_put(intel_fb->obj);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030015685 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080015686 kfree(intel_fb);
15687}
15688
15689static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000015690 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080015691 unsigned int *handle)
15692{
15693 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000015694 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080015695
Chris Wilsoncc917ab2015-10-13 14:22:26 +010015696 if (obj->userptr.mm) {
15697 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
15698 return -EINVAL;
15699 }
15700
Chris Wilson05394f32010-11-08 19:18:58 +000015701 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080015702}
15703
Rodrigo Vivi86c98582015-07-08 16:22:45 -070015704static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
15705 struct drm_file *file,
15706 unsigned flags, unsigned color,
15707 struct drm_clip_rect *clips,
15708 unsigned num_clips)
15709{
15710 struct drm_device *dev = fb->dev;
15711 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
15712 struct drm_i915_gem_object *obj = intel_fb->obj;
15713
15714 mutex_lock(&dev->struct_mutex);
Chris Wilsona6a7cc42016-11-18 21:17:46 +000015715 if (obj->pin_display && obj->cache_dirty)
15716 i915_gem_clflush_object(obj, true);
Paulo Zanoni74b4ea12015-07-14 16:29:14 -030015717 intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
Rodrigo Vivi86c98582015-07-08 16:22:45 -070015718 mutex_unlock(&dev->struct_mutex);
15719
15720 return 0;
15721}
15722
Jesse Barnes79e53942008-11-07 14:24:08 -080015723static const struct drm_framebuffer_funcs intel_fb_funcs = {
15724 .destroy = intel_user_framebuffer_destroy,
15725 .create_handle = intel_user_framebuffer_create_handle,
Rodrigo Vivi86c98582015-07-08 16:22:45 -070015726 .dirty = intel_user_framebuffer_dirty,
Jesse Barnes79e53942008-11-07 14:24:08 -080015727};
15728
Damien Lespiaub3218032015-02-27 11:15:18 +000015729static
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010015730u32 intel_fb_pitch_limit(struct drm_i915_private *dev_priv,
15731 uint64_t fb_modifier, uint32_t pixel_format)
Damien Lespiaub3218032015-02-27 11:15:18 +000015732{
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010015733 u32 gen = INTEL_INFO(dev_priv)->gen;
Damien Lespiaub3218032015-02-27 11:15:18 +000015734
15735 if (gen >= 9) {
Ville Syrjäläac484962016-01-20 21:05:26 +020015736 int cpp = drm_format_plane_cpp(pixel_format, 0);
15737
Damien Lespiaub3218032015-02-27 11:15:18 +000015738 /* "The stride in bytes must not exceed the of the size of 8K
15739 * pixels and 32K bytes."
15740 */
Ville Syrjäläac484962016-01-20 21:05:26 +020015741 return min(8192 * cpp, 32768);
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010015742 } else if (gen >= 5 && !IS_VALLEYVIEW(dev_priv) &&
15743 !IS_CHERRYVIEW(dev_priv)) {
Damien Lespiaub3218032015-02-27 11:15:18 +000015744 return 32*1024;
15745 } else if (gen >= 4) {
15746 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
15747 return 16*1024;
15748 else
15749 return 32*1024;
15750 } else if (gen >= 3) {
15751 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
15752 return 8*1024;
15753 else
15754 return 16*1024;
15755 } else {
15756 /* XXX DSPC is limited to 4k tiled */
15757 return 8*1024;
15758 }
15759}
15760
Daniel Vetterb5ea6422014-03-02 21:18:00 +010015761static int intel_framebuffer_init(struct drm_device *dev,
15762 struct intel_framebuffer *intel_fb,
15763 struct drm_mode_fb_cmd2 *mode_cmd,
15764 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080015765{
Ville Syrjälä7b49f942016-01-12 21:08:32 +020015766 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläc2ff7372016-02-11 19:16:37 +020015767 unsigned int tiling = i915_gem_object_get_tiling(obj);
Jesse Barnes79e53942008-11-07 14:24:08 -080015768 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000015769 u32 pitch_limit, stride_alignment;
Eric Engestromb3c11ac2016-11-12 01:12:56 +000015770 struct drm_format_name_buf format_name;
Jesse Barnes79e53942008-11-07 14:24:08 -080015771
Daniel Vetterdd4916c2013-10-09 21:23:51 +020015772 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
15773
Daniel Vetter2a80ead2015-02-10 17:16:06 +000015774 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
Ville Syrjäläc2ff7372016-02-11 19:16:37 +020015775 /*
15776 * If there's a fence, enforce that
15777 * the fb modifier and tiling mode match.
15778 */
15779 if (tiling != I915_TILING_NONE &&
15780 tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
Daniel Vetter2a80ead2015-02-10 17:16:06 +000015781 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
15782 return -EINVAL;
15783 }
15784 } else {
Ville Syrjäläc2ff7372016-02-11 19:16:37 +020015785 if (tiling == I915_TILING_X) {
Daniel Vetter2a80ead2015-02-10 17:16:06 +000015786 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
Ville Syrjäläc2ff7372016-02-11 19:16:37 +020015787 } else if (tiling == I915_TILING_Y) {
Daniel Vetter2a80ead2015-02-10 17:16:06 +000015788 DRM_DEBUG("No Y tiling for legacy addfb\n");
15789 return -EINVAL;
15790 }
15791 }
15792
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000015793 /* Passed in modifier sanity checking. */
15794 switch (mode_cmd->modifier[0]) {
15795 case I915_FORMAT_MOD_Y_TILED:
15796 case I915_FORMAT_MOD_Yf_TILED:
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000015797 if (INTEL_GEN(dev_priv) < 9) {
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000015798 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
15799 mode_cmd->modifier[0]);
15800 return -EINVAL;
15801 }
15802 case DRM_FORMAT_MOD_NONE:
15803 case I915_FORMAT_MOD_X_TILED:
15804 break;
15805 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070015806 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
15807 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010015808 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015809 }
Chris Wilson57cd6502010-08-08 12:34:44 +010015810
Ville Syrjäläc2ff7372016-02-11 19:16:37 +020015811 /*
15812 * gen2/3 display engine uses the fence if present,
15813 * so the tiling mode must match the fb modifier exactly.
15814 */
15815 if (INTEL_INFO(dev_priv)->gen < 4 &&
15816 tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
15817 DRM_DEBUG("tiling_mode must match fb modifier exactly on gen2/3\n");
15818 return -EINVAL;
15819 }
15820
Ville Syrjälä7b49f942016-01-12 21:08:32 +020015821 stride_alignment = intel_fb_stride_alignment(dev_priv,
15822 mode_cmd->modifier[0],
Damien Lespiaub3218032015-02-27 11:15:18 +000015823 mode_cmd->pixel_format);
15824 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
15825 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
15826 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010015827 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015828 }
Chris Wilson57cd6502010-08-08 12:34:44 +010015829
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010015830 pitch_limit = intel_fb_pitch_limit(dev_priv, mode_cmd->modifier[0],
Damien Lespiaub3218032015-02-27 11:15:18 +000015831 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010015832 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000015833 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
15834 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000015835 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010015836 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020015837 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015838 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020015839
Ville Syrjäläc2ff7372016-02-11 19:16:37 +020015840 /*
15841 * If there's a fence, enforce that
15842 * the fb pitch and fence stride match.
15843 */
15844 if (tiling != I915_TILING_NONE &&
Chris Wilson3e510a82016-08-05 10:14:23 +010015845 mode_cmd->pitches[0] != i915_gem_object_get_stride(obj)) {
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015846 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
Chris Wilson3e510a82016-08-05 10:14:23 +010015847 mode_cmd->pitches[0],
15848 i915_gem_object_get_stride(obj));
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020015849 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015850 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020015851
Ville Syrjälä57779d02012-10-31 17:50:14 +020015852 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080015853 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020015854 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020015855 case DRM_FORMAT_RGB565:
15856 case DRM_FORMAT_XRGB8888:
15857 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020015858 break;
15859 case DRM_FORMAT_XRGB1555:
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000015860 if (INTEL_GEN(dev_priv) > 3) {
Eric Engestromb3c11ac2016-11-12 01:12:56 +000015861 DRM_DEBUG("unsupported pixel format: %s\n",
15862 drm_get_format_name(mode_cmd->pixel_format, &format_name));
Ville Syrjälä57779d02012-10-31 17:50:14 +020015863 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015864 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020015865 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +020015866 case DRM_FORMAT_ABGR8888:
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010015867 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000015868 INTEL_GEN(dev_priv) < 9) {
Eric Engestromb3c11ac2016-11-12 01:12:56 +000015869 DRM_DEBUG("unsupported pixel format: %s\n",
15870 drm_get_format_name(mode_cmd->pixel_format, &format_name));
Damien Lespiau6c0fd452015-05-19 12:29:16 +010015871 return -EINVAL;
15872 }
15873 break;
15874 case DRM_FORMAT_XBGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020015875 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020015876 case DRM_FORMAT_XBGR2101010:
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000015877 if (INTEL_GEN(dev_priv) < 4) {
Eric Engestromb3c11ac2016-11-12 01:12:56 +000015878 DRM_DEBUG("unsupported pixel format: %s\n",
15879 drm_get_format_name(mode_cmd->pixel_format, &format_name));
Ville Syrjälä57779d02012-10-31 17:50:14 +020015880 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015881 }
Jesse Barnesb5626742011-06-24 12:19:27 -070015882 break;
Damien Lespiau75312082015-05-15 19:06:01 +010015883 case DRM_FORMAT_ABGR2101010:
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010015884 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
Eric Engestromb3c11ac2016-11-12 01:12:56 +000015885 DRM_DEBUG("unsupported pixel format: %s\n",
15886 drm_get_format_name(mode_cmd->pixel_format, &format_name));
Damien Lespiau75312082015-05-15 19:06:01 +010015887 return -EINVAL;
15888 }
15889 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020015890 case DRM_FORMAT_YUYV:
15891 case DRM_FORMAT_UYVY:
15892 case DRM_FORMAT_YVYU:
15893 case DRM_FORMAT_VYUY:
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000015894 if (INTEL_GEN(dev_priv) < 5) {
Eric Engestromb3c11ac2016-11-12 01:12:56 +000015895 DRM_DEBUG("unsupported pixel format: %s\n",
15896 drm_get_format_name(mode_cmd->pixel_format, &format_name));
Ville Syrjälä57779d02012-10-31 17:50:14 +020015897 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015898 }
Chris Wilson57cd6502010-08-08 12:34:44 +010015899 break;
15900 default:
Eric Engestromb3c11ac2016-11-12 01:12:56 +000015901 DRM_DEBUG("unsupported pixel format: %s\n",
15902 drm_get_format_name(mode_cmd->pixel_format, &format_name));
Chris Wilson57cd6502010-08-08 12:34:44 +010015903 return -EINVAL;
15904 }
15905
Ville Syrjälä90f9a332012-10-31 17:50:19 +020015906 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
15907 if (mode_cmd->offsets[0] != 0)
15908 return -EINVAL;
15909
Daniel Vetterc7d73f62012-12-13 23:38:38 +010015910 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
15911 intel_fb->obj = obj;
15912
Ville Syrjälä6687c902015-09-15 13:16:41 +030015913 ret = intel_fill_fb_info(dev_priv, &intel_fb->base);
15914 if (ret)
15915 return ret;
Ville Syrjälä2d7a2152016-02-15 22:54:47 +020015916
Jesse Barnes79e53942008-11-07 14:24:08 -080015917 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
15918 if (ret) {
15919 DRM_ERROR("framebuffer init failed %d\n", ret);
15920 return ret;
15921 }
15922
Ville Syrjälä0b05e1e2016-01-14 15:22:09 +020015923 intel_fb->obj->framebuffer_references++;
15924
Jesse Barnes79e53942008-11-07 14:24:08 -080015925 return 0;
15926}
15927
Jesse Barnes79e53942008-11-07 14:24:08 -080015928static struct drm_framebuffer *
15929intel_user_framebuffer_create(struct drm_device *dev,
15930 struct drm_file *filp,
Ville Syrjälä1eb83452015-11-11 19:11:29 +020015931 const struct drm_mode_fb_cmd2 *user_mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080015932{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020015933 struct drm_framebuffer *fb;
Chris Wilson05394f32010-11-08 19:18:58 +000015934 struct drm_i915_gem_object *obj;
Ville Syrjälä76dc3762015-11-11 19:11:28 +020015935 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
Jesse Barnes79e53942008-11-07 14:24:08 -080015936
Chris Wilson03ac0642016-07-20 13:31:51 +010015937 obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
15938 if (!obj)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010015939 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080015940
Daniel Vetter92907cb2015-11-23 09:04:05 +010015941 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020015942 if (IS_ERR(fb))
Chris Wilsonf0cd5182016-10-28 13:58:43 +010015943 i915_gem_object_put(obj);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020015944
15945 return fb;
Jesse Barnes79e53942008-11-07 14:24:08 -080015946}
15947
Jesse Barnes79e53942008-11-07 14:24:08 -080015948static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080015949 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020015950 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080015951 .atomic_check = intel_atomic_check,
15952 .atomic_commit = intel_atomic_commit,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +020015953 .atomic_state_alloc = intel_atomic_state_alloc,
15954 .atomic_state_clear = intel_atomic_state_clear,
Jesse Barnes79e53942008-11-07 14:24:08 -080015955};
15956
Imre Deak88212942016-03-16 13:38:53 +020015957/**
15958 * intel_init_display_hooks - initialize the display modesetting hooks
15959 * @dev_priv: device private
15960 */
15961void intel_init_display_hooks(struct drm_i915_private *dev_priv)
Jesse Barnese70236a2009-09-21 10:42:27 -070015962{
Imre Deak88212942016-03-16 13:38:53 +020015963 if (INTEL_INFO(dev_priv)->gen >= 9) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000015964 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015965 dev_priv->display.get_initial_plane_config =
15966 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000015967 dev_priv->display.crtc_compute_clock =
15968 haswell_crtc_compute_clock;
15969 dev_priv->display.crtc_enable = haswell_crtc_enable;
15970 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020015971 } else if (HAS_DDI(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010015972 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015973 dev_priv->display.get_initial_plane_config =
15974 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020015975 dev_priv->display.crtc_compute_clock =
15976 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020015977 dev_priv->display.crtc_enable = haswell_crtc_enable;
15978 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020015979 } else if (HAS_PCH_SPLIT(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010015980 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015981 dev_priv->display.get_initial_plane_config =
15982 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020015983 dev_priv->display.crtc_compute_clock =
15984 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020015985 dev_priv->display.crtc_enable = ironlake_crtc_enable;
15986 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +020015987 } else if (IS_CHERRYVIEW(dev_priv)) {
Jesse Barnes89b667f2013-04-18 14:51:36 -070015988 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015989 dev_priv->display.get_initial_plane_config =
15990 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +020015991 dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
15992 dev_priv->display.crtc_enable = valleyview_crtc_enable;
15993 dev_priv->display.crtc_disable = i9xx_crtc_disable;
15994 } else if (IS_VALLEYVIEW(dev_priv)) {
15995 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15996 dev_priv->display.get_initial_plane_config =
15997 i9xx_get_initial_plane_config;
15998 dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070015999 dev_priv->display.crtc_enable = valleyview_crtc_enable;
16000 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +020016001 } else if (IS_G4X(dev_priv)) {
16002 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
16003 dev_priv->display.get_initial_plane_config =
16004 i9xx_get_initial_plane_config;
16005 dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
16006 dev_priv->display.crtc_enable = i9xx_crtc_enable;
16007 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +020016008 } else if (IS_PINEVIEW(dev_priv)) {
16009 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
16010 dev_priv->display.get_initial_plane_config =
16011 i9xx_get_initial_plane_config;
16012 dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
16013 dev_priv->display.crtc_enable = i9xx_crtc_enable;
16014 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +020016015 } else if (!IS_GEN2(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010016016 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000016017 dev_priv->display.get_initial_plane_config =
16018 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020016019 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020016020 dev_priv->display.crtc_enable = i9xx_crtc_enable;
16021 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +020016022 } else {
16023 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
16024 dev_priv->display.get_initial_plane_config =
16025 i9xx_get_initial_plane_config;
16026 dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
16027 dev_priv->display.crtc_enable = i9xx_crtc_enable;
16028 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Eric Anholtf564048e2011-03-30 13:01:02 -070016029 }
Jesse Barnese70236a2009-09-21 10:42:27 -070016030
Jesse Barnese70236a2009-09-21 10:42:27 -070016031 /* Returns the core display clock speed */
Imre Deak88212942016-03-16 13:38:53 +020016032 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030016033 dev_priv->display.get_display_clock_speed =
16034 skylake_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020016035 else if (IS_BROXTON(dev_priv))
Bob Paauweacd3f3d2015-06-23 14:14:26 -070016036 dev_priv->display.get_display_clock_speed =
16037 broxton_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020016038 else if (IS_BROADWELL(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030016039 dev_priv->display.get_display_clock_speed =
16040 broadwell_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020016041 else if (IS_HASWELL(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030016042 dev_priv->display.get_display_clock_speed =
16043 haswell_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020016044 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070016045 dev_priv->display.get_display_clock_speed =
16046 valleyview_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020016047 else if (IS_GEN5(dev_priv))
Ville Syrjäläb37a6432015-03-31 14:11:54 +030016048 dev_priv->display.get_display_clock_speed =
16049 ilk_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020016050 else if (IS_I945G(dev_priv) || IS_BROADWATER(dev_priv) ||
16051 IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070016052 dev_priv->display.get_display_clock_speed =
16053 i945_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020016054 else if (IS_GM45(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030016055 dev_priv->display.get_display_clock_speed =
16056 gm45_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020016057 else if (IS_CRESTLINE(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030016058 dev_priv->display.get_display_clock_speed =
16059 i965gm_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020016060 else if (IS_PINEVIEW(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030016061 dev_priv->display.get_display_clock_speed =
16062 pnv_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020016063 else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030016064 dev_priv->display.get_display_clock_speed =
16065 g33_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020016066 else if (IS_I915G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070016067 dev_priv->display.get_display_clock_speed =
16068 i915_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020016069 else if (IS_I945GM(dev_priv) || IS_845G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070016070 dev_priv->display.get_display_clock_speed =
16071 i9xx_misc_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020016072 else if (IS_I915GM(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070016073 dev_priv->display.get_display_clock_speed =
16074 i915gm_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020016075 else if (IS_I865G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070016076 dev_priv->display.get_display_clock_speed =
16077 i865_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020016078 else if (IS_I85X(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070016079 dev_priv->display.get_display_clock_speed =
Ville Syrjälä1b1d2712015-05-22 11:22:31 +030016080 i85x_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030016081 else { /* 830 */
Imre Deak88212942016-03-16 13:38:53 +020016082 WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
Jesse Barnese70236a2009-09-21 10:42:27 -070016083 dev_priv->display.get_display_clock_speed =
16084 i830_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030016085 }
Jesse Barnese70236a2009-09-21 10:42:27 -070016086
Imre Deak88212942016-03-16 13:38:53 +020016087 if (IS_GEN5(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053016088 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020016089 } else if (IS_GEN6(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053016090 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020016091 } else if (IS_IVYBRIDGE(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053016092 /* FIXME: detect B0+ stepping and use auto training */
16093 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020016094 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053016095 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Ville Syrjälä445e7802016-05-11 22:44:42 +030016096 }
16097
16098 if (IS_BROADWELL(dev_priv)) {
16099 dev_priv->display.modeset_commit_cdclk =
16100 broadwell_modeset_commit_cdclk;
16101 dev_priv->display.modeset_calc_cdclk =
16102 broadwell_modeset_calc_cdclk;
Imre Deak88212942016-03-16 13:38:53 +020016103 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020016104 dev_priv->display.modeset_commit_cdclk =
16105 valleyview_modeset_commit_cdclk;
16106 dev_priv->display.modeset_calc_cdclk =
16107 valleyview_modeset_calc_cdclk;
Imre Deak88212942016-03-16 13:38:53 +020016108 } else if (IS_BROXTON(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020016109 dev_priv->display.modeset_commit_cdclk =
Imre Deak324513c2016-06-13 16:44:36 +030016110 bxt_modeset_commit_cdclk;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020016111 dev_priv->display.modeset_calc_cdclk =
Imre Deak324513c2016-06-13 16:44:36 +030016112 bxt_modeset_calc_cdclk;
Clint Taylorc89e39f2016-05-13 23:41:21 +030016113 } else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
16114 dev_priv->display.modeset_commit_cdclk =
16115 skl_modeset_commit_cdclk;
16116 dev_priv->display.modeset_calc_cdclk =
16117 skl_modeset_calc_cdclk;
Jesse Barnese70236a2009-09-21 10:42:27 -070016118 }
Daniel Vetter5a21b662016-05-24 17:13:53 +020016119
Lyude27082492016-08-24 07:48:10 +020016120 if (dev_priv->info.gen >= 9)
16121 dev_priv->display.update_crtcs = skl_update_crtcs;
16122 else
16123 dev_priv->display.update_crtcs = intel_update_crtcs;
16124
Daniel Vetter5a21b662016-05-24 17:13:53 +020016125 switch (INTEL_INFO(dev_priv)->gen) {
16126 case 2:
16127 dev_priv->display.queue_flip = intel_gen2_queue_flip;
16128 break;
16129
16130 case 3:
16131 dev_priv->display.queue_flip = intel_gen3_queue_flip;
16132 break;
16133
16134 case 4:
16135 case 5:
16136 dev_priv->display.queue_flip = intel_gen4_queue_flip;
16137 break;
16138
16139 case 6:
16140 dev_priv->display.queue_flip = intel_gen6_queue_flip;
16141 break;
16142 case 7:
16143 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
16144 dev_priv->display.queue_flip = intel_gen7_queue_flip;
16145 break;
16146 case 9:
16147 /* Drop through - unsupported since execlist only. */
16148 default:
16149 /* Default just returns -ENODEV to indicate unsupported */
16150 dev_priv->display.queue_flip = intel_default_queue_flip;
16151 }
Jesse Barnese70236a2009-09-21 10:42:27 -070016152}
16153
Jesse Barnesb690e962010-07-19 13:53:12 -070016154/*
16155 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
16156 * resume, or other times. This quirk makes sure that's the case for
16157 * affected systems.
16158 */
Akshay Joshi0206e352011-08-16 15:34:10 -040016159static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070016160{
Chris Wilsonfac5e232016-07-04 11:34:36 +010016161 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesb690e962010-07-19 13:53:12 -070016162
16163 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020016164 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070016165}
16166
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030016167static void quirk_pipeb_force(struct drm_device *dev)
16168{
Chris Wilsonfac5e232016-07-04 11:34:36 +010016169 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030016170
16171 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
16172 DRM_INFO("applying pipe b force quirk\n");
16173}
16174
Keith Packard435793d2011-07-12 14:56:22 -070016175/*
16176 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
16177 */
16178static void quirk_ssc_force_disable(struct drm_device *dev)
16179{
Chris Wilsonfac5e232016-07-04 11:34:36 +010016180 struct drm_i915_private *dev_priv = to_i915(dev);
Keith Packard435793d2011-07-12 14:56:22 -070016181 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020016182 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070016183}
16184
Carsten Emde4dca20e2012-03-15 15:56:26 +010016185/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010016186 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
16187 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010016188 */
16189static void quirk_invert_brightness(struct drm_device *dev)
16190{
Chris Wilsonfac5e232016-07-04 11:34:36 +010016191 struct drm_i915_private *dev_priv = to_i915(dev);
Carsten Emde4dca20e2012-03-15 15:56:26 +010016192 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020016193 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070016194}
16195
Scot Doyle9c72cc62014-07-03 23:27:50 +000016196/* Some VBT's incorrectly indicate no backlight is present */
16197static void quirk_backlight_present(struct drm_device *dev)
16198{
Chris Wilsonfac5e232016-07-04 11:34:36 +010016199 struct drm_i915_private *dev_priv = to_i915(dev);
Scot Doyle9c72cc62014-07-03 23:27:50 +000016200 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
16201 DRM_INFO("applying backlight present quirk\n");
16202}
16203
Jesse Barnesb690e962010-07-19 13:53:12 -070016204struct intel_quirk {
16205 int device;
16206 int subsystem_vendor;
16207 int subsystem_device;
16208 void (*hook)(struct drm_device *dev);
16209};
16210
Egbert Eich5f85f172012-10-14 15:46:38 +020016211/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
16212struct intel_dmi_quirk {
16213 void (*hook)(struct drm_device *dev);
16214 const struct dmi_system_id (*dmi_id_list)[];
16215};
16216
16217static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
16218{
16219 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
16220 return 1;
16221}
16222
16223static const struct intel_dmi_quirk intel_dmi_quirks[] = {
16224 {
16225 .dmi_id_list = &(const struct dmi_system_id[]) {
16226 {
16227 .callback = intel_dmi_reverse_brightness,
16228 .ident = "NCR Corporation",
16229 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
16230 DMI_MATCH(DMI_PRODUCT_NAME, ""),
16231 },
16232 },
16233 { } /* terminating entry */
16234 },
16235 .hook = quirk_invert_brightness,
16236 },
16237};
16238
Ben Widawskyc43b5632012-04-16 14:07:40 -070016239static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070016240 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
16241 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
16242
Jesse Barnesb690e962010-07-19 13:53:12 -070016243 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
16244 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
16245
Ville Syrjälä5f080c02014-08-15 01:22:06 +030016246 /* 830 needs to leave pipe A & dpll A up */
16247 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
16248
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030016249 /* 830 needs to leave pipe B & dpll B up */
16250 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
16251
Keith Packard435793d2011-07-12 14:56:22 -070016252 /* Lenovo U160 cannot use SSC on LVDS */
16253 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020016254
16255 /* Sony Vaio Y cannot use SSC on LVDS */
16256 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010016257
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010016258 /* Acer Aspire 5734Z must invert backlight brightness */
16259 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
16260
16261 /* Acer/eMachines G725 */
16262 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
16263
16264 /* Acer/eMachines e725 */
16265 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
16266
16267 /* Acer/Packard Bell NCL20 */
16268 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
16269
16270 /* Acer Aspire 4736Z */
16271 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020016272
16273 /* Acer Aspire 5336 */
16274 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000016275
16276 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
16277 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000016278
Scot Doyledfb3d47b2014-08-21 16:08:02 +000016279 /* Acer C720 Chromebook (Core i3 4005U) */
16280 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
16281
jens steinb2a96012014-10-28 20:25:53 +010016282 /* Apple Macbook 2,1 (Core 2 T7400) */
16283 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
16284
Jani Nikula1b9448b2015-11-05 11:49:59 +020016285 /* Apple Macbook 4,1 */
16286 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
16287
Scot Doyled4967d82014-07-03 23:27:52 +000016288 /* Toshiba CB35 Chromebook (Celeron 2955U) */
16289 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000016290
16291 /* HP Chromebook 14 (Celeron 2955U) */
16292 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020016293
16294 /* Dell Chromebook 11 */
16295 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jani Nikula9be64ee2015-10-30 14:50:24 +020016296
16297 /* Dell Chromebook 11 (2015 version) */
16298 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070016299};
16300
16301static void intel_init_quirks(struct drm_device *dev)
16302{
16303 struct pci_dev *d = dev->pdev;
16304 int i;
16305
16306 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
16307 struct intel_quirk *q = &intel_quirks[i];
16308
16309 if (d->device == q->device &&
16310 (d->subsystem_vendor == q->subsystem_vendor ||
16311 q->subsystem_vendor == PCI_ANY_ID) &&
16312 (d->subsystem_device == q->subsystem_device ||
16313 q->subsystem_device == PCI_ANY_ID))
16314 q->hook(dev);
16315 }
Egbert Eich5f85f172012-10-14 15:46:38 +020016316 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
16317 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
16318 intel_dmi_quirks[i].hook(dev);
16319 }
Jesse Barnesb690e962010-07-19 13:53:12 -070016320}
16321
Jesse Barnes9cce37f2010-08-13 15:11:26 -070016322/* Disable the VGA plane that we never use */
Tvrtko Ursulin29b74b72016-11-16 08:55:39 +000016323static void i915_disable_vga(struct drm_i915_private *dev_priv)
Jesse Barnes9cce37f2010-08-13 15:11:26 -070016324{
David Weinehall52a05c32016-08-22 13:32:44 +030016325 struct pci_dev *pdev = dev_priv->drm.pdev;
Jesse Barnes9cce37f2010-08-13 15:11:26 -070016326 u8 sr1;
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010016327 i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070016328
Ville Syrjälä2b37c612014-01-22 21:32:38 +020016329 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
David Weinehall52a05c32016-08-22 13:32:44 +030016330 vga_get_uninterruptible(pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070016331 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070016332 sr1 = inb(VGA_SR_DATA);
16333 outb(sr1 | 1<<5, VGA_SR_DATA);
David Weinehall52a05c32016-08-22 13:32:44 +030016334 vga_put(pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070016335 udelay(300);
16336
Ville Syrjälä01f5a622014-12-16 18:38:37 +020016337 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070016338 POSTING_READ(vga_reg);
16339}
16340
Daniel Vetterf8175862012-04-10 15:50:11 +020016341void intel_modeset_init_hw(struct drm_device *dev)
16342{
Chris Wilsonfac5e232016-07-04 11:34:36 +010016343 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010016344
Ville Syrjälä4c75b942016-10-31 22:37:12 +020016345 intel_update_cdclk(dev_priv);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010016346
16347 dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
16348
Ville Syrjälä46f16e62016-10-31 22:37:22 +020016349 intel_init_clock_gating(dev_priv);
Daniel Vetterf8175862012-04-10 15:50:11 +020016350}
16351
Matt Roperd93c0372015-12-03 11:37:41 -080016352/*
16353 * Calculate what we think the watermarks should be for the state we've read
16354 * out of the hardware and then immediately program those watermarks so that
16355 * we ensure the hardware settings match our internal state.
16356 *
16357 * We can calculate what we think WM's should be by creating a duplicate of the
16358 * current state (which was constructed during hardware readout) and running it
16359 * through the atomic check code to calculate new watermark values in the
16360 * state object.
16361 */
16362static void sanitize_watermarks(struct drm_device *dev)
16363{
16364 struct drm_i915_private *dev_priv = to_i915(dev);
16365 struct drm_atomic_state *state;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010016366 struct intel_atomic_state *intel_state;
Matt Roperd93c0372015-12-03 11:37:41 -080016367 struct drm_crtc *crtc;
16368 struct drm_crtc_state *cstate;
16369 struct drm_modeset_acquire_ctx ctx;
16370 int ret;
16371 int i;
16372
16373 /* Only supported on platforms that use atomic watermark design */
Matt Ropered4a6a72016-02-23 17:20:13 -080016374 if (!dev_priv->display.optimize_watermarks)
Matt Roperd93c0372015-12-03 11:37:41 -080016375 return;
16376
16377 /*
16378 * We need to hold connection_mutex before calling duplicate_state so
16379 * that the connector loop is protected.
16380 */
16381 drm_modeset_acquire_init(&ctx, 0);
16382retry:
Matt Roper0cd12622016-01-12 07:13:37 -080016383 ret = drm_modeset_lock_all_ctx(dev, &ctx);
Matt Roperd93c0372015-12-03 11:37:41 -080016384 if (ret == -EDEADLK) {
16385 drm_modeset_backoff(&ctx);
16386 goto retry;
16387 } else if (WARN_ON(ret)) {
Matt Roper0cd12622016-01-12 07:13:37 -080016388 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080016389 }
16390
16391 state = drm_atomic_helper_duplicate_state(dev, &ctx);
16392 if (WARN_ON(IS_ERR(state)))
Matt Roper0cd12622016-01-12 07:13:37 -080016393 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080016394
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010016395 intel_state = to_intel_atomic_state(state);
16396
Matt Ropered4a6a72016-02-23 17:20:13 -080016397 /*
16398 * Hardware readout is the only time we don't want to calculate
16399 * intermediate watermarks (since we don't trust the current
16400 * watermarks).
16401 */
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010016402 intel_state->skip_intermediate_wm = true;
Matt Ropered4a6a72016-02-23 17:20:13 -080016403
Matt Roperd93c0372015-12-03 11:37:41 -080016404 ret = intel_atomic_check(dev, state);
16405 if (ret) {
16406 /*
16407 * If we fail here, it means that the hardware appears to be
16408 * programmed in a way that shouldn't be possible, given our
16409 * understanding of watermark requirements. This might mean a
16410 * mistake in the hardware readout code or a mistake in the
16411 * watermark calculations for a given platform. Raise a WARN
16412 * so that this is noticeable.
16413 *
16414 * If this actually happens, we'll have to just leave the
16415 * BIOS-programmed watermarks untouched and hope for the best.
16416 */
16417 WARN(true, "Could not determine valid watermarks for inherited state\n");
Arnd Bergmannb9a1b712016-10-18 17:16:23 +020016418 goto put_state;
Matt Roperd93c0372015-12-03 11:37:41 -080016419 }
16420
16421 /* Write calculated watermark values back */
Matt Roperd93c0372015-12-03 11:37:41 -080016422 for_each_crtc_in_state(state, crtc, cstate, i) {
16423 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
16424
Matt Ropered4a6a72016-02-23 17:20:13 -080016425 cs->wm.need_postvbl_update = true;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010016426 dev_priv->display.optimize_watermarks(intel_state, cs);
Matt Roperd93c0372015-12-03 11:37:41 -080016427 }
16428
Arnd Bergmannb9a1b712016-10-18 17:16:23 +020016429put_state:
Chris Wilson08536952016-10-14 13:18:18 +010016430 drm_atomic_state_put(state);
Matt Roper0cd12622016-01-12 07:13:37 -080016431fail:
Matt Roperd93c0372015-12-03 11:37:41 -080016432 drm_modeset_drop_locks(&ctx);
16433 drm_modeset_acquire_fini(&ctx);
16434}
16435
Ville Syrjäläb079bd172016-10-25 18:58:02 +030016436int intel_modeset_init(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -080016437{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +030016438 struct drm_i915_private *dev_priv = to_i915(dev);
16439 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000016440 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080016441 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080016442
16443 drm_mode_config_init(dev);
16444
16445 dev->mode_config.min_width = 0;
16446 dev->mode_config.min_height = 0;
16447
Dave Airlie019d96c2011-09-29 16:20:42 +010016448 dev->mode_config.preferred_depth = 24;
16449 dev->mode_config.prefer_shadow = 1;
16450
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000016451 dev->mode_config.allow_fb_modifiers = true;
16452
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020016453 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080016454
Jesse Barnesb690e962010-07-19 13:53:12 -070016455 intel_init_quirks(dev);
16456
Ville Syrjälä62d75df2016-10-31 22:37:25 +020016457 intel_init_pm(dev_priv);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030016458
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +000016459 if (INTEL_INFO(dev_priv)->num_pipes == 0)
Ville Syrjäläb079bd172016-10-25 18:58:02 +030016460 return 0;
Ben Widawskye3c74752013-04-05 13:12:39 -070016461
Lukas Wunner69f92f62015-07-15 13:57:35 +020016462 /*
16463 * There may be no VBT; and if the BIOS enabled SSC we can
16464 * just keep using it to avoid unnecessary flicker. Whereas if the
16465 * BIOS isn't using it, don't assume it will work even if the VBT
16466 * indicates as much.
16467 */
Tvrtko Ursulin6e266952016-10-13 11:02:53 +010016468 if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
Lukas Wunner69f92f62015-07-15 13:57:35 +020016469 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
16470 DREF_SSC1_ENABLE);
16471
16472 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
16473 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
16474 bios_lvds_use_ssc ? "en" : "dis",
16475 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
16476 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
16477 }
16478 }
16479
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010016480 if (IS_GEN2(dev_priv)) {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010016481 dev->mode_config.max_width = 2048;
16482 dev->mode_config.max_height = 2048;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010016483 } else if (IS_GEN3(dev_priv)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070016484 dev->mode_config.max_width = 4096;
16485 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080016486 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010016487 dev->mode_config.max_width = 8192;
16488 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080016489 }
Damien Lespiau068be562014-03-28 14:17:49 +000016490
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +010016491 if (IS_845G(dev_priv) || IS_I865G(dev_priv)) {
16492 dev->mode_config.cursor_width = IS_845G(dev_priv) ? 64 : 512;
Ville Syrjälädc41c152014-08-13 11:57:05 +030016493 dev->mode_config.cursor_height = 1023;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010016494 } else if (IS_GEN2(dev_priv)) {
Damien Lespiau068be562014-03-28 14:17:49 +000016495 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
16496 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
16497 } else {
16498 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
16499 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
16500 }
16501
Joonas Lahtinen72e96d62016-03-30 16:57:10 +030016502 dev->mode_config.fb_base = ggtt->mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080016503
Zhao Yakui28c97732009-10-09 11:39:41 +080016504 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +000016505 INTEL_INFO(dev_priv)->num_pipes,
16506 INTEL_INFO(dev_priv)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080016507
Damien Lespiau055e3932014-08-18 13:49:10 +010016508 for_each_pipe(dev_priv, pipe) {
Ville Syrjäläb079bd172016-10-25 18:58:02 +030016509 int ret;
16510
Ville Syrjälä5ab0d852016-10-31 22:37:11 +020016511 ret = intel_crtc_init(dev_priv, pipe);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030016512 if (ret) {
16513 drm_mode_config_cleanup(dev);
16514 return ret;
16515 }
Jesse Barnes79e53942008-11-07 14:24:08 -080016516 }
16517
Ville Syrjäläbfa7df02015-09-24 23:29:18 +030016518 intel_update_czclk(dev_priv);
Ville Syrjälä4c75b942016-10-31 22:37:12 +020016519 intel_update_cdclk(dev_priv);
Ville Syrjäläbfa7df02015-09-24 23:29:18 +030016520
Daniel Vettere72f9fb2013-06-05 13:34:06 +020016521 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010016522
Ville Syrjäläb2045352016-05-13 23:41:27 +030016523 if (dev_priv->max_cdclk_freq == 0)
Ville Syrjälä4c75b942016-10-31 22:37:12 +020016524 intel_update_max_cdclk(dev_priv);
Ville Syrjäläb2045352016-05-13 23:41:27 +030016525
Jesse Barnes9cce37f2010-08-13 15:11:26 -070016526 /* Just disable it once at startup */
Tvrtko Ursulin29b74b72016-11-16 08:55:39 +000016527 i915_disable_vga(dev_priv);
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020016528 intel_setup_outputs(dev_priv);
Chris Wilson11be49e2012-11-15 11:32:20 +000016529
Daniel Vetter6e9f7982014-05-29 23:54:47 +020016530 drm_modeset_lock_all(dev);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016531 intel_modeset_setup_hw_state(dev);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020016532 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080016533
Damien Lespiaud3fcc802014-05-13 23:32:22 +010016534 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020016535 struct intel_initial_plane_config plane_config = {};
16536
Jesse Barnes46f297f2014-03-07 08:57:48 -080016537 if (!crtc->active)
16538 continue;
16539
Jesse Barnes46f297f2014-03-07 08:57:48 -080016540 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080016541 * Note that reserving the BIOS fb up front prevents us
16542 * from stuffing other stolen allocations like the ring
16543 * on top. This prevents some ugliness at boot time, and
16544 * can even allow for smooth boot transitions if the BIOS
16545 * fb is large enough for the active pipe configuration.
16546 */
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020016547 dev_priv->display.get_initial_plane_config(crtc,
16548 &plane_config);
16549
16550 /*
16551 * If the fb is shared between multiple heads, we'll
16552 * just get the first one.
16553 */
16554 intel_find_initial_plane_obj(crtc, &plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080016555 }
Matt Roperd93c0372015-12-03 11:37:41 -080016556
16557 /*
16558 * Make sure hardware watermarks really match the state we read out.
16559 * Note that we need to do this after reconstructing the BIOS fb's
16560 * since the watermark calculation done here will use pstate->fb.
16561 */
16562 sanitize_watermarks(dev);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030016563
16564 return 0;
Chris Wilson2c7111d2011-03-29 10:40:27 +010016565}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080016566
Daniel Vetter7fad7982012-07-04 17:51:47 +020016567static void intel_enable_pipe_a(struct drm_device *dev)
16568{
16569 struct intel_connector *connector;
16570 struct drm_connector *crt = NULL;
16571 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030016572 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020016573
16574 /* We can't just switch on the pipe A, we need to set things up with a
16575 * proper mode and output configuration. As a gross hack, enable pipe A
16576 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020016577 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020016578 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
16579 crt = &connector->base;
16580 break;
16581 }
16582 }
16583
16584 if (!crt)
16585 return;
16586
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030016587 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020016588 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020016589}
16590
Daniel Vetterfa555832012-10-10 23:14:00 +020016591static bool
16592intel_check_plane_mapping(struct intel_crtc *crtc)
16593{
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +000016594 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä649636e2015-09-22 19:50:01 +030016595 u32 val;
Daniel Vetterfa555832012-10-10 23:14:00 +020016596
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +000016597 if (INTEL_INFO(dev_priv)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020016598 return true;
16599
Ville Syrjälä649636e2015-09-22 19:50:01 +030016600 val = I915_READ(DSPCNTR(!crtc->plane));
Daniel Vetterfa555832012-10-10 23:14:00 +020016601
16602 if ((val & DISPLAY_PLANE_ENABLE) &&
16603 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
16604 return false;
16605
16606 return true;
16607}
16608
Ville Syrjälä02e93c32015-08-26 19:39:19 +030016609static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
16610{
16611 struct drm_device *dev = crtc->base.dev;
16612 struct intel_encoder *encoder;
16613
16614 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
16615 return true;
16616
16617 return false;
16618}
16619
Maarten Lankhorst496b0fc2016-08-23 16:18:07 +020016620static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
16621{
16622 struct drm_device *dev = encoder->base.dev;
16623 struct intel_connector *connector;
16624
16625 for_each_connector_on_encoder(dev, &encoder->base, connector)
16626 return connector;
16627
16628 return NULL;
16629}
16630
Ville Syrjäläa168f5b2016-08-05 20:00:17 +030016631static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
16632 enum transcoder pch_transcoder)
16633{
16634 return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
16635 (HAS_PCH_LPT_H(dev_priv) && pch_transcoder == TRANSCODER_A);
16636}
16637
Daniel Vetter24929352012-07-02 20:28:59 +020016638static void intel_sanitize_crtc(struct intel_crtc *crtc)
16639{
16640 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010016641 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikula4d1de972016-03-18 17:05:42 +020016642 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter24929352012-07-02 20:28:59 +020016643
Daniel Vetter24929352012-07-02 20:28:59 +020016644 /* Clear any frame start delays used for debugging left by the BIOS */
Jani Nikula4d1de972016-03-18 17:05:42 +020016645 if (!transcoder_is_dsi(cpu_transcoder)) {
16646 i915_reg_t reg = PIPECONF(cpu_transcoder);
16647
16648 I915_WRITE(reg,
16649 I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
16650 }
Daniel Vetter24929352012-07-02 20:28:59 +020016651
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030016652 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010016653 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030016654 if (crtc->active) {
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030016655 struct intel_plane *plane;
16656
Daniel Vetter96256042015-02-13 21:03:42 +010016657 drm_crtc_vblank_on(&crtc->base);
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030016658
16659 /* Disable everything but the primary plane */
16660 for_each_intel_plane_on_crtc(dev, crtc, plane) {
16661 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
16662 continue;
16663
16664 plane->disable_plane(&plane->base, &crtc->base);
16665 }
Daniel Vetter96256042015-02-13 21:03:42 +010016666 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030016667
Daniel Vetter24929352012-07-02 20:28:59 +020016668 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020016669 * disable the crtc (and hence change the state) if it is wrong. Note
16670 * that gen4+ has a fixed plane -> pipe mapping. */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000016671 if (INTEL_GEN(dev_priv) < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020016672 bool plane;
16673
Ville Syrjälä78108b72016-05-27 20:59:19 +030016674 DRM_DEBUG_KMS("[CRTC:%d:%s] wrong plane connection detected!\n",
16675 crtc->base.base.id, crtc->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020016676
16677 /* Pipe has the wrong plane attached and the plane is active.
16678 * Temporarily change the plane mapping and disable everything
16679 * ... */
16680 plane = crtc->plane;
Ville Syrjälä936e71e2016-07-26 19:06:59 +030016681 to_intel_plane_state(crtc->base.primary->state)->base.visible = true;
Daniel Vetter24929352012-07-02 20:28:59 +020016682 crtc->plane = !plane;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020016683 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020016684 crtc->plane = plane;
Daniel Vetter24929352012-07-02 20:28:59 +020016685 }
Daniel Vetter24929352012-07-02 20:28:59 +020016686
Daniel Vetter7fad7982012-07-04 17:51:47 +020016687 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
16688 crtc->pipe == PIPE_A && !crtc->active) {
16689 /* BIOS forgot to enable pipe A, this mostly happens after
16690 * resume. Force-enable the pipe to fix this, the update_dpms
16691 * call below we restore the pipe to the right state, but leave
16692 * the required bits on. */
16693 intel_enable_pipe_a(dev);
16694 }
16695
Daniel Vetter24929352012-07-02 20:28:59 +020016696 /* Adjust the state of the output pipe according to whether we
16697 * have active connectors/encoders. */
Maarten Lankhorst842e0302016-03-02 15:48:01 +010016698 if (crtc->active && !intel_crtc_has_encoders(crtc))
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020016699 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020016700
Tvrtko Ursulin49cff962016-10-13 11:02:54 +010016701 if (crtc->active || HAS_GMCH_DISPLAY(dev_priv)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010016702 /*
16703 * We start out with underrun reporting disabled to avoid races.
16704 * For correct bookkeeping mark this on active crtcs.
16705 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020016706 * Also on gmch platforms we dont have any hardware bits to
16707 * disable the underrun reporting. Which means we need to start
16708 * out with underrun reporting disabled also on inactive pipes,
16709 * since otherwise we'll complain about the garbage we read when
16710 * e.g. coming up after runtime pm.
16711 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010016712 * No protection against concurrent access is required - at
16713 * worst a fifo underrun happens which also sets this to false.
16714 */
16715 crtc->cpu_fifo_underrun_disabled = true;
Ville Syrjäläa168f5b2016-08-05 20:00:17 +030016716 /*
16717 * We track the PCH trancoder underrun reporting state
16718 * within the crtc. With crtc for pipe A housing the underrun
16719 * reporting state for PCH transcoder A, crtc for pipe B housing
16720 * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
16721 * and marking underrun reporting as disabled for the non-existing
16722 * PCH transcoders B and C would prevent enabling the south
16723 * error interrupt (see cpt_can_enable_serr_int()).
16724 */
16725 if (has_pch_trancoder(dev_priv, (enum transcoder)crtc->pipe))
16726 crtc->pch_fifo_underrun_disabled = true;
Daniel Vetter4cc31482014-03-24 00:01:41 +010016727 }
Daniel Vetter24929352012-07-02 20:28:59 +020016728}
16729
16730static void intel_sanitize_encoder(struct intel_encoder *encoder)
16731{
16732 struct intel_connector *connector;
Daniel Vetter24929352012-07-02 20:28:59 +020016733
16734 /* We need to check both for a crtc link (meaning that the
16735 * encoder is active and trying to read from a pipe) and the
16736 * pipe itself being active. */
16737 bool has_active_crtc = encoder->base.crtc &&
16738 to_intel_crtc(encoder->base.crtc)->active;
16739
Maarten Lankhorst496b0fc2016-08-23 16:18:07 +020016740 connector = intel_encoder_find_connector(encoder);
16741 if (connector && !has_active_crtc) {
Daniel Vetter24929352012-07-02 20:28:59 +020016742 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
16743 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030016744 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020016745
16746 /* Connector is active, but has no active pipe. This is
16747 * fallout from our resume register restoring. Disable
16748 * the encoder manually again. */
16749 if (encoder->base.crtc) {
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +020016750 struct drm_crtc_state *crtc_state = encoder->base.crtc->state;
16751
Daniel Vetter24929352012-07-02 20:28:59 +020016752 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
16753 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030016754 encoder->base.name);
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +020016755 encoder->disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030016756 if (encoder->post_disable)
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +020016757 encoder->post_disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
Daniel Vetter24929352012-07-02 20:28:59 +020016758 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020016759 encoder->base.crtc = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020016760
16761 /* Inconsistent output/port/pipe state happens presumably due to
16762 * a bug in one of the get_hw_state functions. Or someplace else
16763 * in our code, like the register restore mess on resume. Clamp
16764 * things to off as a safer default. */
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +020016765
16766 connector->base.dpms = DRM_MODE_DPMS_OFF;
16767 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020016768 }
16769 /* Enabled encoders without active connectors will be fixed in
16770 * the crtc fixup. */
16771}
16772
Tvrtko Ursulin29b74b72016-11-16 08:55:39 +000016773void i915_redisable_vga_power_on(struct drm_i915_private *dev_priv)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010016774{
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010016775 i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010016776
Imre Deak04098752014-02-18 00:02:16 +020016777 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
16778 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
Tvrtko Ursulin29b74b72016-11-16 08:55:39 +000016779 i915_disable_vga(dev_priv);
Imre Deak04098752014-02-18 00:02:16 +020016780 }
16781}
16782
Tvrtko Ursulin29b74b72016-11-16 08:55:39 +000016783void i915_redisable_vga(struct drm_i915_private *dev_priv)
Imre Deak04098752014-02-18 00:02:16 +020016784{
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030016785 /* This function can be called both from intel_modeset_setup_hw_state or
16786 * at a very early point in our resume sequence, where the power well
16787 * structures are not yet restored. Since this function is at a very
16788 * paranoid "someone might have enabled VGA while we were not looking"
16789 * level, just check if the power well is enabled instead of trying to
16790 * follow the "don't touch the power well if we don't need it" policy
16791 * the rest of the driver uses. */
Imre Deak6392f842016-02-12 18:55:13 +020016792 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030016793 return;
16794
Tvrtko Ursulin29b74b72016-11-16 08:55:39 +000016795 i915_redisable_vga_power_on(dev_priv);
Imre Deak6392f842016-02-12 18:55:13 +020016796
16797 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010016798}
16799
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030016800static bool primary_get_hw_state(struct intel_plane *plane)
Ville Syrjälä98ec7732014-04-30 17:43:01 +030016801{
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030016802 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030016803
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030016804 return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020016805}
Ville Syrjälä98ec7732014-04-30 17:43:01 +030016806
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030016807/* FIXME read out full plane state for all planes */
16808static void readout_plane_state(struct intel_crtc *crtc)
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020016809{
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020016810 struct drm_plane *primary = crtc->base.primary;
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030016811 struct intel_plane_state *plane_state =
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020016812 to_intel_plane_state(primary->state);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020016813
Ville Syrjälä936e71e2016-07-26 19:06:59 +030016814 plane_state->base.visible = crtc->active &&
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020016815 primary_get_hw_state(to_intel_plane(primary));
16816
Ville Syrjälä936e71e2016-07-26 19:06:59 +030016817 if (plane_state->base.visible)
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020016818 crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030016819}
16820
Daniel Vetter30e984d2013-06-05 13:34:17 +020016821static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020016822{
Chris Wilsonfac5e232016-07-04 11:34:36 +010016823 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020016824 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020016825 struct intel_crtc *crtc;
16826 struct intel_encoder *encoder;
16827 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020016828 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020016829
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016830 dev_priv->active_crtcs = 0;
16831
Damien Lespiaud3fcc802014-05-13 23:32:22 +010016832 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016833 struct intel_crtc_state *crtc_state = crtc->config;
16834 int pixclk = 0;
Daniel Vetter3b117c82013-04-17 20:15:07 +020016835
Daniel Vetterec2dc6a2016-05-09 16:34:09 +020016836 __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016837 memset(crtc_state, 0, sizeof(*crtc_state));
16838 crtc_state->base.crtc = &crtc->base;
Daniel Vetter24929352012-07-02 20:28:59 +020016839
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016840 crtc_state->base.active = crtc_state->base.enable =
16841 dev_priv->display.get_pipe_config(crtc, crtc_state);
16842
16843 crtc->base.enabled = crtc_state->base.enable;
16844 crtc->active = crtc_state->base.active;
16845
16846 if (crtc_state->base.active) {
16847 dev_priv->active_crtcs |= 1 << crtc->pipe;
16848
Clint Taylorc89e39f2016-05-13 23:41:21 +030016849 if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016850 pixclk = ilk_pipe_pixel_rate(crtc_state);
Ville Syrjälä9558d152016-05-13 23:41:20 +030016851 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016852 pixclk = crtc_state->base.adjusted_mode.crtc_clock;
16853 else
16854 WARN_ON(dev_priv->display.modeset_calc_cdclk);
Ville Syrjälä9558d152016-05-13 23:41:20 +030016855
16856 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
16857 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
16858 pixclk = DIV_ROUND_UP(pixclk * 100, 95);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016859 }
16860
16861 dev_priv->min_pixclk[crtc->pipe] = pixclk;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030016862
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030016863 readout_plane_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020016864
Ville Syrjälä78108b72016-05-27 20:59:19 +030016865 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
16866 crtc->base.base.id, crtc->base.name,
Tvrtko Ursulin08c4d7f2016-11-17 12:30:14 +000016867 enableddisabled(crtc->active));
Daniel Vetter24929352012-07-02 20:28:59 +020016868 }
16869
Daniel Vetter53589012013-06-05 13:34:16 +020016870 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
16871 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
16872
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020016873 pll->on = pll->funcs.get_hw_state(dev_priv, pll,
16874 &pll->config.hw_state);
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020016875 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010016876 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010016877 if (crtc->active && crtc->config->shared_dpll == pll)
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020016878 pll->config.crtc_mask |= 1 << crtc->pipe;
Daniel Vetter53589012013-06-05 13:34:16 +020016879 }
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010016880 pll->active_mask = pll->config.crtc_mask;
Daniel Vetter53589012013-06-05 13:34:16 +020016881
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020016882 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020016883 pll->name, pll->config.crtc_mask, pll->on);
Daniel Vetter53589012013-06-05 13:34:16 +020016884 }
16885
Damien Lespiaub2784e12014-08-05 11:29:37 +010016886 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020016887 pipe = 0;
16888
16889 if (encoder->get_hw_state(encoder, &pipe)) {
Ville Syrjälä98187832016-10-31 22:37:10 +020016890 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020016891
Jesse Barnes045ac3b2013-05-14 17:08:26 -070016892 encoder->base.crtc = &crtc->base;
Ville Syrjälä253c84c2016-06-22 21:57:01 +030016893 crtc->config->output_types |= 1 << encoder->type;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020016894 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020016895 } else {
16896 encoder->base.crtc = NULL;
16897 }
16898
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010016899 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Tvrtko Ursulin08c4d7f2016-11-17 12:30:14 +000016900 encoder->base.base.id, encoder->base.name,
16901 enableddisabled(encoder->base.crtc),
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010016902 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020016903 }
16904
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020016905 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020016906 if (connector->get_hw_state(connector)) {
16907 connector->base.dpms = DRM_MODE_DPMS_ON;
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010016908
16909 encoder = connector->encoder;
16910 connector->base.encoder = &encoder->base;
16911
16912 if (encoder->base.crtc &&
16913 encoder->base.crtc->state->active) {
16914 /*
16915 * This has to be done during hardware readout
16916 * because anything calling .crtc_disable may
16917 * rely on the connector_mask being accurate.
16918 */
16919 encoder->base.crtc->state->connector_mask |=
16920 1 << drm_connector_index(&connector->base);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +010016921 encoder->base.crtc->state->encoder_mask |=
16922 1 << drm_encoder_index(&encoder->base);
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010016923 }
16924
Daniel Vetter24929352012-07-02 20:28:59 +020016925 } else {
16926 connector->base.dpms = DRM_MODE_DPMS_OFF;
16927 connector->base.encoder = NULL;
16928 }
16929 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
Tvrtko Ursulin08c4d7f2016-11-17 12:30:14 +000016930 connector->base.base.id, connector->base.name,
16931 enableddisabled(connector->base.encoder));
Daniel Vetter24929352012-07-02 20:28:59 +020016932 }
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030016933
16934 for_each_intel_crtc(dev, crtc) {
16935 crtc->base.hwmode = crtc->config->base.adjusted_mode;
16936
16937 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
16938 if (crtc->base.state->active) {
16939 intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
16940 intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
16941 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
16942
16943 /*
16944 * The initial mode needs to be set in order to keep
16945 * the atomic core happy. It wants a valid mode if the
16946 * crtc's enabled, so we do the above call.
16947 *
16948 * At this point some state updated by the connectors
16949 * in their ->detect() callback has not run yet, so
16950 * no recalculation can be done yet.
16951 *
16952 * Even if we could do a recalculation and modeset
16953 * right now it would cause a double modeset if
16954 * fbdev or userspace chooses a different initial mode.
16955 *
16956 * If that happens, someone indicated they wanted a
16957 * mode change, which means it's safe to do a full
16958 * recalculation.
16959 */
16960 crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
Ville Syrjälä9eca68322015-09-10 18:59:10 +030016961
16962 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
16963 update_scanline_offset(crtc);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030016964 }
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020016965
16966 intel_pipe_config_sanity_check(dev_priv, crtc->config);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030016967 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020016968}
16969
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016970/* Scan out the current hw modeset state,
16971 * and sanitizes it to the current state
16972 */
16973static void
16974intel_modeset_setup_hw_state(struct drm_device *dev)
Daniel Vetter30e984d2013-06-05 13:34:17 +020016975{
Chris Wilsonfac5e232016-07-04 11:34:36 +010016976 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter30e984d2013-06-05 13:34:17 +020016977 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020016978 struct intel_crtc *crtc;
16979 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020016980 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020016981
16982 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020016983
16984 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010016985 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020016986 intel_sanitize_encoder(encoder);
16987 }
16988
Damien Lespiau055e3932014-08-18 13:49:10 +010016989 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä98187832016-10-31 22:37:10 +020016990 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020016991
Daniel Vetter24929352012-07-02 20:28:59 +020016992 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020016993 intel_dump_pipe_config(crtc, crtc->config,
16994 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020016995 }
Daniel Vetter9a935852012-07-05 22:34:27 +020016996
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020016997 intel_modeset_update_connector_atomic_state(dev);
16998
Daniel Vetter35c95372013-07-17 06:55:04 +020016999 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
17000 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
17001
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010017002 if (!pll->on || pll->active_mask)
Daniel Vetter35c95372013-07-17 06:55:04 +020017003 continue;
17004
17005 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
17006
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020017007 pll->funcs.disable(dev_priv, pll);
Daniel Vetter35c95372013-07-17 06:55:04 +020017008 pll->on = false;
17009 }
17010
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010017011 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Ville Syrjälä6eb1a682015-06-24 22:00:03 +030017012 vlv_wm_get_hw_state(dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010017013 else if (IS_GEN9(dev_priv))
Pradeep Bhat30789992014-11-04 17:06:45 +000017014 skl_wm_get_hw_state(dev);
Tvrtko Ursulin6e266952016-10-13 11:02:53 +010017015 else if (HAS_PCH_SPLIT(dev_priv))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030017016 ilk_wm_get_hw_state(dev);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020017017
17018 for_each_intel_crtc(dev, crtc) {
17019 unsigned long put_domains;
17020
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +010017021 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020017022 if (WARN_ON(put_domains))
17023 modeset_put_power_domains(dev_priv, put_domains);
17024 }
17025 intel_display_set_init_power(dev_priv, false);
Paulo Zanoni010cf732016-01-19 11:35:48 -020017026
17027 intel_fbc_init_pipe_state(dev_priv);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020017028}
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030017029
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020017030void intel_display_resume(struct drm_device *dev)
17031{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010017032 struct drm_i915_private *dev_priv = to_i915(dev);
17033 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
17034 struct drm_modeset_acquire_ctx ctx;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020017035 int ret;
Daniel Vetterf30da182013-04-11 20:22:50 +020017036
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010017037 dev_priv->modeset_restore_state = NULL;
Maarten Lankhorst73974892016-08-05 23:28:27 +030017038 if (state)
17039 state->acquire_ctx = &ctx;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020017040
Maarten Lankhorstea49c9a2016-02-16 15:27:42 +010017041 /*
17042 * This is a cludge because with real atomic modeset mode_config.mutex
17043 * won't be taken. Unfortunately some probed state like
17044 * audio_codec_enable is still protected by mode_config.mutex, so lock
17045 * it here for now.
17046 */
17047 mutex_lock(&dev->mode_config.mutex);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010017048 drm_modeset_acquire_init(&ctx, 0);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020017049
Maarten Lankhorst73974892016-08-05 23:28:27 +030017050 while (1) {
17051 ret = drm_modeset_lock_all_ctx(dev, &ctx);
17052 if (ret != -EDEADLK)
17053 break;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020017054
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010017055 drm_modeset_backoff(&ctx);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020017056 }
17057
Maarten Lankhorst73974892016-08-05 23:28:27 +030017058 if (!ret)
17059 ret = __intel_display_resume(dev, state);
17060
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010017061 drm_modeset_drop_locks(&ctx);
17062 drm_modeset_acquire_fini(&ctx);
Maarten Lankhorstea49c9a2016-02-16 15:27:42 +010017063 mutex_unlock(&dev->mode_config.mutex);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020017064
Chris Wilson08536952016-10-14 13:18:18 +010017065 if (ret)
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010017066 DRM_ERROR("Restoring old state failed with %i\n", ret);
Chris Wilson08536952016-10-14 13:18:18 +010017067 drm_atomic_state_put(state);
Chris Wilson2c7111d2011-03-29 10:40:27 +010017068}
17069
17070void intel_modeset_gem_init(struct drm_device *dev)
17071{
Chris Wilsondc979972016-05-10 14:10:04 +010017072 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080017073 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070017074 struct drm_i915_gem_object *obj;
Jesse Barnes484b41d2014-03-07 08:57:55 -080017075
Chris Wilsondc979972016-05-10 14:10:04 +010017076 intel_init_gt_powersave(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +030017077
Chris Wilson1833b132012-05-09 11:56:28 +010017078 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020017079
Chris Wilson1ee8da62016-05-12 12:43:23 +010017080 intel_setup_overlay(dev_priv);
Jesse Barnes484b41d2014-03-07 08:57:55 -080017081
17082 /*
17083 * Make sure any fbs we allocated at startup are properly
17084 * pinned & fenced. When we do the allocation it's too early
17085 * for this.
17086 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010017087 for_each_crtc(dev, c) {
Chris Wilson058d88c2016-08-15 10:49:06 +010017088 struct i915_vma *vma;
17089
Matt Roper2ff8fde2014-07-08 07:50:07 -070017090 obj = intel_fb_obj(c->primary->fb);
17091 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080017092 continue;
17093
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010017094 mutex_lock(&dev->struct_mutex);
Chris Wilson058d88c2016-08-15 10:49:06 +010017095 vma = intel_pin_and_fence_fb_obj(c->primary->fb,
Ville Syrjälä3465c582016-02-15 22:54:43 +020017096 c->primary->state->rotation);
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010017097 mutex_unlock(&dev->struct_mutex);
Chris Wilson058d88c2016-08-15 10:49:06 +010017098 if (IS_ERR(vma)) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080017099 DRM_ERROR("failed to pin boot fb on pipe %d\n",
17100 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100017101 drm_framebuffer_unreference(c->primary->fb);
Daniel Vetter5a21b662016-05-24 17:13:53 +020017102 c->primary->fb = NULL;
Maarten Lankhorst36750f22015-06-01 12:49:54 +020017103 c->primary->crtc = c->primary->state->crtc = NULL;
Daniel Vetter5a21b662016-05-24 17:13:53 +020017104 update_state_fb(c->primary);
Maarten Lankhorst36750f22015-06-01 12:49:54 +020017105 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
Jesse Barnes484b41d2014-03-07 08:57:55 -080017106 }
17107 }
Chris Wilson1ebaa0b2016-06-24 14:00:15 +010017108}
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020017109
Chris Wilson1ebaa0b2016-06-24 14:00:15 +010017110int intel_connector_register(struct drm_connector *connector)
17111{
17112 struct intel_connector *intel_connector = to_intel_connector(connector);
17113 int ret;
17114
17115 ret = intel_backlight_device_register(intel_connector);
17116 if (ret)
17117 goto err;
17118
17119 return 0;
17120
17121err:
17122 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080017123}
17124
Chris Wilsonc191eca2016-06-17 11:40:33 +010017125void intel_connector_unregister(struct drm_connector *connector)
Imre Deak4932e2c2014-02-11 17:12:48 +020017126{
Chris Wilsone63d87c2016-06-17 11:40:34 +010017127 struct intel_connector *intel_connector = to_intel_connector(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020017128
Chris Wilsone63d87c2016-06-17 11:40:34 +010017129 intel_backlight_device_unregister(intel_connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020017130 intel_panel_destroy_backlight(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020017131}
17132
Jesse Barnes79e53942008-11-07 14:24:08 -080017133void intel_modeset_cleanup(struct drm_device *dev)
17134{
Chris Wilsonfac5e232016-07-04 11:34:36 +010017135 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes652c3932009-08-17 13:31:43 -070017136
Chris Wilsondc979972016-05-10 14:10:04 +010017137 intel_disable_gt_powersave(dev_priv);
Imre Deak2eb52522014-11-19 15:30:05 +020017138
Daniel Vetterfd0c0642013-04-24 11:13:35 +020017139 /*
17140 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020017141 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020017142 * experience fancy races otherwise.
17143 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020017144 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070017145
Daniel Vetterfd0c0642013-04-24 11:13:35 +020017146 /*
17147 * Due to the hpd irq storm handling the hotplug work can re-arm the
17148 * poll handlers. Hence disable polling after hpd handling is shut down.
17149 */
Keith Packardf87ea762010-10-03 19:36:26 -070017150 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020017151
Jesse Barnes723bfd72010-10-07 16:01:13 -070017152 intel_unregister_dsm_handler();
17153
Paulo Zanonic937ab3e52016-01-19 11:35:46 -020017154 intel_fbc_global_disable(dev_priv);
Kristian Høgsberg69341a52009-11-11 12:19:17 -050017155
Chris Wilson1630fe72011-07-08 12:22:42 +010017156 /* flush any delayed tasks or pending work */
17157 flush_scheduled_work();
17158
Jesse Barnes79e53942008-11-07 14:24:08 -080017159 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010017160
Chris Wilson1ee8da62016-05-12 12:43:23 +010017161 intel_cleanup_overlay(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +030017162
Chris Wilsondc979972016-05-10 14:10:04 +010017163 intel_cleanup_gt_powersave(dev_priv);
Daniel Vetterf5949142016-01-13 11:55:28 +010017164
17165 intel_teardown_gmbus(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080017166}
17167
Chris Wilsondf0e9242010-09-09 16:20:55 +010017168void intel_connector_attach_encoder(struct intel_connector *connector,
17169 struct intel_encoder *encoder)
17170{
17171 connector->encoder = encoder;
17172 drm_mode_connector_attach_encoder(&connector->base,
17173 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080017174}
Dave Airlie28d52042009-09-21 14:33:58 +100017175
17176/*
17177 * set vga decode state - true == enable VGA decode
17178 */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000017179int intel_modeset_vga_set_state(struct drm_i915_private *dev_priv, bool state)
Dave Airlie28d52042009-09-21 14:33:58 +100017180{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000017181 unsigned reg = INTEL_GEN(dev_priv) >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100017182 u16 gmch_ctrl;
17183
Chris Wilson75fa0412014-02-07 18:37:02 -020017184 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
17185 DRM_ERROR("failed to read control word\n");
17186 return -EIO;
17187 }
17188
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020017189 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
17190 return 0;
17191
Dave Airlie28d52042009-09-21 14:33:58 +100017192 if (state)
17193 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
17194 else
17195 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020017196
17197 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
17198 DRM_ERROR("failed to write control word\n");
17199 return -EIO;
17200 }
17201
Dave Airlie28d52042009-09-21 14:33:58 +100017202 return 0;
17203}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017204
Chris Wilson98a2f412016-10-12 10:05:18 +010017205#if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
17206
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017207struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030017208
17209 u32 power_well_driver;
17210
Chris Wilson63b66e52013-08-08 15:12:06 +020017211 int num_transcoders;
17212
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017213 struct intel_cursor_error_state {
17214 u32 control;
17215 u32 position;
17216 u32 base;
17217 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010017218 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017219
17220 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020017221 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017222 u32 source;
Imre Deakf301b1e2014-04-18 15:55:04 +030017223 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010017224 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017225
17226 struct intel_plane_error_state {
17227 u32 control;
17228 u32 stride;
17229 u32 size;
17230 u32 pos;
17231 u32 addr;
17232 u32 surface;
17233 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010017234 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020017235
17236 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020017237 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020017238 enum transcoder cpu_transcoder;
17239
17240 u32 conf;
17241
17242 u32 htotal;
17243 u32 hblank;
17244 u32 hsync;
17245 u32 vtotal;
17246 u32 vblank;
17247 u32 vsync;
17248 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017249};
17250
17251struct intel_display_error_state *
Chris Wilsonc0336662016-05-06 15:40:21 +010017252intel_display_capture_error_state(struct drm_i915_private *dev_priv)
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017253{
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017254 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020017255 int transcoders[] = {
17256 TRANSCODER_A,
17257 TRANSCODER_B,
17258 TRANSCODER_C,
17259 TRANSCODER_EDP,
17260 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017261 int i;
17262
Chris Wilsonc0336662016-05-06 15:40:21 +010017263 if (INTEL_INFO(dev_priv)->num_pipes == 0)
Chris Wilson63b66e52013-08-08 15:12:06 +020017264 return NULL;
17265
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020017266 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017267 if (error == NULL)
17268 return NULL;
17269
Chris Wilsonc0336662016-05-06 15:40:21 +010017270 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030017271 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
17272
Damien Lespiau055e3932014-08-18 13:49:10 +010017273 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020017274 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020017275 __intel_display_power_is_enabled(dev_priv,
17276 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020017277 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020017278 continue;
17279
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030017280 error->cursor[i].control = I915_READ(CURCNTR(i));
17281 error->cursor[i].position = I915_READ(CURPOS(i));
17282 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017283
17284 error->plane[i].control = I915_READ(DSPCNTR(i));
17285 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Chris Wilsonc0336662016-05-06 15:40:21 +010017286 if (INTEL_GEN(dev_priv) <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030017287 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030017288 error->plane[i].pos = I915_READ(DSPPOS(i));
17289 }
Chris Wilsonc0336662016-05-06 15:40:21 +010017290 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
Paulo Zanonica291362013-03-06 20:03:14 -030017291 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc0336662016-05-06 15:40:21 +010017292 if (INTEL_GEN(dev_priv) >= 4) {
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017293 error->plane[i].surface = I915_READ(DSPSURF(i));
17294 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
17295 }
17296
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017297 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e2014-04-18 15:55:04 +030017298
Chris Wilsonc0336662016-05-06 15:40:21 +010017299 if (HAS_GMCH_DISPLAY(dev_priv))
Imre Deakf301b1e2014-04-18 15:55:04 +030017300 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020017301 }
17302
Jani Nikula4d1de972016-03-18 17:05:42 +020017303 /* Note: this does not include DSI transcoders. */
Chris Wilsonc0336662016-05-06 15:40:21 +010017304 error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +030017305 if (HAS_DDI(dev_priv))
Chris Wilson63b66e52013-08-08 15:12:06 +020017306 error->num_transcoders++; /* Account for eDP. */
17307
17308 for (i = 0; i < error->num_transcoders; i++) {
17309 enum transcoder cpu_transcoder = transcoders[i];
17310
Imre Deakddf9c532013-11-27 22:02:02 +020017311 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020017312 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020017313 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020017314 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020017315 continue;
17316
Chris Wilson63b66e52013-08-08 15:12:06 +020017317 error->transcoder[i].cpu_transcoder = cpu_transcoder;
17318
17319 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
17320 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
17321 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
17322 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
17323 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
17324 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
17325 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017326 }
17327
17328 return error;
17329}
17330
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030017331#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
17332
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017333void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030017334intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Tvrtko Ursulin5f56d5f2016-11-16 08:55:37 +000017335 struct drm_i915_private *dev_priv,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017336 struct intel_display_error_state *error)
17337{
17338 int i;
17339
Chris Wilson63b66e52013-08-08 15:12:06 +020017340 if (!error)
17341 return;
17342
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +000017343 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev_priv)->num_pipes);
Tvrtko Ursulin86527442016-10-13 11:03:00 +010017344 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030017345 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030017346 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010017347 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030017348 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020017349 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020017350 onoff(error->pipe[i].power_domain_on));
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030017351 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e2014-04-18 15:55:04 +030017352 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017353
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030017354 err_printf(m, "Plane [%d]:\n", i);
17355 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
17356 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Tvrtko Ursulin5f56d5f2016-11-16 08:55:37 +000017357 if (INTEL_GEN(dev_priv) <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030017358 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
17359 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030017360 }
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +010017361 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030017362 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Tvrtko Ursulin5f56d5f2016-11-16 08:55:37 +000017363 if (INTEL_GEN(dev_priv) >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030017364 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
17365 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017366 }
17367
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030017368 err_printf(m, "Cursor [%d]:\n", i);
17369 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
17370 err_printf(m, " POS: %08x\n", error->cursor[i].position);
17371 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017372 }
Chris Wilson63b66e52013-08-08 15:12:06 +020017373
17374 for (i = 0; i < error->num_transcoders; i++) {
Jani Nikulada205632016-03-15 21:51:10 +020017375 err_printf(m, "CPU transcoder: %s\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020017376 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020017377 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020017378 onoff(error->transcoder[i].power_domain_on));
Chris Wilson63b66e52013-08-08 15:12:06 +020017379 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
17380 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
17381 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
17382 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
17383 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
17384 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
17385 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
17386 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017387}
Chris Wilson98a2f412016-10-12 10:05:18 +010017388
17389#endif