blob: 175595fc3e45d239ce528662696167bac4c73327 [file] [log] [blame]
Jesse Barnes79e53942008-11-07 14:24:08 -08001/*
2 * Copyright © 2006-2007 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 */
26
Daniel Vetter618563e2012-04-01 13:38:50 +020027#include <linux/dmi.h>
Jesse Barnesc1c7af62009-09-10 15:28:03 -070028#include <linux/module.h>
29#include <linux/input.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080030#include <linux/i2c.h>
Shaohua Li7662c8b2009-06-26 11:23:55 +080031#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Jesse Barnes9cce37f2010-08-13 15:11:26 -070033#include <linux/vgaarb.h>
Wu Fengguange0dac652011-09-05 14:25:34 +080034#include <drm/drm_edid.h>
David Howells760285e2012-10-02 18:01:07 +010035#include <drm/drmP.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080036#include "intel_drv.h"
David Howells760285e2012-10-02 18:01:07 +010037#include <drm/i915_drm.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080038#include "i915_drv.h"
Chris Wilsonc37efb92016-06-17 08:28:47 +010039#include "i915_gem_dmabuf.h"
Imre Deakdb18b6a2016-03-24 12:41:40 +020040#include "intel_dsi.h"
Jesse Barnese5510fa2010-07-01 16:48:37 -070041#include "i915_trace.h"
Xi Ruoyao319c1d42015-03-12 20:16:32 +080042#include <drm/drm_atomic.h>
Matt Roperc196e1d2015-01-21 16:35:48 -080043#include <drm/drm_atomic_helper.h>
David Howells760285e2012-10-02 18:01:07 +010044#include <drm/drm_dp_helper.h>
45#include <drm/drm_crtc_helper.h>
Matt Roper465c1202014-05-29 08:06:54 -070046#include <drm/drm_plane_helper.h>
47#include <drm/drm_rect.h>
Keith Packardc0f372b32011-11-16 22:24:52 -080048#include <linux/dma_remapping.h>
Alex Goinsfd8e0582015-11-25 18:43:38 -080049#include <linux/reservation.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080050
Daniel Vetter5a21b662016-05-24 17:13:53 +020051static bool is_mmio_work(struct intel_flip_work *work)
52{
53 return work->mmio_work.func;
54}
55
Matt Roper465c1202014-05-29 08:06:54 -070056/* Primary plane formats for gen <= 3 */
Damien Lespiau568db4f2015-05-12 16:13:18 +010057static const uint32_t i8xx_primary_formats[] = {
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010058 DRM_FORMAT_C8,
59 DRM_FORMAT_RGB565,
Matt Roper465c1202014-05-29 08:06:54 -070060 DRM_FORMAT_XRGB1555,
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010061 DRM_FORMAT_XRGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070062};
63
64/* Primary plane formats for gen >= 4 */
Damien Lespiau568db4f2015-05-12 16:13:18 +010065static const uint32_t i965_primary_formats[] = {
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010066 DRM_FORMAT_C8,
67 DRM_FORMAT_RGB565,
68 DRM_FORMAT_XRGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070069 DRM_FORMAT_XBGR8888,
Damien Lespiau6c0fd452015-05-19 12:29:16 +010070 DRM_FORMAT_XRGB2101010,
71 DRM_FORMAT_XBGR2101010,
72};
73
74static const uint32_t skl_primary_formats[] = {
75 DRM_FORMAT_C8,
76 DRM_FORMAT_RGB565,
77 DRM_FORMAT_XRGB8888,
78 DRM_FORMAT_XBGR8888,
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010079 DRM_FORMAT_ARGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070080 DRM_FORMAT_ABGR8888,
81 DRM_FORMAT_XRGB2101010,
Matt Roper465c1202014-05-29 08:06:54 -070082 DRM_FORMAT_XBGR2101010,
Kumar, Maheshea916ea2015-09-03 16:17:09 +053083 DRM_FORMAT_YUYV,
84 DRM_FORMAT_YVYU,
85 DRM_FORMAT_UYVY,
86 DRM_FORMAT_VYUY,
Matt Roper465c1202014-05-29 08:06:54 -070087};
88
Matt Roper3d7d6512014-06-10 08:28:13 -070089/* Cursor formats */
90static const uint32_t intel_cursor_formats[] = {
91 DRM_FORMAT_ARGB8888,
92};
93
Jesse Barnesf1f644d2013-06-27 00:39:25 +030094static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020095 struct intel_crtc_state *pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +030096static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020097 struct intel_crtc_state *pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030098
Jesse Barneseb1bfe82014-02-12 12:26:25 -080099static int intel_framebuffer_init(struct drm_device *dev,
100 struct intel_framebuffer *ifb,
101 struct drm_mode_fb_cmd2 *mode_cmd,
102 struct drm_i915_gem_object *obj);
Daniel Vetter5b18e572014-04-24 23:55:06 +0200103static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
104static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +0200105static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +0200106static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -0700107 struct intel_link_m_n *m_n,
108 struct intel_link_m_n *m2_n2);
Daniel Vetter29407aa2014-04-24 23:55:08 +0200109static void ironlake_set_pipeconf(struct drm_crtc *crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +0200110static void haswell_set_pipeconf(struct drm_crtc *crtc);
Jani Nikula391bf042016-03-18 17:05:40 +0200111static void haswell_set_pipemisc(struct drm_crtc *crtc);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200112static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200113 const struct intel_crtc_state *pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200114static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200115 const struct intel_crtc_state *pipe_config);
Daniel Vetter5a21b662016-05-24 17:13:53 +0200116static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
117static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
Chandra Konduru549e2bf2015-04-07 15:28:38 -0700118static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
119 struct intel_crtc_state *crtc_state);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +0200120static void skylake_pfit_enable(struct intel_crtc *crtc);
121static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
122static void ironlake_pfit_enable(struct intel_crtc *crtc);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +0200123static void intel_modeset_setup_hw_state(struct drm_device *dev);
Ville Syrjälä2622a082016-03-09 19:07:26 +0200124static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
Ville Syrjälä4e5ca602016-05-11 22:44:44 +0300125static int ilk_max_pixel_rate(struct drm_atomic_state *state);
Imre Deak324513c2016-06-13 16:44:36 +0300126static int bxt_calc_cdclk(int max_pixclk);
Damien Lespiaue7457a92013-08-08 22:28:59 +0100127
Ma Lingd4906092009-03-18 20:13:27 +0800128struct intel_limit {
Ander Conselvan de Oliveira4c5def92016-05-04 12:11:58 +0300129 struct {
130 int min, max;
131 } dot, vco, n, m, m1, m2, p, p1;
132
133 struct {
134 int dot_limit;
135 int p2_slow, p2_fast;
136 } p2;
Ma Lingd4906092009-03-18 20:13:27 +0800137};
Jesse Barnes79e53942008-11-07 14:24:08 -0800138
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300139/* returns HPLL frequency in kHz */
140static int valleyview_get_vco(struct drm_i915_private *dev_priv)
141{
142 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
143
144 /* Obtain SKU information */
145 mutex_lock(&dev_priv->sb_lock);
146 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
147 CCK_FUSE_HPLL_FREQ_MASK;
148 mutex_unlock(&dev_priv->sb_lock);
149
150 return vco_freq[hpll_freq] * 1000;
151}
152
Ville Syrjäläc30fec62016-03-04 21:43:02 +0200153int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
154 const char *name, u32 reg, int ref_freq)
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300155{
156 u32 val;
157 int divider;
158
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300159 mutex_lock(&dev_priv->sb_lock);
160 val = vlv_cck_read(dev_priv, reg);
161 mutex_unlock(&dev_priv->sb_lock);
162
163 divider = val & CCK_FREQUENCY_VALUES;
164
165 WARN((val & CCK_FREQUENCY_STATUS) !=
166 (divider << CCK_FREQUENCY_STATUS_SHIFT),
167 "%s change in progress\n", name);
168
Ville Syrjäläc30fec62016-03-04 21:43:02 +0200169 return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
170}
171
172static int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
173 const char *name, u32 reg)
174{
175 if (dev_priv->hpll_freq == 0)
176 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
177
178 return vlv_get_cck_clock(dev_priv, name, reg,
179 dev_priv->hpll_freq);
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300180}
181
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200182static int
183intel_pch_rawclk(struct drm_i915_private *dev_priv)
Daniel Vetterd2acd212012-10-20 20:57:43 +0200184{
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200185 return (I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK) * 1000;
Daniel Vetterd2acd212012-10-20 20:57:43 +0200186}
187
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200188static int
189intel_vlv_hrawclk(struct drm_i915_private *dev_priv)
Jani Nikula79e50a42015-08-26 10:58:20 +0300190{
Ville Syrjälä19ab4ed2016-04-27 17:43:22 +0300191 /* RAWCLK_FREQ_VLV register updated from power well code */
Ville Syrjälä35d38d12016-03-02 17:22:16 +0200192 return vlv_get_cck_clock_hpll(dev_priv, "hrawclk",
193 CCK_DISPLAY_REF_CLOCK_CONTROL);
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200194}
195
196static int
197intel_g4x_hrawclk(struct drm_i915_private *dev_priv)
198{
Jani Nikula79e50a42015-08-26 10:58:20 +0300199 uint32_t clkcfg;
200
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200201 /* hrawclock is 1/4 the FSB frequency */
Jani Nikula79e50a42015-08-26 10:58:20 +0300202 clkcfg = I915_READ(CLKCFG);
203 switch (clkcfg & CLKCFG_FSB_MASK) {
204 case CLKCFG_FSB_400:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200205 return 100000;
Jani Nikula79e50a42015-08-26 10:58:20 +0300206 case CLKCFG_FSB_533:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200207 return 133333;
Jani Nikula79e50a42015-08-26 10:58:20 +0300208 case CLKCFG_FSB_667:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200209 return 166667;
Jani Nikula79e50a42015-08-26 10:58:20 +0300210 case CLKCFG_FSB_800:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200211 return 200000;
Jani Nikula79e50a42015-08-26 10:58:20 +0300212 case CLKCFG_FSB_1067:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200213 return 266667;
Jani Nikula79e50a42015-08-26 10:58:20 +0300214 case CLKCFG_FSB_1333:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200215 return 333333;
Jani Nikula79e50a42015-08-26 10:58:20 +0300216 /* these two are just a guess; one of them might be right */
217 case CLKCFG_FSB_1600:
218 case CLKCFG_FSB_1600_ALT:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200219 return 400000;
Jani Nikula79e50a42015-08-26 10:58:20 +0300220 default:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200221 return 133333;
Jani Nikula79e50a42015-08-26 10:58:20 +0300222 }
223}
224
Ville Syrjälä19ab4ed2016-04-27 17:43:22 +0300225void intel_update_rawclk(struct drm_i915_private *dev_priv)
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200226{
227 if (HAS_PCH_SPLIT(dev_priv))
228 dev_priv->rawclk_freq = intel_pch_rawclk(dev_priv);
229 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
230 dev_priv->rawclk_freq = intel_vlv_hrawclk(dev_priv);
231 else if (IS_G4X(dev_priv) || IS_PINEVIEW(dev_priv))
232 dev_priv->rawclk_freq = intel_g4x_hrawclk(dev_priv);
233 else
234 return; /* no rawclk on other platforms, or no need to know it */
235
236 DRM_DEBUG_DRIVER("rawclk rate: %d kHz\n", dev_priv->rawclk_freq);
237}
238
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300239static void intel_update_czclk(struct drm_i915_private *dev_priv)
240{
Wayne Boyer666a4532015-12-09 12:29:35 -0800241 if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300242 return;
243
244 dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
245 CCK_CZ_CLOCK_CONTROL);
246
247 DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
248}
249
Chris Wilson021357a2010-09-07 20:54:59 +0100250static inline u32 /* units of 100MHz */
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200251intel_fdi_link_freq(struct drm_i915_private *dev_priv,
252 const struct intel_crtc_state *pipe_config)
Chris Wilson021357a2010-09-07 20:54:59 +0100253{
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200254 if (HAS_DDI(dev_priv))
255 return pipe_config->port_clock; /* SPLL */
256 else if (IS_GEN5(dev_priv))
257 return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
Ville Syrjäläe3b247d2016-02-17 21:41:09 +0200258 else
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200259 return 270000;
Chris Wilson021357a2010-09-07 20:54:59 +0100260}
261
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300262static const struct intel_limit intel_limits_i8xx_dac = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400263 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200264 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200265 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400266 .m = { .min = 96, .max = 140 },
267 .m1 = { .min = 18, .max = 26 },
268 .m2 = { .min = 6, .max = 16 },
269 .p = { .min = 4, .max = 128 },
270 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700271 .p2 = { .dot_limit = 165000,
272 .p2_slow = 4, .p2_fast = 2 },
Keith Packarde4b36692009-06-05 19:22:17 -0700273};
274
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300275static const struct intel_limit intel_limits_i8xx_dvo = {
Daniel Vetter5d536e22013-07-06 12:52:06 +0200276 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200277 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200278 .n = { .min = 2, .max = 16 },
Daniel Vetter5d536e22013-07-06 12:52:06 +0200279 .m = { .min = 96, .max = 140 },
280 .m1 = { .min = 18, .max = 26 },
281 .m2 = { .min = 6, .max = 16 },
282 .p = { .min = 4, .max = 128 },
283 .p1 = { .min = 2, .max = 33 },
284 .p2 = { .dot_limit = 165000,
285 .p2_slow = 4, .p2_fast = 4 },
286};
287
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300288static const struct intel_limit intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400289 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200290 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200291 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400292 .m = { .min = 96, .max = 140 },
293 .m1 = { .min = 18, .max = 26 },
294 .m2 = { .min = 6, .max = 16 },
295 .p = { .min = 4, .max = 128 },
296 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700297 .p2 = { .dot_limit = 165000,
298 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700299};
Eric Anholt273e27c2011-03-30 13:01:10 -0700300
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300301static const struct intel_limit intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400302 .dot = { .min = 20000, .max = 400000 },
303 .vco = { .min = 1400000, .max = 2800000 },
304 .n = { .min = 1, .max = 6 },
305 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100306 .m1 = { .min = 8, .max = 18 },
307 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400308 .p = { .min = 5, .max = 80 },
309 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700310 .p2 = { .dot_limit = 200000,
311 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700312};
313
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300314static const struct intel_limit intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400315 .dot = { .min = 20000, .max = 400000 },
316 .vco = { .min = 1400000, .max = 2800000 },
317 .n = { .min = 1, .max = 6 },
318 .m = { .min = 70, .max = 120 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100319 .m1 = { .min = 8, .max = 18 },
320 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400321 .p = { .min = 7, .max = 98 },
322 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700323 .p2 = { .dot_limit = 112000,
324 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700325};
326
Eric Anholt273e27c2011-03-30 13:01:10 -0700327
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300328static const struct intel_limit intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700329 .dot = { .min = 25000, .max = 270000 },
330 .vco = { .min = 1750000, .max = 3500000},
331 .n = { .min = 1, .max = 4 },
332 .m = { .min = 104, .max = 138 },
333 .m1 = { .min = 17, .max = 23 },
334 .m2 = { .min = 5, .max = 11 },
335 .p = { .min = 10, .max = 30 },
336 .p1 = { .min = 1, .max = 3},
337 .p2 = { .dot_limit = 270000,
338 .p2_slow = 10,
339 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800340 },
Keith Packarde4b36692009-06-05 19:22:17 -0700341};
342
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300343static const struct intel_limit intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700344 .dot = { .min = 22000, .max = 400000 },
345 .vco = { .min = 1750000, .max = 3500000},
346 .n = { .min = 1, .max = 4 },
347 .m = { .min = 104, .max = 138 },
348 .m1 = { .min = 16, .max = 23 },
349 .m2 = { .min = 5, .max = 11 },
350 .p = { .min = 5, .max = 80 },
351 .p1 = { .min = 1, .max = 8},
352 .p2 = { .dot_limit = 165000,
353 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700354};
355
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300356static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700357 .dot = { .min = 20000, .max = 115000 },
358 .vco = { .min = 1750000, .max = 3500000 },
359 .n = { .min = 1, .max = 3 },
360 .m = { .min = 104, .max = 138 },
361 .m1 = { .min = 17, .max = 23 },
362 .m2 = { .min = 5, .max = 11 },
363 .p = { .min = 28, .max = 112 },
364 .p1 = { .min = 2, .max = 8 },
365 .p2 = { .dot_limit = 0,
366 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800367 },
Keith Packarde4b36692009-06-05 19:22:17 -0700368};
369
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300370static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700371 .dot = { .min = 80000, .max = 224000 },
372 .vco = { .min = 1750000, .max = 3500000 },
373 .n = { .min = 1, .max = 3 },
374 .m = { .min = 104, .max = 138 },
375 .m1 = { .min = 17, .max = 23 },
376 .m2 = { .min = 5, .max = 11 },
377 .p = { .min = 14, .max = 42 },
378 .p1 = { .min = 2, .max = 6 },
379 .p2 = { .dot_limit = 0,
380 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800381 },
Keith Packarde4b36692009-06-05 19:22:17 -0700382};
383
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300384static const struct intel_limit intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400385 .dot = { .min = 20000, .max = 400000},
386 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700387 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400388 .n = { .min = 3, .max = 6 },
389 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700390 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400391 .m1 = { .min = 0, .max = 0 },
392 .m2 = { .min = 0, .max = 254 },
393 .p = { .min = 5, .max = 80 },
394 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700395 .p2 = { .dot_limit = 200000,
396 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700397};
398
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300399static const struct intel_limit intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400400 .dot = { .min = 20000, .max = 400000 },
401 .vco = { .min = 1700000, .max = 3500000 },
402 .n = { .min = 3, .max = 6 },
403 .m = { .min = 2, .max = 256 },
404 .m1 = { .min = 0, .max = 0 },
405 .m2 = { .min = 0, .max = 254 },
406 .p = { .min = 7, .max = 112 },
407 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700408 .p2 = { .dot_limit = 112000,
409 .p2_slow = 14, .p2_fast = 14 },
Keith Packarde4b36692009-06-05 19:22:17 -0700410};
411
Eric Anholt273e27c2011-03-30 13:01:10 -0700412/* Ironlake / Sandybridge
413 *
414 * We calculate clock using (register_value + 2) for N/M1/M2, so here
415 * the range value for them is (actual_value - 2).
416 */
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300417static const struct intel_limit intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700418 .dot = { .min = 25000, .max = 350000 },
419 .vco = { .min = 1760000, .max = 3510000 },
420 .n = { .min = 1, .max = 5 },
421 .m = { .min = 79, .max = 127 },
422 .m1 = { .min = 12, .max = 22 },
423 .m2 = { .min = 5, .max = 9 },
424 .p = { .min = 5, .max = 80 },
425 .p1 = { .min = 1, .max = 8 },
426 .p2 = { .dot_limit = 225000,
427 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700428};
429
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300430static const struct intel_limit intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700431 .dot = { .min = 25000, .max = 350000 },
432 .vco = { .min = 1760000, .max = 3510000 },
433 .n = { .min = 1, .max = 3 },
434 .m = { .min = 79, .max = 118 },
435 .m1 = { .min = 12, .max = 22 },
436 .m2 = { .min = 5, .max = 9 },
437 .p = { .min = 28, .max = 112 },
438 .p1 = { .min = 2, .max = 8 },
439 .p2 = { .dot_limit = 225000,
440 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800441};
442
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300443static const struct intel_limit intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700444 .dot = { .min = 25000, .max = 350000 },
445 .vco = { .min = 1760000, .max = 3510000 },
446 .n = { .min = 1, .max = 3 },
447 .m = { .min = 79, .max = 127 },
448 .m1 = { .min = 12, .max = 22 },
449 .m2 = { .min = 5, .max = 9 },
450 .p = { .min = 14, .max = 56 },
451 .p1 = { .min = 2, .max = 8 },
452 .p2 = { .dot_limit = 225000,
453 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800454};
455
Eric Anholt273e27c2011-03-30 13:01:10 -0700456/* LVDS 100mhz refclk limits. */
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300457static const struct intel_limit intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700458 .dot = { .min = 25000, .max = 350000 },
459 .vco = { .min = 1760000, .max = 3510000 },
460 .n = { .min = 1, .max = 2 },
461 .m = { .min = 79, .max = 126 },
462 .m1 = { .min = 12, .max = 22 },
463 .m2 = { .min = 5, .max = 9 },
464 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400465 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700466 .p2 = { .dot_limit = 225000,
467 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800468};
469
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300470static const struct intel_limit intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700471 .dot = { .min = 25000, .max = 350000 },
472 .vco = { .min = 1760000, .max = 3510000 },
473 .n = { .min = 1, .max = 3 },
474 .m = { .min = 79, .max = 126 },
475 .m1 = { .min = 12, .max = 22 },
476 .m2 = { .min = 5, .max = 9 },
477 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400478 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700479 .p2 = { .dot_limit = 225000,
480 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800481};
482
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300483static const struct intel_limit intel_limits_vlv = {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300484 /*
485 * These are the data rate limits (measured in fast clocks)
486 * since those are the strictest limits we have. The fast
487 * clock and actual rate limits are more relaxed, so checking
488 * them would make no difference.
489 */
490 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200491 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700492 .n = { .min = 1, .max = 7 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700493 .m1 = { .min = 2, .max = 3 },
494 .m2 = { .min = 11, .max = 156 },
Ville Syrjäläb99ab662013-09-24 21:26:26 +0300495 .p1 = { .min = 2, .max = 3 },
Ville Syrjälä5fdc9c492013-09-24 21:26:29 +0300496 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700497};
498
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300499static const struct intel_limit intel_limits_chv = {
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300500 /*
501 * These are the data rate limits (measured in fast clocks)
502 * since those are the strictest limits we have. The fast
503 * clock and actual rate limits are more relaxed, so checking
504 * them would make no difference.
505 */
506 .dot = { .min = 25000 * 5, .max = 540000 * 5},
Ville Syrjälä17fe1022015-02-26 21:01:52 +0200507 .vco = { .min = 4800000, .max = 6480000 },
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300508 .n = { .min = 1, .max = 1 },
509 .m1 = { .min = 2, .max = 2 },
510 .m2 = { .min = 24 << 22, .max = 175 << 22 },
511 .p1 = { .min = 2, .max = 4 },
512 .p2 = { .p2_slow = 1, .p2_fast = 14 },
513};
514
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300515static const struct intel_limit intel_limits_bxt = {
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200516 /* FIXME: find real dot limits */
517 .dot = { .min = 0, .max = INT_MAX },
Vandana Kannane6292552015-07-01 17:02:57 +0530518 .vco = { .min = 4800000, .max = 6700000 },
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200519 .n = { .min = 1, .max = 1 },
520 .m1 = { .min = 2, .max = 2 },
521 /* FIXME: find real m2 limits */
522 .m2 = { .min = 2 << 22, .max = 255 << 22 },
523 .p1 = { .min = 2, .max = 4 },
524 .p2 = { .p2_slow = 1, .p2_fast = 20 },
525};
526
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200527static bool
528needs_modeset(struct drm_crtc_state *state)
529{
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200530 return drm_atomic_crtc_needs_modeset(state);
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200531}
532
Imre Deakdccbea32015-06-22 23:35:51 +0300533/*
534 * Platform specific helpers to calculate the port PLL loopback- (clock.m),
535 * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
536 * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
537 * The helpers' return value is the rate of the clock that is fed to the
538 * display engine's pipe which can be the above fast dot clock rate or a
539 * divided-down version of it.
540 */
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500541/* m1 is reserved as 0 in Pineview, n is a ring counter */
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300542static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800543{
Shaohua Li21778322009-02-23 15:19:16 +0800544 clock->m = clock->m2 + 2;
545 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200546 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300547 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300548 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
549 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300550
551 return clock->dot;
Shaohua Li21778322009-02-23 15:19:16 +0800552}
553
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200554static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
555{
556 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
557}
558
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300559static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800560{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200561 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800562 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200563 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300564 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300565 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
566 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300567
568 return clock->dot;
Jesse Barnes79e53942008-11-07 14:24:08 -0800569}
570
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300571static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
Imre Deak589eca62015-06-22 23:35:50 +0300572{
573 clock->m = clock->m1 * clock->m2;
574 clock->p = clock->p1 * clock->p2;
575 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300576 return 0;
Imre Deak589eca62015-06-22 23:35:50 +0300577 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
578 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300579
580 return clock->dot / 5;
Imre Deak589eca62015-06-22 23:35:50 +0300581}
582
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300583int chv_calc_dpll_params(int refclk, struct dpll *clock)
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300584{
585 clock->m = clock->m1 * clock->m2;
586 clock->p = clock->p1 * clock->p2;
587 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300588 return 0;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300589 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
590 clock->n << 22);
591 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300592
593 return clock->dot / 5;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300594}
595
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800596#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800597/**
598 * Returns whether the given set of divisors are valid for a given refclk with
599 * the given connectors.
600 */
601
Chris Wilson1b894b52010-12-14 20:04:54 +0000602static bool intel_PLL_is_valid(struct drm_device *dev,
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300603 const struct intel_limit *limit,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300604 const struct dpll *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800605{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300606 if (clock->n < limit->n.min || limit->n.max < clock->n)
607 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800608 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400609 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800610 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400611 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800612 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400613 INTELPllInvalid("m1 out of range\n");
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300614
Wayne Boyer666a4532015-12-09 12:29:35 -0800615 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) &&
616 !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev))
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300617 if (clock->m1 <= clock->m2)
618 INTELPllInvalid("m1 <= m2\n");
619
Wayne Boyer666a4532015-12-09 12:29:35 -0800620 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev)) {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300621 if (clock->p < limit->p.min || limit->p.max < clock->p)
622 INTELPllInvalid("p out of range\n");
623 if (clock->m < limit->m.min || limit->m.max < clock->m)
624 INTELPllInvalid("m out of range\n");
625 }
626
Jesse Barnes79e53942008-11-07 14:24:08 -0800627 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400628 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800629 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
630 * connector, etc., rather than just a single range.
631 */
632 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400633 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800634
635 return true;
636}
637
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300638static int
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300639i9xx_select_p2_div(const struct intel_limit *limit,
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300640 const struct intel_crtc_state *crtc_state,
641 int target)
Jesse Barnes79e53942008-11-07 14:24:08 -0800642{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300643 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800644
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +0300645 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800646 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100647 * For LVDS just rely on its current settings for dual-channel.
648 * We haven't figured out how to reliably set up different
649 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800650 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100651 if (intel_is_dual_link_lvds(dev))
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300652 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800653 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300654 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800655 } else {
656 if (target < limit->p2.dot_limit)
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300657 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800658 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300659 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800660 }
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300661}
662
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200663/*
664 * Returns a set of divisors for the desired target clock with the given
665 * refclk, or FALSE. The returned values represent the clock equation:
666 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
667 *
668 * Target and reference clocks are specified in kHz.
669 *
670 * If match_clock is provided, then best_clock P divider must match the P
671 * divider from @match_clock used for LVDS downclocking.
672 */
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300673static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300674i9xx_find_best_dpll(const struct intel_limit *limit,
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300675 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300676 int target, int refclk, struct dpll *match_clock,
677 struct dpll *best_clock)
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300678{
679 struct drm_device *dev = crtc_state->base.crtc->dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300680 struct dpll clock;
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300681 int err = target;
Jesse Barnes79e53942008-11-07 14:24:08 -0800682
Akshay Joshi0206e352011-08-16 15:34:10 -0400683 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800684
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300685 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
686
Zhao Yakui42158662009-11-20 11:24:18 +0800687 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
688 clock.m1++) {
689 for (clock.m2 = limit->m2.min;
690 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200691 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800692 break;
693 for (clock.n = limit->n.min;
694 clock.n <= limit->n.max; clock.n++) {
695 for (clock.p1 = limit->p1.min;
696 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800697 int this_err;
698
Imre Deakdccbea32015-06-22 23:35:51 +0300699 i9xx_calc_dpll_params(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000700 if (!intel_PLL_is_valid(dev, limit,
701 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800702 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800703 if (match_clock &&
704 clock.p != match_clock->p)
705 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800706
707 this_err = abs(clock.dot - target);
708 if (this_err < err) {
709 *best_clock = clock;
710 err = this_err;
711 }
712 }
713 }
714 }
715 }
716
717 return (err != target);
718}
719
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200720/*
721 * Returns a set of divisors for the desired target clock with the given
722 * refclk, or FALSE. The returned values represent the clock equation:
723 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
724 *
725 * Target and reference clocks are specified in kHz.
726 *
727 * If match_clock is provided, then best_clock P divider must match the P
728 * divider from @match_clock used for LVDS downclocking.
729 */
Ma Lingd4906092009-03-18 20:13:27 +0800730static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300731pnv_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200732 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300733 int target, int refclk, struct dpll *match_clock,
734 struct dpll *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200735{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300736 struct drm_device *dev = crtc_state->base.crtc->dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300737 struct dpll clock;
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200738 int err = target;
739
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200740 memset(best_clock, 0, sizeof(*best_clock));
741
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300742 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
743
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200744 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
745 clock.m1++) {
746 for (clock.m2 = limit->m2.min;
747 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200748 for (clock.n = limit->n.min;
749 clock.n <= limit->n.max; clock.n++) {
750 for (clock.p1 = limit->p1.min;
751 clock.p1 <= limit->p1.max; clock.p1++) {
752 int this_err;
753
Imre Deakdccbea32015-06-22 23:35:51 +0300754 pnv_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800755 if (!intel_PLL_is_valid(dev, limit,
756 &clock))
757 continue;
758 if (match_clock &&
759 clock.p != match_clock->p)
760 continue;
761
762 this_err = abs(clock.dot - target);
763 if (this_err < err) {
764 *best_clock = clock;
765 err = this_err;
766 }
767 }
768 }
769 }
770 }
771
772 return (err != target);
773}
774
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +0200775/*
776 * Returns a set of divisors for the desired target clock with the given
777 * refclk, or FALSE. The returned values represent the clock equation:
778 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200779 *
780 * Target and reference clocks are specified in kHz.
781 *
782 * If match_clock is provided, then best_clock P divider must match the P
783 * divider from @match_clock used for LVDS downclocking.
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +0200784 */
Ma Lingd4906092009-03-18 20:13:27 +0800785static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300786g4x_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200787 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300788 int target, int refclk, struct dpll *match_clock,
789 struct dpll *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800790{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300791 struct drm_device *dev = crtc_state->base.crtc->dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300792 struct dpll clock;
Ma Lingd4906092009-03-18 20:13:27 +0800793 int max_n;
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300794 bool found = false;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400795 /* approximately equals target * 0.00585 */
796 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800797
798 memset(best_clock, 0, sizeof(*best_clock));
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300799
800 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
801
Ma Lingd4906092009-03-18 20:13:27 +0800802 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200803 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800804 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200805 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800806 for (clock.m1 = limit->m1.max;
807 clock.m1 >= limit->m1.min; clock.m1--) {
808 for (clock.m2 = limit->m2.max;
809 clock.m2 >= limit->m2.min; clock.m2--) {
810 for (clock.p1 = limit->p1.max;
811 clock.p1 >= limit->p1.min; clock.p1--) {
812 int this_err;
813
Imre Deakdccbea32015-06-22 23:35:51 +0300814 i9xx_calc_dpll_params(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000815 if (!intel_PLL_is_valid(dev, limit,
816 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800817 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000818
819 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800820 if (this_err < err_most) {
821 *best_clock = clock;
822 err_most = this_err;
823 max_n = clock.n;
824 found = true;
825 }
826 }
827 }
828 }
829 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800830 return found;
831}
Ma Lingd4906092009-03-18 20:13:27 +0800832
Imre Deakd5dd62b2015-03-17 11:40:03 +0200833/*
834 * Check if the calculated PLL configuration is more optimal compared to the
835 * best configuration and error found so far. Return the calculated error.
836 */
837static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300838 const struct dpll *calculated_clock,
839 const struct dpll *best_clock,
Imre Deakd5dd62b2015-03-17 11:40:03 +0200840 unsigned int best_error_ppm,
841 unsigned int *error_ppm)
842{
Imre Deak9ca3ba02015-03-17 11:40:05 +0200843 /*
844 * For CHV ignore the error and consider only the P value.
845 * Prefer a bigger P value based on HW requirements.
846 */
847 if (IS_CHERRYVIEW(dev)) {
848 *error_ppm = 0;
849
850 return calculated_clock->p > best_clock->p;
851 }
852
Imre Deak24be4e42015-03-17 11:40:04 +0200853 if (WARN_ON_ONCE(!target_freq))
854 return false;
855
Imre Deakd5dd62b2015-03-17 11:40:03 +0200856 *error_ppm = div_u64(1000000ULL *
857 abs(target_freq - calculated_clock->dot),
858 target_freq);
859 /*
860 * Prefer a better P value over a better (smaller) error if the error
861 * is small. Ensure this preference for future configurations too by
862 * setting the error to 0.
863 */
864 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
865 *error_ppm = 0;
866
867 return true;
868 }
869
870 return *error_ppm + 10 < best_error_ppm;
871}
872
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200873/*
874 * Returns a set of divisors for the desired target clock with the given
875 * refclk, or FALSE. The returned values represent the clock equation:
876 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
877 */
Zhenyu Wang2c072452009-06-05 15:38:42 +0800878static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300879vlv_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200880 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300881 int target, int refclk, struct dpll *match_clock,
882 struct dpll *best_clock)
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700883{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200884 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300885 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300886 struct dpll clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300887 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300888 /* min update 19.2 MHz */
889 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300890 bool found = false;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700891
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300892 target *= 5; /* fast clock */
893
894 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700895
896 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300897 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300898 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300899 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300900 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300901 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700902 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300903 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Imre Deakd5dd62b2015-03-17 11:40:03 +0200904 unsigned int ppm;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300905
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300906 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
907 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300908
Imre Deakdccbea32015-06-22 23:35:51 +0300909 vlv_calc_dpll_params(refclk, &clock);
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300910
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300911 if (!intel_PLL_is_valid(dev, limit,
912 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300913 continue;
914
Imre Deakd5dd62b2015-03-17 11:40:03 +0200915 if (!vlv_PLL_is_optimal(dev, target,
916 &clock,
917 best_clock,
918 bestppm, &ppm))
919 continue;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300920
Imre Deakd5dd62b2015-03-17 11:40:03 +0200921 *best_clock = clock;
922 bestppm = ppm;
923 found = true;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700924 }
925 }
926 }
927 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700928
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300929 return found;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700930}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700931
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200932/*
933 * Returns a set of divisors for the desired target clock with the given
934 * refclk, or FALSE. The returned values represent the clock equation:
935 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
936 */
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300937static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300938chv_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200939 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300940 int target, int refclk, struct dpll *match_clock,
941 struct dpll *best_clock)
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300942{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200943 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300944 struct drm_device *dev = crtc->base.dev;
Imre Deak9ca3ba02015-03-17 11:40:05 +0200945 unsigned int best_error_ppm;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300946 struct dpll clock;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300947 uint64_t m2;
948 int found = false;
949
950 memset(best_clock, 0, sizeof(*best_clock));
Imre Deak9ca3ba02015-03-17 11:40:05 +0200951 best_error_ppm = 1000000;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300952
953 /*
954 * Based on hardware doc, the n always set to 1, and m1 always
955 * set to 2. If requires to support 200Mhz refclk, we need to
956 * revisit this because n may not 1 anymore.
957 */
958 clock.n = 1, clock.m1 = 2;
959 target *= 5; /* fast clock */
960
961 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
962 for (clock.p2 = limit->p2.p2_fast;
963 clock.p2 >= limit->p2.p2_slow;
964 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Imre Deak9ca3ba02015-03-17 11:40:05 +0200965 unsigned int error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300966
967 clock.p = clock.p1 * clock.p2;
968
969 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
970 clock.n) << 22, refclk * clock.m1);
971
972 if (m2 > INT_MAX/clock.m1)
973 continue;
974
975 clock.m2 = m2;
976
Imre Deakdccbea32015-06-22 23:35:51 +0300977 chv_calc_dpll_params(refclk, &clock);
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300978
979 if (!intel_PLL_is_valid(dev, limit, &clock))
980 continue;
981
Imre Deak9ca3ba02015-03-17 11:40:05 +0200982 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
983 best_error_ppm, &error_ppm))
984 continue;
985
986 *best_clock = clock;
987 best_error_ppm = error_ppm;
988 found = true;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300989 }
990 }
991
992 return found;
993}
994
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200995bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300996 struct dpll *best_clock)
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200997{
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200998 int refclk = 100000;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300999 const struct intel_limit *limit = &intel_limits_bxt;
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001000
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02001001 return chv_find_best_dpll(limit, crtc_state,
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001002 target_clock, refclk, NULL, best_clock);
1003}
1004
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001005bool intel_crtc_active(struct drm_crtc *crtc)
1006{
1007 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1008
1009 /* Be paranoid as we can arrive here with only partial
1010 * state retrieved from the hardware during setup.
1011 *
Damien Lespiau241bfc32013-09-25 16:45:37 +01001012 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001013 * as Haswell has gained clock readout/fastboot support.
1014 *
Dave Airlie66e514c2014-04-03 07:51:54 +10001015 * We can ditch the crtc->primary->fb check as soon as we can
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001016 * properly reconstruct framebuffers.
Matt Roperc3d1f432015-03-09 10:19:23 -07001017 *
1018 * FIXME: The intel_crtc->active here should be switched to
1019 * crtc->state->active once we have proper CRTC states wired up
1020 * for atomic.
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001021 */
Matt Roperc3d1f432015-03-09 10:19:23 -07001022 return intel_crtc->active && crtc->primary->state->fb &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001023 intel_crtc->config->base.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001024}
1025
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001026enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1027 enum pipe pipe)
1028{
1029 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1030 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1031
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001032 return intel_crtc->config->cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001033}
1034
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001035static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1036{
Chris Wilsonfac5e232016-07-04 11:34:36 +01001037 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001038 i915_reg_t reg = PIPEDSL(pipe);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001039 u32 line1, line2;
1040 u32 line_mask;
1041
1042 if (IS_GEN2(dev))
1043 line_mask = DSL_LINEMASK_GEN2;
1044 else
1045 line_mask = DSL_LINEMASK_GEN3;
1046
1047 line1 = I915_READ(reg) & line_mask;
Daniel Vetter6adfb1e2015-07-07 09:10:40 +02001048 msleep(5);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001049 line2 = I915_READ(reg) & line_mask;
1050
1051 return line1 == line2;
1052}
1053
Keith Packardab7ad7f2010-10-03 00:33:06 -07001054/*
1055 * intel_wait_for_pipe_off - wait for pipe to turn off
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001056 * @crtc: crtc whose pipe to wait for
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001057 *
1058 * After disabling a pipe, we can't wait for vblank in the usual way,
1059 * spinning on the vblank interrupt status bit, since we won't actually
1060 * see an interrupt when the pipe is disabled.
1061 *
Keith Packardab7ad7f2010-10-03 00:33:06 -07001062 * On Gen4 and above:
1063 * wait for the pipe register state bit to turn off
1064 *
1065 * Otherwise:
1066 * wait for the display line value to settle (it usually
1067 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +01001068 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001069 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001070static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001071{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001072 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01001073 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001074 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001075 enum pipe pipe = crtc->pipe;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001076
Keith Packardab7ad7f2010-10-03 00:33:06 -07001077 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001078 i915_reg_t reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001079
Keith Packardab7ad7f2010-10-03 00:33:06 -07001080 /* Wait for the Pipe State to go off */
Chris Wilsonb8511f52016-06-30 15:32:53 +01001081 if (intel_wait_for_register(dev_priv,
1082 reg, I965_PIPECONF_ACTIVE, 0,
1083 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001084 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001085 } else {
Keith Packardab7ad7f2010-10-03 00:33:06 -07001086 /* Wait for the display line to settle */
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001087 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001088 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001089 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001090}
1091
Jesse Barnesb24e7172011-01-04 15:09:30 -08001092/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +02001093void assert_pll(struct drm_i915_private *dev_priv,
1094 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001095{
Jesse Barnesb24e7172011-01-04 15:09:30 -08001096 u32 val;
1097 bool cur_state;
1098
Ville Syrjälä649636e2015-09-22 19:50:01 +03001099 val = I915_READ(DPLL(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001100 cur_state = !!(val & DPLL_VCO_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001101 I915_STATE_WARN(cur_state != state,
Jesse Barnesb24e7172011-01-04 15:09:30 -08001102 "PLL state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001103 onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001104}
Jesse Barnesb24e7172011-01-04 15:09:30 -08001105
Jani Nikula23538ef2013-08-27 15:12:22 +03001106/* XXX: the dsi pll is shared between MIPI DSI ports */
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +00001107void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
Jani Nikula23538ef2013-08-27 15:12:22 +03001108{
1109 u32 val;
1110 bool cur_state;
1111
Ville Syrjäläa5805162015-05-26 20:42:30 +03001112 mutex_lock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001113 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
Ville Syrjäläa5805162015-05-26 20:42:30 +03001114 mutex_unlock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001115
1116 cur_state = val & DSI_PLL_VCO_EN;
Rob Clarke2c719b2014-12-15 13:56:32 -05001117 I915_STATE_WARN(cur_state != state,
Jani Nikula23538ef2013-08-27 15:12:22 +03001118 "DSI PLL state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001119 onoff(state), onoff(cur_state));
Jani Nikula23538ef2013-08-27 15:12:22 +03001120}
Jani Nikula23538ef2013-08-27 15:12:22 +03001121
Jesse Barnes040484a2011-01-03 12:14:26 -08001122static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1123 enum pipe pipe, bool state)
1124{
Jesse Barnes040484a2011-01-03 12:14:26 -08001125 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001126 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1127 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001128
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001129 if (HAS_DDI(dev_priv)) {
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001130 /* DDI does not have a specific FDI_TX register */
Ville Syrjälä649636e2015-09-22 19:50:01 +03001131 u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
Paulo Zanoniad80a812012-10-24 16:06:19 -02001132 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001133 } else {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001134 u32 val = I915_READ(FDI_TX_CTL(pipe));
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001135 cur_state = !!(val & FDI_TX_ENABLE);
1136 }
Rob Clarke2c719b2014-12-15 13:56:32 -05001137 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001138 "FDI TX state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001139 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001140}
1141#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1142#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1143
1144static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1145 enum pipe pipe, bool state)
1146{
Jesse Barnes040484a2011-01-03 12:14:26 -08001147 u32 val;
1148 bool cur_state;
1149
Ville Syrjälä649636e2015-09-22 19:50:01 +03001150 val = I915_READ(FDI_RX_CTL(pipe));
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001151 cur_state = !!(val & FDI_RX_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001152 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001153 "FDI RX state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001154 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001155}
1156#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1157#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1158
1159static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1160 enum pipe pipe)
1161{
Jesse Barnes040484a2011-01-03 12:14:26 -08001162 u32 val;
1163
1164 /* ILK FDI PLL is always enabled */
Tvrtko Ursulin7e22dbb2016-05-10 10:57:06 +01001165 if (IS_GEN5(dev_priv))
Jesse Barnes040484a2011-01-03 12:14:26 -08001166 return;
1167
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001168 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001169 if (HAS_DDI(dev_priv))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001170 return;
1171
Ville Syrjälä649636e2015-09-22 19:50:01 +03001172 val = I915_READ(FDI_TX_CTL(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001173 I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
Jesse Barnes040484a2011-01-03 12:14:26 -08001174}
1175
Daniel Vetter55607e82013-06-16 21:42:39 +02001176void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1177 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001178{
Jesse Barnes040484a2011-01-03 12:14:26 -08001179 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001180 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001181
Ville Syrjälä649636e2015-09-22 19:50:01 +03001182 val = I915_READ(FDI_RX_CTL(pipe));
Daniel Vetter55607e82013-06-16 21:42:39 +02001183 cur_state = !!(val & FDI_RX_PLL_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001184 I915_STATE_WARN(cur_state != state,
Daniel Vetter55607e82013-06-16 21:42:39 +02001185 "FDI RX PLL assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001186 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001187}
1188
Daniel Vetterb680c372014-09-19 18:27:27 +02001189void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1190 enum pipe pipe)
Jesse Barnesea0760c2011-01-04 15:09:32 -08001191{
Chris Wilson91c8a322016-07-05 10:40:23 +01001192 struct drm_device *dev = &dev_priv->drm;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001193 i915_reg_t pp_reg;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001194 u32 val;
1195 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001196 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001197
Jani Nikulabedd4db2014-08-22 15:04:13 +03001198 if (WARN_ON(HAS_DDI(dev)))
1199 return;
1200
1201 if (HAS_PCH_SPLIT(dev)) {
1202 u32 port_sel;
1203
Jesse Barnesea0760c2011-01-04 15:09:32 -08001204 pp_reg = PCH_PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001205 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1206
1207 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1208 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1209 panel_pipe = PIPE_B;
1210 /* XXX: else fix for eDP */
Wayne Boyer666a4532015-12-09 12:29:35 -08001211 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Jani Nikulabedd4db2014-08-22 15:04:13 +03001212 /* presumably write lock depends on pipe, not port select */
1213 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1214 panel_pipe = pipe;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001215 } else {
1216 pp_reg = PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001217 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1218 panel_pipe = PIPE_B;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001219 }
1220
1221 val = I915_READ(pp_reg);
1222 if (!(val & PANEL_POWER_ON) ||
Jani Nikulaec49ba22014-08-21 15:06:25 +03001223 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
Jesse Barnesea0760c2011-01-04 15:09:32 -08001224 locked = false;
1225
Rob Clarke2c719b2014-12-15 13:56:32 -05001226 I915_STATE_WARN(panel_pipe == pipe && locked,
Jesse Barnesea0760c2011-01-04 15:09:32 -08001227 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001228 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001229}
1230
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001231static void assert_cursor(struct drm_i915_private *dev_priv,
1232 enum pipe pipe, bool state)
1233{
Chris Wilson91c8a322016-07-05 10:40:23 +01001234 struct drm_device *dev = &dev_priv->drm;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001235 bool cur_state;
1236
Paulo Zanonid9d82082014-02-27 16:30:56 -03001237 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä0b87c242015-09-22 19:47:51 +03001238 cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
Paulo Zanonid9d82082014-02-27 16:30:56 -03001239 else
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03001240 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001241
Rob Clarke2c719b2014-12-15 13:56:32 -05001242 I915_STATE_WARN(cur_state != state,
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001243 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001244 pipe_name(pipe), onoff(state), onoff(cur_state));
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001245}
1246#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1247#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1248
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001249void assert_pipe(struct drm_i915_private *dev_priv,
1250 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001251{
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001252 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001253 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1254 pipe);
Imre Deak4feed0e2016-02-12 18:55:14 +02001255 enum intel_display_power_domain power_domain;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001256
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001257 /* if we need the pipe quirk it must be always on */
1258 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1259 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetter8e636782012-01-22 01:36:48 +01001260 state = true;
1261
Imre Deak4feed0e2016-02-12 18:55:14 +02001262 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
1263 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001264 u32 val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni69310162013-01-29 16:35:19 -02001265 cur_state = !!(val & PIPECONF_ENABLE);
Imre Deak4feed0e2016-02-12 18:55:14 +02001266
1267 intel_display_power_put(dev_priv, power_domain);
1268 } else {
1269 cur_state = false;
Paulo Zanoni69310162013-01-29 16:35:19 -02001270 }
1271
Rob Clarke2c719b2014-12-15 13:56:32 -05001272 I915_STATE_WARN(cur_state != state,
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001273 "pipe %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001274 pipe_name(pipe), onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001275}
1276
Chris Wilson931872f2012-01-16 23:01:13 +00001277static void assert_plane(struct drm_i915_private *dev_priv,
1278 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001279{
Jesse Barnesb24e7172011-01-04 15:09:30 -08001280 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001281 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001282
Ville Syrjälä649636e2015-09-22 19:50:01 +03001283 val = I915_READ(DSPCNTR(plane));
Chris Wilson931872f2012-01-16 23:01:13 +00001284 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001285 I915_STATE_WARN(cur_state != state,
Chris Wilson931872f2012-01-16 23:01:13 +00001286 "plane %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001287 plane_name(plane), onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001288}
1289
Chris Wilson931872f2012-01-16 23:01:13 +00001290#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1291#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1292
Jesse Barnesb24e7172011-01-04 15:09:30 -08001293static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1294 enum pipe pipe)
1295{
Chris Wilson91c8a322016-07-05 10:40:23 +01001296 struct drm_device *dev = &dev_priv->drm;
Ville Syrjälä649636e2015-09-22 19:50:01 +03001297 int i;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001298
Ville Syrjälä653e1022013-06-04 13:49:05 +03001299 /* Primary planes are fixed to pipes on gen4+ */
1300 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001301 u32 val = I915_READ(DSPCNTR(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001302 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
Adam Jackson28c057942011-10-07 14:38:42 -04001303 "plane %c assertion failure, should be disabled but not\n",
1304 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001305 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001306 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001307
Jesse Barnesb24e7172011-01-04 15:09:30 -08001308 /* Need to check both planes against the pipe */
Damien Lespiau055e3932014-08-18 13:49:10 +01001309 for_each_pipe(dev_priv, i) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001310 u32 val = I915_READ(DSPCNTR(i));
1311 enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
Jesse Barnesb24e7172011-01-04 15:09:30 -08001312 DISPPLANE_SEL_PIPE_SHIFT;
Rob Clarke2c719b2014-12-15 13:56:32 -05001313 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001314 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1315 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001316 }
1317}
1318
Jesse Barnes19332d72013-03-28 09:55:38 -07001319static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1320 enum pipe pipe)
1321{
Chris Wilson91c8a322016-07-05 10:40:23 +01001322 struct drm_device *dev = &dev_priv->drm;
Ville Syrjälä649636e2015-09-22 19:50:01 +03001323 int sprite;
Jesse Barnes19332d72013-03-28 09:55:38 -07001324
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001325 if (INTEL_INFO(dev)->gen >= 9) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001326 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001327 u32 val = I915_READ(PLANE_CTL(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001328 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001329 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1330 sprite, pipe_name(pipe));
1331 }
Wayne Boyer666a4532015-12-09 12:29:35 -08001332 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001333 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001334 u32 val = I915_READ(SPCNTR(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001335 I915_STATE_WARN(val & SP_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001336 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +00001337 sprite_name(pipe, sprite), pipe_name(pipe));
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001338 }
1339 } else if (INTEL_INFO(dev)->gen >= 7) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001340 u32 val = I915_READ(SPRCTL(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001341 I915_STATE_WARN(val & SPRITE_ENABLE,
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001342 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001343 plane_name(pipe), pipe_name(pipe));
1344 } else if (INTEL_INFO(dev)->gen >= 5) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001345 u32 val = I915_READ(DVSCNTR(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001346 I915_STATE_WARN(val & DVS_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001347 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1348 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001349 }
1350}
1351
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001352static void assert_vblank_disabled(struct drm_crtc *crtc)
1353{
Rob Clarke2c719b2014-12-15 13:56:32 -05001354 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001355 drm_crtc_vblank_put(crtc);
1356}
1357
Ander Conselvan de Oliveira7abd4b32016-03-08 17:46:15 +02001358void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1359 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001360{
Jesse Barnes92f25842011-01-04 15:09:34 -08001361 u32 val;
1362 bool enabled;
1363
Ville Syrjälä649636e2015-09-22 19:50:01 +03001364 val = I915_READ(PCH_TRANSCONF(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001365 enabled = !!(val & TRANS_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001366 I915_STATE_WARN(enabled,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001367 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1368 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001369}
1370
Keith Packard4e634382011-08-06 10:39:45 -07001371static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1372 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001373{
1374 if ((val & DP_PORT_EN) == 0)
1375 return false;
1376
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001377 if (HAS_PCH_CPT(dev_priv)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001378 u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
Keith Packardf0575e92011-07-25 22:12:43 -07001379 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1380 return false;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001381 } else if (IS_CHERRYVIEW(dev_priv)) {
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001382 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1383 return false;
Keith Packardf0575e92011-07-25 22:12:43 -07001384 } else {
1385 if ((val & DP_PIPE_MASK) != (pipe << 30))
1386 return false;
1387 }
1388 return true;
1389}
1390
Keith Packard1519b992011-08-06 10:35:34 -07001391static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1392 enum pipe pipe, u32 val)
1393{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001394 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001395 return false;
1396
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001397 if (HAS_PCH_CPT(dev_priv)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001398 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001399 return false;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001400 } else if (IS_CHERRYVIEW(dev_priv)) {
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001401 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1402 return false;
Keith Packard1519b992011-08-06 10:35:34 -07001403 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001404 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001405 return false;
1406 }
1407 return true;
1408}
1409
1410static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1411 enum pipe pipe, u32 val)
1412{
1413 if ((val & LVDS_PORT_EN) == 0)
1414 return false;
1415
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001416 if (HAS_PCH_CPT(dev_priv)) {
Keith Packard1519b992011-08-06 10:35:34 -07001417 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1418 return false;
1419 } else {
1420 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1421 return false;
1422 }
1423 return true;
1424}
1425
1426static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1427 enum pipe pipe, u32 val)
1428{
1429 if ((val & ADPA_DAC_ENABLE) == 0)
1430 return false;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001431 if (HAS_PCH_CPT(dev_priv)) {
Keith Packard1519b992011-08-06 10:35:34 -07001432 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1433 return false;
1434 } else {
1435 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1436 return false;
1437 }
1438 return true;
1439}
1440
Jesse Barnes291906f2011-02-02 12:28:03 -08001441static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001442 enum pipe pipe, i915_reg_t reg,
1443 u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001444{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001445 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001446 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001447 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001448 i915_mmio_reg_offset(reg), pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001449
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001450 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001451 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001452 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001453}
1454
1455static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001456 enum pipe pipe, i915_reg_t reg)
Jesse Barnes291906f2011-02-02 12:28:03 -08001457{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001458 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001459 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001460 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001461 i915_mmio_reg_offset(reg), pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001462
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001463 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001464 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001465 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001466}
1467
1468static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1469 enum pipe pipe)
1470{
Jesse Barnes291906f2011-02-02 12:28:03 -08001471 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001472
Keith Packardf0575e92011-07-25 22:12:43 -07001473 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1474 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1475 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001476
Ville Syrjälä649636e2015-09-22 19:50:01 +03001477 val = I915_READ(PCH_ADPA);
Rob Clarke2c719b2014-12-15 13:56:32 -05001478 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001479 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001480 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001481
Ville Syrjälä649636e2015-09-22 19:50:01 +03001482 val = I915_READ(PCH_LVDS);
Rob Clarke2c719b2014-12-15 13:56:32 -05001483 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001484 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001485 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001486
Paulo Zanonie2debe92013-02-18 19:00:27 -03001487 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1488 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1489 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001490}
1491
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001492static void _vlv_enable_pll(struct intel_crtc *crtc,
1493 const struct intel_crtc_state *pipe_config)
1494{
1495 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1496 enum pipe pipe = crtc->pipe;
1497
1498 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
1499 POSTING_READ(DPLL(pipe));
1500 udelay(150);
1501
Chris Wilson2c30b432016-06-30 15:32:54 +01001502 if (intel_wait_for_register(dev_priv,
1503 DPLL(pipe),
1504 DPLL_LOCK_VLV,
1505 DPLL_LOCK_VLV,
1506 1))
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001507 DRM_ERROR("DPLL %d failed to lock\n", pipe);
1508}
1509
Ville Syrjäläd288f652014-10-28 13:20:22 +02001510static void vlv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001511 const struct intel_crtc_state *pipe_config)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001512{
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001513 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001514 enum pipe pipe = crtc->pipe;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001515
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001516 assert_pipe_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001517
Daniel Vetter87442f72013-06-06 00:52:17 +02001518 /* PLL is protected by panel, make sure we can write it */
Ville Syrjälä7d1a83c2016-03-15 16:39:58 +02001519 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001520
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001521 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1522 _vlv_enable_pll(crtc, pipe_config);
Daniel Vetter426115c2013-07-11 22:13:42 +02001523
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001524 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1525 POSTING_READ(DPLL_MD(pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001526}
1527
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001528
1529static void _chv_enable_pll(struct intel_crtc *crtc,
1530 const struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001531{
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001532 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001533 enum pipe pipe = crtc->pipe;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001534 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001535 u32 tmp;
1536
Ville Syrjäläa5805162015-05-26 20:42:30 +03001537 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001538
1539 /* Enable back the 10bit clock to display controller */
1540 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1541 tmp |= DPIO_DCLKP_EN;
1542 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1543
Ville Syrjälä54433e92015-05-26 20:42:31 +03001544 mutex_unlock(&dev_priv->sb_lock);
1545
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001546 /*
1547 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1548 */
1549 udelay(1);
1550
1551 /* Enable PLL */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001552 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001553
1554 /* Check PLL is locked */
Chris Wilson6b188262016-06-30 15:32:55 +01001555 if (intel_wait_for_register(dev_priv,
1556 DPLL(pipe), DPLL_LOCK_VLV, DPLL_LOCK_VLV,
1557 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001558 DRM_ERROR("PLL %d failed to lock\n", pipe);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001559}
1560
1561static void chv_enable_pll(struct intel_crtc *crtc,
1562 const struct intel_crtc_state *pipe_config)
1563{
1564 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1565 enum pipe pipe = crtc->pipe;
1566
1567 assert_pipe_disabled(dev_priv, pipe);
1568
1569 /* PLL is protected by panel, make sure we can write it */
1570 assert_panel_unlocked(dev_priv, pipe);
1571
1572 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1573 _chv_enable_pll(crtc, pipe_config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001574
Ville Syrjäläc2317752016-03-15 16:39:56 +02001575 if (pipe != PIPE_A) {
1576 /*
1577 * WaPixelRepeatModeFixForC0:chv
1578 *
1579 * DPLLCMD is AWOL. Use chicken bits to propagate
1580 * the value from DPLLBMD to either pipe B or C.
1581 */
1582 I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
1583 I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
1584 I915_WRITE(CBR4_VLV, 0);
1585 dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
1586
1587 /*
1588 * DPLLB VGA mode also seems to cause problems.
1589 * We should always have it disabled.
1590 */
1591 WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
1592 } else {
1593 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1594 POSTING_READ(DPLL_MD(pipe));
1595 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001596}
1597
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001598static int intel_num_dvo_pipes(struct drm_device *dev)
1599{
1600 struct intel_crtc *crtc;
1601 int count = 0;
1602
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03001603 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001604 count += crtc->base.state->active &&
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03001605 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO);
1606 }
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001607
1608 return count;
1609}
1610
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001611static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001612{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001613 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01001614 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001615 i915_reg_t reg = DPLL(crtc->pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001616 u32 dpll = crtc->config->dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001617
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001618 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001619
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001620 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001621 if (IS_MOBILE(dev) && !IS_I830(dev))
1622 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001623
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001624 /* Enable DVO 2x clock on both PLLs if necessary */
1625 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1626 /*
1627 * It appears to be important that we don't enable this
1628 * for the current pipe before otherwise configuring the
1629 * PLL. No idea how this should be handled if multiple
1630 * DVO outputs are enabled simultaneosly.
1631 */
1632 dpll |= DPLL_DVO_2X_MODE;
1633 I915_WRITE(DPLL(!crtc->pipe),
1634 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1635 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001636
Ville Syrjäläc2b63372015-10-07 22:08:25 +03001637 /*
1638 * Apparently we need to have VGA mode enabled prior to changing
1639 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1640 * dividers, even though the register value does change.
1641 */
1642 I915_WRITE(reg, 0);
1643
Ville Syrjälä8e7a65a2015-10-07 22:08:24 +03001644 I915_WRITE(reg, dpll);
1645
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001646 /* Wait for the clocks to stabilize. */
1647 POSTING_READ(reg);
1648 udelay(150);
1649
1650 if (INTEL_INFO(dev)->gen >= 4) {
1651 I915_WRITE(DPLL_MD(crtc->pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001652 crtc->config->dpll_hw_state.dpll_md);
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001653 } else {
1654 /* The pixel multiplier can only be updated once the
1655 * DPLL is enabled and the clocks are stable.
1656 *
1657 * So write it again.
1658 */
1659 I915_WRITE(reg, dpll);
1660 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001661
1662 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001663 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001664 POSTING_READ(reg);
1665 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001666 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001667 POSTING_READ(reg);
1668 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001669 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001670 POSTING_READ(reg);
1671 udelay(150); /* wait for warmup */
1672}
1673
1674/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001675 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001676 * @dev_priv: i915 private structure
1677 * @pipe: pipe PLL to disable
1678 *
1679 * Disable the PLL for @pipe, making sure the pipe is off first.
1680 *
1681 * Note! This is for pre-ILK only.
1682 */
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001683static void i9xx_disable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001684{
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001685 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01001686 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001687 enum pipe pipe = crtc->pipe;
1688
1689 /* Disable DVO 2x clock on both PLLs if necessary */
1690 if (IS_I830(dev) &&
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03001691 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO) &&
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001692 !intel_num_dvo_pipes(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001693 I915_WRITE(DPLL(PIPE_B),
1694 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1695 I915_WRITE(DPLL(PIPE_A),
1696 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1697 }
1698
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001699 /* Don't disable pipe or pipe PLLs if needed */
1700 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1701 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001702 return;
1703
1704 /* Make sure the pipe isn't still relying on us */
1705 assert_pipe_disabled(dev_priv, pipe);
1706
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001707 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
Daniel Vetter50b44a42013-06-05 13:34:33 +02001708 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001709}
1710
Jesse Barnesf6071162013-10-01 10:41:38 -07001711static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1712{
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001713 u32 val;
Jesse Barnesf6071162013-10-01 10:41:38 -07001714
1715 /* Make sure the pipe isn't still relying on us */
1716 assert_pipe_disabled(dev_priv, pipe);
1717
Ville Syrjälä03ed5cb2016-03-15 16:39:55 +02001718 val = DPLL_INTEGRATED_REF_CLK_VLV |
1719 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1720 if (pipe != PIPE_A)
1721 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1722
Jesse Barnesf6071162013-10-01 10:41:38 -07001723 I915_WRITE(DPLL(pipe), val);
1724 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001725}
1726
1727static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1728{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001729 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001730 u32 val;
1731
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001732 /* Make sure the pipe isn't still relying on us */
1733 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001734
Ville Syrjälä60bfe442015-06-29 15:25:49 +03001735 val = DPLL_SSC_REF_CLK_CHV |
1736 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001737 if (pipe != PIPE_A)
1738 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä03ed5cb2016-03-15 16:39:55 +02001739
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001740 I915_WRITE(DPLL(pipe), val);
1741 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001742
Ville Syrjäläa5805162015-05-26 20:42:30 +03001743 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd7520482014-04-09 13:28:59 +03001744
1745 /* Disable 10bit clock to display controller */
1746 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1747 val &= ~DPIO_DCLKP_EN;
1748 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1749
Ville Syrjäläa5805162015-05-26 20:42:30 +03001750 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001751}
1752
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001753void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001754 struct intel_digital_port *dport,
1755 unsigned int expected_mask)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001756{
1757 u32 port_mask;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001758 i915_reg_t dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001759
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001760 switch (dport->port) {
1761 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001762 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001763 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001764 break;
1765 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001766 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001767 dpll_reg = DPLL(0);
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001768 expected_mask <<= 4;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001769 break;
1770 case PORT_D:
1771 port_mask = DPLL_PORTD_READY_MASK;
1772 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001773 break;
1774 default:
1775 BUG();
1776 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001777
Chris Wilson370004d2016-06-30 15:32:56 +01001778 if (intel_wait_for_register(dev_priv,
1779 dpll_reg, port_mask, expected_mask,
1780 1000))
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001781 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1782 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
Jesse Barnes89b667f2013-04-18 14:51:36 -07001783}
1784
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001785static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1786 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001787{
Chris Wilson91c8a322016-07-05 10:40:23 +01001788 struct drm_device *dev = &dev_priv->drm;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001789 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001790 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001791 i915_reg_t reg;
1792 uint32_t val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001793
Jesse Barnes040484a2011-01-03 12:14:26 -08001794 /* Make sure PCH DPLL is enabled */
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02001795 assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
Jesse Barnes040484a2011-01-03 12:14:26 -08001796
1797 /* FDI must be feeding us bits for PCH ports */
1798 assert_fdi_tx_enabled(dev_priv, pipe);
1799 assert_fdi_rx_enabled(dev_priv, pipe);
1800
Daniel Vetter23670b322012-11-01 09:15:30 +01001801 if (HAS_PCH_CPT(dev)) {
1802 /* Workaround: Set the timing override bit before enabling the
1803 * pch transcoder. */
1804 reg = TRANS_CHICKEN2(pipe);
1805 val = I915_READ(reg);
1806 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1807 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001808 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001809
Daniel Vetterab9412b2013-05-03 11:49:46 +02001810 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001811 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001812 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001813
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001814 if (HAS_PCH_IBX(dev_priv)) {
Jesse Barnese9bcff52011-06-24 12:19:20 -07001815 /*
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001816 * Make the BPC in transcoder be consistent with
1817 * that in pipeconf reg. For HDMI we must use 8bpc
1818 * here for both 8bpc and 12bpc.
Jesse Barnese9bcff52011-06-24 12:19:20 -07001819 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001820 val &= ~PIPECONF_BPC_MASK;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03001821 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_HDMI))
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001822 val |= PIPECONF_8BPC;
1823 else
1824 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001825 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001826
1827 val &= ~TRANS_INTERLACE_MASK;
1828 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001829 if (HAS_PCH_IBX(dev_priv) &&
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03001830 intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001831 val |= TRANS_LEGACY_INTERLACED_ILK;
1832 else
1833 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001834 else
1835 val |= TRANS_PROGRESSIVE;
1836
Jesse Barnes040484a2011-01-03 12:14:26 -08001837 I915_WRITE(reg, val | TRANS_ENABLE);
Chris Wilson650fbd82016-06-30 15:32:57 +01001838 if (intel_wait_for_register(dev_priv,
1839 reg, TRANS_STATE_ENABLE, TRANS_STATE_ENABLE,
1840 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001841 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08001842}
1843
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001844static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001845 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001846{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001847 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001848
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001849 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001850 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001851 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001852
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001853 /* Workaround: set timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001854 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01001855 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001856 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001857
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001858 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001859 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001860
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001861 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1862 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001863 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001864 else
1865 val |= TRANS_PROGRESSIVE;
1866
Daniel Vetterab9412b2013-05-03 11:49:46 +02001867 I915_WRITE(LPT_TRANSCONF, val);
Chris Wilsond9f96242016-06-30 15:32:58 +01001868 if (intel_wait_for_register(dev_priv,
1869 LPT_TRANSCONF,
1870 TRANS_STATE_ENABLE,
1871 TRANS_STATE_ENABLE,
1872 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02001873 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001874}
1875
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001876static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1877 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001878{
Chris Wilson91c8a322016-07-05 10:40:23 +01001879 struct drm_device *dev = &dev_priv->drm;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001880 i915_reg_t reg;
1881 uint32_t val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001882
1883 /* FDI relies on the transcoder */
1884 assert_fdi_tx_disabled(dev_priv, pipe);
1885 assert_fdi_rx_disabled(dev_priv, pipe);
1886
Jesse Barnes291906f2011-02-02 12:28:03 -08001887 /* Ports must be off as well */
1888 assert_pch_ports_disabled(dev_priv, pipe);
1889
Daniel Vetterab9412b2013-05-03 11:49:46 +02001890 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001891 val = I915_READ(reg);
1892 val &= ~TRANS_ENABLE;
1893 I915_WRITE(reg, val);
1894 /* wait for PCH transcoder off, transcoder state */
Chris Wilsona7d04662016-06-30 15:32:59 +01001895 if (intel_wait_for_register(dev_priv,
1896 reg, TRANS_STATE_ENABLE, 0,
1897 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001898 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01001899
Ville Syrjäläc4656132015-10-29 21:25:56 +02001900 if (HAS_PCH_CPT(dev)) {
Daniel Vetter23670b322012-11-01 09:15:30 +01001901 /* Workaround: Clear the timing override chicken bit again. */
1902 reg = TRANS_CHICKEN2(pipe);
1903 val = I915_READ(reg);
1904 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1905 I915_WRITE(reg, val);
1906 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001907}
1908
Paulo Zanoniab4d9662012-10-31 18:12:55 -02001909static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001910{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001911 u32 val;
1912
Daniel Vetterab9412b2013-05-03 11:49:46 +02001913 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001914 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02001915 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001916 /* wait for PCH transcoder off, transcoder state */
Chris Wilsondfdb4742016-06-30 15:33:00 +01001917 if (intel_wait_for_register(dev_priv,
1918 LPT_TRANSCONF, TRANS_STATE_ENABLE, 0,
1919 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001920 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001921
1922 /* Workaround: clear timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001923 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01001924 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001925 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Jesse Barnes92f25842011-01-04 15:09:34 -08001926}
1927
1928/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001929 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02001930 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08001931 *
Paulo Zanoni03722642014-01-17 13:51:09 -02001932 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001933 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08001934 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02001935static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001936{
Paulo Zanoni03722642014-01-17 13:51:09 -02001937 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01001938 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanoni03722642014-01-17 13:51:09 -02001939 enum pipe pipe = crtc->pipe;
Ville Syrjälä1a70a7282015-10-29 21:25:50 +02001940 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter1a240d42012-11-29 22:18:51 +01001941 enum pipe pch_transcoder;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001942 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001943 u32 val;
1944
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03001945 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
1946
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001947 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001948 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001949 assert_sprites_disabled(dev_priv, pipe);
1950
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001951 if (HAS_PCH_LPT(dev_priv))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001952 pch_transcoder = TRANSCODER_A;
1953 else
1954 pch_transcoder = pipe;
1955
Jesse Barnesb24e7172011-01-04 15:09:30 -08001956 /*
1957 * A pipe without a PLL won't actually be able to drive bits from
1958 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1959 * need the check.
1960 */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001961 if (HAS_GMCH_DISPLAY(dev_priv))
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03001962 if (intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DSI))
Jani Nikula23538ef2013-08-27 15:12:22 +03001963 assert_dsi_pll_enabled(dev_priv);
1964 else
1965 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001966 else {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001967 if (crtc->config->has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08001968 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001969 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001970 assert_fdi_tx_pll_enabled(dev_priv,
1971 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08001972 }
1973 /* FIXME: assert CPU port conditions for SNB+ */
1974 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08001975
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001976 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001977 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02001978 if (val & PIPECONF_ENABLE) {
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001979 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1980 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
Chris Wilson00d70b12011-03-17 07:18:29 +00001981 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02001982 }
Chris Wilson00d70b12011-03-17 07:18:29 +00001983
1984 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02001985 POSTING_READ(reg);
Ville Syrjäläb7792d82015-12-14 18:23:43 +02001986
1987 /*
1988 * Until the pipe starts DSL will read as 0, which would cause
1989 * an apparent vblank timestamp jump, which messes up also the
1990 * frame count when it's derived from the timestamps. So let's
1991 * wait for the pipe to start properly before we call
1992 * drm_crtc_vblank_on()
1993 */
1994 if (dev->max_vblank_count == 0 &&
1995 wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
1996 DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001997}
1998
1999/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002000 * intel_disable_pipe - disable a pipe, asserting requirements
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002001 * @crtc: crtc whose pipes is to be disabled
Jesse Barnesb24e7172011-01-04 15:09:30 -08002002 *
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002003 * Disable the pipe of @crtc, making sure that various hardware
2004 * specific requirements are met, if applicable, e.g. plane
2005 * disabled, panel fitter off, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002006 *
2007 * Will wait until the pipe has shut down before returning.
2008 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002009static void intel_disable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002010{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002011 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002012 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002013 enum pipe pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002014 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002015 u32 val;
2016
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03002017 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
2018
Jesse Barnesb24e7172011-01-04 15:09:30 -08002019 /*
2020 * Make sure planes won't keep trying to pump pixels to us,
2021 * or we might hang the display.
2022 */
2023 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002024 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07002025 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002026
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002027 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002028 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00002029 if ((val & PIPECONF_ENABLE) == 0)
2030 return;
2031
Ville Syrjälä67adc642014-08-15 01:21:57 +03002032 /*
2033 * Double wide has implications for planes
2034 * so best keep it disabled when not needed.
2035 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002036 if (crtc->config->double_wide)
Ville Syrjälä67adc642014-08-15 01:21:57 +03002037 val &= ~PIPECONF_DOUBLE_WIDE;
2038
2039 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002040 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2041 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Ville Syrjälä67adc642014-08-15 01:21:57 +03002042 val &= ~PIPECONF_ENABLE;
2043
2044 I915_WRITE(reg, val);
2045 if ((val & PIPECONF_ENABLE) == 0)
2046 intel_wait_for_pipe_off(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002047}
2048
Ville Syrjälä832be822016-01-12 21:08:33 +02002049static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
2050{
2051 return IS_GEN2(dev_priv) ? 2048 : 4096;
2052}
2053
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002054static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
2055 uint64_t fb_modifier, unsigned int cpp)
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002056{
2057 switch (fb_modifier) {
2058 case DRM_FORMAT_MOD_NONE:
2059 return cpp;
2060 case I915_FORMAT_MOD_X_TILED:
2061 if (IS_GEN2(dev_priv))
2062 return 128;
2063 else
2064 return 512;
2065 case I915_FORMAT_MOD_Y_TILED:
2066 if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
2067 return 128;
2068 else
2069 return 512;
2070 case I915_FORMAT_MOD_Yf_TILED:
2071 switch (cpp) {
2072 case 1:
2073 return 64;
2074 case 2:
2075 case 4:
2076 return 128;
2077 case 8:
2078 case 16:
2079 return 256;
2080 default:
2081 MISSING_CASE(cpp);
2082 return cpp;
2083 }
2084 break;
2085 default:
2086 MISSING_CASE(fb_modifier);
2087 return cpp;
2088 }
2089}
2090
Ville Syrjälä832be822016-01-12 21:08:33 +02002091unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
2092 uint64_t fb_modifier, unsigned int cpp)
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002093{
Ville Syrjälä832be822016-01-12 21:08:33 +02002094 if (fb_modifier == DRM_FORMAT_MOD_NONE)
2095 return 1;
2096 else
2097 return intel_tile_size(dev_priv) /
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002098 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002099}
2100
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002101/* Return the tile dimensions in pixel units */
2102static void intel_tile_dims(const struct drm_i915_private *dev_priv,
2103 unsigned int *tile_width,
2104 unsigned int *tile_height,
2105 uint64_t fb_modifier,
2106 unsigned int cpp)
2107{
2108 unsigned int tile_width_bytes =
2109 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
2110
2111 *tile_width = tile_width_bytes / cpp;
2112 *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
2113}
2114
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002115unsigned int
2116intel_fb_align_height(struct drm_device *dev, unsigned int height,
Ville Syrjälä832be822016-01-12 21:08:33 +02002117 uint32_t pixel_format, uint64_t fb_modifier)
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002118{
Ville Syrjälä832be822016-01-12 21:08:33 +02002119 unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
2120 unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
2121
2122 return ALIGN(height, tile_height);
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002123}
2124
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002125unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
2126{
2127 unsigned int size = 0;
2128 int i;
2129
2130 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
2131 size += rot_info->plane[i].width * rot_info->plane[i].height;
2132
2133 return size;
2134}
2135
Daniel Vetter75c82a52015-10-14 16:51:04 +02002136static void
Ville Syrjälä3465c582016-02-15 22:54:43 +02002137intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
2138 const struct drm_framebuffer *fb,
2139 unsigned int rotation)
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002140{
Ville Syrjälä2d7a2152016-02-15 22:54:47 +02002141 if (intel_rotation_90_or_270(rotation)) {
2142 *view = i915_ggtt_view_rotated;
2143 view->params.rotated = to_intel_framebuffer(fb)->rot_info;
2144 } else {
2145 *view = i915_ggtt_view_normal;
2146 }
2147}
2148
2149static void
2150intel_fill_fb_info(struct drm_i915_private *dev_priv,
2151 struct drm_framebuffer *fb)
2152{
2153 struct intel_rotation_info *info = &to_intel_framebuffer(fb)->rot_info;
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002154 unsigned int tile_size, tile_width, tile_height, cpp;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002155
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002156 tile_size = intel_tile_size(dev_priv);
2157
2158 cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002159 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2160 fb->modifier[0], cpp);
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002161
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002162 info->plane[0].width = DIV_ROUND_UP(fb->pitches[0], tile_width * cpp);
2163 info->plane[0].height = DIV_ROUND_UP(fb->height, tile_height);
Tvrtko Ursulin84fe03f2015-06-23 14:26:46 +01002164
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01002165 if (info->pixel_format == DRM_FORMAT_NV12) {
Ville Syrjälä832be822016-01-12 21:08:33 +02002166 cpp = drm_format_plane_cpp(fb->pixel_format, 1);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002167 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2168 fb->modifier[1], cpp);
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002169
Ville Syrjälä2d7a2152016-02-15 22:54:47 +02002170 info->uv_offset = fb->offsets[1];
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002171 info->plane[1].width = DIV_ROUND_UP(fb->pitches[1], tile_width * cpp);
2172 info->plane[1].height = DIV_ROUND_UP(fb->height / 2, tile_height);
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01002173 }
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002174}
2175
Ville Syrjälä603525d2016-01-12 21:08:37 +02002176static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002177{
2178 if (INTEL_INFO(dev_priv)->gen >= 9)
2179 return 256 * 1024;
Ville Syrjälä985b8bb2015-06-11 16:31:15 +03002180 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
Wayne Boyer666a4532015-12-09 12:29:35 -08002181 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002182 return 128 * 1024;
2183 else if (INTEL_INFO(dev_priv)->gen >= 4)
2184 return 4 * 1024;
2185 else
Ville Syrjälä44c59052015-06-11 16:31:16 +03002186 return 0;
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002187}
2188
Ville Syrjälä603525d2016-01-12 21:08:37 +02002189static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
2190 uint64_t fb_modifier)
2191{
2192 switch (fb_modifier) {
2193 case DRM_FORMAT_MOD_NONE:
2194 return intel_linear_alignment(dev_priv);
2195 case I915_FORMAT_MOD_X_TILED:
2196 if (INTEL_INFO(dev_priv)->gen >= 9)
2197 return 256 * 1024;
2198 return 0;
2199 case I915_FORMAT_MOD_Y_TILED:
2200 case I915_FORMAT_MOD_Yf_TILED:
2201 return 1 * 1024 * 1024;
2202 default:
2203 MISSING_CASE(fb_modifier);
2204 return 0;
2205 }
2206}
2207
Chris Wilson127bd2a2010-07-23 23:32:05 +01002208int
Ville Syrjälä3465c582016-02-15 22:54:43 +02002209intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
2210 unsigned int rotation)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002211{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002212 struct drm_device *dev = fb->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002213 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002214 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002215 struct i915_ggtt_view view;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002216 u32 alignment;
2217 int ret;
2218
Matt Roperebcdd392014-07-09 16:22:11 -07002219 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2220
Ville Syrjälä603525d2016-01-12 21:08:37 +02002221 alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002222
Ville Syrjälä3465c582016-02-15 22:54:43 +02002223 intel_fill_fb_ggtt_view(&view, fb, rotation);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002224
Chris Wilson693db182013-03-05 14:52:39 +00002225 /* Note that the w/a also requires 64 PTE of padding following the
2226 * bo. We currently fill all unused PTE with the shadow page and so
2227 * we should always have valid PTE following the scanout preventing
2228 * the VT-d warning.
2229 */
Chris Wilson48f112f2016-06-24 14:07:14 +01002230 if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
Chris Wilson693db182013-03-05 14:52:39 +00002231 alignment = 256 * 1024;
2232
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002233 /*
2234 * Global gtt pte registers are special registers which actually forward
2235 * writes to a chunk of system memory. Which means that there is no risk
2236 * that the register values disappear as soon as we call
2237 * intel_runtime_pm_put(), so it is correct to wrap only the
2238 * pin/unpin/fence and not more.
2239 */
2240 intel_runtime_pm_get(dev_priv);
2241
Maarten Lankhorst7580d772015-08-18 13:40:06 +02002242 ret = i915_gem_object_pin_to_display_plane(obj, alignment,
2243 &view);
Chris Wilson48b956c2010-09-14 12:50:34 +01002244 if (ret)
Maarten Lankhorstb26a6b32015-09-23 13:27:09 +02002245 goto err_pm;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002246
2247 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2248 * fence, whereas 965+ only requires a fence if using
2249 * framebuffer compression. For simplicity, we always install
2250 * a fence as the cost is not that onerous.
2251 */
Vivek Kasireddy98072162015-10-29 18:54:38 -07002252 if (view.type == I915_GGTT_VIEW_NORMAL) {
2253 ret = i915_gem_object_get_fence(obj);
2254 if (ret == -EDEADLK) {
2255 /*
2256 * -EDEADLK means there are no free fences
2257 * no pending flips.
2258 *
2259 * This is propagated to atomic, but it uses
2260 * -EDEADLK to force a locking recovery, so
2261 * change the returned error to -EBUSY.
2262 */
2263 ret = -EBUSY;
2264 goto err_unpin;
2265 } else if (ret)
2266 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002267
Vivek Kasireddy98072162015-10-29 18:54:38 -07002268 i915_gem_object_pin_fence(obj);
2269 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002270
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002271 intel_runtime_pm_put(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002272 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002273
2274err_unpin:
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002275 i915_gem_object_unpin_from_display_plane(obj, &view);
Maarten Lankhorstb26a6b32015-09-23 13:27:09 +02002276err_pm:
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002277 intel_runtime_pm_put(dev_priv);
Chris Wilson48b956c2010-09-14 12:50:34 +01002278 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002279}
2280
Chris Wilsonfb4b8ce2016-04-28 09:56:35 +01002281void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
Chris Wilson1690e1e2011-12-14 13:57:08 +01002282{
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002283 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002284 struct i915_ggtt_view view;
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002285
Matt Roperebcdd392014-07-09 16:22:11 -07002286 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2287
Ville Syrjälä3465c582016-02-15 22:54:43 +02002288 intel_fill_fb_ggtt_view(&view, fb, rotation);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002289
Vivek Kasireddy98072162015-10-29 18:54:38 -07002290 if (view.type == I915_GGTT_VIEW_NORMAL)
2291 i915_gem_object_unpin_fence(obj);
2292
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002293 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002294}
2295
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002296/*
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002297 * Adjust the tile offset by moving the difference into
2298 * the x/y offsets.
2299 *
2300 * Input tile dimensions and pitch must already be
2301 * rotated to match x and y, and in pixel units.
2302 */
2303static u32 intel_adjust_tile_offset(int *x, int *y,
2304 unsigned int tile_width,
2305 unsigned int tile_height,
2306 unsigned int tile_size,
2307 unsigned int pitch_tiles,
2308 u32 old_offset,
2309 u32 new_offset)
2310{
2311 unsigned int tiles;
2312
2313 WARN_ON(old_offset & (tile_size - 1));
2314 WARN_ON(new_offset & (tile_size - 1));
2315 WARN_ON(new_offset > old_offset);
2316
2317 tiles = (old_offset - new_offset) / tile_size;
2318
2319 *y += tiles / pitch_tiles * tile_height;
2320 *x += tiles % pitch_tiles * tile_width;
2321
2322 return new_offset;
2323}
2324
2325/*
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002326 * Computes the linear offset to the base tile and adjusts
2327 * x, y. bytes per pixel is assumed to be a power-of-two.
2328 *
2329 * In the 90/270 rotated case, x and y are assumed
2330 * to be already rotated to match the rotated GTT view, and
2331 * pitch is the tile_height aligned framebuffer height.
2332 */
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002333u32 intel_compute_tile_offset(int *x, int *y,
2334 const struct drm_framebuffer *fb, int plane,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002335 unsigned int pitch,
2336 unsigned int rotation)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002337{
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002338 const struct drm_i915_private *dev_priv = to_i915(fb->dev);
2339 uint64_t fb_modifier = fb->modifier[plane];
2340 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002341 u32 offset, offset_aligned, alignment;
2342
2343 alignment = intel_surf_alignment(dev_priv, fb_modifier);
2344 if (alignment)
2345 alignment--;
2346
Ville Syrjäläb5c65332016-01-12 21:08:31 +02002347 if (fb_modifier != DRM_FORMAT_MOD_NONE) {
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002348 unsigned int tile_size, tile_width, tile_height;
2349 unsigned int tile_rows, tiles, pitch_tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002350
Ville Syrjäläd8433102016-01-12 21:08:35 +02002351 tile_size = intel_tile_size(dev_priv);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002352 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2353 fb_modifier, cpp);
2354
2355 if (intel_rotation_90_or_270(rotation)) {
2356 pitch_tiles = pitch / tile_height;
2357 swap(tile_width, tile_height);
2358 } else {
2359 pitch_tiles = pitch / (tile_width * cpp);
2360 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002361
Ville Syrjäläd8433102016-01-12 21:08:35 +02002362 tile_rows = *y / tile_height;
2363 *y %= tile_height;
Chris Wilsonbc752862013-02-21 20:04:31 +00002364
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002365 tiles = *x / tile_width;
2366 *x %= tile_width;
Ville Syrjäläd8433102016-01-12 21:08:35 +02002367
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002368 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2369 offset_aligned = offset & ~alignment;
Chris Wilsonbc752862013-02-21 20:04:31 +00002370
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002371 intel_adjust_tile_offset(x, y, tile_width, tile_height,
2372 tile_size, pitch_tiles,
2373 offset, offset_aligned);
2374 } else {
Chris Wilsonbc752862013-02-21 20:04:31 +00002375 offset = *y * pitch + *x * cpp;
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002376 offset_aligned = offset & ~alignment;
2377
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002378 *y = (offset & alignment) / pitch;
2379 *x = ((offset & alignment) - *y * pitch) / cpp;
Chris Wilsonbc752862013-02-21 20:04:31 +00002380 }
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002381
2382 return offset_aligned;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002383}
2384
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002385static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002386{
2387 switch (format) {
2388 case DISPPLANE_8BPP:
2389 return DRM_FORMAT_C8;
2390 case DISPPLANE_BGRX555:
2391 return DRM_FORMAT_XRGB1555;
2392 case DISPPLANE_BGRX565:
2393 return DRM_FORMAT_RGB565;
2394 default:
2395 case DISPPLANE_BGRX888:
2396 return DRM_FORMAT_XRGB8888;
2397 case DISPPLANE_RGBX888:
2398 return DRM_FORMAT_XBGR8888;
2399 case DISPPLANE_BGRX101010:
2400 return DRM_FORMAT_XRGB2101010;
2401 case DISPPLANE_RGBX101010:
2402 return DRM_FORMAT_XBGR2101010;
2403 }
2404}
2405
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002406static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2407{
2408 switch (format) {
2409 case PLANE_CTL_FORMAT_RGB_565:
2410 return DRM_FORMAT_RGB565;
2411 default:
2412 case PLANE_CTL_FORMAT_XRGB_8888:
2413 if (rgb_order) {
2414 if (alpha)
2415 return DRM_FORMAT_ABGR8888;
2416 else
2417 return DRM_FORMAT_XBGR8888;
2418 } else {
2419 if (alpha)
2420 return DRM_FORMAT_ARGB8888;
2421 else
2422 return DRM_FORMAT_XRGB8888;
2423 }
2424 case PLANE_CTL_FORMAT_XRGB_2101010:
2425 if (rgb_order)
2426 return DRM_FORMAT_XBGR2101010;
2427 else
2428 return DRM_FORMAT_XRGB2101010;
2429 }
2430}
2431
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002432static bool
Daniel Vetterf6936e22015-03-26 12:17:05 +01002433intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2434 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002435{
2436 struct drm_device *dev = crtc->base.dev;
Paulo Zanoni3badb492015-09-23 12:52:23 -03002437 struct drm_i915_private *dev_priv = to_i915(dev);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002438 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002439 struct drm_i915_gem_object *obj = NULL;
2440 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Damien Lespiau2d140302015-02-05 17:22:18 +00002441 struct drm_framebuffer *fb = &plane_config->fb->base;
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002442 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2443 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2444 PAGE_SIZE);
2445
2446 size_aligned -= base_aligned;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002447
Chris Wilsonff2652e2014-03-10 08:07:02 +00002448 if (plane_config->size == 0)
2449 return false;
2450
Paulo Zanoni3badb492015-09-23 12:52:23 -03002451 /* If the FB is too big, just don't use it since fbdev is not very
2452 * important and we should probably use that space with FBC or other
2453 * features. */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002454 if (size_aligned * 2 > ggtt->stolen_usable_size)
Paulo Zanoni3badb492015-09-23 12:52:23 -03002455 return false;
2456
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002457 mutex_lock(&dev->struct_mutex);
2458
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002459 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2460 base_aligned,
2461 base_aligned,
2462 size_aligned);
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002463 if (!obj) {
2464 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002465 return false;
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002466 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002467
Damien Lespiau49af4492015-01-20 12:51:44 +00002468 obj->tiling_mode = plane_config->tiling;
2469 if (obj->tiling_mode == I915_TILING_X)
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002470 obj->stride = fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002471
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002472 mode_cmd.pixel_format = fb->pixel_format;
2473 mode_cmd.width = fb->width;
2474 mode_cmd.height = fb->height;
2475 mode_cmd.pitches[0] = fb->pitches[0];
Daniel Vetter18c52472015-02-10 17:16:09 +00002476 mode_cmd.modifier[0] = fb->modifier[0];
2477 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002478
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002479 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002480 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002481 DRM_DEBUG_KMS("intel fb init failed\n");
2482 goto out_unref_obj;
2483 }
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002484
Jesse Barnes46f297f2014-03-07 08:57:48 -08002485 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002486
Daniel Vetterf6936e22015-03-26 12:17:05 +01002487 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002488 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002489
2490out_unref_obj:
2491 drm_gem_object_unreference(&obj->base);
2492 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002493 return false;
2494}
2495
Daniel Vetter5a21b662016-05-24 17:13:53 +02002496/* Update plane->state->fb to match plane->fb after driver-internal updates */
2497static void
2498update_state_fb(struct drm_plane *plane)
2499{
2500 if (plane->fb == plane->state->fb)
2501 return;
2502
2503 if (plane->state->fb)
2504 drm_framebuffer_unreference(plane->state->fb);
2505 plane->state->fb = plane->fb;
2506 if (plane->state->fb)
2507 drm_framebuffer_reference(plane->state->fb);
2508}
2509
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002510static void
Daniel Vetterf6936e22015-03-26 12:17:05 +01002511intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2512 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002513{
2514 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002515 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002516 struct drm_crtc *c;
2517 struct intel_crtc *i;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002518 struct drm_i915_gem_object *obj;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002519 struct drm_plane *primary = intel_crtc->base.primary;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002520 struct drm_plane_state *plane_state = primary->state;
Matt Roper200757f2015-12-03 11:37:36 -08002521 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
2522 struct intel_plane *intel_plane = to_intel_plane(primary);
Matt Roper0a8d8a82015-12-03 11:37:38 -08002523 struct intel_plane_state *intel_state =
2524 to_intel_plane_state(plane_state);
Daniel Vetter88595ac2015-03-26 12:42:24 +01002525 struct drm_framebuffer *fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002526
Damien Lespiau2d140302015-02-05 17:22:18 +00002527 if (!plane_config->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002528 return;
2529
Daniel Vetterf6936e22015-03-26 12:17:05 +01002530 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002531 fb = &plane_config->fb->base;
2532 goto valid_fb;
Damien Lespiauf55548b2015-02-05 18:30:20 +00002533 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002534
Damien Lespiau2d140302015-02-05 17:22:18 +00002535 kfree(plane_config->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002536
2537 /*
2538 * Failed to alloc the obj, check to see if we should share
2539 * an fb with another CRTC instead
2540 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002541 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002542 i = to_intel_crtc(c);
2543
2544 if (c == &intel_crtc->base)
2545 continue;
2546
Matt Roper2ff8fde2014-07-08 07:50:07 -07002547 if (!i->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002548 continue;
2549
Daniel Vetter88595ac2015-03-26 12:42:24 +01002550 fb = c->primary->fb;
2551 if (!fb)
Matt Roper2ff8fde2014-07-08 07:50:07 -07002552 continue;
2553
Daniel Vetter88595ac2015-03-26 12:42:24 +01002554 obj = intel_fb_obj(fb);
Matt Roper2ff8fde2014-07-08 07:50:07 -07002555 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002556 drm_framebuffer_reference(fb);
2557 goto valid_fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002558 }
2559 }
Daniel Vetter88595ac2015-03-26 12:42:24 +01002560
Matt Roper200757f2015-12-03 11:37:36 -08002561 /*
2562 * We've failed to reconstruct the BIOS FB. Current display state
2563 * indicates that the primary plane is visible, but has a NULL FB,
2564 * which will lead to problems later if we don't fix it up. The
2565 * simplest solution is to just disable the primary plane now and
2566 * pretend the BIOS never had it enabled.
2567 */
2568 to_intel_plane_state(plane_state)->visible = false;
2569 crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
Ville Syrjälä2622a082016-03-09 19:07:26 +02002570 intel_pre_disable_primary_noatomic(&intel_crtc->base);
Matt Roper200757f2015-12-03 11:37:36 -08002571 intel_plane->disable_plane(primary, &intel_crtc->base);
2572
Daniel Vetter88595ac2015-03-26 12:42:24 +01002573 return;
2574
2575valid_fb:
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002576 plane_state->src_x = 0;
2577 plane_state->src_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002578 plane_state->src_w = fb->width << 16;
2579 plane_state->src_h = fb->height << 16;
2580
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002581 plane_state->crtc_x = 0;
2582 plane_state->crtc_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002583 plane_state->crtc_w = fb->width;
2584 plane_state->crtc_h = fb->height;
2585
Matt Roper0a8d8a82015-12-03 11:37:38 -08002586 intel_state->src.x1 = plane_state->src_x;
2587 intel_state->src.y1 = plane_state->src_y;
2588 intel_state->src.x2 = plane_state->src_x + plane_state->src_w;
2589 intel_state->src.y2 = plane_state->src_y + plane_state->src_h;
2590 intel_state->dst.x1 = plane_state->crtc_x;
2591 intel_state->dst.y1 = plane_state->crtc_y;
2592 intel_state->dst.x2 = plane_state->crtc_x + plane_state->crtc_w;
2593 intel_state->dst.y2 = plane_state->crtc_y + plane_state->crtc_h;
2594
Daniel Vetter88595ac2015-03-26 12:42:24 +01002595 obj = intel_fb_obj(fb);
2596 if (obj->tiling_mode != I915_TILING_NONE)
2597 dev_priv->preserve_bios_swizzle = true;
2598
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002599 drm_framebuffer_reference(fb);
2600 primary->fb = primary->state->fb = fb;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002601 primary->crtc = primary->state->crtc = &intel_crtc->base;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002602 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
Ville Syrjäläa9ff8712015-06-24 21:59:34 +03002603 obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002604}
2605
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002606static void i9xx_update_primary_plane(struct drm_plane *primary,
2607 const struct intel_crtc_state *crtc_state,
2608 const struct intel_plane_state *plane_state)
Jesse Barnes81255562010-08-02 12:07:50 -07002609{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002610 struct drm_device *dev = primary->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002611 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002612 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2613 struct drm_framebuffer *fb = plane_state->base.fb;
2614 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Jesse Barnes81255562010-08-02 12:07:50 -07002615 int plane = intel_crtc->plane;
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002616 u32 linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002617 u32 dspcntr;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002618 i915_reg_t reg = DSPCNTR(plane);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002619 unsigned int rotation = plane_state->base.rotation;
Ville Syrjäläac484962016-01-20 21:05:26 +02002620 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002621 int x = plane_state->src.x1 >> 16;
2622 int y = plane_state->src.y1 >> 16;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002623
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002624 dspcntr = DISPPLANE_GAMMA_ENABLE;
2625
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002626 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002627
2628 if (INTEL_INFO(dev)->gen < 4) {
2629 if (intel_crtc->pipe == PIPE_B)
2630 dspcntr |= DISPPLANE_SEL_PIPE_B;
2631
2632 /* pipesrc and dspsize control the size that is scaled from,
2633 * which should always be the user's requested size.
2634 */
2635 I915_WRITE(DSPSIZE(plane),
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002636 ((crtc_state->pipe_src_h - 1) << 16) |
2637 (crtc_state->pipe_src_w - 1));
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002638 I915_WRITE(DSPPOS(plane), 0);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002639 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2640 I915_WRITE(PRIMSIZE(plane),
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002641 ((crtc_state->pipe_src_h - 1) << 16) |
2642 (crtc_state->pipe_src_w - 1));
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002643 I915_WRITE(PRIMPOS(plane), 0);
2644 I915_WRITE(PRIMCNSTALPHA(plane), 0);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002645 }
2646
Ville Syrjälä57779d02012-10-31 17:50:14 +02002647 switch (fb->pixel_format) {
2648 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002649 dspcntr |= DISPPLANE_8BPP;
2650 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002651 case DRM_FORMAT_XRGB1555:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002652 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002653 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002654 case DRM_FORMAT_RGB565:
2655 dspcntr |= DISPPLANE_BGRX565;
2656 break;
2657 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002658 dspcntr |= DISPPLANE_BGRX888;
2659 break;
2660 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002661 dspcntr |= DISPPLANE_RGBX888;
2662 break;
2663 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002664 dspcntr |= DISPPLANE_BGRX101010;
2665 break;
2666 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002667 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002668 break;
2669 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002670 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002671 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002672
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002673 if (INTEL_INFO(dev)->gen >= 4 &&
2674 obj->tiling_mode != I915_TILING_NONE)
2675 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07002676
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002677 if (IS_G4X(dev))
2678 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2679
Ville Syrjäläac484962016-01-20 21:05:26 +02002680 linear_offset = y * fb->pitches[0] + x * cpp;
Jesse Barnes81255562010-08-02 12:07:50 -07002681
Daniel Vetterc2c75132012-07-05 12:17:30 +02002682 if (INTEL_INFO(dev)->gen >= 4) {
2683 intel_crtc->dspaddr_offset =
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002684 intel_compute_tile_offset(&x, &y, fb, 0,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002685 fb->pitches[0], rotation);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002686 linear_offset -= intel_crtc->dspaddr_offset;
2687 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002688 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002689 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002690
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002691 if (rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302692 dspcntr |= DISPPLANE_ROTATE_180;
2693
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002694 x += (crtc_state->pipe_src_w - 1);
2695 y += (crtc_state->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302696
2697 /* Finding the last pixel of the last line of the display
2698 data and adding to linear_offset*/
2699 linear_offset +=
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002700 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
Ville Syrjäläac484962016-01-20 21:05:26 +02002701 (crtc_state->pipe_src_w - 1) * cpp;
Sonika Jindal48404c12014-08-22 14:06:04 +05302702 }
2703
Paulo Zanoni2db33662015-09-14 15:20:03 -03002704 intel_crtc->adjusted_x = x;
2705 intel_crtc->adjusted_y = y;
2706
Sonika Jindal48404c12014-08-22 14:06:04 +05302707 I915_WRITE(reg, dspcntr);
2708
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002709 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002710 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002711 I915_WRITE(DSPSURF(plane),
2712 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002713 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002714 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002715 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002716 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002717 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002718}
2719
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002720static void i9xx_disable_primary_plane(struct drm_plane *primary,
2721 struct drm_crtc *crtc)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002722{
2723 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002724 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002725 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002726 int plane = intel_crtc->plane;
2727
2728 I915_WRITE(DSPCNTR(plane), 0);
2729 if (INTEL_INFO(dev_priv)->gen >= 4)
2730 I915_WRITE(DSPSURF(plane), 0);
2731 else
2732 I915_WRITE(DSPADDR(plane), 0);
2733 POSTING_READ(DSPCNTR(plane));
2734}
2735
2736static void ironlake_update_primary_plane(struct drm_plane *primary,
2737 const struct intel_crtc_state *crtc_state,
2738 const struct intel_plane_state *plane_state)
2739{
2740 struct drm_device *dev = primary->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002741 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002742 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2743 struct drm_framebuffer *fb = plane_state->base.fb;
2744 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002745 int plane = intel_crtc->plane;
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002746 u32 linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002747 u32 dspcntr;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002748 i915_reg_t reg = DSPCNTR(plane);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002749 unsigned int rotation = plane_state->base.rotation;
Ville Syrjäläac484962016-01-20 21:05:26 +02002750 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002751 int x = plane_state->src.x1 >> 16;
2752 int y = plane_state->src.y1 >> 16;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002753
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002754 dspcntr = DISPPLANE_GAMMA_ENABLE;
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002755 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002756
2757 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2758 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
2759
Ville Syrjälä57779d02012-10-31 17:50:14 +02002760 switch (fb->pixel_format) {
2761 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002762 dspcntr |= DISPPLANE_8BPP;
2763 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002764 case DRM_FORMAT_RGB565:
2765 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002766 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002767 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002768 dspcntr |= DISPPLANE_BGRX888;
2769 break;
2770 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002771 dspcntr |= DISPPLANE_RGBX888;
2772 break;
2773 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002774 dspcntr |= DISPPLANE_BGRX101010;
2775 break;
2776 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002777 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002778 break;
2779 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002780 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002781 }
2782
2783 if (obj->tiling_mode != I915_TILING_NONE)
2784 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002785
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002786 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002787 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002788
Ville Syrjäläac484962016-01-20 21:05:26 +02002789 linear_offset = y * fb->pitches[0] + x * cpp;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002790 intel_crtc->dspaddr_offset =
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002791 intel_compute_tile_offset(&x, &y, fb, 0,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002792 fb->pitches[0], rotation);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002793 linear_offset -= intel_crtc->dspaddr_offset;
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002794 if (rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302795 dspcntr |= DISPPLANE_ROTATE_180;
2796
2797 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002798 x += (crtc_state->pipe_src_w - 1);
2799 y += (crtc_state->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302800
2801 /* Finding the last pixel of the last line of the display
2802 data and adding to linear_offset*/
2803 linear_offset +=
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002804 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
Ville Syrjäläac484962016-01-20 21:05:26 +02002805 (crtc_state->pipe_src_w - 1) * cpp;
Sonika Jindal48404c12014-08-22 14:06:04 +05302806 }
2807 }
2808
Paulo Zanoni2db33662015-09-14 15:20:03 -03002809 intel_crtc->adjusted_x = x;
2810 intel_crtc->adjusted_y = y;
2811
Sonika Jindal48404c12014-08-22 14:06:04 +05302812 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002813
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002814 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002815 I915_WRITE(DSPSURF(plane),
2816 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002817 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002818 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2819 } else {
2820 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2821 I915_WRITE(DSPLINOFF(plane), linear_offset);
2822 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002823 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002824}
2825
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002826u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
2827 uint64_t fb_modifier, uint32_t pixel_format)
Damien Lespiaub3218032015-02-27 11:15:18 +00002828{
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002829 if (fb_modifier == DRM_FORMAT_MOD_NONE) {
2830 return 64;
2831 } else {
2832 int cpp = drm_format_plane_cpp(pixel_format, 0);
Damien Lespiaub3218032015-02-27 11:15:18 +00002833
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002834 return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
Damien Lespiaub3218032015-02-27 11:15:18 +00002835 }
2836}
2837
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002838u32 intel_plane_obj_offset(struct intel_plane *intel_plane,
2839 struct drm_i915_gem_object *obj,
2840 unsigned int plane)
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002841{
Daniel Vetterce7f1722015-10-14 16:51:06 +02002842 struct i915_ggtt_view view;
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002843 struct i915_vma *vma;
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002844 u64 offset;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002845
Ville Syrjäläe7941292016-01-19 18:23:17 +02002846 intel_fill_fb_ggtt_view(&view, intel_plane->base.state->fb,
Ville Syrjälä3465c582016-02-15 22:54:43 +02002847 intel_plane->base.state->rotation);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002848
Daniel Vetterce7f1722015-10-14 16:51:06 +02002849 vma = i915_gem_obj_to_ggtt_view(obj, &view);
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002850 if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
Daniel Vetterce7f1722015-10-14 16:51:06 +02002851 view.type))
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002852 return -1;
2853
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002854 offset = vma->node.start;
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002855
2856 if (plane == 1) {
Ville Syrjälä7723f47d2016-01-20 21:05:22 +02002857 offset += vma->ggtt_view.params.rotated.uv_start_page *
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002858 PAGE_SIZE;
2859 }
2860
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002861 WARN_ON(upper_32_bits(offset));
2862
2863 return lower_32_bits(offset);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002864}
2865
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002866static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
2867{
2868 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002869 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002870
2871 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
2872 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
2873 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002874}
2875
Chandra Kondurua1b22782015-04-07 15:28:45 -07002876/*
2877 * This function detaches (aka. unbinds) unused scalers in hardware
2878 */
Maarten Lankhorst05832362015-06-15 12:33:48 +02002879static void skl_detach_scalers(struct intel_crtc *intel_crtc)
Chandra Kondurua1b22782015-04-07 15:28:45 -07002880{
Chandra Kondurua1b22782015-04-07 15:28:45 -07002881 struct intel_crtc_scaler_state *scaler_state;
2882 int i;
2883
Chandra Kondurua1b22782015-04-07 15:28:45 -07002884 scaler_state = &intel_crtc->config->scaler_state;
2885
2886 /* loop through and disable scalers that aren't in use */
2887 for (i = 0; i < intel_crtc->num_scalers; i++) {
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002888 if (!scaler_state->scalers[i].in_use)
2889 skl_detach_scaler(intel_crtc, i);
Chandra Kondurua1b22782015-04-07 15:28:45 -07002890 }
2891}
2892
Chandra Konduru6156a452015-04-27 13:48:39 -07002893u32 skl_plane_ctl_format(uint32_t pixel_format)
2894{
Chandra Konduru6156a452015-04-27 13:48:39 -07002895 switch (pixel_format) {
Damien Lespiaud161cf72015-05-12 16:13:17 +01002896 case DRM_FORMAT_C8:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002897 return PLANE_CTL_FORMAT_INDEXED;
Chandra Konduru6156a452015-04-27 13:48:39 -07002898 case DRM_FORMAT_RGB565:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002899 return PLANE_CTL_FORMAT_RGB_565;
Chandra Konduru6156a452015-04-27 13:48:39 -07002900 case DRM_FORMAT_XBGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002901 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
Chandra Konduru6156a452015-04-27 13:48:39 -07002902 case DRM_FORMAT_XRGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002903 return PLANE_CTL_FORMAT_XRGB_8888;
Chandra Konduru6156a452015-04-27 13:48:39 -07002904 /*
2905 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2906 * to be already pre-multiplied. We need to add a knob (or a different
2907 * DRM_FORMAT) for user-space to configure that.
2908 */
2909 case DRM_FORMAT_ABGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002910 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
Chandra Konduru6156a452015-04-27 13:48:39 -07002911 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002912 case DRM_FORMAT_ARGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002913 return PLANE_CTL_FORMAT_XRGB_8888 |
Chandra Konduru6156a452015-04-27 13:48:39 -07002914 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002915 case DRM_FORMAT_XRGB2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002916 return PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002917 case DRM_FORMAT_XBGR2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002918 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002919 case DRM_FORMAT_YUYV:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002920 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
Chandra Konduru6156a452015-04-27 13:48:39 -07002921 case DRM_FORMAT_YVYU:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002922 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
Chandra Konduru6156a452015-04-27 13:48:39 -07002923 case DRM_FORMAT_UYVY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002924 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002925 case DRM_FORMAT_VYUY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002926 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002927 default:
Damien Lespiau4249eee2015-05-12 16:13:16 +01002928 MISSING_CASE(pixel_format);
Chandra Konduru6156a452015-04-27 13:48:39 -07002929 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002930
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002931 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002932}
2933
2934u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
2935{
Chandra Konduru6156a452015-04-27 13:48:39 -07002936 switch (fb_modifier) {
2937 case DRM_FORMAT_MOD_NONE:
2938 break;
2939 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002940 return PLANE_CTL_TILED_X;
Chandra Konduru6156a452015-04-27 13:48:39 -07002941 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002942 return PLANE_CTL_TILED_Y;
Chandra Konduru6156a452015-04-27 13:48:39 -07002943 case I915_FORMAT_MOD_Yf_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002944 return PLANE_CTL_TILED_YF;
Chandra Konduru6156a452015-04-27 13:48:39 -07002945 default:
2946 MISSING_CASE(fb_modifier);
2947 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002948
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002949 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002950}
2951
2952u32 skl_plane_ctl_rotation(unsigned int rotation)
2953{
Chandra Konduru6156a452015-04-27 13:48:39 -07002954 switch (rotation) {
2955 case BIT(DRM_ROTATE_0):
2956 break;
Sonika Jindal1e8df162015-05-20 13:40:48 +05302957 /*
2958 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
2959 * while i915 HW rotation is clockwise, thats why this swapping.
2960 */
Chandra Konduru6156a452015-04-27 13:48:39 -07002961 case BIT(DRM_ROTATE_90):
Sonika Jindal1e8df162015-05-20 13:40:48 +05302962 return PLANE_CTL_ROTATE_270;
Chandra Konduru6156a452015-04-27 13:48:39 -07002963 case BIT(DRM_ROTATE_180):
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002964 return PLANE_CTL_ROTATE_180;
Chandra Konduru6156a452015-04-27 13:48:39 -07002965 case BIT(DRM_ROTATE_270):
Sonika Jindal1e8df162015-05-20 13:40:48 +05302966 return PLANE_CTL_ROTATE_90;
Chandra Konduru6156a452015-04-27 13:48:39 -07002967 default:
2968 MISSING_CASE(rotation);
2969 }
2970
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002971 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002972}
2973
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002974static void skylake_update_primary_plane(struct drm_plane *plane,
2975 const struct intel_crtc_state *crtc_state,
2976 const struct intel_plane_state *plane_state)
Damien Lespiau70d21f02013-07-03 21:06:04 +01002977{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002978 struct drm_device *dev = plane->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002979 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002980 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2981 struct drm_framebuffer *fb = plane_state->base.fb;
2982 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Damien Lespiau70d21f02013-07-03 21:06:04 +01002983 int pipe = intel_crtc->pipe;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05302984 u32 plane_ctl, stride_div, stride;
2985 u32 tile_height, plane_offset, plane_size;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002986 unsigned int rotation = plane_state->base.rotation;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05302987 int x_offset, y_offset;
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002988 u32 surf_addr;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002989 int scaler_id = plane_state->scaler_id;
2990 int src_x = plane_state->src.x1 >> 16;
2991 int src_y = plane_state->src.y1 >> 16;
2992 int src_w = drm_rect_width(&plane_state->src) >> 16;
2993 int src_h = drm_rect_height(&plane_state->src) >> 16;
2994 int dst_x = plane_state->dst.x1;
2995 int dst_y = plane_state->dst.y1;
2996 int dst_w = drm_rect_width(&plane_state->dst);
2997 int dst_h = drm_rect_height(&plane_state->dst);
Damien Lespiau70d21f02013-07-03 21:06:04 +01002998
2999 plane_ctl = PLANE_CTL_ENABLE |
3000 PLANE_CTL_PIPE_GAMMA_ENABLE |
3001 PLANE_CTL_PIPE_CSC_ENABLE;
3002
Chandra Konduru6156a452015-04-27 13:48:39 -07003003 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3004 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003005 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Chandra Konduru6156a452015-04-27 13:48:39 -07003006 plane_ctl |= skl_plane_ctl_rotation(rotation);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003007
Ville Syrjälä7b49f942016-01-12 21:08:32 +02003008 stride_div = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
Damien Lespiaub3218032015-02-27 11:15:18 +00003009 fb->pixel_format);
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01003010 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj, 0);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303011
Paulo Zanonia42e5a22015-09-30 17:05:43 -03003012 WARN_ON(drm_rect_width(&plane_state->src) == 0);
Chandra Konduru6156a452015-04-27 13:48:39 -07003013
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303014 if (intel_rotation_90_or_270(rotation)) {
Ville Syrjälä832be822016-01-12 21:08:33 +02003015 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
3016
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303017 /* stride = Surface height in tiles */
Ville Syrjälä832be822016-01-12 21:08:33 +02003018 tile_height = intel_tile_height(dev_priv, fb->modifier[0], cpp);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303019 stride = DIV_ROUND_UP(fb->height, tile_height);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003020 x_offset = stride * tile_height - src_y - src_h;
3021 y_offset = src_x;
Chandra Konduru6156a452015-04-27 13:48:39 -07003022 plane_size = (src_w - 1) << 16 | (src_h - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303023 } else {
3024 stride = fb->pitches[0] / stride_div;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003025 x_offset = src_x;
3026 y_offset = src_y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003027 plane_size = (src_h - 1) << 16 | (src_w - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303028 }
3029 plane_offset = y_offset << 16 | x_offset;
Damien Lespiaub3218032015-02-27 11:15:18 +00003030
Paulo Zanoni2db33662015-09-14 15:20:03 -03003031 intel_crtc->adjusted_x = x_offset;
3032 intel_crtc->adjusted_y = y_offset;
3033
Damien Lespiau70d21f02013-07-03 21:06:04 +01003034 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303035 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3036 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3037 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
Chandra Konduru6156a452015-04-27 13:48:39 -07003038
3039 if (scaler_id >= 0) {
3040 uint32_t ps_ctrl = 0;
3041
3042 WARN_ON(!dst_w || !dst_h);
3043 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3044 crtc_state->scaler_state.scalers[scaler_id].mode;
3045 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3046 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3047 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3048 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3049 I915_WRITE(PLANE_POS(pipe, 0), 0);
3050 } else {
3051 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3052 }
3053
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003054 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003055
3056 POSTING_READ(PLANE_SURF(pipe, 0));
3057}
3058
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003059static void skylake_disable_primary_plane(struct drm_plane *primary,
3060 struct drm_crtc *crtc)
3061{
3062 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003063 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003064 int pipe = to_intel_crtc(crtc)->pipe;
3065
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003066 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3067 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3068 POSTING_READ(PLANE_SURF(pipe, 0));
3069}
3070
Jesse Barnes17638cd2011-06-24 12:19:23 -07003071/* Assume fb object is pinned & idle & fenced and just update base pointers */
3072static int
3073intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3074 int x, int y, enum mode_set_atomic state)
3075{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003076 /* Support for kgdboc is disabled, this needs a major rework. */
3077 DRM_ERROR("legacy panic handler not supported any more.\n");
Jesse Barnes17638cd2011-06-24 12:19:23 -07003078
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003079 return -ENODEV;
Jesse Barnes81255562010-08-02 12:07:50 -07003080}
3081
Daniel Vetter5a21b662016-05-24 17:13:53 +02003082static void intel_complete_page_flips(struct drm_i915_private *dev_priv)
3083{
3084 struct intel_crtc *crtc;
3085
Chris Wilson91c8a322016-07-05 10:40:23 +01003086 for_each_intel_crtc(&dev_priv->drm, crtc)
Daniel Vetter5a21b662016-05-24 17:13:53 +02003087 intel_finish_page_flip_cs(dev_priv, crtc->pipe);
3088}
3089
Ville Syrjälä75147472014-11-24 18:28:11 +02003090static void intel_update_primary_planes(struct drm_device *dev)
3091{
Ville Syrjälä75147472014-11-24 18:28:11 +02003092 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003093
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003094 for_each_crtc(dev, crtc) {
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003095 struct intel_plane *plane = to_intel_plane(crtc->primary);
Maarten Lankhorstdfa29972016-08-05 23:28:27 +03003096 struct intel_plane_state *plane_state =
3097 to_intel_plane_state(plane->base.state);
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003098
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003099 if (plane_state->visible)
3100 plane->update_plane(&plane->base,
3101 to_intel_crtc_state(crtc->state),
3102 plane_state);
Ville Syrjälä96a02912013-02-18 19:08:49 +02003103 }
3104}
3105
Maarten Lankhorstdfa29972016-08-05 23:28:27 +03003106static int
3107__intel_display_resume(struct drm_device *dev,
3108 struct drm_atomic_state *state)
3109{
3110 struct drm_crtc_state *crtc_state;
3111 struct drm_crtc *crtc;
3112 int i, ret;
3113
3114 intel_modeset_setup_hw_state(dev);
3115 i915_redisable_vga(dev);
3116
3117 if (!state)
3118 return 0;
3119
3120 for_each_crtc_in_state(state, crtc, crtc_state, i) {
3121 /*
3122 * Force recalculation even if we restore
3123 * current state. With fast modeset this may not result
3124 * in a modeset when the state is compatible.
3125 */
3126 crtc_state->mode_changed = true;
3127 }
3128
3129 /* ignore any reset values/BIOS leftovers in the WM registers */
3130 to_intel_atomic_state(state)->skip_intermediate_wm = true;
3131
3132 ret = drm_atomic_commit(state);
3133
3134 WARN_ON(ret == -EDEADLK);
3135 return ret;
3136}
3137
Chris Wilsonc0336662016-05-06 15:40:21 +01003138void intel_prepare_reset(struct drm_i915_private *dev_priv)
Ville Syrjälä75147472014-11-24 18:28:11 +02003139{
Maarten Lankhorstdfa29972016-08-05 23:28:27 +03003140 struct drm_device *dev = &dev_priv->drm;
3141 struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
3142 struct drm_atomic_state *state;
3143 int ret;
3144
Ville Syrjälä75147472014-11-24 18:28:11 +02003145 /* no reset support for gen2 */
Chris Wilsonc0336662016-05-06 15:40:21 +01003146 if (IS_GEN2(dev_priv))
Ville Syrjälä75147472014-11-24 18:28:11 +02003147 return;
3148
Maarten Lankhorstdfa29972016-08-05 23:28:27 +03003149 /*
3150 * Need mode_config.mutex so that we don't
3151 * trample ongoing ->detect() and whatnot.
3152 */
3153 mutex_lock(&dev->mode_config.mutex);
3154 drm_modeset_acquire_init(ctx, 0);
3155 while (1) {
3156 ret = drm_modeset_lock_all_ctx(dev, ctx);
3157 if (ret != -EDEADLK)
3158 break;
3159
3160 drm_modeset_backoff(ctx);
3161 }
3162
3163 /* reset doesn't touch the display, but flips might get nuked anyway, */
Chris Wilsonc0336662016-05-06 15:40:21 +01003164 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
Ville Syrjälä75147472014-11-24 18:28:11 +02003165 return;
3166
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003167 /*
3168 * Disabling the crtcs gracefully seems nicer. Also the
3169 * g33 docs say we should at least disable all the planes.
3170 */
Maarten Lankhorstdfa29972016-08-05 23:28:27 +03003171 state = drm_atomic_helper_duplicate_state(dev, ctx);
3172 if (IS_ERR(state)) {
3173 ret = PTR_ERR(state);
3174 state = NULL;
3175 DRM_ERROR("Duplicating state failed with %i\n", ret);
3176 goto err;
3177 }
3178
3179 ret = drm_atomic_helper_disable_all(dev, ctx);
3180 if (ret) {
3181 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
3182 goto err;
3183 }
3184
3185 dev_priv->modeset_restore_state = state;
3186 state->acquire_ctx = ctx;
3187 return;
3188
3189err:
3190 drm_atomic_state_free(state);
Ville Syrjälä75147472014-11-24 18:28:11 +02003191}
3192
Chris Wilsonc0336662016-05-06 15:40:21 +01003193void intel_finish_reset(struct drm_i915_private *dev_priv)
Ville Syrjälä75147472014-11-24 18:28:11 +02003194{
Maarten Lankhorstdfa29972016-08-05 23:28:27 +03003195 struct drm_device *dev = &dev_priv->drm;
3196 struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
3197 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
3198 int ret;
3199
Daniel Vetter5a21b662016-05-24 17:13:53 +02003200 /*
3201 * Flips in the rings will be nuked by the reset,
3202 * so complete all pending flips so that user space
3203 * will get its events and not get stuck.
3204 */
3205 intel_complete_page_flips(dev_priv);
3206
Ville Syrjälä75147472014-11-24 18:28:11 +02003207 /* no reset support for gen2 */
Chris Wilsonc0336662016-05-06 15:40:21 +01003208 if (IS_GEN2(dev_priv))
Ville Syrjälä75147472014-11-24 18:28:11 +02003209 return;
3210
Maarten Lankhorstdfa29972016-08-05 23:28:27 +03003211 dev_priv->modeset_restore_state = NULL;
3212
Ville Syrjälä75147472014-11-24 18:28:11 +02003213 /* reset doesn't touch the display */
Chris Wilsonc0336662016-05-06 15:40:21 +01003214 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv)) {
Ville Syrjälä75147472014-11-24 18:28:11 +02003215 /*
3216 * Flips in the rings have been nuked by the reset,
3217 * so update the base address of all primary
3218 * planes to the the last fb to make sure we're
3219 * showing the correct fb after a reset.
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003220 *
3221 * FIXME: Atomic will make this obsolete since we won't schedule
3222 * CS-based flips (which might get lost in gpu resets) any more.
Ville Syrjälä75147472014-11-24 18:28:11 +02003223 */
Maarten Lankhorstdfa29972016-08-05 23:28:27 +03003224 intel_update_primary_planes(dev);
3225 } else {
3226 /*
3227 * The display has been reset as well,
3228 * so need a full re-initialization.
3229 */
3230 intel_runtime_pm_disable_interrupts(dev_priv);
3231 intel_runtime_pm_enable_interrupts(dev_priv);
3232
3233 intel_modeset_init_hw(dev);
3234
3235 spin_lock_irq(&dev_priv->irq_lock);
3236 if (dev_priv->display.hpd_irq_setup)
3237 dev_priv->display.hpd_irq_setup(dev_priv);
3238 spin_unlock_irq(&dev_priv->irq_lock);
3239
3240 ret = __intel_display_resume(dev, state);
3241 if (ret)
3242 DRM_ERROR("Restoring old state failed with %i\n", ret);
3243
3244 intel_hpd_init(dev_priv);
Ville Syrjälä75147472014-11-24 18:28:11 +02003245 }
3246
Maarten Lankhorstdfa29972016-08-05 23:28:27 +03003247 drm_modeset_drop_locks(ctx);
3248 drm_modeset_acquire_fini(ctx);
3249 mutex_unlock(&dev->mode_config.mutex);
Ville Syrjälä75147472014-11-24 18:28:11 +02003250}
3251
Chris Wilson7d5e3792014-03-04 13:15:08 +00003252static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3253{
Daniel Vetter5a21b662016-05-24 17:13:53 +02003254 struct drm_device *dev = crtc->dev;
3255 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3256 unsigned reset_counter;
3257 bool pending;
3258
3259 reset_counter = i915_reset_counter(&to_i915(dev)->gpu_error);
3260 if (intel_crtc->reset_counter != reset_counter)
3261 return false;
3262
3263 spin_lock_irq(&dev->event_lock);
3264 pending = to_intel_crtc(crtc)->flip_work != NULL;
3265 spin_unlock_irq(&dev->event_lock);
3266
3267 return pending;
Chris Wilson7d5e3792014-03-04 13:15:08 +00003268}
3269
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003270static void intel_update_pipe_config(struct intel_crtc *crtc,
3271 struct intel_crtc_state *old_crtc_state)
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003272{
3273 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003274 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003275 struct intel_crtc_state *pipe_config =
3276 to_intel_crtc_state(crtc->base.state);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003277
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003278 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
3279 crtc->base.mode = crtc->base.state->mode;
3280
3281 DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
3282 old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
3283 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003284
3285 /*
3286 * Update pipe size and adjust fitter if needed: the reason for this is
3287 * that in compute_mode_changes we check the native mode (not the pfit
3288 * mode) to see if we can flip rather than do a full mode set. In the
3289 * fastboot case, we'll flip, but if we don't update the pipesrc and
3290 * pfit state, we'll end up with a big fb scanned out into the wrong
3291 * sized surface.
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003292 */
3293
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003294 I915_WRITE(PIPESRC(crtc->pipe),
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003295 ((pipe_config->pipe_src_w - 1) << 16) |
3296 (pipe_config->pipe_src_h - 1));
3297
3298 /* on skylake this is done by detaching scalers */
3299 if (INTEL_INFO(dev)->gen >= 9) {
3300 skl_detach_scalers(crtc);
3301
3302 if (pipe_config->pch_pfit.enabled)
3303 skylake_pfit_enable(crtc);
3304 } else if (HAS_PCH_SPLIT(dev)) {
3305 if (pipe_config->pch_pfit.enabled)
3306 ironlake_pfit_enable(crtc);
3307 else if (old_crtc_state->pch_pfit.enabled)
3308 ironlake_pfit_disable(crtc, true);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003309 }
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003310}
3311
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003312static void intel_fdi_normal_train(struct drm_crtc *crtc)
3313{
3314 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003315 struct drm_i915_private *dev_priv = to_i915(dev);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003316 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3317 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003318 i915_reg_t reg;
3319 u32 temp;
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003320
3321 /* enable normal train */
3322 reg = FDI_TX_CTL(pipe);
3323 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07003324 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003325 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3326 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003327 } else {
3328 temp &= ~FDI_LINK_TRAIN_NONE;
3329 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003330 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003331 I915_WRITE(reg, temp);
3332
3333 reg = FDI_RX_CTL(pipe);
3334 temp = I915_READ(reg);
3335 if (HAS_PCH_CPT(dev)) {
3336 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3337 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3338 } else {
3339 temp &= ~FDI_LINK_TRAIN_NONE;
3340 temp |= FDI_LINK_TRAIN_NONE;
3341 }
3342 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3343
3344 /* wait one idle pattern time */
3345 POSTING_READ(reg);
3346 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003347
3348 /* IVB wants error correction enabled */
3349 if (IS_IVYBRIDGE(dev))
3350 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3351 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003352}
3353
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003354/* The FDI link training functions for ILK/Ibexpeak. */
3355static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3356{
3357 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003358 struct drm_i915_private *dev_priv = to_i915(dev);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003359 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3360 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003361 i915_reg_t reg;
3362 u32 temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003363
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003364 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003365 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003366
Adam Jacksone1a44742010-06-25 15:32:14 -04003367 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3368 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003369 reg = FDI_RX_IMR(pipe);
3370 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003371 temp &= ~FDI_RX_SYMBOL_LOCK;
3372 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003373 I915_WRITE(reg, temp);
3374 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003375 udelay(150);
3376
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003377 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003378 reg = FDI_TX_CTL(pipe);
3379 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003380 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003381 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003382 temp &= ~FDI_LINK_TRAIN_NONE;
3383 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003384 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003385
Chris Wilson5eddb702010-09-11 13:48:45 +01003386 reg = FDI_RX_CTL(pipe);
3387 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003388 temp &= ~FDI_LINK_TRAIN_NONE;
3389 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003390 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3391
3392 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003393 udelay(150);
3394
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003395 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003396 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3397 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3398 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003399
Chris Wilson5eddb702010-09-11 13:48:45 +01003400 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003401 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003402 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003403 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3404
3405 if ((temp & FDI_RX_BIT_LOCK)) {
3406 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003407 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003408 break;
3409 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003410 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003411 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003412 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003413
3414 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003415 reg = FDI_TX_CTL(pipe);
3416 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003417 temp &= ~FDI_LINK_TRAIN_NONE;
3418 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003419 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003420
Chris Wilson5eddb702010-09-11 13:48:45 +01003421 reg = FDI_RX_CTL(pipe);
3422 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003423 temp &= ~FDI_LINK_TRAIN_NONE;
3424 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003425 I915_WRITE(reg, temp);
3426
3427 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003428 udelay(150);
3429
Chris Wilson5eddb702010-09-11 13:48:45 +01003430 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003431 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003432 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003433 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3434
3435 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003436 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003437 DRM_DEBUG_KMS("FDI train 2 done.\n");
3438 break;
3439 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003440 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003441 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003442 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003443
3444 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003445
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003446}
3447
Akshay Joshi0206e352011-08-16 15:34:10 -04003448static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003449 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3450 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3451 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3452 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3453};
3454
3455/* The FDI link training functions for SNB/Cougarpoint. */
3456static void gen6_fdi_link_train(struct drm_crtc *crtc)
3457{
3458 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003459 struct drm_i915_private *dev_priv = to_i915(dev);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003460 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3461 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003462 i915_reg_t reg;
3463 u32 temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003464
Adam Jacksone1a44742010-06-25 15:32:14 -04003465 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3466 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003467 reg = FDI_RX_IMR(pipe);
3468 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003469 temp &= ~FDI_RX_SYMBOL_LOCK;
3470 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003471 I915_WRITE(reg, temp);
3472
3473 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003474 udelay(150);
3475
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003476 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003477 reg = FDI_TX_CTL(pipe);
3478 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003479 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003480 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003481 temp &= ~FDI_LINK_TRAIN_NONE;
3482 temp |= FDI_LINK_TRAIN_PATTERN_1;
3483 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3484 /* SNB-B */
3485 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003486 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003487
Daniel Vetterd74cf322012-10-26 10:58:13 +02003488 I915_WRITE(FDI_RX_MISC(pipe),
3489 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3490
Chris Wilson5eddb702010-09-11 13:48:45 +01003491 reg = FDI_RX_CTL(pipe);
3492 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003493 if (HAS_PCH_CPT(dev)) {
3494 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3495 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3496 } else {
3497 temp &= ~FDI_LINK_TRAIN_NONE;
3498 temp |= FDI_LINK_TRAIN_PATTERN_1;
3499 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003500 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3501
3502 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003503 udelay(150);
3504
Akshay Joshi0206e352011-08-16 15:34:10 -04003505 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003506 reg = FDI_TX_CTL(pipe);
3507 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003508 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3509 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003510 I915_WRITE(reg, temp);
3511
3512 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003513 udelay(500);
3514
Sean Paulfa37d392012-03-02 12:53:39 -05003515 for (retry = 0; retry < 5; retry++) {
3516 reg = FDI_RX_IIR(pipe);
3517 temp = I915_READ(reg);
3518 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3519 if (temp & FDI_RX_BIT_LOCK) {
3520 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3521 DRM_DEBUG_KMS("FDI train 1 done.\n");
3522 break;
3523 }
3524 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003525 }
Sean Paulfa37d392012-03-02 12:53:39 -05003526 if (retry < 5)
3527 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003528 }
3529 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003530 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003531
3532 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003533 reg = FDI_TX_CTL(pipe);
3534 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003535 temp &= ~FDI_LINK_TRAIN_NONE;
3536 temp |= FDI_LINK_TRAIN_PATTERN_2;
3537 if (IS_GEN6(dev)) {
3538 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3539 /* SNB-B */
3540 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3541 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003542 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003543
Chris Wilson5eddb702010-09-11 13:48:45 +01003544 reg = FDI_RX_CTL(pipe);
3545 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003546 if (HAS_PCH_CPT(dev)) {
3547 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3548 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3549 } else {
3550 temp &= ~FDI_LINK_TRAIN_NONE;
3551 temp |= FDI_LINK_TRAIN_PATTERN_2;
3552 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003553 I915_WRITE(reg, temp);
3554
3555 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003556 udelay(150);
3557
Akshay Joshi0206e352011-08-16 15:34:10 -04003558 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003559 reg = FDI_TX_CTL(pipe);
3560 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003561 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3562 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003563 I915_WRITE(reg, temp);
3564
3565 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003566 udelay(500);
3567
Sean Paulfa37d392012-03-02 12:53:39 -05003568 for (retry = 0; retry < 5; retry++) {
3569 reg = FDI_RX_IIR(pipe);
3570 temp = I915_READ(reg);
3571 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3572 if (temp & FDI_RX_SYMBOL_LOCK) {
3573 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3574 DRM_DEBUG_KMS("FDI train 2 done.\n");
3575 break;
3576 }
3577 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003578 }
Sean Paulfa37d392012-03-02 12:53:39 -05003579 if (retry < 5)
3580 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003581 }
3582 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003583 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003584
3585 DRM_DEBUG_KMS("FDI train done.\n");
3586}
3587
Jesse Barnes357555c2011-04-28 15:09:55 -07003588/* Manual link training for Ivy Bridge A0 parts */
3589static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3590{
3591 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003592 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes357555c2011-04-28 15:09:55 -07003593 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3594 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003595 i915_reg_t reg;
3596 u32 temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003597
3598 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3599 for train result */
3600 reg = FDI_RX_IMR(pipe);
3601 temp = I915_READ(reg);
3602 temp &= ~FDI_RX_SYMBOL_LOCK;
3603 temp &= ~FDI_RX_BIT_LOCK;
3604 I915_WRITE(reg, temp);
3605
3606 POSTING_READ(reg);
3607 udelay(150);
3608
Daniel Vetter01a415f2012-10-27 15:58:40 +02003609 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3610 I915_READ(FDI_RX_IIR(pipe)));
3611
Jesse Barnes139ccd32013-08-19 11:04:55 -07003612 /* Try each vswing and preemphasis setting twice before moving on */
3613 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3614 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003615 reg = FDI_TX_CTL(pipe);
3616 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003617 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3618 temp &= ~FDI_TX_ENABLE;
3619 I915_WRITE(reg, temp);
3620
3621 reg = FDI_RX_CTL(pipe);
3622 temp = I915_READ(reg);
3623 temp &= ~FDI_LINK_TRAIN_AUTO;
3624 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3625 temp &= ~FDI_RX_ENABLE;
3626 I915_WRITE(reg, temp);
3627
3628 /* enable CPU FDI TX and PCH FDI RX */
3629 reg = FDI_TX_CTL(pipe);
3630 temp = I915_READ(reg);
3631 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003632 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003633 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003634 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003635 temp |= snb_b_fdi_train_param[j/2];
3636 temp |= FDI_COMPOSITE_SYNC;
3637 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3638
3639 I915_WRITE(FDI_RX_MISC(pipe),
3640 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3641
3642 reg = FDI_RX_CTL(pipe);
3643 temp = I915_READ(reg);
3644 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3645 temp |= FDI_COMPOSITE_SYNC;
3646 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3647
3648 POSTING_READ(reg);
3649 udelay(1); /* should be 0.5us */
3650
3651 for (i = 0; i < 4; i++) {
3652 reg = FDI_RX_IIR(pipe);
3653 temp = I915_READ(reg);
3654 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3655
3656 if (temp & FDI_RX_BIT_LOCK ||
3657 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3658 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3659 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3660 i);
3661 break;
3662 }
3663 udelay(1); /* should be 0.5us */
3664 }
3665 if (i == 4) {
3666 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3667 continue;
3668 }
3669
3670 /* Train 2 */
3671 reg = FDI_TX_CTL(pipe);
3672 temp = I915_READ(reg);
3673 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3674 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3675 I915_WRITE(reg, temp);
3676
3677 reg = FDI_RX_CTL(pipe);
3678 temp = I915_READ(reg);
3679 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3680 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003681 I915_WRITE(reg, temp);
3682
3683 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003684 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003685
Jesse Barnes139ccd32013-08-19 11:04:55 -07003686 for (i = 0; i < 4; i++) {
3687 reg = FDI_RX_IIR(pipe);
3688 temp = I915_READ(reg);
3689 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003690
Jesse Barnes139ccd32013-08-19 11:04:55 -07003691 if (temp & FDI_RX_SYMBOL_LOCK ||
3692 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3693 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3694 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3695 i);
3696 goto train_done;
3697 }
3698 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003699 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003700 if (i == 4)
3701 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003702 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003703
Jesse Barnes139ccd32013-08-19 11:04:55 -07003704train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003705 DRM_DEBUG_KMS("FDI train done.\n");
3706}
3707
Daniel Vetter88cefb62012-08-12 19:27:14 +02003708static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003709{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003710 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003711 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003712 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003713 i915_reg_t reg;
3714 u32 temp;
Jesse Barnesc64e3112010-09-10 11:27:03 -07003715
Jesse Barnes0e23b992010-09-10 11:10:00 -07003716 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003717 reg = FDI_RX_CTL(pipe);
3718 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003719 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003720 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003721 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003722 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3723
3724 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003725 udelay(200);
3726
3727 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003728 temp = I915_READ(reg);
3729 I915_WRITE(reg, temp | FDI_PCDCLK);
3730
3731 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003732 udelay(200);
3733
Paulo Zanoni20749732012-11-23 15:30:38 -02003734 /* Enable CPU FDI TX PLL, always on for Ironlake */
3735 reg = FDI_TX_CTL(pipe);
3736 temp = I915_READ(reg);
3737 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3738 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003739
Paulo Zanoni20749732012-11-23 15:30:38 -02003740 POSTING_READ(reg);
3741 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003742 }
3743}
3744
Daniel Vetter88cefb62012-08-12 19:27:14 +02003745static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3746{
3747 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003748 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter88cefb62012-08-12 19:27:14 +02003749 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003750 i915_reg_t reg;
3751 u32 temp;
Daniel Vetter88cefb62012-08-12 19:27:14 +02003752
3753 /* Switch from PCDclk to Rawclk */
3754 reg = FDI_RX_CTL(pipe);
3755 temp = I915_READ(reg);
3756 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3757
3758 /* Disable CPU FDI TX PLL */
3759 reg = FDI_TX_CTL(pipe);
3760 temp = I915_READ(reg);
3761 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3762
3763 POSTING_READ(reg);
3764 udelay(100);
3765
3766 reg = FDI_RX_CTL(pipe);
3767 temp = I915_READ(reg);
3768 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3769
3770 /* Wait for the clocks to turn off. */
3771 POSTING_READ(reg);
3772 udelay(100);
3773}
3774
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003775static void ironlake_fdi_disable(struct drm_crtc *crtc)
3776{
3777 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003778 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003779 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3780 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003781 i915_reg_t reg;
3782 u32 temp;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003783
3784 /* disable CPU FDI tx and PCH FDI rx */
3785 reg = FDI_TX_CTL(pipe);
3786 temp = I915_READ(reg);
3787 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3788 POSTING_READ(reg);
3789
3790 reg = FDI_RX_CTL(pipe);
3791 temp = I915_READ(reg);
3792 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003793 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003794 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3795
3796 POSTING_READ(reg);
3797 udelay(100);
3798
3799 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003800 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003801 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003802
3803 /* still set train pattern 1 */
3804 reg = FDI_TX_CTL(pipe);
3805 temp = I915_READ(reg);
3806 temp &= ~FDI_LINK_TRAIN_NONE;
3807 temp |= FDI_LINK_TRAIN_PATTERN_1;
3808 I915_WRITE(reg, temp);
3809
3810 reg = FDI_RX_CTL(pipe);
3811 temp = I915_READ(reg);
3812 if (HAS_PCH_CPT(dev)) {
3813 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3814 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3815 } else {
3816 temp &= ~FDI_LINK_TRAIN_NONE;
3817 temp |= FDI_LINK_TRAIN_PATTERN_1;
3818 }
3819 /* BPC in FDI rx is consistent with that in PIPECONF */
3820 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003821 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003822 I915_WRITE(reg, temp);
3823
3824 POSTING_READ(reg);
3825 udelay(100);
3826}
3827
Chris Wilson5dce5b932014-01-20 10:17:36 +00003828bool intel_has_pending_fb_unpin(struct drm_device *dev)
3829{
3830 struct intel_crtc *crtc;
3831
3832 /* Note that we don't need to be called with mode_config.lock here
3833 * as our list of CRTC objects is static for the lifetime of the
3834 * device and so cannot disappear as we iterate. Similarly, we can
3835 * happily treat the predicates as racy, atomic checks as userspace
3836 * cannot claim and pin a new fb without at least acquring the
3837 * struct_mutex and so serialising with us.
3838 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003839 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003840 if (atomic_read(&crtc->unpin_work_count) == 0)
3841 continue;
3842
Daniel Vetter5a21b662016-05-24 17:13:53 +02003843 if (crtc->flip_work)
Chris Wilson5dce5b932014-01-20 10:17:36 +00003844 intel_wait_for_vblank(dev, crtc->pipe);
3845
3846 return true;
3847 }
3848
3849 return false;
3850}
3851
Daniel Vetter5a21b662016-05-24 17:13:53 +02003852static void page_flip_completed(struct intel_crtc *intel_crtc)
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003853{
3854 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +02003855 struct intel_flip_work *work = intel_crtc->flip_work;
3856
3857 intel_crtc->flip_work = NULL;
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003858
3859 if (work->event)
Gustavo Padovan560ce1d2016-04-14 10:48:15 -07003860 drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003861
3862 drm_crtc_vblank_put(&intel_crtc->base);
3863
Daniel Vetter5a21b662016-05-24 17:13:53 +02003864 wake_up_all(&dev_priv->pending_flip_queue);
Maarten Lankhorst143f73b2016-05-17 15:07:54 +02003865 queue_work(dev_priv->wq, &work->unpin_work);
Daniel Vetter5a21b662016-05-24 17:13:53 +02003866
3867 trace_i915_flip_complete(intel_crtc->plane,
3868 work->pending_flip_obj);
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003869}
3870
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003871static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003872{
Chris Wilson0f911282012-04-17 10:05:38 +01003873 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003874 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003875 long ret;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003876
Daniel Vetter2c10d572012-12-20 21:24:07 +01003877 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003878
3879 ret = wait_event_interruptible_timeout(
3880 dev_priv->pending_flip_queue,
3881 !intel_crtc_has_pending_flip(crtc),
3882 60*HZ);
3883
3884 if (ret < 0)
3885 return ret;
3886
Daniel Vetter5a21b662016-05-24 17:13:53 +02003887 if (ret == 0) {
3888 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3889 struct intel_flip_work *work;
3890
3891 spin_lock_irq(&dev->event_lock);
3892 work = intel_crtc->flip_work;
3893 if (work && !is_mmio_work(work)) {
3894 WARN_ONCE(1, "Removing stuck page flip\n");
3895 page_flip_completed(intel_crtc);
3896 }
3897 spin_unlock_irq(&dev->event_lock);
3898 }
Chris Wilson5bb61642012-09-27 21:25:58 +01003899
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003900 return 0;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003901}
3902
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003903static void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
3904{
3905 u32 temp;
3906
3907 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3908
3909 mutex_lock(&dev_priv->sb_lock);
3910
3911 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3912 temp |= SBI_SSCCTL_DISABLE;
3913 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
3914
3915 mutex_unlock(&dev_priv->sb_lock);
3916}
3917
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003918/* Program iCLKIP clock to the desired frequency */
3919static void lpt_program_iclkip(struct drm_crtc *crtc)
3920{
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003921 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003922 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003923 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3924 u32 temp;
3925
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003926 lpt_disable_iclkip(dev_priv);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003927
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003928 /* The iCLK virtual clock root frequency is in MHz,
3929 * but the adjusted_mode->crtc_clock in in KHz. To get the
3930 * divisors, it is necessary to divide one by another, so we
3931 * convert the virtual clock precision to KHz here for higher
3932 * precision.
3933 */
3934 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003935 u32 iclk_virtual_root_freq = 172800 * 1000;
3936 u32 iclk_pi_range = 64;
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003937 u32 desired_divisor;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003938
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003939 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
3940 clock << auxdiv);
3941 divsel = (desired_divisor / iclk_pi_range) - 2;
3942 phaseinc = desired_divisor % iclk_pi_range;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003943
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003944 /*
3945 * Near 20MHz is a corner case which is
3946 * out of range for the 7-bit divisor
3947 */
3948 if (divsel <= 0x7f)
3949 break;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003950 }
3951
3952 /* This should not happen with any sane values */
3953 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3954 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3955 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3956 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3957
3958 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 +03003959 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003960 auxdiv,
3961 divsel,
3962 phasedir,
3963 phaseinc);
3964
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003965 mutex_lock(&dev_priv->sb_lock);
3966
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003967 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003968 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003969 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3970 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3971 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3972 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3973 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3974 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003975 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003976
3977 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003978 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003979 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3980 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003981 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003982
3983 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003984 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003985 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003986 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003987
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003988 mutex_unlock(&dev_priv->sb_lock);
3989
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003990 /* Wait for initialization time */
3991 udelay(24);
3992
3993 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
3994}
3995
Ville Syrjälä8802e5b2016-02-17 21:41:12 +02003996int lpt_get_iclkip(struct drm_i915_private *dev_priv)
3997{
3998 u32 divsel, phaseinc, auxdiv;
3999 u32 iclk_virtual_root_freq = 172800 * 1000;
4000 u32 iclk_pi_range = 64;
4001 u32 desired_divisor;
4002 u32 temp;
4003
4004 if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
4005 return 0;
4006
4007 mutex_lock(&dev_priv->sb_lock);
4008
4009 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
4010 if (temp & SBI_SSCCTL_DISABLE) {
4011 mutex_unlock(&dev_priv->sb_lock);
4012 return 0;
4013 }
4014
4015 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
4016 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
4017 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
4018 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
4019 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
4020
4021 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
4022 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
4023 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
4024
4025 mutex_unlock(&dev_priv->sb_lock);
4026
4027 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
4028
4029 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4030 desired_divisor << auxdiv);
4031}
4032
Daniel Vetter275f01b22013-05-03 11:49:47 +02004033static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4034 enum pipe pch_transcoder)
4035{
4036 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004037 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004038 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02004039
4040 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4041 I915_READ(HTOTAL(cpu_transcoder)));
4042 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4043 I915_READ(HBLANK(cpu_transcoder)));
4044 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4045 I915_READ(HSYNC(cpu_transcoder)));
4046
4047 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4048 I915_READ(VTOTAL(cpu_transcoder)));
4049 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4050 I915_READ(VBLANK(cpu_transcoder)));
4051 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4052 I915_READ(VSYNC(cpu_transcoder)));
4053 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4054 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4055}
4056
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004057static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004058{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004059 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004060 uint32_t temp;
4061
4062 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004063 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004064 return;
4065
4066 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4067 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4068
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004069 temp &= ~FDI_BC_BIFURCATION_SELECT;
4070 if (enable)
4071 temp |= FDI_BC_BIFURCATION_SELECT;
4072
4073 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004074 I915_WRITE(SOUTH_CHICKEN1, temp);
4075 POSTING_READ(SOUTH_CHICKEN1);
4076}
4077
4078static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4079{
4080 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004081
4082 switch (intel_crtc->pipe) {
4083 case PIPE_A:
4084 break;
4085 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004086 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004087 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004088 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004089 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004090
4091 break;
4092 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004093 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004094
4095 break;
4096 default:
4097 BUG();
4098 }
4099}
4100
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004101/* Return which DP Port should be selected for Transcoder DP control */
4102static enum port
4103intel_trans_dp_port_sel(struct drm_crtc *crtc)
4104{
4105 struct drm_device *dev = crtc->dev;
4106 struct intel_encoder *encoder;
4107
4108 for_each_encoder_on_crtc(dev, crtc, encoder) {
Ville Syrjäläcca05022016-06-22 21:57:06 +03004109 if (encoder->type == INTEL_OUTPUT_DP ||
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004110 encoder->type == INTEL_OUTPUT_EDP)
4111 return enc_to_dig_port(&encoder->base)->port;
4112 }
4113
4114 return -1;
4115}
4116
Jesse Barnesf67a5592011-01-05 10:31:48 -08004117/*
4118 * Enable PCH resources required for PCH ports:
4119 * - PCH PLLs
4120 * - FDI training & RX/TX
4121 * - update transcoder timings
4122 * - DP transcoding bits
4123 * - transcoder
4124 */
4125static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08004126{
4127 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004128 struct drm_i915_private *dev_priv = to_i915(dev);
Zhenyu Wang2c072452009-06-05 15:38:42 +08004129 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4130 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004131 u32 temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004132
Daniel Vetterab9412b2013-05-03 11:49:46 +02004133 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004134
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004135 if (IS_IVYBRIDGE(dev))
4136 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4137
Daniel Vettercd986ab2012-10-26 10:58:12 +02004138 /* Write the TU size bits before fdi link training, so that error
4139 * detection works. */
4140 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4141 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4142
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004143 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07004144 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004145
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004146 /* We need to program the right clock selection before writing the pixel
4147 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004148 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004149 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004150
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004151 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004152 temp |= TRANS_DPLL_ENABLE(pipe);
4153 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02004154 if (intel_crtc->config->shared_dpll ==
4155 intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004156 temp |= sel;
4157 else
4158 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004159 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004160 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004161
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004162 /* XXX: pch pll's can be enabled any time before we enable the PCH
4163 * transcoder, and we actually should do this to not upset any PCH
4164 * transcoder that already use the clock when we share it.
4165 *
4166 * Note that enable_shared_dpll tries to do the right thing, but
4167 * get_shared_dpll unconditionally resets the pll - we need that to have
4168 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02004169 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004170
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004171 /* set transcoder timing, panel must allow it */
4172 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02004173 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004174
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004175 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004176
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004177 /* For PCH DP, enable TRANS_DP_CTL */
Ville Syrjälä37a56502016-06-22 21:57:04 +03004178 if (HAS_PCH_CPT(dev) && intel_crtc_has_dp_encoder(intel_crtc->config)) {
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004179 const struct drm_display_mode *adjusted_mode =
4180 &intel_crtc->config->base.adjusted_mode;
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004181 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004182 i915_reg_t reg = TRANS_DP_CTL(pipe);
Chris Wilson5eddb702010-09-11 13:48:45 +01004183 temp = I915_READ(reg);
4184 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004185 TRANS_DP_SYNC_MASK |
4186 TRANS_DP_BPC_MASK);
Ville Syrjäläe3ef4472015-05-05 17:17:31 +03004187 temp |= TRANS_DP_OUTPUT_ENABLE;
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004188 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004189
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004190 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004191 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004192 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004193 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004194
4195 switch (intel_trans_dp_port_sel(crtc)) {
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004196 case PORT_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004197 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004198 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004199 case PORT_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004200 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004201 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004202 case PORT_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004203 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004204 break;
4205 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004206 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004207 }
4208
Chris Wilson5eddb702010-09-11 13:48:45 +01004209 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004210 }
4211
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004212 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004213}
4214
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004215static void lpt_pch_enable(struct drm_crtc *crtc)
4216{
4217 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004218 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004219 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004220 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004221
Daniel Vetterab9412b2013-05-03 11:49:46 +02004222 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004223
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004224 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004225
Paulo Zanoni0540e482012-10-31 18:12:40 -02004226 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02004227 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004228
Paulo Zanoni937bb612012-10-31 18:12:47 -02004229 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004230}
4231
Daniel Vettera1520312013-05-03 11:49:50 +02004232static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004233{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004234 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004235 i915_reg_t dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004236 u32 temp;
4237
4238 temp = I915_READ(dslreg);
4239 udelay(500);
4240 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004241 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004242 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004243 }
4244}
4245
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004246static int
4247skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4248 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4249 int src_w, int src_h, int dst_w, int dst_h)
Chandra Kondurua1b22782015-04-07 15:28:45 -07004250{
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004251 struct intel_crtc_scaler_state *scaler_state =
4252 &crtc_state->scaler_state;
4253 struct intel_crtc *intel_crtc =
4254 to_intel_crtc(crtc_state->base.crtc);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004255 int need_scaling;
Chandra Konduru6156a452015-04-27 13:48:39 -07004256
4257 need_scaling = intel_rotation_90_or_270(rotation) ?
4258 (src_h != dst_w || src_w != dst_h):
4259 (src_w != dst_w || src_h != dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004260
4261 /*
4262 * if plane is being disabled or scaler is no more required or force detach
4263 * - free scaler binded to this plane/crtc
4264 * - in order to do this, update crtc->scaler_usage
4265 *
4266 * Here scaler state in crtc_state is set free so that
4267 * scaler can be assigned to other user. Actual register
4268 * update to free the scaler is done in plane/panel-fit programming.
4269 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4270 */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004271 if (force_detach || !need_scaling) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004272 if (*scaler_id >= 0) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004273 scaler_state->scaler_users &= ~(1 << scaler_user);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004274 scaler_state->scalers[*scaler_id].in_use = 0;
4275
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004276 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4277 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4278 intel_crtc->pipe, scaler_user, *scaler_id,
Chandra Kondurua1b22782015-04-07 15:28:45 -07004279 scaler_state->scaler_users);
4280 *scaler_id = -1;
4281 }
4282 return 0;
4283 }
4284
4285 /* range checks */
4286 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4287 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4288
4289 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4290 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004291 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
Chandra Kondurua1b22782015-04-07 15:28:45 -07004292 "size is out of scaler range\n",
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004293 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004294 return -EINVAL;
4295 }
4296
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004297 /* mark this plane as a scaler user in crtc_state */
4298 scaler_state->scaler_users |= (1 << scaler_user);
4299 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4300 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4301 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4302 scaler_state->scaler_users);
4303
4304 return 0;
4305}
4306
4307/**
4308 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4309 *
4310 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004311 *
4312 * Return
4313 * 0 - scaler_usage updated successfully
4314 * error - requested scaling cannot be supported or other error condition
4315 */
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004316int skl_update_scaler_crtc(struct intel_crtc_state *state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004317{
4318 struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03004319 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004320
Ville Syrjälä78108b72016-05-27 20:59:19 +03004321 DRM_DEBUG_KMS("Updating scaler for [CRTC:%d:%s] scaler_user index %u.%u\n",
4322 intel_crtc->base.base.id, intel_crtc->base.name,
4323 intel_crtc->pipe, SKL_CRTC_INDEX);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004324
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004325 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
Ville Syrjäläfa5a7972015-10-15 17:01:58 +03004326 &state->scaler_state.scaler_id, BIT(DRM_ROTATE_0),
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004327 state->pipe_src_w, state->pipe_src_h,
Ville Syrjäläaad941d2015-09-25 16:38:56 +03004328 adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004329}
4330
4331/**
4332 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4333 *
4334 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004335 * @plane_state: atomic plane state to update
4336 *
4337 * Return
4338 * 0 - scaler_usage updated successfully
4339 * error - requested scaling cannot be supported or other error condition
4340 */
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004341static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4342 struct intel_plane_state *plane_state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004343{
4344
4345 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004346 struct intel_plane *intel_plane =
4347 to_intel_plane(plane_state->base.plane);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004348 struct drm_framebuffer *fb = plane_state->base.fb;
4349 int ret;
4350
4351 bool force_detach = !fb || !plane_state->visible;
4352
Ville Syrjälä72660ce2016-05-27 20:59:20 +03004353 DRM_DEBUG_KMS("Updating scaler for [PLANE:%d:%s] scaler_user index %u.%u\n",
4354 intel_plane->base.base.id, intel_plane->base.name,
4355 intel_crtc->pipe, drm_plane_index(&intel_plane->base));
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004356
4357 ret = skl_update_scaler(crtc_state, force_detach,
4358 drm_plane_index(&intel_plane->base),
4359 &plane_state->scaler_id,
4360 plane_state->base.rotation,
4361 drm_rect_width(&plane_state->src) >> 16,
4362 drm_rect_height(&plane_state->src) >> 16,
4363 drm_rect_width(&plane_state->dst),
4364 drm_rect_height(&plane_state->dst));
4365
4366 if (ret || plane_state->scaler_id < 0)
4367 return ret;
4368
Chandra Kondurua1b22782015-04-07 15:28:45 -07004369 /* check colorkey */
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004370 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
Ville Syrjälä72660ce2016-05-27 20:59:20 +03004371 DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
4372 intel_plane->base.base.id,
4373 intel_plane->base.name);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004374 return -EINVAL;
4375 }
4376
4377 /* Check src format */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004378 switch (fb->pixel_format) {
4379 case DRM_FORMAT_RGB565:
4380 case DRM_FORMAT_XBGR8888:
4381 case DRM_FORMAT_XRGB8888:
4382 case DRM_FORMAT_ABGR8888:
4383 case DRM_FORMAT_ARGB8888:
4384 case DRM_FORMAT_XRGB2101010:
4385 case DRM_FORMAT_XBGR2101010:
4386 case DRM_FORMAT_YUYV:
4387 case DRM_FORMAT_YVYU:
4388 case DRM_FORMAT_UYVY:
4389 case DRM_FORMAT_VYUY:
4390 break;
4391 default:
Ville Syrjälä72660ce2016-05-27 20:59:20 +03004392 DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
4393 intel_plane->base.base.id, intel_plane->base.name,
4394 fb->base.id, fb->pixel_format);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004395 return -EINVAL;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004396 }
4397
Chandra Kondurua1b22782015-04-07 15:28:45 -07004398 return 0;
4399}
4400
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004401static void skylake_scaler_disable(struct intel_crtc *crtc)
4402{
4403 int i;
4404
4405 for (i = 0; i < crtc->num_scalers; i++)
4406 skl_detach_scaler(crtc, i);
4407}
4408
4409static void skylake_pfit_enable(struct intel_crtc *crtc)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004410{
4411 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004412 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004413 int pipe = crtc->pipe;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004414 struct intel_crtc_scaler_state *scaler_state =
4415 &crtc->config->scaler_state;
4416
4417 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4418
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004419 if (crtc->config->pch_pfit.enabled) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004420 int id;
4421
4422 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4423 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4424 return;
4425 }
4426
4427 id = scaler_state->scaler_id;
4428 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4429 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4430 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4431 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4432
4433 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004434 }
4435}
4436
Jesse Barnesb074cec2013-04-25 12:55:02 -07004437static void ironlake_pfit_enable(struct intel_crtc *crtc)
4438{
4439 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004440 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesb074cec2013-04-25 12:55:02 -07004441 int pipe = crtc->pipe;
4442
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004443 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004444 /* Force use of hard-coded filter coefficients
4445 * as some pre-programmed values are broken,
4446 * e.g. x201.
4447 */
4448 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4449 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4450 PF_PIPE_SEL_IVB(pipe));
4451 else
4452 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004453 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4454 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004455 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004456}
4457
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004458void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004459{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004460 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004461 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanonid77e4532013-09-24 13:52:55 -03004462
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004463 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004464 return;
4465
Maarten Lankhorst307e4492016-03-23 14:33:28 +01004466 /*
4467 * We can only enable IPS after we enable a plane and wait for a vblank
4468 * This function is called from post_plane_update, which is run after
4469 * a vblank wait.
4470 */
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004471
Paulo Zanonid77e4532013-09-24 13:52:55 -03004472 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004473 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004474 mutex_lock(&dev_priv->rps.hw_lock);
4475 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4476 mutex_unlock(&dev_priv->rps.hw_lock);
4477 /* Quoting Art Runyan: "its not safe to expect any particular
4478 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004479 * mailbox." Moreover, the mailbox may return a bogus state,
4480 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004481 */
4482 } else {
4483 I915_WRITE(IPS_CTL, IPS_ENABLE);
4484 /* The bit only becomes 1 in the next vblank, so this wait here
4485 * is essentially intel_wait_for_vblank. If we don't have this
4486 * and don't wait for vblanks until the end of crtc_enable, then
4487 * the HW state readout code will complain that the expected
4488 * IPS_CTL value is not the one we read. */
Chris Wilson2ec9ba32016-06-30 15:33:01 +01004489 if (intel_wait_for_register(dev_priv,
4490 IPS_CTL, IPS_ENABLE, IPS_ENABLE,
4491 50))
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004492 DRM_ERROR("Timed out waiting for IPS enable\n");
4493 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004494}
4495
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004496void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004497{
4498 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004499 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanonid77e4532013-09-24 13:52:55 -03004500
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004501 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004502 return;
4503
4504 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004505 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004506 mutex_lock(&dev_priv->rps.hw_lock);
4507 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4508 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004509 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
Chris Wilsonb85c1ec2016-06-30 15:33:02 +01004510 if (intel_wait_for_register(dev_priv,
4511 IPS_CTL, IPS_ENABLE, 0,
4512 42))
Ben Widawsky23d0b132014-04-10 14:32:41 -07004513 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004514 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004515 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004516 POSTING_READ(IPS_CTL);
4517 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004518
4519 /* We need to wait for a vblank before we can disable the plane. */
4520 intel_wait_for_vblank(dev, crtc->pipe);
4521}
4522
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004523static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004524{
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004525 if (intel_crtc->overlay) {
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004526 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004527 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004528
4529 mutex_lock(&dev->struct_mutex);
4530 dev_priv->mm.interruptible = false;
4531 (void) intel_overlay_switch_off(intel_crtc->overlay);
4532 dev_priv->mm.interruptible = true;
4533 mutex_unlock(&dev->struct_mutex);
4534 }
4535
4536 /* Let userspace switch the overlay on again. In most cases userspace
4537 * has to recompute where to put it anyway.
4538 */
4539}
4540
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004541/**
4542 * intel_post_enable_primary - Perform operations after enabling primary plane
4543 * @crtc: the CRTC whose primary plane was just enabled
4544 *
4545 * Performs potentially sleeping operations that must be done after the primary
4546 * plane is enabled, such as updating FBC and IPS. Note that this may be
4547 * called due to an explicit primary plane update, or due to an implicit
4548 * re-enable that is caused when a sprite plane is updated to no longer
4549 * completely hide the primary plane.
4550 */
4551static void
4552intel_post_enable_primary(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004553{
4554 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004555 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004556 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4557 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004558
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004559 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004560 * FIXME IPS should be fine as long as one plane is
4561 * enabled, but in practice it seems to have problems
4562 * when going from primary only to sprite only and vice
4563 * versa.
4564 */
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004565 hsw_enable_ips(intel_crtc);
4566
Daniel Vetterf99d7062014-06-19 16:01:59 +02004567 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004568 * Gen2 reports pipe underruns whenever all planes are disabled.
4569 * So don't enable underrun reporting before at least some planes
4570 * are enabled.
4571 * FIXME: Need to fix the logic to work when we turn off all planes
4572 * but leave the pipe running.
Daniel Vetterf99d7062014-06-19 16:01:59 +02004573 */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004574 if (IS_GEN2(dev))
4575 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4576
Ville Syrjäläaca7b682015-10-30 19:22:21 +02004577 /* Underruns don't always raise interrupts, so check manually. */
4578 intel_check_cpu_fifo_underruns(dev_priv);
4579 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004580}
4581
Ville Syrjälä2622a082016-03-09 19:07:26 +02004582/* FIXME move all this to pre_plane_update() with proper state tracking */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004583static void
4584intel_pre_disable_primary(struct drm_crtc *crtc)
4585{
4586 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004587 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004588 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4589 int pipe = intel_crtc->pipe;
4590
4591 /*
4592 * Gen2 reports pipe underruns whenever all planes are disabled.
4593 * So diasble underrun reporting before all the planes get disabled.
4594 * FIXME: Need to fix the logic to work when we turn off all planes
4595 * but leave the pipe running.
4596 */
4597 if (IS_GEN2(dev))
4598 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4599
4600 /*
Ville Syrjälä2622a082016-03-09 19:07:26 +02004601 * FIXME IPS should be fine as long as one plane is
4602 * enabled, but in practice it seems to have problems
4603 * when going from primary only to sprite only and vice
4604 * versa.
4605 */
4606 hsw_disable_ips(intel_crtc);
4607}
4608
4609/* FIXME get rid of this and use pre_plane_update */
4610static void
4611intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
4612{
4613 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004614 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä2622a082016-03-09 19:07:26 +02004615 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4616 int pipe = intel_crtc->pipe;
4617
4618 intel_pre_disable_primary(crtc);
4619
4620 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004621 * Vblank time updates from the shadow to live plane control register
4622 * are blocked if the memory self-refresh mode is active at that
4623 * moment. So to make sure the plane gets truly disabled, disable
4624 * first the self-refresh mode. The self-refresh enable bit in turn
4625 * will be checked/applied by the HW only at the next frame start
4626 * event which is after the vblank start event, so we need to have a
4627 * wait-for-vblank between disabling the plane and the pipe.
4628 */
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004629 if (HAS_GMCH_DISPLAY(dev)) {
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004630 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004631 dev_priv->wm.vlv.cxsr = false;
4632 intel_wait_for_vblank(dev, pipe);
4633 }
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004634}
4635
Daniel Vetter5a21b662016-05-24 17:13:53 +02004636static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
4637{
4638 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
4639 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4640 struct intel_crtc_state *pipe_config =
4641 to_intel_crtc_state(crtc->base.state);
4642 struct drm_device *dev = crtc->base.dev;
4643 struct drm_plane *primary = crtc->base.primary;
4644 struct drm_plane_state *old_pri_state =
4645 drm_atomic_get_existing_plane_state(old_state, primary);
4646
4647 intel_frontbuffer_flip(dev, pipe_config->fb_bits);
4648
4649 crtc->wm.cxsr_allowed = true;
4650
4651 if (pipe_config->update_wm_post && pipe_config->base.active)
4652 intel_update_watermarks(&crtc->base);
4653
4654 if (old_pri_state) {
4655 struct intel_plane_state *primary_state =
4656 to_intel_plane_state(primary->state);
4657 struct intel_plane_state *old_primary_state =
4658 to_intel_plane_state(old_pri_state);
4659
4660 intel_fbc_post_update(crtc);
4661
4662 if (primary_state->visible &&
4663 (needs_modeset(&pipe_config->base) ||
4664 !old_primary_state->visible))
4665 intel_post_enable_primary(&crtc->base);
4666 }
4667}
4668
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004669static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004670{
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004671 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004672 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004673 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01004674 struct intel_crtc_state *pipe_config =
4675 to_intel_crtc_state(crtc->base.state);
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004676 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4677 struct drm_plane *primary = crtc->base.primary;
4678 struct drm_plane_state *old_pri_state =
4679 drm_atomic_get_existing_plane_state(old_state, primary);
4680 bool modeset = needs_modeset(&pipe_config->base);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004681
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004682 if (old_pri_state) {
4683 struct intel_plane_state *primary_state =
4684 to_intel_plane_state(primary->state);
4685 struct intel_plane_state *old_primary_state =
4686 to_intel_plane_state(old_pri_state);
4687
Maarten Lankhorstfaf68d92016-06-14 14:24:20 +02004688 intel_fbc_pre_update(crtc, pipe_config, primary_state);
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +01004689
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004690 if (old_primary_state->visible &&
4691 (modeset || !primary_state->visible))
4692 intel_pre_disable_primary(&crtc->base);
4693 }
Ville Syrjälä852eb002015-06-24 22:00:07 +03004694
David Weinehalla4015f92016-05-19 15:50:36 +03004695 if (pipe_config->disable_cxsr && HAS_GMCH_DISPLAY(dev)) {
Ville Syrjälä852eb002015-06-24 22:00:07 +03004696 crtc->wm.cxsr_allowed = false;
Maarten Lankhorst2dfd1782016-02-03 16:53:25 +01004697
Ville Syrjälä2622a082016-03-09 19:07:26 +02004698 /*
4699 * Vblank time updates from the shadow to live plane control register
4700 * are blocked if the memory self-refresh mode is active at that
4701 * moment. So to make sure the plane gets truly disabled, disable
4702 * first the self-refresh mode. The self-refresh enable bit in turn
4703 * will be checked/applied by the HW only at the next frame start
4704 * event which is after the vblank start event, so we need to have a
4705 * wait-for-vblank between disabling the plane and the pipe.
4706 */
4707 if (old_crtc_state->base.active) {
Maarten Lankhorst2dfd1782016-02-03 16:53:25 +01004708 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä2622a082016-03-09 19:07:26 +02004709 dev_priv->wm.vlv.cxsr = false;
4710 intel_wait_for_vblank(dev, crtc->pipe);
4711 }
Ville Syrjälä852eb002015-06-24 22:00:07 +03004712 }
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004713
Matt Ropered4a6a72016-02-23 17:20:13 -08004714 /*
4715 * IVB workaround: must disable low power watermarks for at least
4716 * one frame before enabling scaling. LP watermarks can be re-enabled
4717 * when scaling is disabled.
4718 *
4719 * WaCxSRDisabledForSpriteScaling:ivb
4720 */
4721 if (pipe_config->disable_lp_wm) {
4722 ilk_disable_lp_wm(dev);
4723 intel_wait_for_vblank(dev, crtc->pipe);
4724 }
4725
4726 /*
4727 * If we're doing a modeset, we're done. No need to do any pre-vblank
4728 * watermark programming here.
4729 */
4730 if (needs_modeset(&pipe_config->base))
4731 return;
4732
4733 /*
4734 * For platforms that support atomic watermarks, program the
4735 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
4736 * will be the intermediate values that are safe for both pre- and
4737 * post- vblank; when vblank happens, the 'active' values will be set
4738 * to the final 'target' values and we'll do this again to get the
4739 * optimal watermarks. For gen9+ platforms, the values we program here
4740 * will be the final target values which will get automatically latched
4741 * at vblank time; no further programming will be necessary.
4742 *
4743 * If a platform hasn't been transitioned to atomic watermarks yet,
4744 * we'll continue to update watermarks the old way, if flags tell
4745 * us to.
4746 */
4747 if (dev_priv->display.initial_watermarks != NULL)
4748 dev_priv->display.initial_watermarks(pipe_config);
Ville Syrjäläcaed3612016-03-09 19:07:25 +02004749 else if (pipe_config->update_wm_pre)
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004750 intel_update_watermarks(&crtc->base);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004751}
4752
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004753static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004754{
4755 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004756 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004757 struct drm_plane *p;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004758 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004759
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004760 intel_crtc_dpms_overlay_disable(intel_crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004761
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004762 drm_for_each_plane_mask(p, dev, plane_mask)
4763 to_intel_plane(p)->disable_plane(p, crtc);
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004764
Daniel Vetterf99d7062014-06-19 16:01:59 +02004765 /*
4766 * FIXME: Once we grow proper nuclear flip support out of this we need
4767 * to compute the mask of flip planes precisely. For the time being
4768 * consider this a flip to a NULL plane.
4769 */
4770 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004771}
4772
Jesse Barnesf67a5592011-01-05 10:31:48 -08004773static void ironlake_crtc_enable(struct drm_crtc *crtc)
4774{
4775 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004776 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004777 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004778 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004779 int pipe = intel_crtc->pipe;
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004780 struct intel_crtc_state *pipe_config =
4781 to_intel_crtc_state(crtc->state);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004782
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004783 if (WARN_ON(intel_crtc->active))
Jesse Barnesf67a5592011-01-05 10:31:48 -08004784 return;
4785
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004786 /*
4787 * Sometimes spurious CPU pipe underruns happen during FDI
4788 * training, at least with VGA+HDMI cloning. Suppress them.
4789 *
4790 * On ILK we get an occasional spurious CPU pipe underruns
4791 * between eDP port A enable and vdd enable. Also PCH port
4792 * enable seems to result in the occasional CPU pipe underrun.
4793 *
4794 * Spurious PCH underruns also occur during PCH enabling.
4795 */
4796 if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
4797 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004798 if (intel_crtc->config->has_pch_encoder)
Ville Syrjälä81b088c2015-10-30 19:21:31 +02004799 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
4800
4801 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02004802 intel_prepare_shared_dpll(intel_crtc);
4803
Ville Syrjälä37a56502016-06-22 21:57:04 +03004804 if (intel_crtc_has_dp_encoder(intel_crtc->config))
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05304805 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004806
4807 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02004808 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004809
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004810 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02004811 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004812 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004813 }
4814
4815 ironlake_set_pipeconf(crtc);
4816
Jesse Barnesf67a5592011-01-05 10:31:48 -08004817 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004818
Daniel Vetterf6736a12013-06-05 13:34:30 +02004819 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004820 if (encoder->pre_enable)
4821 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004822
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004823 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004824 /* Note: FDI PLL enabling _must_ be done before we enable the
4825 * cpu pipes, hence this is separate from all the other fdi/pch
4826 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004827 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004828 } else {
4829 assert_fdi_tx_disabled(dev_priv, pipe);
4830 assert_fdi_rx_disabled(dev_priv, pipe);
4831 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004832
Jesse Barnesb074cec2013-04-25 12:55:02 -07004833 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004834
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004835 /*
4836 * On ILK+ LUT must be loaded before the pipe is running but with
4837 * clocks enabled
4838 */
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004839 intel_color_load_luts(&pipe_config->base);
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004840
Imre Deak1d5bf5d2016-02-29 22:10:33 +02004841 if (dev_priv->display.initial_watermarks != NULL)
4842 dev_priv->display.initial_watermarks(intel_crtc->config);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004843 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004844
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004845 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004846 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004847
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004848 assert_vblank_disabled(crtc);
4849 drm_crtc_vblank_on(crtc);
4850
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004851 for_each_encoder_on_crtc(dev, crtc, encoder)
4852 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004853
4854 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004855 cpt_verify_modeset(dev, intel_crtc->pipe);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004856
4857 /* Must wait for vblank to avoid spurious PCH FIFO underruns */
4858 if (intel_crtc->config->has_pch_encoder)
4859 intel_wait_for_vblank(dev, pipe);
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004860 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004861 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004862}
4863
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004864/* IPS only exists on ULT machines and is tied to pipe A. */
4865static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4866{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004867 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004868}
4869
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004870static void haswell_crtc_enable(struct drm_crtc *crtc)
4871{
4872 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004873 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004874 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4875 struct intel_encoder *encoder;
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004876 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
Jani Nikula4d1de972016-03-18 17:05:42 +02004877 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004878 struct intel_crtc_state *pipe_config =
4879 to_intel_crtc_state(crtc->state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004880
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004881 if (WARN_ON(intel_crtc->active))
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004882 return;
4883
Ville Syrjälä81b088c2015-10-30 19:21:31 +02004884 if (intel_crtc->config->has_pch_encoder)
4885 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4886 false);
4887
Imre Deak95a7a2a2016-06-13 16:44:35 +03004888 for_each_encoder_on_crtc(dev, crtc, encoder)
4889 if (encoder->pre_pll_enable)
4890 encoder->pre_pll_enable(encoder);
4891
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02004892 if (intel_crtc->config->shared_dpll)
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004893 intel_enable_shared_dpll(intel_crtc);
4894
Ville Syrjälä37a56502016-06-22 21:57:04 +03004895 if (intel_crtc_has_dp_encoder(intel_crtc->config))
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05304896 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02004897
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03004898 if (!transcoder_is_dsi(cpu_transcoder))
Jani Nikula4d1de972016-03-18 17:05:42 +02004899 intel_set_pipe_timings(intel_crtc);
4900
Jani Nikulabc58be62016-03-18 17:05:39 +02004901 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02004902
Jani Nikula4d1de972016-03-18 17:05:42 +02004903 if (cpu_transcoder != TRANSCODER_EDP &&
4904 !transcoder_is_dsi(cpu_transcoder)) {
4905 I915_WRITE(PIPE_MULT(cpu_transcoder),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004906 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07004907 }
4908
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004909 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02004910 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004911 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02004912 }
4913
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03004914 if (!transcoder_is_dsi(cpu_transcoder))
Jani Nikula4d1de972016-03-18 17:05:42 +02004915 haswell_set_pipeconf(crtc);
4916
Jani Nikula391bf042016-03-18 17:05:40 +02004917 haswell_set_pipemisc(crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02004918
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004919 intel_color_set_csc(&pipe_config->base);
Daniel Vetter229fca92014-04-24 23:55:09 +02004920
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004921 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004922
Daniel Vetter6b698512015-11-28 11:05:39 +01004923 if (intel_crtc->config->has_pch_encoder)
4924 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4925 else
4926 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4927
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304928 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004929 if (encoder->pre_enable)
4930 encoder->pre_enable(encoder);
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304931 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004932
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004933 if (intel_crtc->config->has_pch_encoder)
Imre Deak4fe94672014-06-25 22:01:49 +03004934 dev_priv->display.fdi_link_train(crtc);
Imre Deak4fe94672014-06-25 22:01:49 +03004935
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03004936 if (!transcoder_is_dsi(cpu_transcoder))
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304937 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004938
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07004939 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004940 skylake_pfit_enable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08004941 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07004942 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004943
4944 /*
4945 * On ILK+ LUT must be loaded before the pipe is running but with
4946 * clocks enabled
4947 */
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004948 intel_color_load_luts(&pipe_config->base);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004949
Paulo Zanoni1f544382012-10-24 11:32:00 -02004950 intel_ddi_set_pipe_settings(crtc);
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03004951 if (!transcoder_is_dsi(cpu_transcoder))
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304952 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004953
Imre Deak1d5bf5d2016-02-29 22:10:33 +02004954 if (dev_priv->display.initial_watermarks != NULL)
4955 dev_priv->display.initial_watermarks(pipe_config);
4956 else
4957 intel_update_watermarks(crtc);
Jani Nikula4d1de972016-03-18 17:05:42 +02004958
4959 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03004960 if (!transcoder_is_dsi(cpu_transcoder))
Jani Nikula4d1de972016-03-18 17:05:42 +02004961 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004962
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004963 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004964 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004965
Jani Nikulaa65347b2015-11-27 12:21:46 +02004966 if (intel_crtc->config->dp_encoder_is_mst)
Dave Airlie0e32b392014-05-02 14:02:48 +10004967 intel_ddi_set_vc_payload_alloc(crtc, true);
4968
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004969 assert_vblank_disabled(crtc);
4970 drm_crtc_vblank_on(crtc);
4971
Jani Nikula8807e552013-08-30 19:40:32 +03004972 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004973 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004974 intel_opregion_notify_encoder(encoder, true);
4975 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004976
Daniel Vetter6b698512015-11-28 11:05:39 +01004977 if (intel_crtc->config->has_pch_encoder) {
4978 intel_wait_for_vblank(dev, pipe);
4979 intel_wait_for_vblank(dev, pipe);
4980 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004981 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4982 true);
Daniel Vetter6b698512015-11-28 11:05:39 +01004983 }
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004984
Paulo Zanonie4916942013-09-20 16:21:19 -03004985 /* If we change the relative order between pipe/planes enabling, we need
4986 * to change the workaround. */
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004987 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
4988 if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
4989 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4990 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4991 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004992}
4993
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02004994static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004995{
4996 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004997 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004998 int pipe = crtc->pipe;
4999
5000 /* To avoid upsetting the power well on haswell only disable the pfit if
5001 * it's in use. The hw state code will make sure we get this right. */
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005002 if (force || crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005003 I915_WRITE(PF_CTL(pipe), 0);
5004 I915_WRITE(PF_WIN_POS(pipe), 0);
5005 I915_WRITE(PF_WIN_SZ(pipe), 0);
5006 }
5007}
5008
Jesse Barnes6be4a602010-09-10 10:26:01 -07005009static void ironlake_crtc_disable(struct drm_crtc *crtc)
5010{
5011 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005012 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005013 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005014 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005015 int pipe = intel_crtc->pipe;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005016
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005017 /*
5018 * Sometimes spurious CPU pipe underruns happen when the
5019 * pipe is already disabled, but FDI RX/TX is still enabled.
5020 * Happens at least with VGA+HDMI cloning. Suppress them.
5021 */
5022 if (intel_crtc->config->has_pch_encoder) {
5023 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02005024 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005025 }
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02005026
Daniel Vetterea9d7582012-07-10 10:42:52 +02005027 for_each_encoder_on_crtc(dev, crtc, encoder)
5028 encoder->disable(encoder);
5029
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005030 drm_crtc_vblank_off(crtc);
5031 assert_vblank_disabled(crtc);
5032
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005033 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005034
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005035 ironlake_pfit_disable(intel_crtc, false);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005036
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005037 if (intel_crtc->config->has_pch_encoder)
Ville Syrjälä5a74f702015-05-05 17:17:38 +03005038 ironlake_fdi_disable(crtc);
5039
Daniel Vetterbf49ec82012-09-06 22:15:40 +02005040 for_each_encoder_on_crtc(dev, crtc, encoder)
5041 if (encoder->post_disable)
5042 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005043
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005044 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02005045 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005046
Daniel Vetterd925c592013-06-05 13:34:04 +02005047 if (HAS_PCH_CPT(dev)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02005048 i915_reg_t reg;
5049 u32 temp;
5050
Daniel Vetterd925c592013-06-05 13:34:04 +02005051 /* disable TRANS_DP_CTL */
5052 reg = TRANS_DP_CTL(pipe);
5053 temp = I915_READ(reg);
5054 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5055 TRANS_DP_PORT_SEL_MASK);
5056 temp |= TRANS_DP_PORT_SEL_NONE;
5057 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005058
Daniel Vetterd925c592013-06-05 13:34:04 +02005059 /* disable DPLL_SEL */
5060 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02005061 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02005062 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005063 }
Daniel Vetterd925c592013-06-05 13:34:04 +02005064
Daniel Vetterd925c592013-06-05 13:34:04 +02005065 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005066 }
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005067
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005068 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005069 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005070}
5071
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005072static void haswell_crtc_disable(struct drm_crtc *crtc)
5073{
5074 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005075 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005076 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5077 struct intel_encoder *encoder;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005078 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005079
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005080 if (intel_crtc->config->has_pch_encoder)
5081 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5082 false);
5083
Jani Nikula8807e552013-08-30 19:40:32 +03005084 for_each_encoder_on_crtc(dev, crtc, encoder) {
5085 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005086 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005087 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005088
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005089 drm_crtc_vblank_off(crtc);
5090 assert_vblank_disabled(crtc);
5091
Jani Nikula4d1de972016-03-18 17:05:42 +02005092 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005093 if (!transcoder_is_dsi(cpu_transcoder))
Jani Nikula4d1de972016-03-18 17:05:42 +02005094 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005095
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005096 if (intel_crtc->config->dp_encoder_is_mst)
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03005097 intel_ddi_set_vc_payload_alloc(crtc, false);
5098
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005099 if (!transcoder_is_dsi(cpu_transcoder))
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305100 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005101
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07005102 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02005103 skylake_scaler_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005104 else
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005105 ironlake_pfit_disable(intel_crtc, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005106
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005107 if (!transcoder_is_dsi(cpu_transcoder))
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305108 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005109
Imre Deak97b040a2014-06-25 22:01:50 +03005110 for_each_encoder_on_crtc(dev, crtc, encoder)
5111 if (encoder->post_disable)
5112 encoder->post_disable(encoder);
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005113
Ville Syrjälä92966a32015-12-08 16:05:48 +02005114 if (intel_crtc->config->has_pch_encoder) {
5115 lpt_disable_pch_transcoder(dev_priv);
Ville Syrjälä503a74e2015-12-04 22:22:14 +02005116 lpt_disable_iclkip(dev_priv);
Ville Syrjälä92966a32015-12-08 16:05:48 +02005117 intel_ddi_fdi_disable(crtc);
5118
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005119 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5120 true);
Ville Syrjälä92966a32015-12-08 16:05:48 +02005121 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005122}
5123
Jesse Barnes2dd24552013-04-25 12:55:01 -07005124static void i9xx_pfit_enable(struct intel_crtc *crtc)
5125{
5126 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005127 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005128 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07005129
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02005130 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005131 return;
5132
Daniel Vetterc0b03412013-05-28 12:05:54 +02005133 /*
5134 * The panel fitter should only be adjusted whilst the pipe is disabled,
5135 * according to register description and PRM.
5136 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005137 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5138 assert_pipe_disabled(dev_priv, crtc->pipe);
5139
Jesse Barnesb074cec2013-04-25 12:55:02 -07005140 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5141 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005142
5143 /* Border color in case we don't scale up to the full screen. Black by
5144 * default, change to something else for debugging. */
5145 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005146}
5147
Dave Airlied05410f2014-06-05 13:22:59 +10005148static enum intel_display_power_domain port_to_power_domain(enum port port)
5149{
5150 switch (port) {
5151 case PORT_A:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005152 return POWER_DOMAIN_PORT_DDI_A_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005153 case PORT_B:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005154 return POWER_DOMAIN_PORT_DDI_B_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005155 case PORT_C:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005156 return POWER_DOMAIN_PORT_DDI_C_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005157 case PORT_D:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005158 return POWER_DOMAIN_PORT_DDI_D_LANES;
Xiong Zhangd8e19f92015-08-13 18:00:12 +08005159 case PORT_E:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005160 return POWER_DOMAIN_PORT_DDI_E_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005161 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005162 MISSING_CASE(port);
Dave Airlied05410f2014-06-05 13:22:59 +10005163 return POWER_DOMAIN_PORT_OTHER;
5164 }
5165}
5166
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005167static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
5168{
5169 switch (port) {
5170 case PORT_A:
5171 return POWER_DOMAIN_AUX_A;
5172 case PORT_B:
5173 return POWER_DOMAIN_AUX_B;
5174 case PORT_C:
5175 return POWER_DOMAIN_AUX_C;
5176 case PORT_D:
5177 return POWER_DOMAIN_AUX_D;
5178 case PORT_E:
5179 /* FIXME: Check VBT for actual wiring of PORT E */
5180 return POWER_DOMAIN_AUX_D;
5181 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005182 MISSING_CASE(port);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005183 return POWER_DOMAIN_AUX_A;
5184 }
5185}
5186
Imre Deak319be8a2014-03-04 19:22:57 +02005187enum intel_display_power_domain
5188intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02005189{
Imre Deak319be8a2014-03-04 19:22:57 +02005190 struct drm_device *dev = intel_encoder->base.dev;
5191 struct intel_digital_port *intel_dig_port;
5192
5193 switch (intel_encoder->type) {
5194 case INTEL_OUTPUT_UNKNOWN:
5195 /* Only DDI platforms should ever use this output type */
5196 WARN_ON_ONCE(!HAS_DDI(dev));
Ville Syrjäläcca05022016-06-22 21:57:06 +03005197 case INTEL_OUTPUT_DP:
Imre Deak319be8a2014-03-04 19:22:57 +02005198 case INTEL_OUTPUT_HDMI:
5199 case INTEL_OUTPUT_EDP:
5200 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10005201 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10005202 case INTEL_OUTPUT_DP_MST:
5203 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5204 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02005205 case INTEL_OUTPUT_ANALOG:
5206 return POWER_DOMAIN_PORT_CRT;
5207 case INTEL_OUTPUT_DSI:
5208 return POWER_DOMAIN_PORT_DSI;
5209 default:
5210 return POWER_DOMAIN_PORT_OTHER;
5211 }
5212}
5213
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005214enum intel_display_power_domain
5215intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
5216{
5217 struct drm_device *dev = intel_encoder->base.dev;
5218 struct intel_digital_port *intel_dig_port;
5219
5220 switch (intel_encoder->type) {
5221 case INTEL_OUTPUT_UNKNOWN:
Imre Deak651174a2015-11-18 15:57:24 +02005222 case INTEL_OUTPUT_HDMI:
5223 /*
5224 * Only DDI platforms should ever use these output types.
5225 * We can get here after the HDMI detect code has already set
5226 * the type of the shared encoder. Since we can't be sure
5227 * what's the status of the given connectors, play safe and
5228 * run the DP detection too.
5229 */
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005230 WARN_ON_ONCE(!HAS_DDI(dev));
Ville Syrjäläcca05022016-06-22 21:57:06 +03005231 case INTEL_OUTPUT_DP:
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005232 case INTEL_OUTPUT_EDP:
5233 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
5234 return port_to_aux_power_domain(intel_dig_port->port);
5235 case INTEL_OUTPUT_DP_MST:
5236 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5237 return port_to_aux_power_domain(intel_dig_port->port);
5238 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005239 MISSING_CASE(intel_encoder->type);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005240 return POWER_DOMAIN_AUX_A;
5241 }
5242}
5243
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005244static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
5245 struct intel_crtc_state *crtc_state)
Imre Deak319be8a2014-03-04 19:22:57 +02005246{
5247 struct drm_device *dev = crtc->dev;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005248 struct drm_encoder *encoder;
Imre Deak319be8a2014-03-04 19:22:57 +02005249 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5250 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02005251 unsigned long mask;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005252 enum transcoder transcoder = crtc_state->cpu_transcoder;
Imre Deak77d22dc2014-03-05 16:20:52 +02005253
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005254 if (!crtc_state->base.active)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005255 return 0;
5256
Imre Deak77d22dc2014-03-05 16:20:52 +02005257 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5258 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005259 if (crtc_state->pch_pfit.enabled ||
5260 crtc_state->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02005261 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5262
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005263 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
5264 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5265
Imre Deak319be8a2014-03-04 19:22:57 +02005266 mask |= BIT(intel_display_port_power_domain(intel_encoder));
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005267 }
Imre Deak319be8a2014-03-04 19:22:57 +02005268
Maarten Lankhorst15e7ec22016-03-14 09:27:54 +01005269 if (crtc_state->shared_dpll)
5270 mask |= BIT(POWER_DOMAIN_PLLS);
5271
Imre Deak77d22dc2014-03-05 16:20:52 +02005272 return mask;
5273}
5274
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005275static unsigned long
5276modeset_get_crtc_power_domains(struct drm_crtc *crtc,
5277 struct intel_crtc_state *crtc_state)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005278{
Chris Wilsonfac5e232016-07-04 11:34:36 +01005279 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005280 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5281 enum intel_display_power_domain domain;
Daniel Vetter5a21b662016-05-24 17:13:53 +02005282 unsigned long domains, new_domains, old_domains;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005283
5284 old_domains = intel_crtc->enabled_power_domains;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005285 intel_crtc->enabled_power_domains = new_domains =
5286 get_crtc_power_domains(crtc, crtc_state);
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005287
Daniel Vetter5a21b662016-05-24 17:13:53 +02005288 domains = new_domains & ~old_domains;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005289
5290 for_each_power_domain(domain, domains)
5291 intel_display_power_get(dev_priv, domain);
5292
Daniel Vetter5a21b662016-05-24 17:13:53 +02005293 return old_domains & ~new_domains;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005294}
5295
5296static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
5297 unsigned long domains)
5298{
5299 enum intel_display_power_domain domain;
5300
5301 for_each_power_domain(domain, domains)
5302 intel_display_power_put(dev_priv, domain);
5303}
5304
Mika Kaholaadafdc62015-08-18 14:36:59 +03005305static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
5306{
5307 int max_cdclk_freq = dev_priv->max_cdclk_freq;
5308
5309 if (INTEL_INFO(dev_priv)->gen >= 9 ||
5310 IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
5311 return max_cdclk_freq;
5312 else if (IS_CHERRYVIEW(dev_priv))
5313 return max_cdclk_freq*95/100;
5314 else if (INTEL_INFO(dev_priv)->gen < 4)
5315 return 2*max_cdclk_freq*90/100;
5316 else
5317 return max_cdclk_freq*90/100;
5318}
5319
Ville Syrjäläb2045352016-05-13 23:41:27 +03005320static int skl_calc_cdclk(int max_pixclk, int vco);
5321
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005322static void intel_update_max_cdclk(struct drm_device *dev)
5323{
Chris Wilsonfac5e232016-07-04 11:34:36 +01005324 struct drm_i915_private *dev_priv = to_i915(dev);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005325
Rodrigo Vivief11bdb2015-10-28 04:16:45 -07005326 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005327 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
Ville Syrjäläb2045352016-05-13 23:41:27 +03005328 int max_cdclk, vco;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005329
Ville Syrjäläb2045352016-05-13 23:41:27 +03005330 vco = dev_priv->skl_preferred_vco_freq;
Ville Syrjälä63911d72016-05-13 23:41:32 +03005331 WARN_ON(vco != 8100000 && vco != 8640000);
Ville Syrjäläb2045352016-05-13 23:41:27 +03005332
5333 /*
5334 * Use the lower (vco 8640) cdclk values as a
5335 * first guess. skl_calc_cdclk() will correct it
5336 * if the preferred vco is 8100 instead.
5337 */
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005338 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005339 max_cdclk = 617143;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005340 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
Ville Syrjäläb2045352016-05-13 23:41:27 +03005341 max_cdclk = 540000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005342 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
Ville Syrjäläb2045352016-05-13 23:41:27 +03005343 max_cdclk = 432000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005344 else
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005345 max_cdclk = 308571;
Ville Syrjäläb2045352016-05-13 23:41:27 +03005346
5347 dev_priv->max_cdclk_freq = skl_calc_cdclk(max_cdclk, vco);
Matt Roper281c1142016-04-05 14:37:19 -07005348 } else if (IS_BROXTON(dev)) {
5349 dev_priv->max_cdclk_freq = 624000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005350 } else if (IS_BROADWELL(dev)) {
5351 /*
5352 * FIXME with extra cooling we can allow
5353 * 540 MHz for ULX and 675 Mhz for ULT.
5354 * How can we know if extra cooling is
5355 * available? PCI ID, VTB, something else?
5356 */
5357 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5358 dev_priv->max_cdclk_freq = 450000;
5359 else if (IS_BDW_ULX(dev))
5360 dev_priv->max_cdclk_freq = 450000;
5361 else if (IS_BDW_ULT(dev))
5362 dev_priv->max_cdclk_freq = 540000;
5363 else
5364 dev_priv->max_cdclk_freq = 675000;
Mika Kahola0904dea2015-06-12 10:11:32 +03005365 } else if (IS_CHERRYVIEW(dev)) {
5366 dev_priv->max_cdclk_freq = 320000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005367 } else if (IS_VALLEYVIEW(dev)) {
5368 dev_priv->max_cdclk_freq = 400000;
5369 } else {
5370 /* otherwise assume cdclk is fixed */
5371 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5372 }
5373
Mika Kaholaadafdc62015-08-18 14:36:59 +03005374 dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
5375
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005376 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5377 dev_priv->max_cdclk_freq);
Mika Kaholaadafdc62015-08-18 14:36:59 +03005378
5379 DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
5380 dev_priv->max_dotclk_freq);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005381}
5382
5383static void intel_update_cdclk(struct drm_device *dev)
5384{
Chris Wilsonfac5e232016-07-04 11:34:36 +01005385 struct drm_i915_private *dev_priv = to_i915(dev);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005386
5387 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
Ville Syrjälä2f2a1212016-05-13 23:41:25 +03005388
Ville Syrjälä83d7c812016-05-13 23:41:35 +03005389 if (INTEL_GEN(dev_priv) >= 9)
Ville Syrjälä709e05c2016-05-13 23:41:33 +03005390 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz, VCO: %d kHz, ref: %d kHz\n",
5391 dev_priv->cdclk_freq, dev_priv->cdclk_pll.vco,
5392 dev_priv->cdclk_pll.ref);
Ville Syrjälä2f2a1212016-05-13 23:41:25 +03005393 else
5394 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5395 dev_priv->cdclk_freq);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005396
5397 /*
Ville Syrjäläb5d99ff2016-04-26 19:46:34 +03005398 * 9:0 CMBUS [sic] CDCLK frequency (cdfreq):
5399 * Programmng [sic] note: bit[9:2] should be programmed to the number
5400 * of cdclk that generates 4MHz reference clock freq which is used to
5401 * generate GMBus clock. This will vary with the cdclk freq.
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005402 */
Ville Syrjäläb5d99ff2016-04-26 19:46:34 +03005403 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005404 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005405}
5406
Ville Syrjälä92891e42016-05-11 22:44:45 +03005407/* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5408static int skl_cdclk_decimal(int cdclk)
5409{
5410 return DIV_ROUND_CLOSEST(cdclk - 1000, 500);
5411}
5412
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005413static int bxt_de_pll_vco(struct drm_i915_private *dev_priv, int cdclk)
5414{
5415 int ratio;
5416
5417 if (cdclk == dev_priv->cdclk_pll.ref)
5418 return 0;
5419
5420 switch (cdclk) {
5421 default:
5422 MISSING_CASE(cdclk);
5423 case 144000:
5424 case 288000:
5425 case 384000:
5426 case 576000:
5427 ratio = 60;
5428 break;
5429 case 624000:
5430 ratio = 65;
5431 break;
5432 }
5433
5434 return dev_priv->cdclk_pll.ref * ratio;
5435}
5436
Ville Syrjälä2b730012016-05-13 23:41:34 +03005437static void bxt_de_pll_disable(struct drm_i915_private *dev_priv)
5438{
5439 I915_WRITE(BXT_DE_PLL_ENABLE, 0);
5440
5441 /* Timeout 200us */
Chris Wilson95cac282016-06-30 15:33:03 +01005442 if (intel_wait_for_register(dev_priv,
5443 BXT_DE_PLL_ENABLE, BXT_DE_PLL_LOCK, 0,
5444 1))
Ville Syrjälä2b730012016-05-13 23:41:34 +03005445 DRM_ERROR("timeout waiting for DE PLL unlock\n");
Ville Syrjälä83d7c812016-05-13 23:41:35 +03005446
5447 dev_priv->cdclk_pll.vco = 0;
Ville Syrjälä2b730012016-05-13 23:41:34 +03005448}
5449
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005450static void bxt_de_pll_enable(struct drm_i915_private *dev_priv, int vco)
Ville Syrjälä2b730012016-05-13 23:41:34 +03005451{
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005452 int ratio = DIV_ROUND_CLOSEST(vco, dev_priv->cdclk_pll.ref);
Ville Syrjälä2b730012016-05-13 23:41:34 +03005453 u32 val;
5454
5455 val = I915_READ(BXT_DE_PLL_CTL);
5456 val &= ~BXT_DE_PLL_RATIO_MASK;
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005457 val |= BXT_DE_PLL_RATIO(ratio);
Ville Syrjälä2b730012016-05-13 23:41:34 +03005458 I915_WRITE(BXT_DE_PLL_CTL, val);
5459
5460 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5461
5462 /* Timeout 200us */
Chris Wilsone084e1b2016-06-30 15:33:04 +01005463 if (intel_wait_for_register(dev_priv,
5464 BXT_DE_PLL_ENABLE,
5465 BXT_DE_PLL_LOCK,
5466 BXT_DE_PLL_LOCK,
5467 1))
Ville Syrjälä2b730012016-05-13 23:41:34 +03005468 DRM_ERROR("timeout waiting for DE PLL lock\n");
Ville Syrjälä83d7c812016-05-13 23:41:35 +03005469
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005470 dev_priv->cdclk_pll.vco = vco;
Ville Syrjälä2b730012016-05-13 23:41:34 +03005471}
5472
Imre Deak324513c2016-06-13 16:44:36 +03005473static void bxt_set_cdclk(struct drm_i915_private *dev_priv, int cdclk)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305474{
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005475 u32 val, divider;
5476 int vco, ret;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305477
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005478 vco = bxt_de_pll_vco(dev_priv, cdclk);
5479
5480 DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
5481
5482 /* cdclk = vco / 2 / div{1,1.5,2,4} */
5483 switch (DIV_ROUND_CLOSEST(vco, cdclk)) {
5484 case 8:
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305485 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305486 break;
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005487 case 4:
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305488 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305489 break;
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005490 case 3:
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305491 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305492 break;
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005493 case 2:
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305494 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305495 break;
5496 default:
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005497 WARN_ON(cdclk != dev_priv->cdclk_pll.ref);
5498 WARN_ON(vco != 0);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305499
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005500 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5501 break;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305502 }
5503
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305504 /* Inform power controller of upcoming frequency change */
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005505 mutex_lock(&dev_priv->rps.hw_lock);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305506 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5507 0x80000000);
5508 mutex_unlock(&dev_priv->rps.hw_lock);
5509
5510 if (ret) {
5511 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005512 ret, cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305513 return;
5514 }
5515
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005516 if (dev_priv->cdclk_pll.vco != 0 &&
5517 dev_priv->cdclk_pll.vco != vco)
Ville Syrjälä2b730012016-05-13 23:41:34 +03005518 bxt_de_pll_disable(dev_priv);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305519
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005520 if (dev_priv->cdclk_pll.vco != vco)
5521 bxt_de_pll_enable(dev_priv, vco);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305522
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005523 val = divider | skl_cdclk_decimal(cdclk);
5524 /*
5525 * FIXME if only the cd2x divider needs changing, it could be done
5526 * without shutting off the pipe (if only one pipe is active).
5527 */
5528 val |= BXT_CDCLK_CD2X_PIPE_NONE;
5529 /*
5530 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5531 * enable otherwise.
5532 */
5533 if (cdclk >= 500000)
5534 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5535 I915_WRITE(CDCLK_CTL, val);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305536
5537 mutex_lock(&dev_priv->rps.hw_lock);
5538 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005539 DIV_ROUND_UP(cdclk, 25000));
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305540 mutex_unlock(&dev_priv->rps.hw_lock);
5541
5542 if (ret) {
5543 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005544 ret, cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305545 return;
5546 }
5547
Chris Wilson91c8a322016-07-05 10:40:23 +01005548 intel_update_cdclk(&dev_priv->drm);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305549}
5550
Imre Deakd66a2192016-05-24 15:38:33 +03005551static void bxt_sanitize_cdclk(struct drm_i915_private *dev_priv)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305552{
Imre Deakd66a2192016-05-24 15:38:33 +03005553 u32 cdctl, expected;
5554
Chris Wilson91c8a322016-07-05 10:40:23 +01005555 intel_update_cdclk(&dev_priv->drm);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305556
Imre Deakd66a2192016-05-24 15:38:33 +03005557 if (dev_priv->cdclk_pll.vco == 0 ||
5558 dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
5559 goto sanitize;
5560
5561 /* DPLL okay; verify the cdclock
5562 *
5563 * Some BIOS versions leave an incorrect decimal frequency value and
5564 * set reserved MBZ bits in CDCLK_CTL at least during exiting from S4,
5565 * so sanitize this register.
5566 */
5567 cdctl = I915_READ(CDCLK_CTL);
5568 /*
5569 * Let's ignore the pipe field, since BIOS could have configured the
5570 * dividers both synching to an active pipe, or asynchronously
5571 * (PIPE_NONE).
5572 */
5573 cdctl &= ~BXT_CDCLK_CD2X_PIPE_NONE;
5574
5575 expected = (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) |
5576 skl_cdclk_decimal(dev_priv->cdclk_freq);
5577 /*
5578 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5579 * enable otherwise.
5580 */
5581 if (dev_priv->cdclk_freq >= 500000)
5582 expected |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5583
5584 if (cdctl == expected)
5585 /* All well; nothing to sanitize */
5586 return;
5587
5588sanitize:
5589 DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
5590
5591 /* force cdclk programming */
5592 dev_priv->cdclk_freq = 0;
5593
5594 /* force full PLL disable + enable */
5595 dev_priv->cdclk_pll.vco = -1;
5596}
5597
Imre Deak324513c2016-06-13 16:44:36 +03005598void bxt_init_cdclk(struct drm_i915_private *dev_priv)
Imre Deakd66a2192016-05-24 15:38:33 +03005599{
5600 bxt_sanitize_cdclk(dev_priv);
5601
5602 if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0)
Ville Syrjälä089c6fd2016-05-13 23:41:36 +03005603 return;
Imre Deakc2e001e2016-04-01 16:02:43 +03005604
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305605 /*
5606 * FIXME:
5607 * - The initial CDCLK needs to be read from VBT.
5608 * Need to make this change after VBT has changes for BXT.
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305609 */
Imre Deak324513c2016-06-13 16:44:36 +03005610 bxt_set_cdclk(dev_priv, bxt_calc_cdclk(0));
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305611}
5612
Imre Deak324513c2016-06-13 16:44:36 +03005613void bxt_uninit_cdclk(struct drm_i915_private *dev_priv)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305614{
Imre Deak324513c2016-06-13 16:44:36 +03005615 bxt_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305616}
5617
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005618static int skl_calc_cdclk(int max_pixclk, int vco)
5619{
Ville Syrjälä63911d72016-05-13 23:41:32 +03005620 if (vco == 8640000) {
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005621 if (max_pixclk > 540000)
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005622 return 617143;
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005623 else if (max_pixclk > 432000)
5624 return 540000;
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005625 else if (max_pixclk > 308571)
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005626 return 432000;
5627 else
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005628 return 308571;
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005629 } else {
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005630 if (max_pixclk > 540000)
5631 return 675000;
5632 else if (max_pixclk > 450000)
5633 return 540000;
5634 else if (max_pixclk > 337500)
5635 return 450000;
5636 else
5637 return 337500;
5638 }
5639}
5640
Ville Syrjäläea617912016-05-13 23:41:24 +03005641static void
5642skl_dpll0_update(struct drm_i915_private *dev_priv)
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005643{
Ville Syrjäläea617912016-05-13 23:41:24 +03005644 u32 val;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005645
Ville Syrjälä709e05c2016-05-13 23:41:33 +03005646 dev_priv->cdclk_pll.ref = 24000;
Imre Deak1c3f7702016-05-24 15:38:32 +03005647 dev_priv->cdclk_pll.vco = 0;
Ville Syrjälä709e05c2016-05-13 23:41:33 +03005648
Ville Syrjäläea617912016-05-13 23:41:24 +03005649 val = I915_READ(LCPLL1_CTL);
Imre Deak1c3f7702016-05-24 15:38:32 +03005650 if ((val & LCPLL_PLL_ENABLE) == 0)
Ville Syrjäläea617912016-05-13 23:41:24 +03005651 return;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005652
Imre Deak1c3f7702016-05-24 15:38:32 +03005653 if (WARN_ON((val & LCPLL_PLL_LOCK) == 0))
5654 return;
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005655
Ville Syrjäläea617912016-05-13 23:41:24 +03005656 val = I915_READ(DPLL_CTRL1);
5657
Imre Deak1c3f7702016-05-24 15:38:32 +03005658 if (WARN_ON((val & (DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) |
5659 DPLL_CTRL1_SSC(SKL_DPLL0) |
5660 DPLL_CTRL1_OVERRIDE(SKL_DPLL0))) !=
5661 DPLL_CTRL1_OVERRIDE(SKL_DPLL0)))
5662 return;
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005663
Ville Syrjäläea617912016-05-13 23:41:24 +03005664 switch (val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) {
5665 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810, SKL_DPLL0):
5666 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1350, SKL_DPLL0):
5667 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1620, SKL_DPLL0):
5668 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2700, SKL_DPLL0):
Ville Syrjälä63911d72016-05-13 23:41:32 +03005669 dev_priv->cdclk_pll.vco = 8100000;
Ville Syrjäläea617912016-05-13 23:41:24 +03005670 break;
5671 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080, SKL_DPLL0):
5672 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2160, SKL_DPLL0):
Ville Syrjälä63911d72016-05-13 23:41:32 +03005673 dev_priv->cdclk_pll.vco = 8640000;
Ville Syrjäläea617912016-05-13 23:41:24 +03005674 break;
5675 default:
5676 MISSING_CASE(val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
Ville Syrjäläea617912016-05-13 23:41:24 +03005677 break;
5678 }
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005679}
5680
Ville Syrjäläb2045352016-05-13 23:41:27 +03005681void skl_set_preferred_cdclk_vco(struct drm_i915_private *dev_priv, int vco)
5682{
5683 bool changed = dev_priv->skl_preferred_vco_freq != vco;
5684
5685 dev_priv->skl_preferred_vco_freq = vco;
5686
5687 if (changed)
Chris Wilson91c8a322016-07-05 10:40:23 +01005688 intel_update_max_cdclk(&dev_priv->drm);
Ville Syrjäläb2045352016-05-13 23:41:27 +03005689}
5690
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005691static void
Ville Syrjälä3861fc62016-05-11 22:44:50 +03005692skl_dpll0_enable(struct drm_i915_private *dev_priv, int vco)
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005693{
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005694 int min_cdclk = skl_calc_cdclk(0, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005695 u32 val;
5696
Ville Syrjälä63911d72016-05-13 23:41:32 +03005697 WARN_ON(vco != 8100000 && vco != 8640000);
Ville Syrjäläb2045352016-05-13 23:41:27 +03005698
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005699 /* select the minimum CDCLK before enabling DPLL 0 */
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005700 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_cdclk);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005701 I915_WRITE(CDCLK_CTL, val);
5702 POSTING_READ(CDCLK_CTL);
5703
5704 /*
5705 * We always enable DPLL0 with the lowest link rate possible, but still
5706 * taking into account the VCO required to operate the eDP panel at the
5707 * desired frequency. The usual DP link rates operate with a VCO of
5708 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5709 * The modeset code is responsible for the selection of the exact link
5710 * rate later on, with the constraint of choosing a frequency that
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005711 * works with vco.
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005712 */
5713 val = I915_READ(DPLL_CTRL1);
5714
5715 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5716 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5717 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
Ville Syrjälä63911d72016-05-13 23:41:32 +03005718 if (vco == 8640000)
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005719 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5720 SKL_DPLL0);
5721 else
5722 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5723 SKL_DPLL0);
5724
5725 I915_WRITE(DPLL_CTRL1, val);
5726 POSTING_READ(DPLL_CTRL1);
5727
5728 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5729
Chris Wilsone24ca052016-06-30 15:33:05 +01005730 if (intel_wait_for_register(dev_priv,
5731 LCPLL1_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
5732 5))
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005733 DRM_ERROR("DPLL0 not locked\n");
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005734
Ville Syrjälä63911d72016-05-13 23:41:32 +03005735 dev_priv->cdclk_pll.vco = vco;
Ville Syrjäläb2045352016-05-13 23:41:27 +03005736
5737 /* We'll want to keep using the current vco from now on. */
5738 skl_set_preferred_cdclk_vco(dev_priv, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005739}
5740
Ville Syrjälä430e05d2016-05-11 22:44:47 +03005741static void
5742skl_dpll0_disable(struct drm_i915_private *dev_priv)
5743{
5744 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
Chris Wilson8ad32a02016-06-30 15:33:06 +01005745 if (intel_wait_for_register(dev_priv,
5746 LCPLL1_CTL, LCPLL_PLL_LOCK, 0,
5747 1))
Ville Syrjälä430e05d2016-05-11 22:44:47 +03005748 DRM_ERROR("Couldn't disable DPLL0\n");
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005749
Ville Syrjälä63911d72016-05-13 23:41:32 +03005750 dev_priv->cdclk_pll.vco = 0;
Ville Syrjälä430e05d2016-05-11 22:44:47 +03005751}
5752
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005753static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5754{
5755 int ret;
5756 u32 val;
5757
5758 /* inform PCU we want to change CDCLK */
5759 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5760 mutex_lock(&dev_priv->rps.hw_lock);
5761 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5762 mutex_unlock(&dev_priv->rps.hw_lock);
5763
5764 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5765}
5766
5767static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5768{
Ville Syrjälä3b2c1712016-07-13 16:32:03 +03005769 return _wait_for(skl_cdclk_pcu_ready(dev_priv), 3000, 10) == 0;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005770}
5771
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005772static void skl_set_cdclk(struct drm_i915_private *dev_priv, int cdclk, int vco)
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005773{
Chris Wilson91c8a322016-07-05 10:40:23 +01005774 struct drm_device *dev = &dev_priv->drm;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005775 u32 freq_select, pcu_ack;
5776
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005777 WARN_ON((cdclk == 24000) != (vco == 0));
5778
Ville Syrjälä63911d72016-05-13 23:41:32 +03005779 DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005780
5781 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5782 DRM_ERROR("failed to inform PCU about cdclk change\n");
5783 return;
5784 }
5785
5786 /* set CDCLK_CTL */
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005787 switch (cdclk) {
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005788 case 450000:
5789 case 432000:
5790 freq_select = CDCLK_FREQ_450_432;
5791 pcu_ack = 1;
5792 break;
5793 case 540000:
5794 freq_select = CDCLK_FREQ_540;
5795 pcu_ack = 2;
5796 break;
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005797 case 308571:
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005798 case 337500:
5799 default:
5800 freq_select = CDCLK_FREQ_337_308;
5801 pcu_ack = 0;
5802 break;
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005803 case 617143:
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005804 case 675000:
5805 freq_select = CDCLK_FREQ_675_617;
5806 pcu_ack = 3;
5807 break;
5808 }
5809
Ville Syrjälä63911d72016-05-13 23:41:32 +03005810 if (dev_priv->cdclk_pll.vco != 0 &&
5811 dev_priv->cdclk_pll.vco != vco)
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005812 skl_dpll0_disable(dev_priv);
5813
Ville Syrjälä63911d72016-05-13 23:41:32 +03005814 if (dev_priv->cdclk_pll.vco != vco)
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005815 skl_dpll0_enable(dev_priv, vco);
5816
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005817 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(cdclk));
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005818 POSTING_READ(CDCLK_CTL);
5819
5820 /* inform PCU of the change */
5821 mutex_lock(&dev_priv->rps.hw_lock);
5822 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5823 mutex_unlock(&dev_priv->rps.hw_lock);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005824
5825 intel_update_cdclk(dev);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005826}
5827
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005828static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv);
5829
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005830void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5831{
Ville Syrjälä709e05c2016-05-13 23:41:33 +03005832 skl_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref, 0);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005833}
5834
5835void skl_init_cdclk(struct drm_i915_private *dev_priv)
5836{
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005837 int cdclk, vco;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005838
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005839 skl_sanitize_cdclk(dev_priv);
5840
Ville Syrjälä63911d72016-05-13 23:41:32 +03005841 if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0) {
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005842 /*
5843 * Use the current vco as our initial
5844 * guess as to what the preferred vco is.
5845 */
5846 if (dev_priv->skl_preferred_vco_freq == 0)
5847 skl_set_preferred_cdclk_vco(dev_priv,
Ville Syrjälä63911d72016-05-13 23:41:32 +03005848 dev_priv->cdclk_pll.vco);
Ville Syrjälä70c2c182016-05-13 23:41:30 +03005849 return;
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005850 }
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005851
Ville Syrjälä70c2c182016-05-13 23:41:30 +03005852 vco = dev_priv->skl_preferred_vco_freq;
5853 if (vco == 0)
Ville Syrjälä63911d72016-05-13 23:41:32 +03005854 vco = 8100000;
Ville Syrjälä70c2c182016-05-13 23:41:30 +03005855 cdclk = skl_calc_cdclk(0, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005856
Ville Syrjälä70c2c182016-05-13 23:41:30 +03005857 skl_set_cdclk(dev_priv, cdclk, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005858}
5859
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005860static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305861{
Ville Syrjälä09492492016-05-13 23:41:28 +03005862 uint32_t cdctl, expected;
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305863
Shobhit Kumarf1b391a2015-11-05 18:05:32 +05305864 /*
5865 * check if the pre-os intialized the display
5866 * There is SWF18 scratchpad register defined which is set by the
5867 * pre-os which can be used by the OS drivers to check the status
5868 */
5869 if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
5870 goto sanitize;
5871
Chris Wilson91c8a322016-07-05 10:40:23 +01005872 intel_update_cdclk(&dev_priv->drm);
Imre Deak1c3f7702016-05-24 15:38:32 +03005873 /* Is PLL enabled and locked ? */
5874 if (dev_priv->cdclk_pll.vco == 0 ||
5875 dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
5876 goto sanitize;
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005877
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305878 /* DPLL okay; verify the cdclock
5879 *
5880 * Noticed in some instances that the freq selection is correct but
5881 * decimal part is programmed wrong from BIOS where pre-os does not
5882 * enable display. Verify the same as well.
5883 */
Ville Syrjälä09492492016-05-13 23:41:28 +03005884 cdctl = I915_READ(CDCLK_CTL);
5885 expected = (cdctl & CDCLK_FREQ_SEL_MASK) |
5886 skl_cdclk_decimal(dev_priv->cdclk_freq);
5887 if (cdctl == expected)
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305888 /* All well; nothing to sanitize */
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005889 return;
5890
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305891sanitize:
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005892 DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
Clint Taylorc89e39f2016-05-13 23:41:21 +03005893
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005894 /* force cdclk programming */
5895 dev_priv->cdclk_freq = 0;
5896 /* force full PLL disable + enable */
Ville Syrjälä63911d72016-05-13 23:41:32 +03005897 dev_priv->cdclk_pll.vco = -1;
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305898}
5899
Jesse Barnes30a970c2013-11-04 13:48:12 -08005900/* Adjust CDclk dividers to allow high res or save power if possible */
5901static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5902{
Chris Wilsonfac5e232016-07-04 11:34:36 +01005903 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005904 u32 val, cmd;
5905
Vandana Kannan164dfd22014-11-24 13:37:41 +05305906 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5907 != dev_priv->cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02005908
Ville Syrjälädfcab172014-06-13 13:37:47 +03005909 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08005910 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03005911 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005912 cmd = 1;
5913 else
5914 cmd = 0;
5915
5916 mutex_lock(&dev_priv->rps.hw_lock);
5917 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5918 val &= ~DSPFREQGUAR_MASK;
5919 val |= (cmd << DSPFREQGUAR_SHIFT);
5920 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5921 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5922 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5923 50)) {
5924 DRM_ERROR("timed out waiting for CDclk change\n");
5925 }
5926 mutex_unlock(&dev_priv->rps.hw_lock);
5927
Ville Syrjälä54433e92015-05-26 20:42:31 +03005928 mutex_lock(&dev_priv->sb_lock);
5929
Ville Syrjälädfcab172014-06-13 13:37:47 +03005930 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005931 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005932
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005933 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005934
Jesse Barnes30a970c2013-11-04 13:48:12 -08005935 /* adjust cdclk divider */
5936 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Vandana Kannan87d5d252015-09-24 23:29:17 +03005937 val &= ~CCK_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005938 val |= divider;
5939 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03005940
5941 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
Vandana Kannan87d5d252015-09-24 23:29:17 +03005942 CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
Ville Syrjäläa877e802014-06-13 13:37:52 +03005943 50))
5944 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08005945 }
5946
Jesse Barnes30a970c2013-11-04 13:48:12 -08005947 /* adjust self-refresh exit latency value */
5948 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5949 val &= ~0x7f;
5950
5951 /*
5952 * For high bandwidth configs, we set a higher latency in the bunit
5953 * so that the core display fetch happens in time to avoid underruns.
5954 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03005955 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005956 val |= 4500 / 250; /* 4.5 usec */
5957 else
5958 val |= 3000 / 250; /* 3.0 usec */
5959 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
Ville Syrjälä54433e92015-05-26 20:42:31 +03005960
Ville Syrjäläa5805162015-05-26 20:42:30 +03005961 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005962
Ville Syrjäläb6283052015-06-03 15:45:07 +03005963 intel_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005964}
5965
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005966static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5967{
Chris Wilsonfac5e232016-07-04 11:34:36 +01005968 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005969 u32 val, cmd;
5970
Vandana Kannan164dfd22014-11-24 13:37:41 +05305971 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5972 != dev_priv->cdclk_freq);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005973
5974 switch (cdclk) {
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005975 case 333333:
5976 case 320000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005977 case 266667:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005978 case 200000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005979 break;
5980 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01005981 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005982 return;
5983 }
5984
Ville Syrjälä9d0d3fd2015-03-02 20:07:17 +02005985 /*
5986 * Specs are full of misinformation, but testing on actual
5987 * hardware has shown that we just need to write the desired
5988 * CCK divider into the Punit register.
5989 */
5990 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5991
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005992 mutex_lock(&dev_priv->rps.hw_lock);
5993 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5994 val &= ~DSPFREQGUAR_MASK_CHV;
5995 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5996 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5997 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5998 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5999 50)) {
6000 DRM_ERROR("timed out waiting for CDclk change\n");
6001 }
6002 mutex_unlock(&dev_priv->rps.hw_lock);
6003
Ville Syrjäläb6283052015-06-03 15:45:07 +03006004 intel_update_cdclk(dev);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006005}
6006
Jesse Barnes30a970c2013-11-04 13:48:12 -08006007static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
6008 int max_pixclk)
6009{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006010 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02006011 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03006012
Jesse Barnes30a970c2013-11-04 13:48:12 -08006013 /*
6014 * Really only a few cases to deal with, as only 4 CDclks are supported:
6015 * 200MHz
6016 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03006017 * 320/333MHz (depends on HPLL freq)
Ville Syrjälä6cca3192015-03-02 20:07:16 +02006018 * 400MHz (VLV only)
6019 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
6020 * of the lower bin and adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03006021 *
6022 * We seem to get an unstable or solid color picture at 200MHz.
6023 * Not sure what's wrong. For now use 200MHz only when all pipes
6024 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08006025 */
Ville Syrjälä6cca3192015-03-02 20:07:16 +02006026 if (!IS_CHERRYVIEW(dev_priv) &&
6027 max_pixclk > freq_320*limit/100)
Ville Syrjälädfcab172014-06-13 13:37:47 +03006028 return 400000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02006029 else if (max_pixclk > 266667*limit/100)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03006030 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03006031 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03006032 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03006033 else
6034 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006035}
6036
Imre Deak324513c2016-06-13 16:44:36 +03006037static int bxt_calc_cdclk(int max_pixclk)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006038{
Ville Syrjälä760e1472016-05-11 22:44:46 +03006039 if (max_pixclk > 576000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306040 return 624000;
Ville Syrjälä760e1472016-05-11 22:44:46 +03006041 else if (max_pixclk > 384000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306042 return 576000;
Ville Syrjälä760e1472016-05-11 22:44:46 +03006043 else if (max_pixclk > 288000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306044 return 384000;
Ville Syrjälä760e1472016-05-11 22:44:46 +03006045 else if (max_pixclk > 144000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306046 return 288000;
6047 else
6048 return 144000;
6049}
6050
Maarten Lankhorste8788cb2016-02-16 10:25:11 +01006051/* Compute the max pixel clock for new configuration. */
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006052static int intel_mode_max_pixclk(struct drm_device *dev,
6053 struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006054{
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006055 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Chris Wilsonfac5e232016-07-04 11:34:36 +01006056 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006057 struct drm_crtc *crtc;
6058 struct drm_crtc_state *crtc_state;
6059 unsigned max_pixclk = 0, i;
6060 enum pipe pipe;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006061
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006062 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
6063 sizeof(intel_state->min_pixclk));
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006064
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006065 for_each_crtc_in_state(state, crtc, crtc_state, i) {
6066 int pixclk = 0;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006067
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006068 if (crtc_state->enable)
6069 pixclk = crtc_state->adjusted_mode.crtc_clock;
6070
6071 intel_state->min_pixclk[i] = pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006072 }
6073
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006074 for_each_pipe(dev_priv, pipe)
6075 max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
6076
Jesse Barnes30a970c2013-11-04 13:48:12 -08006077 return max_pixclk;
6078}
6079
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006080static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006081{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006082 struct drm_device *dev = state->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006083 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006084 int max_pixclk = intel_mode_max_pixclk(dev, state);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006085 struct intel_atomic_state *intel_state =
6086 to_intel_atomic_state(state);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006087
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006088 intel_state->cdclk = intel_state->dev_cdclk =
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006089 valleyview_calc_cdclk(dev_priv, max_pixclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306090
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006091 if (!intel_state->active_crtcs)
6092 intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
6093
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006094 return 0;
6095}
Jesse Barnes30a970c2013-11-04 13:48:12 -08006096
Imre Deak324513c2016-06-13 16:44:36 +03006097static int bxt_modeset_calc_cdclk(struct drm_atomic_state *state)
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006098{
Ville Syrjälä4e5ca602016-05-11 22:44:44 +03006099 int max_pixclk = ilk_max_pixel_rate(state);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006100 struct intel_atomic_state *intel_state =
6101 to_intel_atomic_state(state);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02006102
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006103 intel_state->cdclk = intel_state->dev_cdclk =
Imre Deak324513c2016-06-13 16:44:36 +03006104 bxt_calc_cdclk(max_pixclk);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02006105
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006106 if (!intel_state->active_crtcs)
Imre Deak324513c2016-06-13 16:44:36 +03006107 intel_state->dev_cdclk = bxt_calc_cdclk(0);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006108
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006109 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006110}
6111
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006112static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
6113{
6114 unsigned int credits, default_credits;
6115
6116 if (IS_CHERRYVIEW(dev_priv))
6117 default_credits = PFI_CREDIT(12);
6118 else
6119 default_credits = PFI_CREDIT(8);
6120
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03006121 if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006122 /* CHV suggested value is 31 or 63 */
6123 if (IS_CHERRYVIEW(dev_priv))
Ville Syrjäläfcc00082015-05-26 20:22:40 +03006124 credits = PFI_CREDIT_63;
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006125 else
6126 credits = PFI_CREDIT(15);
6127 } else {
6128 credits = default_credits;
6129 }
6130
6131 /*
6132 * WA - write default credits before re-programming
6133 * FIXME: should we also set the resend bit here?
6134 */
6135 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6136 default_credits);
6137
6138 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6139 credits | PFI_CREDIT_RESEND);
6140
6141 /*
6142 * FIXME is this guaranteed to clear
6143 * immediately or should we poll for it?
6144 */
6145 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
6146}
6147
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006148static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006149{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006150 struct drm_device *dev = old_state->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006151 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006152 struct intel_atomic_state *old_intel_state =
6153 to_intel_atomic_state(old_state);
6154 unsigned req_cdclk = old_intel_state->dev_cdclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006155
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006156 /*
6157 * FIXME: We can end up here with all power domains off, yet
6158 * with a CDCLK frequency other than the minimum. To account
6159 * for this take the PIPE-A power domain, which covers the HW
6160 * blocks needed for the following programming. This can be
6161 * removed once it's guaranteed that we get here either with
6162 * the minimum CDCLK set, or the required power domains
6163 * enabled.
6164 */
6165 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006166
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006167 if (IS_CHERRYVIEW(dev))
6168 cherryview_set_cdclk(dev, req_cdclk);
6169 else
6170 valleyview_set_cdclk(dev, req_cdclk);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006171
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006172 vlv_program_pfi_credits(dev_priv);
Imre Deak738c05c2014-11-19 16:25:37 +02006173
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006174 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006175}
6176
Jesse Barnes89b667f2013-04-18 14:51:36 -07006177static void valleyview_crtc_enable(struct drm_crtc *crtc)
6178{
6179 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006180 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006181 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6182 struct intel_encoder *encoder;
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006183 struct intel_crtc_state *pipe_config =
6184 to_intel_crtc_state(crtc->state);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006185 int pipe = intel_crtc->pipe;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006186
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006187 if (WARN_ON(intel_crtc->active))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006188 return;
6189
Ville Syrjälä37a56502016-06-22 21:57:04 +03006190 if (intel_crtc_has_dp_encoder(intel_crtc->config))
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306191 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006192
6193 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02006194 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006195
Ville Syrjäläc14b0482014-10-16 20:52:34 +03006196 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
Chris Wilsonfac5e232016-07-04 11:34:36 +01006197 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03006198
6199 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6200 I915_WRITE(CHV_CANVAS(pipe), 0);
6201 }
6202
Daniel Vetter5b18e572014-04-24 23:55:06 +02006203 i9xx_set_pipeconf(intel_crtc);
6204
Jesse Barnes89b667f2013-04-18 14:51:36 -07006205 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006206
Daniel Vettera72e4c92014-09-30 10:56:47 +02006207 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006208
Jesse Barnes89b667f2013-04-18 14:51:36 -07006209 for_each_encoder_on_crtc(dev, crtc, encoder)
6210 if (encoder->pre_pll_enable)
6211 encoder->pre_pll_enable(encoder);
6212
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006213 if (IS_CHERRYVIEW(dev)) {
6214 chv_prepare_pll(intel_crtc, intel_crtc->config);
6215 chv_enable_pll(intel_crtc, intel_crtc->config);
6216 } else {
6217 vlv_prepare_pll(intel_crtc, intel_crtc->config);
6218 vlv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006219 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07006220
6221 for_each_encoder_on_crtc(dev, crtc, encoder)
6222 if (encoder->pre_enable)
6223 encoder->pre_enable(encoder);
6224
Jesse Barnes2dd24552013-04-25 12:55:01 -07006225 i9xx_pfit_enable(intel_crtc);
6226
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006227 intel_color_load_luts(&pipe_config->base);
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006228
Ville Syrjäläcaed3612016-03-09 19:07:25 +02006229 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006230 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006231
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006232 assert_vblank_disabled(crtc);
6233 drm_crtc_vblank_on(crtc);
6234
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006235 for_each_encoder_on_crtc(dev, crtc, encoder)
6236 encoder->enable(encoder);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006237}
6238
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006239static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6240{
6241 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006242 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006243
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006244 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6245 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006246}
6247
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006248static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006249{
6250 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006251 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006252 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006253 struct intel_encoder *encoder;
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006254 struct intel_crtc_state *pipe_config =
6255 to_intel_crtc_state(crtc->state);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006256 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08006257
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006258 if (WARN_ON(intel_crtc->active))
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006259 return;
6260
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006261 i9xx_set_pll_dividers(intel_crtc);
6262
Ville Syrjälä37a56502016-06-22 21:57:04 +03006263 if (intel_crtc_has_dp_encoder(intel_crtc->config))
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306264 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006265
6266 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02006267 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006268
Daniel Vetter5b18e572014-04-24 23:55:06 +02006269 i9xx_set_pipeconf(intel_crtc);
6270
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006271 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01006272
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006273 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006274 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006275
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02006276 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02006277 if (encoder->pre_enable)
6278 encoder->pre_enable(encoder);
6279
Daniel Vetterf6736a12013-06-05 13:34:30 +02006280 i9xx_enable_pll(intel_crtc);
6281
Jesse Barnes2dd24552013-04-25 12:55:01 -07006282 i9xx_pfit_enable(intel_crtc);
6283
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006284 intel_color_load_luts(&pipe_config->base);
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006285
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006286 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006287 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006288
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006289 assert_vblank_disabled(crtc);
6290 drm_crtc_vblank_on(crtc);
6291
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006292 for_each_encoder_on_crtc(dev, crtc, encoder)
6293 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006294}
6295
Daniel Vetter87476d62013-04-11 16:29:06 +02006296static void i9xx_pfit_disable(struct intel_crtc *crtc)
6297{
6298 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006299 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter328d8e82013-05-08 10:36:31 +02006300
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006301 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02006302 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02006303
6304 assert_pipe_disabled(dev_priv, crtc->pipe);
6305
Daniel Vetter328d8e82013-05-08 10:36:31 +02006306 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6307 I915_READ(PFIT_CONTROL));
6308 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02006309}
6310
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006311static void i9xx_crtc_disable(struct drm_crtc *crtc)
6312{
6313 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006314 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006315 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006316 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006317 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006318
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006319 /*
6320 * On gen2 planes are double buffered but the pipe isn't, so we must
6321 * wait for planes to fully turn off before disabling the pipe.
6322 */
Ander Conselvan de Oliveira90e83e52016-03-22 10:11:24 +02006323 if (IS_GEN2(dev))
6324 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006325
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006326 for_each_encoder_on_crtc(dev, crtc, encoder)
6327 encoder->disable(encoder);
6328
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006329 drm_crtc_vblank_off(crtc);
6330 assert_vblank_disabled(crtc);
6331
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03006332 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006333
Daniel Vetter87476d62013-04-11 16:29:06 +02006334 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006335
Jesse Barnes89b667f2013-04-18 14:51:36 -07006336 for_each_encoder_on_crtc(dev, crtc, encoder)
6337 if (encoder->post_disable)
6338 encoder->post_disable(encoder);
6339
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03006340 if (!intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DSI)) {
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006341 if (IS_CHERRYVIEW(dev))
6342 chv_disable_pll(dev_priv, pipe);
6343 else if (IS_VALLEYVIEW(dev))
6344 vlv_disable_pll(dev_priv, pipe);
6345 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03006346 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006347 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006348
Ville Syrjäläd6db9952015-07-08 23:45:49 +03006349 for_each_encoder_on_crtc(dev, crtc, encoder)
6350 if (encoder->post_pll_disable)
6351 encoder->post_pll_disable(encoder);
6352
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006353 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006354 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006355}
6356
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006357static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006358{
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006359 struct intel_encoder *encoder;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006360 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006361 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006362 enum intel_display_power_domain domain;
6363 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006364
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006365 if (!intel_crtc->active)
6366 return;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006367
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006368 if (to_intel_plane_state(crtc->primary->state)->visible) {
Daniel Vetter5a21b662016-05-24 17:13:53 +02006369 WARN_ON(intel_crtc->flip_work);
Maarten Lankhorstfc32b1f2015-10-19 17:09:23 +02006370
Ville Syrjälä2622a082016-03-09 19:07:26 +02006371 intel_pre_disable_primary_noatomic(crtc);
Maarten Lankhorst54a419612015-11-23 10:25:28 +01006372
6373 intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
6374 to_intel_plane_state(crtc->primary->state)->visible = false;
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006375 }
6376
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006377 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006378
Ville Syrjälä78108b72016-05-27 20:59:19 +03006379 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
6380 crtc->base.id, crtc->name);
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006381
6382 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
6383 crtc->state->active = false;
Matt Roper37d90782015-09-24 15:53:06 -07006384 intel_crtc->active = false;
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006385 crtc->enabled = false;
6386 crtc->state->connector_mask = 0;
6387 crtc->state->encoder_mask = 0;
6388
6389 for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
6390 encoder->base.crtc = NULL;
6391
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -02006392 intel_fbc_disable(intel_crtc);
Matt Roper37d90782015-09-24 15:53:06 -07006393 intel_update_watermarks(crtc);
Maarten Lankhorst1f7457b2015-07-13 11:55:05 +02006394 intel_disable_shared_dpll(intel_crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006395
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006396 domains = intel_crtc->enabled_power_domains;
6397 for_each_power_domain(domain, domains)
6398 intel_display_power_put(dev_priv, domain);
6399 intel_crtc->enabled_power_domains = 0;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006400
6401 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
6402 dev_priv->min_pixclk[intel_crtc->pipe] = 0;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006403}
6404
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006405/*
6406 * turn all crtc's off, but do not adjust state
6407 * This has to be paired with a call to intel_modeset_setup_hw_state.
6408 */
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006409int intel_display_suspend(struct drm_device *dev)
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006410{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006411 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006412 struct drm_atomic_state *state;
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006413 int ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006414
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006415 state = drm_atomic_helper_suspend(dev);
6416 ret = PTR_ERR_OR_ZERO(state);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006417 if (ret)
6418 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006419 else
6420 dev_priv->modeset_restore_state = state;
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006421 return ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006422}
6423
Chris Wilsonea5b2132010-08-04 13:50:23 +01006424void intel_encoder_destroy(struct drm_encoder *encoder)
6425{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006426 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01006427
Chris Wilsonea5b2132010-08-04 13:50:23 +01006428 drm_encoder_cleanup(encoder);
6429 kfree(intel_encoder);
6430}
6431
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006432/* Cross check the actual hw state with our own modeset state tracking (and it's
6433 * internal consistency). */
Daniel Vetter5a21b662016-05-24 17:13:53 +02006434static void intel_connector_verify_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006435{
Daniel Vetter5a21b662016-05-24 17:13:53 +02006436 struct drm_crtc *crtc = connector->base.state->crtc;
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006437
6438 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6439 connector->base.base.id,
6440 connector->base.name);
6441
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006442 if (connector->get_hw_state(connector)) {
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006443 struct intel_encoder *encoder = connector->encoder;
Daniel Vetter5a21b662016-05-24 17:13:53 +02006444 struct drm_connector_state *conn_state = connector->base.state;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006445
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006446 I915_STATE_WARN(!crtc,
6447 "connector enabled without attached crtc\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006448
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006449 if (!crtc)
6450 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006451
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006452 I915_STATE_WARN(!crtc->state->active,
6453 "connector is active, but attached crtc isn't\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006454
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006455 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006456 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006457
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006458 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006459 "atomic encoder doesn't match attached encoder\n");
Dave Airlie36cd7442014-05-02 13:44:18 +10006460
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006461 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006462 "attached encoder crtc differs from connector crtc\n");
6463 } else {
Maarten Lankhorst4d688a22015-08-05 12:37:06 +02006464 I915_STATE_WARN(crtc && crtc->state->active,
6465 "attached crtc is active, but connector isn't\n");
Daniel Vetter5a21b662016-05-24 17:13:53 +02006466 I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006467 "best encoder set without crtc!\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006468 }
6469}
6470
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006471int intel_connector_init(struct intel_connector *connector)
6472{
Maarten Lankhorst5350a032016-01-04 12:53:15 +01006473 drm_atomic_helper_connector_reset(&connector->base);
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006474
Maarten Lankhorst5350a032016-01-04 12:53:15 +01006475 if (!connector->base.state)
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006476 return -ENOMEM;
6477
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006478 return 0;
6479}
6480
6481struct intel_connector *intel_connector_alloc(void)
6482{
6483 struct intel_connector *connector;
6484
6485 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6486 if (!connector)
6487 return NULL;
6488
6489 if (intel_connector_init(connector) < 0) {
6490 kfree(connector);
6491 return NULL;
6492 }
6493
6494 return connector;
6495}
6496
Daniel Vetterf0947c32012-07-02 13:10:34 +02006497/* Simple connector->get_hw_state implementation for encoders that support only
6498 * one connector and no cloning and hence the encoder state determines the state
6499 * of the connector. */
6500bool intel_connector_get_hw_state(struct intel_connector *connector)
6501{
Daniel Vetter24929352012-07-02 20:28:59 +02006502 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02006503 struct intel_encoder *encoder = connector->encoder;
6504
6505 return encoder->get_hw_state(encoder, &pipe);
6506}
6507
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006508static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006509{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006510 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6511 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006512
6513 return 0;
6514}
6515
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006516static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006517 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006518{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006519 struct drm_atomic_state *state = pipe_config->base.state;
6520 struct intel_crtc *other_crtc;
6521 struct intel_crtc_state *other_crtc_state;
6522
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006523 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6524 pipe_name(pipe), pipe_config->fdi_lanes);
6525 if (pipe_config->fdi_lanes > 4) {
6526 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6527 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006528 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006529 }
6530
Paulo Zanonibafb6552013-11-02 21:07:44 -07006531 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006532 if (pipe_config->fdi_lanes > 2) {
6533 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6534 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006535 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006536 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006537 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006538 }
6539 }
6540
6541 if (INTEL_INFO(dev)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006542 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006543
6544 /* Ivybridge 3 pipe is really complicated */
6545 switch (pipe) {
6546 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006547 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006548 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006549 if (pipe_config->fdi_lanes <= 2)
6550 return 0;
6551
6552 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6553 other_crtc_state =
6554 intel_atomic_get_crtc_state(state, other_crtc);
6555 if (IS_ERR(other_crtc_state))
6556 return PTR_ERR(other_crtc_state);
6557
6558 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006559 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6560 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006561 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006562 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006563 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006564 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02006565 if (pipe_config->fdi_lanes > 2) {
6566 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6567 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006568 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02006569 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006570
6571 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6572 other_crtc_state =
6573 intel_atomic_get_crtc_state(state, other_crtc);
6574 if (IS_ERR(other_crtc_state))
6575 return PTR_ERR(other_crtc_state);
6576
6577 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006578 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006579 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006580 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006581 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006582 default:
6583 BUG();
6584 }
6585}
6586
Daniel Vettere29c22c2013-02-21 00:00:16 +01006587#define RETRY 1
6588static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006589 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02006590{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006591 struct drm_device *dev = intel_crtc->base.dev;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006592 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006593 int lane, link_bw, fdi_dotclock, ret;
6594 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006595
Daniel Vettere29c22c2013-02-21 00:00:16 +01006596retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02006597 /* FDI is a binary signal running at ~2.7GHz, encoding
6598 * each output octet as 10 bits. The actual frequency
6599 * is stored as a divider into a 100MHz clock, and the
6600 * mode pixel clock is stored in units of 1KHz.
6601 * Hence the bw of each lane in terms of the mode signal
6602 * is:
6603 */
Ville Syrjälä21a727b2016-02-17 21:41:10 +02006604 link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006605
Damien Lespiau241bfc32013-09-25 16:45:37 +01006606 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006607
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006608 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006609 pipe_config->pipe_bpp);
6610
6611 pipe_config->fdi_lanes = lane;
6612
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006613 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006614 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006615
Ville Syrjäläe3b247d2016-02-17 21:41:09 +02006616 ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006617 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01006618 pipe_config->pipe_bpp -= 2*3;
6619 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6620 pipe_config->pipe_bpp);
6621 needs_recompute = true;
6622 pipe_config->bw_constrained = true;
6623
6624 goto retry;
6625 }
6626
6627 if (needs_recompute)
6628 return RETRY;
6629
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006630 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006631}
6632
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006633static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6634 struct intel_crtc_state *pipe_config)
6635{
6636 if (pipe_config->pipe_bpp > 24)
6637 return false;
6638
6639 /* HSW can handle pixel rate up to cdclk? */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03006640 if (IS_HASWELL(dev_priv))
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006641 return true;
6642
6643 /*
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03006644 * We compare against max which means we must take
6645 * the increased cdclk requirement into account when
6646 * calculating the new cdclk.
6647 *
6648 * Should measure whether using a lower cdclk w/o IPS
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006649 */
6650 return ilk_pipe_pixel_rate(pipe_config) <=
6651 dev_priv->max_cdclk_freq * 95 / 100;
6652}
6653
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006654static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006655 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006656{
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006657 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006658 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006659
Jani Nikulad330a952014-01-21 11:24:25 +02006660 pipe_config->ips_enabled = i915.enable_ips &&
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006661 hsw_crtc_supports_ips(crtc) &&
6662 pipe_config_supports_ips(dev_priv, pipe_config);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006663}
6664
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006665static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
6666{
6667 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6668
6669 /* GDG double wide on either pipe, otherwise pipe A only */
6670 return INTEL_INFO(dev_priv)->gen < 4 &&
6671 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
6672}
6673
Daniel Vettera43f6e02013-06-07 23:10:32 +02006674static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006675 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006676{
Daniel Vettera43f6e02013-06-07 23:10:32 +02006677 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006678 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006679 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ville Syrjäläf3261152016-05-24 21:34:18 +03006680 int clock_limit = dev_priv->max_dotclk_freq;
Chris Wilson89749352010-09-12 18:25:19 +01006681
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006682 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjäläf3261152016-05-24 21:34:18 +03006683 clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006684
6685 /*
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006686 * Enable double wide mode when the dot clock
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006687 * is > 90% of the (display) core speed.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006688 */
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006689 if (intel_crtc_supports_double_wide(crtc) &&
6690 adjusted_mode->crtc_clock > clock_limit) {
Ville Syrjäläf3261152016-05-24 21:34:18 +03006691 clock_limit = dev_priv->max_dotclk_freq;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006692 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006693 }
Ville Syrjäläf3261152016-05-24 21:34:18 +03006694 }
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006695
Ville Syrjäläf3261152016-05-24 21:34:18 +03006696 if (adjusted_mode->crtc_clock > clock_limit) {
6697 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
6698 adjusted_mode->crtc_clock, clock_limit,
6699 yesno(pipe_config->double_wide));
6700 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08006701 }
Chris Wilson89749352010-09-12 18:25:19 +01006702
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006703 /*
6704 * Pipe horizontal size must be even in:
6705 * - DVO ganged mode
6706 * - LVDS dual channel mode
6707 * - Double wide pipe
6708 */
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03006709 if ((intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006710 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6711 pipe_config->pipe_src_w &= ~1;
6712
Damien Lespiau8693a822013-05-03 18:48:11 +01006713 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6714 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03006715 */
6716 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
Ville Syrjäläaad941d2015-09-25 16:38:56 +03006717 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006718 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03006719
Damien Lespiauf5adf942013-06-24 18:29:34 +01006720 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02006721 hsw_compute_ips_config(crtc, pipe_config);
6722
Daniel Vetter877d48d2013-04-19 11:24:43 +02006723 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02006724 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006725
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +02006726 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006727}
6728
Ville Syrjälä1652d192015-03-31 14:12:01 +03006729static int skylake_get_display_clock_speed(struct drm_device *dev)
6730{
6731 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläea617912016-05-13 23:41:24 +03006732 uint32_t cdctl;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006733
Ville Syrjäläea617912016-05-13 23:41:24 +03006734 skl_dpll0_update(dev_priv);
6735
Ville Syrjälä63911d72016-05-13 23:41:32 +03006736 if (dev_priv->cdclk_pll.vco == 0)
Ville Syrjälä709e05c2016-05-13 23:41:33 +03006737 return dev_priv->cdclk_pll.ref;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006738
Ville Syrjäläea617912016-05-13 23:41:24 +03006739 cdctl = I915_READ(CDCLK_CTL);
Ville Syrjälä1652d192015-03-31 14:12:01 +03006740
Ville Syrjälä63911d72016-05-13 23:41:32 +03006741 if (dev_priv->cdclk_pll.vco == 8640000) {
Ville Syrjälä1652d192015-03-31 14:12:01 +03006742 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6743 case CDCLK_FREQ_450_432:
6744 return 432000;
6745 case CDCLK_FREQ_337_308:
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03006746 return 308571;
Ville Syrjäläea617912016-05-13 23:41:24 +03006747 case CDCLK_FREQ_540:
6748 return 540000;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006749 case CDCLK_FREQ_675_617:
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03006750 return 617143;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006751 default:
Ville Syrjäläea617912016-05-13 23:41:24 +03006752 MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
Ville Syrjälä1652d192015-03-31 14:12:01 +03006753 }
6754 } else {
Ville Syrjälä1652d192015-03-31 14:12:01 +03006755 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6756 case CDCLK_FREQ_450_432:
6757 return 450000;
6758 case CDCLK_FREQ_337_308:
6759 return 337500;
Ville Syrjäläea617912016-05-13 23:41:24 +03006760 case CDCLK_FREQ_540:
6761 return 540000;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006762 case CDCLK_FREQ_675_617:
6763 return 675000;
6764 default:
Ville Syrjäläea617912016-05-13 23:41:24 +03006765 MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
Ville Syrjälä1652d192015-03-31 14:12:01 +03006766 }
6767 }
6768
Ville Syrjälä709e05c2016-05-13 23:41:33 +03006769 return dev_priv->cdclk_pll.ref;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006770}
6771
Ville Syrjälä83d7c812016-05-13 23:41:35 +03006772static void bxt_de_pll_update(struct drm_i915_private *dev_priv)
6773{
6774 u32 val;
6775
6776 dev_priv->cdclk_pll.ref = 19200;
Imre Deak1c3f7702016-05-24 15:38:32 +03006777 dev_priv->cdclk_pll.vco = 0;
Ville Syrjälä83d7c812016-05-13 23:41:35 +03006778
6779 val = I915_READ(BXT_DE_PLL_ENABLE);
Imre Deak1c3f7702016-05-24 15:38:32 +03006780 if ((val & BXT_DE_PLL_PLL_ENABLE) == 0)
Ville Syrjälä83d7c812016-05-13 23:41:35 +03006781 return;
Ville Syrjälä83d7c812016-05-13 23:41:35 +03006782
Imre Deak1c3f7702016-05-24 15:38:32 +03006783 if (WARN_ON((val & BXT_DE_PLL_LOCK) == 0))
6784 return;
Ville Syrjälä83d7c812016-05-13 23:41:35 +03006785
6786 val = I915_READ(BXT_DE_PLL_CTL);
6787 dev_priv->cdclk_pll.vco = (val & BXT_DE_PLL_RATIO_MASK) *
6788 dev_priv->cdclk_pll.ref;
6789}
6790
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006791static int broxton_get_display_clock_speed(struct drm_device *dev)
6792{
6793 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläf5986242016-05-13 23:41:37 +03006794 u32 divider;
6795 int div, vco;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006796
Ville Syrjälä83d7c812016-05-13 23:41:35 +03006797 bxt_de_pll_update(dev_priv);
6798
Ville Syrjäläf5986242016-05-13 23:41:37 +03006799 vco = dev_priv->cdclk_pll.vco;
6800 if (vco == 0)
6801 return dev_priv->cdclk_pll.ref;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006802
Ville Syrjäläf5986242016-05-13 23:41:37 +03006803 divider = I915_READ(CDCLK_CTL) & BXT_CDCLK_CD2X_DIV_SEL_MASK;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006804
Ville Syrjäläf5986242016-05-13 23:41:37 +03006805 switch (divider) {
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006806 case BXT_CDCLK_CD2X_DIV_SEL_1:
Ville Syrjäläf5986242016-05-13 23:41:37 +03006807 div = 2;
6808 break;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006809 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
Ville Syrjäläf5986242016-05-13 23:41:37 +03006810 div = 3;
6811 break;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006812 case BXT_CDCLK_CD2X_DIV_SEL_2:
Ville Syrjäläf5986242016-05-13 23:41:37 +03006813 div = 4;
6814 break;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006815 case BXT_CDCLK_CD2X_DIV_SEL_4:
Ville Syrjäläf5986242016-05-13 23:41:37 +03006816 div = 8;
6817 break;
6818 default:
6819 MISSING_CASE(divider);
6820 return dev_priv->cdclk_pll.ref;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006821 }
6822
Ville Syrjäläf5986242016-05-13 23:41:37 +03006823 return DIV_ROUND_CLOSEST(vco, div);
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006824}
6825
Ville Syrjälä1652d192015-03-31 14:12:01 +03006826static int broadwell_get_display_clock_speed(struct drm_device *dev)
6827{
Chris Wilsonfac5e232016-07-04 11:34:36 +01006828 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä1652d192015-03-31 14:12:01 +03006829 uint32_t lcpll = I915_READ(LCPLL_CTL);
6830 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6831
6832 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6833 return 800000;
6834 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6835 return 450000;
6836 else if (freq == LCPLL_CLK_FREQ_450)
6837 return 450000;
6838 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6839 return 540000;
6840 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6841 return 337500;
6842 else
6843 return 675000;
6844}
6845
6846static int haswell_get_display_clock_speed(struct drm_device *dev)
6847{
Chris Wilsonfac5e232016-07-04 11:34:36 +01006848 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä1652d192015-03-31 14:12:01 +03006849 uint32_t lcpll = I915_READ(LCPLL_CTL);
6850 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6851
6852 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6853 return 800000;
6854 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6855 return 450000;
6856 else if (freq == LCPLL_CLK_FREQ_450)
6857 return 450000;
6858 else if (IS_HSW_ULT(dev))
6859 return 337500;
6860 else
6861 return 540000;
Jesse Barnes79e53942008-11-07 14:24:08 -08006862}
6863
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006864static int valleyview_get_display_clock_speed(struct drm_device *dev)
6865{
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03006866 return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
6867 CCK_DISPLAY_CLOCK_CONTROL);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006868}
6869
Ville Syrjäläb37a6432015-03-31 14:11:54 +03006870static int ilk_get_display_clock_speed(struct drm_device *dev)
6871{
6872 return 450000;
6873}
6874
Jesse Barnese70236a2009-09-21 10:42:27 -07006875static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006876{
Jesse Barnese70236a2009-09-21 10:42:27 -07006877 return 400000;
6878}
Jesse Barnes79e53942008-11-07 14:24:08 -08006879
Jesse Barnese70236a2009-09-21 10:42:27 -07006880static int i915_get_display_clock_speed(struct drm_device *dev)
6881{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006882 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006883}
Jesse Barnes79e53942008-11-07 14:24:08 -08006884
Jesse Barnese70236a2009-09-21 10:42:27 -07006885static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6886{
6887 return 200000;
6888}
Jesse Barnes79e53942008-11-07 14:24:08 -08006889
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006890static int pnv_get_display_clock_speed(struct drm_device *dev)
6891{
6892 u16 gcfgc = 0;
6893
6894 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6895
6896 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6897 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006898 return 266667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006899 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006900 return 333333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006901 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006902 return 444444;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006903 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6904 return 200000;
6905 default:
6906 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6907 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006908 return 133333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006909 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006910 return 166667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006911 }
6912}
6913
Jesse Barnese70236a2009-09-21 10:42:27 -07006914static int i915gm_get_display_clock_speed(struct drm_device *dev)
6915{
6916 u16 gcfgc = 0;
6917
6918 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6919
6920 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Ville Syrjäläe907f172015-03-31 14:09:47 +03006921 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006922 else {
6923 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6924 case GC_DISPLAY_CLOCK_333_MHZ:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006925 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006926 default:
6927 case GC_DISPLAY_CLOCK_190_200_MHZ:
6928 return 190000;
6929 }
6930 }
6931}
Jesse Barnes79e53942008-11-07 14:24:08 -08006932
Jesse Barnese70236a2009-09-21 10:42:27 -07006933static int i865_get_display_clock_speed(struct drm_device *dev)
6934{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006935 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006936}
6937
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006938static int i85x_get_display_clock_speed(struct drm_device *dev)
Jesse Barnese70236a2009-09-21 10:42:27 -07006939{
6940 u16 hpllcc = 0;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006941
Ville Syrjälä65cd2b32015-05-22 11:22:32 +03006942 /*
6943 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6944 * encoding is different :(
6945 * FIXME is this the right way to detect 852GM/852GMV?
6946 */
6947 if (dev->pdev->revision == 0x1)
6948 return 133333;
6949
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006950 pci_bus_read_config_word(dev->pdev->bus,
6951 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6952
Jesse Barnese70236a2009-09-21 10:42:27 -07006953 /* Assume that the hardware is in the high speed state. This
6954 * should be the default.
6955 */
6956 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6957 case GC_CLOCK_133_200:
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006958 case GC_CLOCK_133_200_2:
Jesse Barnese70236a2009-09-21 10:42:27 -07006959 case GC_CLOCK_100_200:
6960 return 200000;
6961 case GC_CLOCK_166_250:
6962 return 250000;
6963 case GC_CLOCK_100_133:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006964 return 133333;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006965 case GC_CLOCK_133_266:
6966 case GC_CLOCK_133_266_2:
6967 case GC_CLOCK_166_266:
6968 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006969 }
6970
6971 /* Shouldn't happen */
6972 return 0;
6973}
6974
6975static int i830_get_display_clock_speed(struct drm_device *dev)
6976{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006977 return 133333;
Jesse Barnes79e53942008-11-07 14:24:08 -08006978}
6979
Ville Syrjälä34edce22015-05-22 11:22:33 +03006980static unsigned int intel_hpll_vco(struct drm_device *dev)
6981{
Chris Wilsonfac5e232016-07-04 11:34:36 +01006982 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä34edce22015-05-22 11:22:33 +03006983 static const unsigned int blb_vco[8] = {
6984 [0] = 3200000,
6985 [1] = 4000000,
6986 [2] = 5333333,
6987 [3] = 4800000,
6988 [4] = 6400000,
6989 };
6990 static const unsigned int pnv_vco[8] = {
6991 [0] = 3200000,
6992 [1] = 4000000,
6993 [2] = 5333333,
6994 [3] = 4800000,
6995 [4] = 2666667,
6996 };
6997 static const unsigned int cl_vco[8] = {
6998 [0] = 3200000,
6999 [1] = 4000000,
7000 [2] = 5333333,
7001 [3] = 6400000,
7002 [4] = 3333333,
7003 [5] = 3566667,
7004 [6] = 4266667,
7005 };
7006 static const unsigned int elk_vco[8] = {
7007 [0] = 3200000,
7008 [1] = 4000000,
7009 [2] = 5333333,
7010 [3] = 4800000,
7011 };
7012 static const unsigned int ctg_vco[8] = {
7013 [0] = 3200000,
7014 [1] = 4000000,
7015 [2] = 5333333,
7016 [3] = 6400000,
7017 [4] = 2666667,
7018 [5] = 4266667,
7019 };
7020 const unsigned int *vco_table;
7021 unsigned int vco;
7022 uint8_t tmp = 0;
7023
7024 /* FIXME other chipsets? */
7025 if (IS_GM45(dev))
7026 vco_table = ctg_vco;
7027 else if (IS_G4X(dev))
7028 vco_table = elk_vco;
7029 else if (IS_CRESTLINE(dev))
7030 vco_table = cl_vco;
7031 else if (IS_PINEVIEW(dev))
7032 vco_table = pnv_vco;
7033 else if (IS_G33(dev))
7034 vco_table = blb_vco;
7035 else
7036 return 0;
7037
7038 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
7039
7040 vco = vco_table[tmp & 0x7];
7041 if (vco == 0)
7042 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
7043 else
7044 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
7045
7046 return vco;
7047}
7048
7049static int gm45_get_display_clock_speed(struct drm_device *dev)
7050{
7051 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7052 uint16_t tmp = 0;
7053
7054 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7055
7056 cdclk_sel = (tmp >> 12) & 0x1;
7057
7058 switch (vco) {
7059 case 2666667:
7060 case 4000000:
7061 case 5333333:
7062 return cdclk_sel ? 333333 : 222222;
7063 case 3200000:
7064 return cdclk_sel ? 320000 : 228571;
7065 default:
7066 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
7067 return 222222;
7068 }
7069}
7070
7071static int i965gm_get_display_clock_speed(struct drm_device *dev)
7072{
7073 static const uint8_t div_3200[] = { 16, 10, 8 };
7074 static const uint8_t div_4000[] = { 20, 12, 10 };
7075 static const uint8_t div_5333[] = { 24, 16, 14 };
7076 const uint8_t *div_table;
7077 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7078 uint16_t tmp = 0;
7079
7080 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7081
7082 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
7083
7084 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7085 goto fail;
7086
7087 switch (vco) {
7088 case 3200000:
7089 div_table = div_3200;
7090 break;
7091 case 4000000:
7092 div_table = div_4000;
7093 break;
7094 case 5333333:
7095 div_table = div_5333;
7096 break;
7097 default:
7098 goto fail;
7099 }
7100
7101 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7102
Damien Lespiaucaf4e252015-06-04 16:56:18 +01007103fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03007104 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
7105 return 200000;
7106}
7107
7108static int g33_get_display_clock_speed(struct drm_device *dev)
7109{
7110 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
7111 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
7112 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
7113 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
7114 const uint8_t *div_table;
7115 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7116 uint16_t tmp = 0;
7117
7118 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7119
7120 cdclk_sel = (tmp >> 4) & 0x7;
7121
7122 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7123 goto fail;
7124
7125 switch (vco) {
7126 case 3200000:
7127 div_table = div_3200;
7128 break;
7129 case 4000000:
7130 div_table = div_4000;
7131 break;
7132 case 4800000:
7133 div_table = div_4800;
7134 break;
7135 case 5333333:
7136 div_table = div_5333;
7137 break;
7138 default:
7139 goto fail;
7140 }
7141
7142 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7143
Damien Lespiaucaf4e252015-06-04 16:56:18 +01007144fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03007145 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
7146 return 190476;
7147}
7148
Zhenyu Wang2c072452009-06-05 15:38:42 +08007149static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007150intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007151{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007152 while (*num > DATA_LINK_M_N_MASK ||
7153 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08007154 *num >>= 1;
7155 *den >>= 1;
7156 }
7157}
7158
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007159static void compute_m_n(unsigned int m, unsigned int n,
7160 uint32_t *ret_m, uint32_t *ret_n)
7161{
7162 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7163 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7164 intel_reduce_m_n_ratio(ret_m, ret_n);
7165}
7166
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007167void
7168intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7169 int pixel_clock, int link_clock,
7170 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007171{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007172 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007173
7174 compute_m_n(bits_per_pixel * pixel_clock,
7175 link_clock * nlanes * 8,
7176 &m_n->gmch_m, &m_n->gmch_n);
7177
7178 compute_m_n(pixel_clock, link_clock,
7179 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08007180}
7181
Chris Wilsona7615032011-01-12 17:04:08 +00007182static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7183{
Jani Nikulad330a952014-01-21 11:24:25 +02007184 if (i915.panel_use_ssc >= 0)
7185 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007186 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07007187 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00007188}
7189
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007190static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007191{
Daniel Vetter7df00d72013-05-21 21:54:55 +02007192 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007193}
Daniel Vetterf47709a2013-03-28 10:42:02 +01007194
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007195static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7196{
7197 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007198}
7199
Daniel Vetterf47709a2013-03-28 10:42:02 +01007200static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007201 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03007202 struct dpll *reduced_clock)
Jesse Barnesa7516a02011-12-15 12:30:37 -08007203{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007204 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007205 u32 fp, fp2 = 0;
7206
7207 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007208 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007209 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007210 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007211 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007212 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007213 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007214 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007215 }
7216
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007217 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007218
Daniel Vetterf47709a2013-03-28 10:42:02 +01007219 crtc->lowfreq_avail = false;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007220 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07007221 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007222 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007223 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007224 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007225 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007226 }
7227}
7228
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007229static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7230 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07007231{
7232 u32 reg_val;
7233
7234 /*
7235 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7236 * and set it to a reasonable value instead.
7237 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007238 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007239 reg_val &= 0xffffff00;
7240 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007241 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007242
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007243 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007244 reg_val &= 0x8cffffff;
7245 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007246 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007247
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007248 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007249 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007250 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007251
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007252 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007253 reg_val &= 0x00ffffff;
7254 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007255 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007256}
7257
Daniel Vetterb5518422013-05-03 11:49:48 +02007258static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7259 struct intel_link_m_n *m_n)
7260{
7261 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007262 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetterb5518422013-05-03 11:49:48 +02007263 int pipe = crtc->pipe;
7264
Daniel Vettere3b95f12013-05-03 11:49:49 +02007265 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7266 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7267 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7268 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007269}
7270
7271static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07007272 struct intel_link_m_n *m_n,
7273 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02007274{
7275 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007276 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetterb5518422013-05-03 11:49:48 +02007277 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007278 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02007279
7280 if (INTEL_INFO(dev)->gen >= 5) {
7281 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7282 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7283 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7284 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07007285 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7286 * for gen < 8) and if DRRS is supported (to make sure the
7287 * registers are not unnecessarily accessed).
7288 */
Durgadoss R44395bf2015-02-13 15:33:02 +05307289 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007290 crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07007291 I915_WRITE(PIPE_DATA_M2(transcoder),
7292 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7293 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7294 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7295 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7296 }
Daniel Vetterb5518422013-05-03 11:49:48 +02007297 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02007298 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7299 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7300 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7301 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007302 }
7303}
7304
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05307305void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007306{
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05307307 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7308
7309 if (m_n == M1_N1) {
7310 dp_m_n = &crtc->config->dp_m_n;
7311 dp_m2_n2 = &crtc->config->dp_m2_n2;
7312 } else if (m_n == M2_N2) {
7313
7314 /*
7315 * M2_N2 registers are not supported. Hence m2_n2 divider value
7316 * needs to be programmed into M1_N1.
7317 */
7318 dp_m_n = &crtc->config->dp_m2_n2;
7319 } else {
7320 DRM_ERROR("Unsupported divider value\n");
7321 return;
7322 }
7323
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007324 if (crtc->config->has_pch_encoder)
7325 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007326 else
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05307327 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007328}
7329
Daniel Vetter251ac862015-06-18 10:30:24 +02007330static void vlv_compute_dpll(struct intel_crtc *crtc,
7331 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007332{
Ville Syrjälä03ed5cb2016-03-15 16:39:55 +02007333 pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007334 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjälä03ed5cb2016-03-15 16:39:55 +02007335 if (crtc->pipe != PIPE_A)
7336 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007337
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007338 /* DPLL not used with DSI, but still need the rest set up */
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03007339 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007340 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
7341 DPLL_EXT_BUFFER_ENABLE_VLV;
7342
Ville Syrjälä03ed5cb2016-03-15 16:39:55 +02007343 pipe_config->dpll_hw_state.dpll_md =
7344 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
7345}
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007346
Ville Syrjälä03ed5cb2016-03-15 16:39:55 +02007347static void chv_compute_dpll(struct intel_crtc *crtc,
7348 struct intel_crtc_state *pipe_config)
7349{
7350 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007351 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjälä03ed5cb2016-03-15 16:39:55 +02007352 if (crtc->pipe != PIPE_A)
7353 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7354
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007355 /* DPLL not used with DSI, but still need the rest set up */
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03007356 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007357 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
7358
Ville Syrjälä03ed5cb2016-03-15 16:39:55 +02007359 pipe_config->dpll_hw_state.dpll_md =
7360 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007361}
7362
Ville Syrjäläd288f652014-10-28 13:20:22 +02007363static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007364 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007365{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007366 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007367 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007368 enum pipe pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007369 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007370 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007371 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007372
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007373 /* Enable Refclk */
7374 I915_WRITE(DPLL(pipe),
7375 pipe_config->dpll_hw_state.dpll &
7376 ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
7377
7378 /* No need to actually set up the DPLL with DSI */
7379 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7380 return;
7381
Ville Syrjäläa5805162015-05-26 20:42:30 +03007382 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01007383
Ville Syrjäläd288f652014-10-28 13:20:22 +02007384 bestn = pipe_config->dpll.n;
7385 bestm1 = pipe_config->dpll.m1;
7386 bestm2 = pipe_config->dpll.m2;
7387 bestp1 = pipe_config->dpll.p1;
7388 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007389
Jesse Barnes89b667f2013-04-18 14:51:36 -07007390 /* See eDP HDMI DPIO driver vbios notes doc */
7391
7392 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007393 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007394 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007395
7396 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007397 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007398
7399 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007400 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007401 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007402 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007403
7404 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007405 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007406
7407 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007408 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7409 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7410 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007411 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07007412
7413 /*
7414 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7415 * but we don't support that).
7416 * Note: don't use the DAC post divider as it seems unstable.
7417 */
7418 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007419 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007420
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007421 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007422 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007423
Jesse Barnes89b667f2013-04-18 14:51:36 -07007424 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02007425 if (pipe_config->port_clock == 162000 ||
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007426 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
7427 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007428 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b0122013-07-05 19:21:38 +03007429 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007430 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007431 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007432 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007433
Ville Syrjälä37a56502016-06-22 21:57:04 +03007434 if (intel_crtc_has_dp_encoder(pipe_config)) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07007435 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007436 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007437 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007438 0x0df40000);
7439 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007440 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007441 0x0df70000);
7442 } else { /* HDMI or VGA */
7443 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007444 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007445 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007446 0x0df70000);
7447 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007448 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007449 0x0df40000);
7450 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007451
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007452 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007453 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ville Syrjälä2210ce72016-06-22 21:57:05 +03007454 if (intel_crtc_has_dp_encoder(crtc->config))
Jesse Barnes89b667f2013-04-18 14:51:36 -07007455 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007456 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007457
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007458 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Ville Syrjäläa5805162015-05-26 20:42:30 +03007459 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007460}
7461
Ville Syrjäläd288f652014-10-28 13:20:22 +02007462static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007463 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007464{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007465 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007466 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007467 enum pipe pipe = crtc->pipe;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007468 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307469 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007470 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307471 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307472 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007473
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007474 /* Enable Refclk and SSC */
7475 I915_WRITE(DPLL(pipe),
7476 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
7477
7478 /* No need to actually set up the DPLL with DSI */
7479 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7480 return;
7481
Ville Syrjäläd288f652014-10-28 13:20:22 +02007482 bestn = pipe_config->dpll.n;
7483 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7484 bestm1 = pipe_config->dpll.m1;
7485 bestm2 = pipe_config->dpll.m2 >> 22;
7486 bestp1 = pipe_config->dpll.p1;
7487 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307488 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307489 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307490 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007491
Ville Syrjäläa5805162015-05-26 20:42:30 +03007492 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007493
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007494 /* p1 and p2 divider */
7495 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7496 5 << DPIO_CHV_S1_DIV_SHIFT |
7497 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7498 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7499 1 << DPIO_CHV_K_DIV_SHIFT);
7500
7501 /* Feedback post-divider - m2 */
7502 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7503
7504 /* Feedback refclk divider - n and m1 */
7505 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7506 DPIO_CHV_M1_DIV_BY_2 |
7507 1 << DPIO_CHV_N_DIV_SHIFT);
7508
7509 /* M2 fraction division */
Ville Syrjälä25a25df2015-07-08 23:45:47 +03007510 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007511
7512 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307513 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7514 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7515 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7516 if (bestm2_frac)
7517 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7518 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007519
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05307520 /* Program digital lock detect threshold */
7521 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7522 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7523 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7524 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7525 if (!bestm2_frac)
7526 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7527 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7528
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007529 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307530 if (vco == 5400000) {
7531 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7532 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7533 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7534 tribuf_calcntr = 0x9;
7535 } else if (vco <= 6200000) {
7536 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7537 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7538 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7539 tribuf_calcntr = 0x9;
7540 } else if (vco <= 6480000) {
7541 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7542 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7543 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7544 tribuf_calcntr = 0x8;
7545 } else {
7546 /* Not supported. Apply the same limits as in the max case */
7547 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7548 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7549 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7550 tribuf_calcntr = 0;
7551 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007552 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7553
Ville Syrjälä968040b2015-03-11 22:52:08 +02007554 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307555 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7556 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7557 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7558
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007559 /* AFC Recal */
7560 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7561 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7562 DPIO_AFC_RECAL);
7563
Ville Syrjäläa5805162015-05-26 20:42:30 +03007564 mutex_unlock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007565}
7566
Ville Syrjäläd288f652014-10-28 13:20:22 +02007567/**
7568 * vlv_force_pll_on - forcibly enable just the PLL
7569 * @dev_priv: i915 private structure
7570 * @pipe: pipe PLL to enable
7571 * @dpll: PLL configuration
7572 *
7573 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7574 * in cases where we need the PLL enabled even when @pipe is not going to
7575 * be enabled.
7576 */
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007577int vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7578 const struct dpll *dpll)
Ville Syrjäläd288f652014-10-28 13:20:22 +02007579{
7580 struct intel_crtc *crtc =
7581 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007582 struct intel_crtc_state *pipe_config;
7583
7584 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
7585 if (!pipe_config)
7586 return -ENOMEM;
7587
7588 pipe_config->base.crtc = &crtc->base;
7589 pipe_config->pixel_multiplier = 1;
7590 pipe_config->dpll = *dpll;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007591
7592 if (IS_CHERRYVIEW(dev)) {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007593 chv_compute_dpll(crtc, pipe_config);
7594 chv_prepare_pll(crtc, pipe_config);
7595 chv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007596 } else {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007597 vlv_compute_dpll(crtc, pipe_config);
7598 vlv_prepare_pll(crtc, pipe_config);
7599 vlv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007600 }
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007601
7602 kfree(pipe_config);
7603
7604 return 0;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007605}
7606
7607/**
7608 * vlv_force_pll_off - forcibly disable just the PLL
7609 * @dev_priv: i915 private structure
7610 * @pipe: pipe PLL to disable
7611 *
7612 * Disable the PLL for @pipe. To be used in cases where we need
7613 * the PLL enabled even when @pipe is not going to be enabled.
7614 */
7615void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7616{
7617 if (IS_CHERRYVIEW(dev))
7618 chv_disable_pll(to_i915(dev), pipe);
7619 else
7620 vlv_disable_pll(to_i915(dev), pipe);
7621}
7622
Daniel Vetter251ac862015-06-18 10:30:24 +02007623static void i9xx_compute_dpll(struct intel_crtc *crtc,
7624 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03007625 struct dpll *reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007626{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007627 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007628 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007629 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007630 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007631
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007632 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307633
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007634 dpll = DPLL_VGA_MODE_DIS;
7635
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007636 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007637 dpll |= DPLLB_MODE_LVDS;
7638 else
7639 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007640
Daniel Vetteref1b4602013-06-01 17:17:04 +02007641 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007642 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02007643 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007644 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02007645
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03007646 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7647 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007648 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007649
Ville Syrjälä37a56502016-06-22 21:57:04 +03007650 if (intel_crtc_has_dp_encoder(crtc_state))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007651 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007652
7653 /* compute bitmask from p1 value */
7654 if (IS_PINEVIEW(dev))
7655 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7656 else {
7657 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7658 if (IS_G4X(dev) && reduced_clock)
7659 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7660 }
7661 switch (clock->p2) {
7662 case 5:
7663 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7664 break;
7665 case 7:
7666 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7667 break;
7668 case 10:
7669 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7670 break;
7671 case 14:
7672 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7673 break;
7674 }
7675 if (INTEL_INFO(dev)->gen >= 4)
7676 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7677
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007678 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007679 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007680 else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007681 intel_panel_use_ssc(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007682 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7683 else
7684 dpll |= PLL_REF_INPUT_DREFCLK;
7685
7686 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007687 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007688
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007689 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007690 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007691 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007692 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007693 }
7694}
7695
Daniel Vetter251ac862015-06-18 10:30:24 +02007696static void i8xx_compute_dpll(struct intel_crtc *crtc,
7697 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03007698 struct dpll *reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007699{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007700 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007701 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007702 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007703 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007704
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007705 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307706
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007707 dpll = DPLL_VGA_MODE_DIS;
7708
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007709 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007710 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7711 } else {
7712 if (clock->p1 == 2)
7713 dpll |= PLL_P1_DIVIDE_BY_TWO;
7714 else
7715 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7716 if (clock->p2 == 4)
7717 dpll |= PLL_P2_DIVIDE_BY_4;
7718 }
7719
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007720 if (!IS_I830(dev) && intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007721 dpll |= DPLL_DVO_2X_MODE;
7722
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007723 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007724 intel_panel_use_ssc(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007725 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7726 else
7727 dpll |= PLL_REF_INPUT_DREFCLK;
7728
7729 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007730 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007731}
7732
Daniel Vetter8a654f32013-06-01 17:16:22 +02007733static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007734{
7735 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007736 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007737 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007738 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03007739 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007740 uint32_t crtc_vtotal, crtc_vblank_end;
7741 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007742
7743 /* We need to be careful not to changed the adjusted mode, for otherwise
7744 * the hw state checker will get angry at the mismatch. */
7745 crtc_vtotal = adjusted_mode->crtc_vtotal;
7746 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007747
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007748 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007749 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007750 crtc_vtotal -= 1;
7751 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007752
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007753 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007754 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7755 else
7756 vsyncshift = adjusted_mode->crtc_hsync_start -
7757 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007758 if (vsyncshift < 0)
7759 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007760 }
7761
7762 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007763 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007764
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007765 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007766 (adjusted_mode->crtc_hdisplay - 1) |
7767 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007768 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007769 (adjusted_mode->crtc_hblank_start - 1) |
7770 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007771 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007772 (adjusted_mode->crtc_hsync_start - 1) |
7773 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7774
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007775 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007776 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007777 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007778 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007779 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007780 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007781 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007782 (adjusted_mode->crtc_vsync_start - 1) |
7783 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7784
Paulo Zanonib5e508d2012-10-24 11:34:43 -02007785 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7786 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7787 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7788 * bits. */
7789 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7790 (pipe == PIPE_B || pipe == PIPE_C))
7791 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7792
Jani Nikulabc58be62016-03-18 17:05:39 +02007793}
7794
7795static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
7796{
7797 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007798 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikulabc58be62016-03-18 17:05:39 +02007799 enum pipe pipe = intel_crtc->pipe;
7800
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007801 /* pipesrc controls the size that is scaled from, which should
7802 * always be the user's requested size.
7803 */
7804 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007805 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7806 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007807}
7808
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007809static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007810 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007811{
7812 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007813 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007814 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7815 uint32_t tmp;
7816
7817 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007818 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7819 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007820 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007821 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7822 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007823 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007824 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7825 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007826
7827 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007828 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7829 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007830 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007831 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7832 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007833 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007834 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7835 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007836
7837 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007838 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7839 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7840 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007841 }
Jani Nikulabc58be62016-03-18 17:05:39 +02007842}
7843
7844static void intel_get_pipe_src_size(struct intel_crtc *crtc,
7845 struct intel_crtc_state *pipe_config)
7846{
7847 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007848 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikulabc58be62016-03-18 17:05:39 +02007849 u32 tmp;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007850
7851 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03007852 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7853 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7854
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007855 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7856 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007857}
7858
Daniel Vetterf6a83282014-02-11 15:28:57 -08007859void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007860 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03007861{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007862 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7863 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7864 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7865 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007866
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007867 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7868 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7869 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7870 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007871
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007872 mode->flags = pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007873 mode->type = DRM_MODE_TYPE_DRIVER;
Jesse Barnesbabea612013-06-26 18:57:38 +03007874
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007875 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7876 mode->flags |= pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007877
7878 mode->hsync = drm_mode_hsync(mode);
7879 mode->vrefresh = drm_mode_vrefresh(mode);
7880 drm_mode_set_name(mode);
Jesse Barnesbabea612013-06-26 18:57:38 +03007881}
7882
Daniel Vetter84b046f2013-02-19 18:48:54 +01007883static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7884{
7885 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007886 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter84b046f2013-02-19 18:48:54 +01007887 uint32_t pipeconf;
7888
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007889 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007890
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03007891 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7892 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7893 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02007894
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007895 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007896 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007897
Daniel Vetterff9ce462013-04-24 14:57:17 +02007898 /* only g4x and later have fancy bpc/dither controls */
Wayne Boyer666a4532015-12-09 12:29:35 -08007899 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007900 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007901 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02007902 pipeconf |= PIPECONF_DITHER_EN |
7903 PIPECONF_DITHER_TYPE_SP;
7904
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007905 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007906 case 18:
7907 pipeconf |= PIPECONF_6BPC;
7908 break;
7909 case 24:
7910 pipeconf |= PIPECONF_8BPC;
7911 break;
7912 case 30:
7913 pipeconf |= PIPECONF_10BPC;
7914 break;
7915 default:
7916 /* Case prevented by intel_choose_pipe_bpp_dither. */
7917 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01007918 }
7919 }
7920
7921 if (HAS_PIPE_CXSR(dev)) {
7922 if (intel_crtc->lowfreq_avail) {
7923 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7924 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7925 } else {
7926 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01007927 }
7928 }
7929
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007930 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007931 if (INTEL_INFO(dev)->gen < 4 ||
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007932 intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007933 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7934 else
7935 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7936 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01007937 pipeconf |= PIPECONF_PROGRESSIVE;
7938
Wayne Boyer666a4532015-12-09 12:29:35 -08007939 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
7940 intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007941 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03007942
Daniel Vetter84b046f2013-02-19 18:48:54 +01007943 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7944 POSTING_READ(PIPECONF(intel_crtc->pipe));
7945}
7946
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007947static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
7948 struct intel_crtc_state *crtc_state)
7949{
7950 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007951 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007952 const struct intel_limit *limit;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007953 int refclk = 48000;
7954
7955 memset(&crtc_state->dpll_hw_state, 0,
7956 sizeof(crtc_state->dpll_hw_state));
7957
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007958 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007959 if (intel_panel_use_ssc(dev_priv)) {
7960 refclk = dev_priv->vbt.lvds_ssc_freq;
7961 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7962 }
7963
7964 limit = &intel_limits_i8xx_lvds;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007965 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007966 limit = &intel_limits_i8xx_dvo;
7967 } else {
7968 limit = &intel_limits_i8xx_dac;
7969 }
7970
7971 if (!crtc_state->clock_set &&
7972 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7973 refclk, NULL, &crtc_state->dpll)) {
7974 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7975 return -EINVAL;
7976 }
7977
7978 i8xx_compute_dpll(crtc, crtc_state, NULL);
7979
7980 return 0;
7981}
7982
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007983static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
7984 struct intel_crtc_state *crtc_state)
7985{
7986 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007987 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007988 const struct intel_limit *limit;
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007989 int refclk = 96000;
7990
7991 memset(&crtc_state->dpll_hw_state, 0,
7992 sizeof(crtc_state->dpll_hw_state));
7993
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007994 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007995 if (intel_panel_use_ssc(dev_priv)) {
7996 refclk = dev_priv->vbt.lvds_ssc_freq;
7997 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7998 }
7999
8000 if (intel_is_dual_link_lvds(dev))
8001 limit = &intel_limits_g4x_dual_channel_lvds;
8002 else
8003 limit = &intel_limits_g4x_single_channel_lvds;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008004 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
8005 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02008006 limit = &intel_limits_g4x_hdmi;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008007 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02008008 limit = &intel_limits_g4x_sdvo;
8009 } else {
8010 /* The option is for other outputs */
8011 limit = &intel_limits_i9xx_sdvo;
8012 }
8013
8014 if (!crtc_state->clock_set &&
8015 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8016 refclk, NULL, &crtc_state->dpll)) {
8017 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8018 return -EINVAL;
8019 }
8020
8021 i9xx_compute_dpll(crtc, crtc_state, NULL);
8022
8023 return 0;
8024}
8025
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008026static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
8027 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008028{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008029 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008030 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008031 const struct intel_limit *limit;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02008032 int refclk = 96000;
Jesse Barnes79e53942008-11-07 14:24:08 -08008033
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03008034 memset(&crtc_state->dpll_hw_state, 0,
8035 sizeof(crtc_state->dpll_hw_state));
8036
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008037 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008038 if (intel_panel_use_ssc(dev_priv)) {
8039 refclk = dev_priv->vbt.lvds_ssc_freq;
8040 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
8041 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008042
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008043 limit = &intel_limits_pineview_lvds;
8044 } else {
8045 limit = &intel_limits_pineview_sdvo;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02008046 }
Jani Nikulaf2335332013-09-13 11:03:09 +03008047
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008048 if (!crtc_state->clock_set &&
8049 !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8050 refclk, NULL, &crtc_state->dpll)) {
8051 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8052 return -EINVAL;
8053 }
8054
8055 i9xx_compute_dpll(crtc, crtc_state, NULL);
8056
8057 return 0;
8058}
8059
8060static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
8061 struct intel_crtc_state *crtc_state)
8062{
8063 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008064 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008065 const struct intel_limit *limit;
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008066 int refclk = 96000;
8067
8068 memset(&crtc_state->dpll_hw_state, 0,
8069 sizeof(crtc_state->dpll_hw_state));
8070
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008071 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008072 if (intel_panel_use_ssc(dev_priv)) {
8073 refclk = dev_priv->vbt.lvds_ssc_freq;
8074 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03008075 }
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008076
8077 limit = &intel_limits_i9xx_lvds;
8078 } else {
8079 limit = &intel_limits_i9xx_sdvo;
8080 }
8081
8082 if (!crtc_state->clock_set &&
8083 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8084 refclk, NULL, &crtc_state->dpll)) {
8085 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8086 return -EINVAL;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008087 }
Eric Anholtf564048e2011-03-30 13:01:02 -07008088
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02008089 i9xx_compute_dpll(crtc, crtc_state, NULL);
Eric Anholtf564048e2011-03-30 13:01:02 -07008090
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008091 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07008092}
8093
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008094static int chv_crtc_compute_clock(struct intel_crtc *crtc,
8095 struct intel_crtc_state *crtc_state)
8096{
8097 int refclk = 100000;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008098 const struct intel_limit *limit = &intel_limits_chv;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008099
8100 memset(&crtc_state->dpll_hw_state, 0,
8101 sizeof(crtc_state->dpll_hw_state));
8102
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008103 if (!crtc_state->clock_set &&
8104 !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8105 refclk, NULL, &crtc_state->dpll)) {
8106 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8107 return -EINVAL;
8108 }
8109
8110 chv_compute_dpll(crtc, crtc_state);
8111
8112 return 0;
8113}
8114
8115static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
8116 struct intel_crtc_state *crtc_state)
8117{
8118 int refclk = 100000;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008119 const struct intel_limit *limit = &intel_limits_vlv;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008120
8121 memset(&crtc_state->dpll_hw_state, 0,
8122 sizeof(crtc_state->dpll_hw_state));
8123
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008124 if (!crtc_state->clock_set &&
8125 !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8126 refclk, NULL, &crtc_state->dpll)) {
8127 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8128 return -EINVAL;
8129 }
8130
8131 vlv_compute_dpll(crtc, crtc_state);
8132
8133 return 0;
8134}
8135
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008136static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008137 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008138{
8139 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008140 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008141 uint32_t tmp;
8142
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02008143 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
8144 return;
8145
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008146 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02008147 if (!(tmp & PFIT_ENABLE))
8148 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008149
Daniel Vetter06922822013-07-11 13:35:40 +02008150 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008151 if (INTEL_INFO(dev)->gen < 4) {
8152 if (crtc->pipe != PIPE_B)
8153 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008154 } else {
8155 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
8156 return;
8157 }
8158
Daniel Vetter06922822013-07-11 13:35:40 +02008159 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008160 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008161}
8162
Jesse Barnesacbec812013-09-20 11:29:32 -07008163static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008164 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07008165{
8166 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008167 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesacbec812013-09-20 11:29:32 -07008168 int pipe = pipe_config->cpu_transcoder;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03008169 struct dpll clock;
Jesse Barnesacbec812013-09-20 11:29:32 -07008170 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07008171 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07008172
Ville Syrjäläb5219732016-03-15 16:40:01 +02008173 /* In case of DSI, DPLL will not be used */
8174 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
Shobhit Kumarf573de52014-07-30 20:32:37 +05308175 return;
8176
Ville Syrjäläa5805162015-05-26 20:42:30 +03008177 mutex_lock(&dev_priv->sb_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08008178 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008179 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesacbec812013-09-20 11:29:32 -07008180
8181 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
8182 clock.m2 = mdiv & DPIO_M2DIV_MASK;
8183 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
8184 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
8185 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
8186
Imre Deakdccbea32015-06-22 23:35:51 +03008187 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07008188}
8189
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008190static void
8191i9xx_get_initial_plane_config(struct intel_crtc *crtc,
8192 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008193{
8194 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008195 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008196 u32 val, base, offset;
8197 int pipe = crtc->pipe, plane = crtc->plane;
8198 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008199 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008200 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008201 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008202
Damien Lespiau42a7b082015-02-05 19:35:13 +00008203 val = I915_READ(DSPCNTR(plane));
8204 if (!(val & DISPLAY_PLANE_ENABLE))
8205 return;
8206
Damien Lespiaud9806c92015-01-21 14:07:19 +00008207 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008208 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008209 DRM_DEBUG_KMS("failed to alloc fb\n");
8210 return;
8211 }
8212
Damien Lespiau1b842c82015-01-21 13:50:54 +00008213 fb = &intel_fb->base;
8214
Daniel Vetter18c52472015-02-10 17:16:09 +00008215 if (INTEL_INFO(dev)->gen >= 4) {
8216 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008217 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00008218 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8219 }
8220 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008221
8222 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00008223 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008224 fb->pixel_format = fourcc;
8225 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008226
8227 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008228 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008229 offset = I915_READ(DSPTILEOFF(plane));
8230 else
8231 offset = I915_READ(DSPLINOFF(plane));
8232 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8233 } else {
8234 base = I915_READ(DSPADDR(plane));
8235 }
8236 plane_config->base = base;
8237
8238 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008239 fb->width = ((val >> 16) & 0xfff) + 1;
8240 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008241
8242 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008243 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008244
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008245 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008246 fb->pixel_format,
8247 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008248
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008249 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008250
Damien Lespiau2844a922015-01-20 12:51:48 +00008251 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8252 pipe_name(pipe), plane, fb->width, fb->height,
8253 fb->bits_per_pixel, base, fb->pitches[0],
8254 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008255
Damien Lespiau2d140302015-02-05 17:22:18 +00008256 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008257}
8258
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008259static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008260 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008261{
8262 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008263 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008264 int pipe = pipe_config->cpu_transcoder;
8265 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03008266 struct dpll clock;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008267 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008268 int refclk = 100000;
8269
Ville Syrjäläb5219732016-03-15 16:40:01 +02008270 /* In case of DSI, DPLL will not be used */
8271 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
8272 return;
8273
Ville Syrjäläa5805162015-05-26 20:42:30 +03008274 mutex_lock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008275 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8276 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8277 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8278 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
Imre Deak0d7b6b12015-07-02 14:29:58 +03008279 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008280 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008281
8282 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008283 clock.m2 = (pll_dw0 & 0xff) << 22;
8284 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
8285 clock.m2 |= pll_dw2 & 0x3fffff;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008286 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8287 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8288 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8289
Imre Deakdccbea32015-06-22 23:35:51 +03008290 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008291}
8292
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008293static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008294 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008295{
8296 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008297 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak17290502016-02-12 18:55:11 +02008298 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008299 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02008300 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008301
Imre Deak17290502016-02-12 18:55:11 +02008302 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8303 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +02008304 return false;
8305
Daniel Vettere143a212013-07-04 12:01:15 +02008306 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008307 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02008308
Imre Deak17290502016-02-12 18:55:11 +02008309 ret = false;
8310
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008311 tmp = I915_READ(PIPECONF(crtc->pipe));
8312 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02008313 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008314
Wayne Boyer666a4532015-12-09 12:29:35 -08008315 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008316 switch (tmp & PIPECONF_BPC_MASK) {
8317 case PIPECONF_6BPC:
8318 pipe_config->pipe_bpp = 18;
8319 break;
8320 case PIPECONF_8BPC:
8321 pipe_config->pipe_bpp = 24;
8322 break;
8323 case PIPECONF_10BPC:
8324 pipe_config->pipe_bpp = 30;
8325 break;
8326 default:
8327 break;
8328 }
8329 }
8330
Wayne Boyer666a4532015-12-09 12:29:35 -08008331 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
8332 (tmp & PIPECONF_COLOR_RANGE_SELECT))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008333 pipe_config->limited_color_range = true;
8334
Ville Syrjälä282740f2013-09-04 18:30:03 +03008335 if (INTEL_INFO(dev)->gen < 4)
8336 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8337
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008338 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +02008339 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008340
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008341 i9xx_get_pfit_config(crtc, pipe_config);
8342
Daniel Vetter6c49f242013-06-06 12:45:25 +02008343 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjäläc2317752016-03-15 16:39:56 +02008344 /* No way to read it out on pipes B and C */
8345 if (IS_CHERRYVIEW(dev) && crtc->pipe != PIPE_A)
8346 tmp = dev_priv->chv_dpll_md[crtc->pipe];
8347 else
8348 tmp = I915_READ(DPLL_MD(crtc->pipe));
Daniel Vetter6c49f242013-06-06 12:45:25 +02008349 pipe_config->pixel_multiplier =
8350 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8351 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008352 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02008353 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8354 tmp = I915_READ(DPLL(crtc->pipe));
8355 pipe_config->pixel_multiplier =
8356 ((tmp & SDVO_MULTIPLIER_MASK)
8357 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8358 } else {
8359 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8360 * port and will be fixed up in the encoder->get_config
8361 * function. */
8362 pipe_config->pixel_multiplier = 1;
8363 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008364 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
Wayne Boyer666a4532015-12-09 12:29:35 -08008365 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03008366 /*
8367 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8368 * on 830. Filter it out here so that we don't
8369 * report errors due to that.
8370 */
8371 if (IS_I830(dev))
8372 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8373
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008374 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8375 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03008376 } else {
8377 /* Mask out read-only status bits. */
8378 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8379 DPLL_PORTC_READY_MASK |
8380 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008381 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02008382
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008383 if (IS_CHERRYVIEW(dev))
8384 chv_crtc_clock_get(crtc, pipe_config);
8385 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07008386 vlv_crtc_clock_get(crtc, pipe_config);
8387 else
8388 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03008389
Ville Syrjälä0f646142015-08-26 19:39:18 +03008390 /*
8391 * Normally the dotclock is filled in by the encoder .get_config()
8392 * but in case the pipe is enabled w/o any ports we need a sane
8393 * default.
8394 */
8395 pipe_config->base.adjusted_mode.crtc_clock =
8396 pipe_config->port_clock / pipe_config->pixel_multiplier;
8397
Imre Deak17290502016-02-12 18:55:11 +02008398 ret = true;
8399
8400out:
8401 intel_display_power_put(dev_priv, power_domain);
8402
8403 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008404}
8405
Paulo Zanonidde86e22012-12-01 12:04:25 -02008406static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07008407{
Chris Wilsonfac5e232016-07-04 11:34:36 +01008408 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008409 struct intel_encoder *encoder;
Lyude1c1a24d2016-06-14 11:04:09 -04008410 int i;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008411 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008412 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008413 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008414 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07008415 bool has_ck505 = false;
8416 bool can_ssc = false;
Lyude1c1a24d2016-06-14 11:04:09 -04008417 bool using_ssc_source = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008418
8419 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01008420 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07008421 switch (encoder->type) {
8422 case INTEL_OUTPUT_LVDS:
8423 has_panel = true;
8424 has_lvds = true;
8425 break;
8426 case INTEL_OUTPUT_EDP:
8427 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03008428 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07008429 has_cpu_edp = true;
8430 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008431 default:
8432 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008433 }
8434 }
8435
Keith Packard99eb6a02011-09-26 14:29:12 -07008436 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008437 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07008438 can_ssc = has_ck505;
8439 } else {
8440 has_ck505 = false;
8441 can_ssc = true;
8442 }
8443
Lyude1c1a24d2016-06-14 11:04:09 -04008444 /* Check if any DPLLs are using the SSC source */
8445 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
8446 u32 temp = I915_READ(PCH_DPLL(i));
8447
8448 if (!(temp & DPLL_VCO_ENABLE))
8449 continue;
8450
8451 if ((temp & PLL_REF_INPUT_MASK) ==
8452 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
8453 using_ssc_source = true;
8454 break;
8455 }
8456 }
8457
8458 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
8459 has_panel, has_lvds, has_ck505, using_ssc_source);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008460
8461 /* Ironlake: try to setup display ref clock before DPLL
8462 * enabling. This is only under driver's control after
8463 * PCH B stepping, previous chipset stepping should be
8464 * ignoring this setting.
8465 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008466 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008467
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008468 /* As we must carefully and slowly disable/enable each source in turn,
8469 * compute the final state we want first and check if we need to
8470 * make any changes at all.
8471 */
8472 final = val;
8473 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07008474 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008475 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07008476 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008477 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8478
Daniel Vetter8c07eb62016-06-09 18:39:07 +02008479 final &= ~DREF_SSC_SOURCE_MASK;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008480 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Daniel Vetter8c07eb62016-06-09 18:39:07 +02008481 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008482
Keith Packard199e5d72011-09-22 12:01:57 -07008483 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008484 final |= DREF_SSC_SOURCE_ENABLE;
8485
8486 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8487 final |= DREF_SSC1_ENABLE;
8488
8489 if (has_cpu_edp) {
8490 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8491 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8492 else
8493 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8494 } else
8495 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Lyude1c1a24d2016-06-14 11:04:09 -04008496 } else if (using_ssc_source) {
8497 final |= DREF_SSC_SOURCE_ENABLE;
8498 final |= DREF_SSC1_ENABLE;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008499 }
8500
8501 if (final == val)
8502 return;
8503
8504 /* Always enable nonspread source */
8505 val &= ~DREF_NONSPREAD_SOURCE_MASK;
8506
8507 if (has_ck505)
8508 val |= DREF_NONSPREAD_CK505_ENABLE;
8509 else
8510 val |= DREF_NONSPREAD_SOURCE_ENABLE;
8511
8512 if (has_panel) {
8513 val &= ~DREF_SSC_SOURCE_MASK;
8514 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008515
Keith Packard199e5d72011-09-22 12:01:57 -07008516 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07008517 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008518 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008519 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02008520 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008521 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008522
8523 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008524 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008525 POSTING_READ(PCH_DREF_CONTROL);
8526 udelay(200);
8527
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008528 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008529
8530 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07008531 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07008532 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008533 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008534 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02008535 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008536 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07008537 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008538 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008539
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008540 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008541 POSTING_READ(PCH_DREF_CONTROL);
8542 udelay(200);
8543 } else {
Lyude1c1a24d2016-06-14 11:04:09 -04008544 DRM_DEBUG_KMS("Disabling CPU source output\n");
Keith Packard199e5d72011-09-22 12:01:57 -07008545
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008546 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07008547
8548 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008549 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008550
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008551 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008552 POSTING_READ(PCH_DREF_CONTROL);
8553 udelay(200);
8554
Lyude1c1a24d2016-06-14 11:04:09 -04008555 if (!using_ssc_source) {
8556 DRM_DEBUG_KMS("Disabling SSC source\n");
Keith Packard199e5d72011-09-22 12:01:57 -07008557
Lyude1c1a24d2016-06-14 11:04:09 -04008558 /* Turn off the SSC source */
8559 val &= ~DREF_SSC_SOURCE_MASK;
8560 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008561
Lyude1c1a24d2016-06-14 11:04:09 -04008562 /* Turn off SSC1 */
8563 val &= ~DREF_SSC1_ENABLE;
8564
8565 I915_WRITE(PCH_DREF_CONTROL, val);
8566 POSTING_READ(PCH_DREF_CONTROL);
8567 udelay(200);
8568 }
Jesse Barnes13d83a62011-08-03 12:59:20 -07008569 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008570
8571 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008572}
8573
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008574static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02008575{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008576 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008577
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008578 tmp = I915_READ(SOUTH_CHICKEN2);
8579 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8580 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008581
Imre Deakcf3598c2016-06-28 13:37:31 +03008582 if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
8583 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008584 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02008585
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008586 tmp = I915_READ(SOUTH_CHICKEN2);
8587 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8588 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008589
Imre Deakcf3598c2016-06-28 13:37:31 +03008590 if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
8591 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008592 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008593}
8594
8595/* WaMPhyProgramming:hsw */
8596static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8597{
8598 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008599
8600 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8601 tmp &= ~(0xFF << 24);
8602 tmp |= (0x12 << 24);
8603 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8604
Paulo Zanonidde86e22012-12-01 12:04:25 -02008605 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8606 tmp |= (1 << 11);
8607 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8608
8609 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8610 tmp |= (1 << 11);
8611 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8612
Paulo Zanonidde86e22012-12-01 12:04:25 -02008613 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8614 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8615 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8616
8617 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8618 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8619 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8620
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008621 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8622 tmp &= ~(7 << 13);
8623 tmp |= (5 << 13);
8624 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008625
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008626 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8627 tmp &= ~(7 << 13);
8628 tmp |= (5 << 13);
8629 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008630
8631 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8632 tmp &= ~0xFF;
8633 tmp |= 0x1C;
8634 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8635
8636 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8637 tmp &= ~0xFF;
8638 tmp |= 0x1C;
8639 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8640
8641 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8642 tmp &= ~(0xFF << 16);
8643 tmp |= (0x1C << 16);
8644 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8645
8646 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8647 tmp &= ~(0xFF << 16);
8648 tmp |= (0x1C << 16);
8649 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8650
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008651 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8652 tmp |= (1 << 27);
8653 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008654
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008655 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8656 tmp |= (1 << 27);
8657 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008658
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008659 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8660 tmp &= ~(0xF << 28);
8661 tmp |= (4 << 28);
8662 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008663
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008664 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8665 tmp &= ~(0xF << 28);
8666 tmp |= (4 << 28);
8667 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008668}
8669
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008670/* Implements 3 different sequences from BSpec chapter "Display iCLK
8671 * Programming" based on the parameters passed:
8672 * - Sequence to enable CLKOUT_DP
8673 * - Sequence to enable CLKOUT_DP without spread
8674 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8675 */
8676static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8677 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008678{
Chris Wilsonfac5e232016-07-04 11:34:36 +01008679 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008680 uint32_t reg, tmp;
8681
8682 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8683 with_spread = true;
Ville Syrjäläc2699522015-08-27 23:55:59 +03008684 if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008685 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008686
Ville Syrjäläa5805162015-05-26 20:42:30 +03008687 mutex_lock(&dev_priv->sb_lock);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008688
8689 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8690 tmp &= ~SBI_SSCCTL_DISABLE;
8691 tmp |= SBI_SSCCTL_PATHALT;
8692 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8693
8694 udelay(24);
8695
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008696 if (with_spread) {
8697 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8698 tmp &= ~SBI_SSCCTL_PATHALT;
8699 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008700
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008701 if (with_fdi) {
8702 lpt_reset_fdi_mphy(dev_priv);
8703 lpt_program_fdi_mphy(dev_priv);
8704 }
8705 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02008706
Ville Syrjäläc2699522015-08-27 23:55:59 +03008707 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008708 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8709 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8710 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01008711
Ville Syrjäläa5805162015-05-26 20:42:30 +03008712 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008713}
8714
Paulo Zanoni47701c32013-07-23 11:19:25 -03008715/* Sequence to disable CLKOUT_DP */
8716static void lpt_disable_clkout_dp(struct drm_device *dev)
8717{
Chris Wilsonfac5e232016-07-04 11:34:36 +01008718 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008719 uint32_t reg, tmp;
8720
Ville Syrjäläa5805162015-05-26 20:42:30 +03008721 mutex_lock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008722
Ville Syrjäläc2699522015-08-27 23:55:59 +03008723 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni47701c32013-07-23 11:19:25 -03008724 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8725 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8726 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8727
8728 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8729 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8730 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8731 tmp |= SBI_SSCCTL_PATHALT;
8732 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8733 udelay(32);
8734 }
8735 tmp |= SBI_SSCCTL_DISABLE;
8736 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8737 }
8738
Ville Syrjäläa5805162015-05-26 20:42:30 +03008739 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008740}
8741
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008742#define BEND_IDX(steps) ((50 + (steps)) / 5)
8743
8744static const uint16_t sscdivintphase[] = {
8745 [BEND_IDX( 50)] = 0x3B23,
8746 [BEND_IDX( 45)] = 0x3B23,
8747 [BEND_IDX( 40)] = 0x3C23,
8748 [BEND_IDX( 35)] = 0x3C23,
8749 [BEND_IDX( 30)] = 0x3D23,
8750 [BEND_IDX( 25)] = 0x3D23,
8751 [BEND_IDX( 20)] = 0x3E23,
8752 [BEND_IDX( 15)] = 0x3E23,
8753 [BEND_IDX( 10)] = 0x3F23,
8754 [BEND_IDX( 5)] = 0x3F23,
8755 [BEND_IDX( 0)] = 0x0025,
8756 [BEND_IDX( -5)] = 0x0025,
8757 [BEND_IDX(-10)] = 0x0125,
8758 [BEND_IDX(-15)] = 0x0125,
8759 [BEND_IDX(-20)] = 0x0225,
8760 [BEND_IDX(-25)] = 0x0225,
8761 [BEND_IDX(-30)] = 0x0325,
8762 [BEND_IDX(-35)] = 0x0325,
8763 [BEND_IDX(-40)] = 0x0425,
8764 [BEND_IDX(-45)] = 0x0425,
8765 [BEND_IDX(-50)] = 0x0525,
8766};
8767
8768/*
8769 * Bend CLKOUT_DP
8770 * steps -50 to 50 inclusive, in steps of 5
8771 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
8772 * change in clock period = -(steps / 10) * 5.787 ps
8773 */
8774static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
8775{
8776 uint32_t tmp;
8777 int idx = BEND_IDX(steps);
8778
8779 if (WARN_ON(steps % 5 != 0))
8780 return;
8781
8782 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
8783 return;
8784
8785 mutex_lock(&dev_priv->sb_lock);
8786
8787 if (steps % 10 != 0)
8788 tmp = 0xAAAAAAAB;
8789 else
8790 tmp = 0x00000000;
8791 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
8792
8793 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
8794 tmp &= 0xffff0000;
8795 tmp |= sscdivintphase[idx];
8796 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
8797
8798 mutex_unlock(&dev_priv->sb_lock);
8799}
8800
8801#undef BEND_IDX
8802
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008803static void lpt_init_pch_refclk(struct drm_device *dev)
8804{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008805 struct intel_encoder *encoder;
8806 bool has_vga = false;
8807
Damien Lespiaub2784e12014-08-05 11:29:37 +01008808 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008809 switch (encoder->type) {
8810 case INTEL_OUTPUT_ANALOG:
8811 has_vga = true;
8812 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008813 default:
8814 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008815 }
8816 }
8817
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008818 if (has_vga) {
8819 lpt_bend_clkout_dp(to_i915(dev), 0);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008820 lpt_enable_clkout_dp(dev, true, true);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008821 } else {
Paulo Zanoni47701c32013-07-23 11:19:25 -03008822 lpt_disable_clkout_dp(dev);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008823 }
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008824}
8825
Paulo Zanonidde86e22012-12-01 12:04:25 -02008826/*
8827 * Initialize reference clocks when the driver loads
8828 */
8829void intel_init_pch_refclk(struct drm_device *dev)
8830{
8831 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8832 ironlake_init_pch_refclk(dev);
8833 else if (HAS_PCH_LPT(dev))
8834 lpt_init_pch_refclk(dev);
8835}
8836
Daniel Vetter6ff93602013-04-19 11:24:36 +02008837static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03008838{
Chris Wilsonfac5e232016-07-04 11:34:36 +01008839 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Paulo Zanonic8203562012-09-12 10:06:29 -03008840 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8841 int pipe = intel_crtc->pipe;
8842 uint32_t val;
8843
Daniel Vetter78114072013-06-13 00:54:57 +02008844 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03008845
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008846 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03008847 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008848 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008849 break;
8850 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008851 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008852 break;
8853 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008854 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008855 break;
8856 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008857 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008858 break;
8859 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03008860 /* Case prevented by intel_choose_pipe_bpp_dither. */
8861 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03008862 }
8863
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008864 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03008865 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8866
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008867 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03008868 val |= PIPECONF_INTERLACED_ILK;
8869 else
8870 val |= PIPECONF_PROGRESSIVE;
8871
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008872 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008873 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008874
Paulo Zanonic8203562012-09-12 10:06:29 -03008875 I915_WRITE(PIPECONF(pipe), val);
8876 POSTING_READ(PIPECONF(pipe));
8877}
8878
Daniel Vetter6ff93602013-04-19 11:24:36 +02008879static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008880{
Chris Wilsonfac5e232016-07-04 11:34:36 +01008881 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008882 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008883 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jani Nikula391bf042016-03-18 17:05:40 +02008884 u32 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008885
Jani Nikula391bf042016-03-18 17:05:40 +02008886 if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008887 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8888
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008889 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008890 val |= PIPECONF_INTERLACED_ILK;
8891 else
8892 val |= PIPECONF_PROGRESSIVE;
8893
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008894 I915_WRITE(PIPECONF(cpu_transcoder), val);
8895 POSTING_READ(PIPECONF(cpu_transcoder));
Jani Nikula391bf042016-03-18 17:05:40 +02008896}
8897
Jani Nikula391bf042016-03-18 17:05:40 +02008898static void haswell_set_pipemisc(struct drm_crtc *crtc)
8899{
Chris Wilsonfac5e232016-07-04 11:34:36 +01008900 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Jani Nikula391bf042016-03-18 17:05:40 +02008901 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8902
8903 if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
8904 u32 val = 0;
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008905
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008906 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008907 case 18:
8908 val |= PIPEMISC_DITHER_6_BPC;
8909 break;
8910 case 24:
8911 val |= PIPEMISC_DITHER_8_BPC;
8912 break;
8913 case 30:
8914 val |= PIPEMISC_DITHER_10_BPC;
8915 break;
8916 case 36:
8917 val |= PIPEMISC_DITHER_12_BPC;
8918 break;
8919 default:
8920 /* Case prevented by pipe_config_set_bpp. */
8921 BUG();
8922 }
8923
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008924 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008925 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8926
Jani Nikula391bf042016-03-18 17:05:40 +02008927 I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008928 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008929}
8930
Paulo Zanonid4b19312012-11-29 11:29:32 -02008931int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8932{
8933 /*
8934 * Account for spread spectrum to avoid
8935 * oversubscribing the link. Max center spread
8936 * is 2.5%; use 5% for safety's sake.
8937 */
8938 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02008939 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02008940}
8941
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008942static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02008943{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008944 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03008945}
8946
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008947static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
8948 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03008949 struct dpll *reduced_clock)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008950{
8951 struct drm_crtc *crtc = &intel_crtc->base;
8952 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008953 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008954 u32 dpll, fp, fp2;
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03008955 int factor;
Jesse Barnes79e53942008-11-07 14:24:08 -08008956
Chris Wilsonc1858122010-12-03 21:35:48 +00008957 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07008958 factor = 21;
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03008959 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Eric Anholt8febb292011-03-30 13:01:07 -07008960 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008961 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02008962 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07008963 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008964 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07008965 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00008966
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008967 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Chris Wilsonc1858122010-12-03 21:35:48 +00008968
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008969 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
8970 fp |= FP_CB_TUNE;
8971
8972 if (reduced_clock) {
8973 fp2 = i9xx_dpll_compute_fp(reduced_clock);
8974
8975 if (reduced_clock->m < factor * reduced_clock->n)
8976 fp2 |= FP_CB_TUNE;
8977 } else {
8978 fp2 = fp;
8979 }
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008980
Chris Wilson5eddb702010-09-11 13:48:45 +01008981 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08008982
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03008983 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
Eric Anholta07d6782011-03-30 13:01:08 -07008984 dpll |= DPLLB_MODE_LVDS;
8985 else
8986 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008987
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008988 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02008989 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008990
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03008991 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
8992 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
Daniel Vetter4a33e482013-07-06 12:52:05 +02008993 dpll |= DPLL_SDVO_HIGH_SPEED;
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03008994
Ville Syrjälä37a56502016-06-22 21:57:04 +03008995 if (intel_crtc_has_dp_encoder(crtc_state))
Daniel Vetter4a33e482013-07-06 12:52:05 +02008996 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08008997
Eric Anholta07d6782011-03-30 13:01:08 -07008998 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008999 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07009000 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009001 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07009002
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009003 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07009004 case 5:
9005 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
9006 break;
9007 case 7:
9008 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
9009 break;
9010 case 10:
9011 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
9012 break;
9013 case 14:
9014 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
9015 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08009016 }
9017
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03009018 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
9019 intel_panel_use_ssc(dev_priv))
Kristian Høgsberg43565a02009-02-13 20:56:52 -05009020 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08009021 else
9022 dpll |= PLL_REF_INPUT_DREFCLK;
9023
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02009024 dpll |= DPLL_VCO_ENABLE;
9025
9026 crtc_state->dpll_hw_state.dpll = dpll;
9027 crtc_state->dpll_hw_state.fp0 = fp;
9028 crtc_state->dpll_hw_state.fp1 = fp2;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03009029}
9030
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009031static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
9032 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08009033{
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02009034 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009035 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03009036 struct dpll reduced_clock;
Ander Conselvan de Oliveira7ed9f892016-03-21 18:00:07 +02009037 bool has_reduced_clock = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02009038 struct intel_shared_dpll *pll;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03009039 const struct intel_limit *limit;
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02009040 int refclk = 120000;
Jesse Barnes79e53942008-11-07 14:24:08 -08009041
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03009042 memset(&crtc_state->dpll_hw_state, 0,
9043 sizeof(crtc_state->dpll_hw_state));
9044
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02009045 crtc->lowfreq_avail = false;
9046
9047 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
9048 if (!crtc_state->has_pch_encoder)
9049 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009050
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03009051 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02009052 if (intel_panel_use_ssc(dev_priv)) {
9053 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
9054 dev_priv->vbt.lvds_ssc_freq);
9055 refclk = dev_priv->vbt.lvds_ssc_freq;
9056 }
9057
9058 if (intel_is_dual_link_lvds(dev)) {
9059 if (refclk == 100000)
9060 limit = &intel_limits_ironlake_dual_lvds_100m;
9061 else
9062 limit = &intel_limits_ironlake_dual_lvds;
9063 } else {
9064 if (refclk == 100000)
9065 limit = &intel_limits_ironlake_single_lvds_100m;
9066 else
9067 limit = &intel_limits_ironlake_single_lvds;
9068 }
9069 } else {
9070 limit = &intel_limits_ironlake_dac;
9071 }
9072
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02009073 if (!crtc_state->clock_set &&
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02009074 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
9075 refclk, NULL, &crtc_state->dpll)) {
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02009076 DRM_ERROR("Couldn't find PLL settings for mode!\n");
9077 return -EINVAL;
Daniel Vetterf47709a2013-03-28 10:42:02 +01009078 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009079
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02009080 ironlake_compute_dpll(crtc, crtc_state,
9081 has_reduced_clock ? &reduced_clock : NULL);
Daniel Vetter66e985c2013-06-05 13:34:20 +02009082
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02009083 pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
9084 if (pll == NULL) {
9085 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
9086 pipe_name(crtc->pipe));
9087 return -EINVAL;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02009088 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009089
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03009090 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02009091 has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009092 crtc->lowfreq_avail = true;
Daniel Vettere2b78262013-06-07 23:10:03 +02009093
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009094 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009095}
9096
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009097static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
9098 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02009099{
9100 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009101 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009102 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02009103
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009104 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
9105 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
9106 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
9107 & ~TU_SIZE_MASK;
9108 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
9109 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
9110 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9111}
9112
9113static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
9114 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009115 struct intel_link_m_n *m_n,
9116 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009117{
9118 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009119 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009120 enum pipe pipe = crtc->pipe;
9121
9122 if (INTEL_INFO(dev)->gen >= 5) {
9123 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
9124 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
9125 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
9126 & ~TU_SIZE_MASK;
9127 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
9128 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
9129 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009130 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
9131 * gen < 8) and if DRRS is supported (to make sure the
9132 * registers are not unnecessarily read).
9133 */
9134 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009135 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009136 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
9137 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
9138 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
9139 & ~TU_SIZE_MASK;
9140 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
9141 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
9142 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9143 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009144 } else {
9145 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
9146 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
9147 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
9148 & ~TU_SIZE_MASK;
9149 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
9150 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
9151 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9152 }
9153}
9154
9155void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009156 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009157{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02009158 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009159 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
9160 else
9161 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009162 &pipe_config->dp_m_n,
9163 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009164}
9165
Daniel Vetter72419202013-04-04 13:28:53 +02009166static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009167 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02009168{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009169 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009170 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02009171}
9172
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009173static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009174 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009175{
9176 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009177 struct drm_i915_private *dev_priv = to_i915(dev);
Chandra Kondurua1b22782015-04-07 15:28:45 -07009178 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
9179 uint32_t ps_ctrl = 0;
9180 int id = -1;
9181 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009182
Chandra Kondurua1b22782015-04-07 15:28:45 -07009183 /* find scaler attached to this pipe */
9184 for (i = 0; i < crtc->num_scalers; i++) {
9185 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
9186 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
9187 id = i;
9188 pipe_config->pch_pfit.enabled = true;
9189 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9190 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9191 break;
9192 }
9193 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009194
Chandra Kondurua1b22782015-04-07 15:28:45 -07009195 scaler_state->scaler_id = id;
9196 if (id >= 0) {
9197 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9198 } else {
9199 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009200 }
9201}
9202
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009203static void
9204skylake_get_initial_plane_config(struct intel_crtc *crtc,
9205 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009206{
9207 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009208 struct drm_i915_private *dev_priv = to_i915(dev);
Damien Lespiau40f46282015-02-27 11:15:21 +00009209 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009210 int pipe = crtc->pipe;
9211 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009212 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009213 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009214 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009215
Damien Lespiaud9806c92015-01-21 14:07:19 +00009216 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009217 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009218 DRM_DEBUG_KMS("failed to alloc fb\n");
9219 return;
9220 }
9221
Damien Lespiau1b842c82015-01-21 13:50:54 +00009222 fb = &intel_fb->base;
9223
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009224 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00009225 if (!(val & PLANE_CTL_ENABLE))
9226 goto error;
9227
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009228 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9229 fourcc = skl_format_to_fourcc(pixel_format,
9230 val & PLANE_CTL_ORDER_RGBX,
9231 val & PLANE_CTL_ALPHA_MASK);
9232 fb->pixel_format = fourcc;
9233 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9234
Damien Lespiau40f46282015-02-27 11:15:21 +00009235 tiling = val & PLANE_CTL_TILED_MASK;
9236 switch (tiling) {
9237 case PLANE_CTL_TILED_LINEAR:
9238 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9239 break;
9240 case PLANE_CTL_TILED_X:
9241 plane_config->tiling = I915_TILING_X;
9242 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9243 break;
9244 case PLANE_CTL_TILED_Y:
9245 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9246 break;
9247 case PLANE_CTL_TILED_YF:
9248 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9249 break;
9250 default:
9251 MISSING_CASE(tiling);
9252 goto error;
9253 }
9254
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009255 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9256 plane_config->base = base;
9257
9258 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9259
9260 val = I915_READ(PLANE_SIZE(pipe, 0));
9261 fb->height = ((val >> 16) & 0xfff) + 1;
9262 fb->width = ((val >> 0) & 0x1fff) + 1;
9263
9264 val = I915_READ(PLANE_STRIDE(pipe, 0));
Ville Syrjälä7b49f942016-01-12 21:08:32 +02009265 stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
Damien Lespiau40f46282015-02-27 11:15:21 +00009266 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009267 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9268
9269 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009270 fb->pixel_format,
9271 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009272
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009273 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009274
9275 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9276 pipe_name(pipe), fb->width, fb->height,
9277 fb->bits_per_pixel, base, fb->pitches[0],
9278 plane_config->size);
9279
Damien Lespiau2d140302015-02-05 17:22:18 +00009280 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009281 return;
9282
9283error:
9284 kfree(fb);
9285}
9286
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009287static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009288 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009289{
9290 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009291 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009292 uint32_t tmp;
9293
9294 tmp = I915_READ(PF_CTL(crtc->pipe));
9295
9296 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009297 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009298 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9299 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02009300
9301 /* We currently do not free assignements of panel fitters on
9302 * ivb/hsw (since we don't use the higher upscaling modes which
9303 * differentiates them) so just WARN about this case for now. */
9304 if (IS_GEN7(dev)) {
9305 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9306 PF_PIPE_SEL_IVB(crtc->pipe));
9307 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009308 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009309}
9310
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009311static void
9312ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9313 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009314{
9315 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009316 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009317 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009318 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009319 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009320 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009321 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009322 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009323
Damien Lespiau42a7b082015-02-05 19:35:13 +00009324 val = I915_READ(DSPCNTR(pipe));
9325 if (!(val & DISPLAY_PLANE_ENABLE))
9326 return;
9327
Damien Lespiaud9806c92015-01-21 14:07:19 +00009328 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009329 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009330 DRM_DEBUG_KMS("failed to alloc fb\n");
9331 return;
9332 }
9333
Damien Lespiau1b842c82015-01-21 13:50:54 +00009334 fb = &intel_fb->base;
9335
Daniel Vetter18c52472015-02-10 17:16:09 +00009336 if (INTEL_INFO(dev)->gen >= 4) {
9337 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00009338 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00009339 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9340 }
9341 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009342
9343 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00009344 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009345 fb->pixel_format = fourcc;
9346 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009347
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009348 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009349 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009350 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009351 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00009352 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009353 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009354 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009355 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009356 }
9357 plane_config->base = base;
9358
9359 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009360 fb->width = ((val >> 16) & 0xfff) + 1;
9361 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009362
9363 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009364 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009365
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009366 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009367 fb->pixel_format,
9368 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009369
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009370 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009371
Damien Lespiau2844a922015-01-20 12:51:48 +00009372 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9373 pipe_name(pipe), fb->width, fb->height,
9374 fb->bits_per_pixel, base, fb->pitches[0],
9375 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009376
Damien Lespiau2d140302015-02-05 17:22:18 +00009377 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009378}
9379
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009380static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009381 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009382{
9383 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009384 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak17290502016-02-12 18:55:11 +02009385 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009386 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02009387 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009388
Imre Deak17290502016-02-12 18:55:11 +02009389 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9390 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03009391 return false;
9392
Daniel Vettere143a212013-07-04 12:01:15 +02009393 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009394 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02009395
Imre Deak17290502016-02-12 18:55:11 +02009396 ret = false;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009397 tmp = I915_READ(PIPECONF(crtc->pipe));
9398 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02009399 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009400
Ville Syrjälä42571ae2013-09-06 23:29:00 +03009401 switch (tmp & PIPECONF_BPC_MASK) {
9402 case PIPECONF_6BPC:
9403 pipe_config->pipe_bpp = 18;
9404 break;
9405 case PIPECONF_8BPC:
9406 pipe_config->pipe_bpp = 24;
9407 break;
9408 case PIPECONF_10BPC:
9409 pipe_config->pipe_bpp = 30;
9410 break;
9411 case PIPECONF_12BPC:
9412 pipe_config->pipe_bpp = 36;
9413 break;
9414 default:
9415 break;
9416 }
9417
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02009418 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9419 pipe_config->limited_color_range = true;
9420
Daniel Vetterab9412b2013-05-03 11:49:46 +02009421 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02009422 struct intel_shared_dpll *pll;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009423 enum intel_dpll_id pll_id;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009424
Daniel Vetter88adfff2013-03-28 10:42:01 +01009425 pipe_config->has_pch_encoder = true;
9426
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009427 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9428 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9429 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02009430
9431 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009432
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009433 if (HAS_PCH_IBX(dev_priv)) {
Imre Deakd9a7bc62016-05-12 16:18:50 +03009434 /*
9435 * The pipe->pch transcoder and pch transcoder->pll
9436 * mapping is fixed.
9437 */
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009438 pll_id = (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009439 } else {
9440 tmp = I915_READ(PCH_DPLL_SEL);
9441 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009442 pll_id = DPLL_ID_PCH_PLL_B;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009443 else
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009444 pll_id= DPLL_ID_PCH_PLL_A;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009445 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02009446
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009447 pipe_config->shared_dpll =
9448 intel_get_shared_dpll_by_id(dev_priv, pll_id);
9449 pll = pipe_config->shared_dpll;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009450
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +02009451 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9452 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02009453
9454 tmp = pipe_config->dpll_hw_state.dpll;
9455 pipe_config->pixel_multiplier =
9456 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9457 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03009458
9459 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009460 } else {
9461 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009462 }
9463
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009464 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +02009465 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009466
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009467 ironlake_get_pfit_config(crtc, pipe_config);
9468
Imre Deak17290502016-02-12 18:55:11 +02009469 ret = true;
9470
9471out:
9472 intel_display_power_put(dev_priv, power_domain);
9473
9474 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009475}
9476
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009477static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9478{
Chris Wilson91c8a322016-07-05 10:40:23 +01009479 struct drm_device *dev = &dev_priv->drm;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009480 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009481
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009482 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05009483 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009484 pipe_name(crtc->pipe));
9485
Rob Clarke2c719b2014-12-15 13:56:32 -05009486 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9487 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
Ville Syrjälä01403de2015-09-18 20:03:33 +03009488 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9489 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009490 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9491 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009492 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03009493 if (IS_HASWELL(dev))
Rob Clarke2c719b2014-12-15 13:56:32 -05009494 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03009495 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009496 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009497 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009498 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009499 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009500 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009501
Paulo Zanoni9926ada2014-04-01 19:39:47 -03009502 /*
9503 * In theory we can still leave IRQs enabled, as long as only the HPD
9504 * interrupts remain enabled. We used to check for that, but since it's
9505 * gen-specific and since we only disable LCPLL after we fully disable
9506 * the interrupts, the check below should be enough.
9507 */
Rob Clarke2c719b2014-12-15 13:56:32 -05009508 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009509}
9510
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009511static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9512{
Chris Wilson91c8a322016-07-05 10:40:23 +01009513 struct drm_device *dev = &dev_priv->drm;
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009514
9515 if (IS_HASWELL(dev))
9516 return I915_READ(D_COMP_HSW);
9517 else
9518 return I915_READ(D_COMP_BDW);
9519}
9520
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009521static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9522{
Chris Wilson91c8a322016-07-05 10:40:23 +01009523 struct drm_device *dev = &dev_priv->drm;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009524
9525 if (IS_HASWELL(dev)) {
9526 mutex_lock(&dev_priv->rps.hw_lock);
9527 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9528 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03009529 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009530 mutex_unlock(&dev_priv->rps.hw_lock);
9531 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009532 I915_WRITE(D_COMP_BDW, val);
9533 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009534 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009535}
9536
9537/*
9538 * This function implements pieces of two sequences from BSpec:
9539 * - Sequence for display software to disable LCPLL
9540 * - Sequence for display software to allow package C8+
9541 * The steps implemented here are just the steps that actually touch the LCPLL
9542 * register. Callers should take care of disabling all the display engine
9543 * functions, doing the mode unset, fixing interrupts, etc.
9544 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009545static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9546 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009547{
9548 uint32_t val;
9549
9550 assert_can_disable_lcpll(dev_priv);
9551
9552 val = I915_READ(LCPLL_CTL);
9553
9554 if (switch_to_fclk) {
9555 val |= LCPLL_CD_SOURCE_FCLK;
9556 I915_WRITE(LCPLL_CTL, val);
9557
Imre Deakf53dd632016-06-28 13:37:32 +03009558 if (wait_for_us(I915_READ(LCPLL_CTL) &
9559 LCPLL_CD_SOURCE_FCLK_DONE, 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009560 DRM_ERROR("Switching to FCLK failed\n");
9561
9562 val = I915_READ(LCPLL_CTL);
9563 }
9564
9565 val |= LCPLL_PLL_DISABLE;
9566 I915_WRITE(LCPLL_CTL, val);
9567 POSTING_READ(LCPLL_CTL);
9568
Chris Wilson24d84412016-06-30 15:33:07 +01009569 if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009570 DRM_ERROR("LCPLL still locked\n");
9571
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009572 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009573 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009574 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009575 ndelay(100);
9576
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009577 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9578 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009579 DRM_ERROR("D_COMP RCOMP still in progress\n");
9580
9581 if (allow_power_down) {
9582 val = I915_READ(LCPLL_CTL);
9583 val |= LCPLL_POWER_DOWN_ALLOW;
9584 I915_WRITE(LCPLL_CTL, val);
9585 POSTING_READ(LCPLL_CTL);
9586 }
9587}
9588
9589/*
9590 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9591 * source.
9592 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009593static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009594{
9595 uint32_t val;
9596
9597 val = I915_READ(LCPLL_CTL);
9598
9599 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9600 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9601 return;
9602
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009603 /*
9604 * Make sure we're not on PC8 state before disabling PC8, otherwise
9605 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009606 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02009607 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03009608
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009609 if (val & LCPLL_POWER_DOWN_ALLOW) {
9610 val &= ~LCPLL_POWER_DOWN_ALLOW;
9611 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02009612 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009613 }
9614
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009615 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009616 val |= D_COMP_COMP_FORCE;
9617 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009618 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009619
9620 val = I915_READ(LCPLL_CTL);
9621 val &= ~LCPLL_PLL_DISABLE;
9622 I915_WRITE(LCPLL_CTL, val);
9623
Chris Wilson93220c02016-06-30 15:33:08 +01009624 if (intel_wait_for_register(dev_priv,
9625 LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
9626 5))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009627 DRM_ERROR("LCPLL not locked yet\n");
9628
9629 if (val & LCPLL_CD_SOURCE_FCLK) {
9630 val = I915_READ(LCPLL_CTL);
9631 val &= ~LCPLL_CD_SOURCE_FCLK;
9632 I915_WRITE(LCPLL_CTL, val);
9633
Imre Deakf53dd632016-06-28 13:37:32 +03009634 if (wait_for_us((I915_READ(LCPLL_CTL) &
9635 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009636 DRM_ERROR("Switching back to LCPLL failed\n");
9637 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03009638
Mika Kuoppala59bad942015-01-16 11:34:40 +02009639 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Chris Wilson91c8a322016-07-05 10:40:23 +01009640 intel_update_cdclk(&dev_priv->drm);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009641}
9642
Paulo Zanoni765dab672014-03-07 20:08:18 -03009643/*
9644 * Package states C8 and deeper are really deep PC states that can only be
9645 * reached when all the devices on the system allow it, so even if the graphics
9646 * device allows PC8+, it doesn't mean the system will actually get to these
9647 * states. Our driver only allows PC8+ when going into runtime PM.
9648 *
9649 * The requirements for PC8+ are that all the outputs are disabled, the power
9650 * well is disabled and most interrupts are disabled, and these are also
9651 * requirements for runtime PM. When these conditions are met, we manually do
9652 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9653 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9654 * hang the machine.
9655 *
9656 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9657 * the state of some registers, so when we come back from PC8+ we need to
9658 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9659 * need to take care of the registers kept by RC6. Notice that this happens even
9660 * if we don't put the device in PCI D3 state (which is what currently happens
9661 * because of the runtime PM support).
9662 *
9663 * For more, read "Display Sequences for Package C8" on the hardware
9664 * documentation.
9665 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009666void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009667{
Chris Wilson91c8a322016-07-05 10:40:23 +01009668 struct drm_device *dev = &dev_priv->drm;
Paulo Zanonic67a4702013-08-19 13:18:09 -03009669 uint32_t val;
9670
Paulo Zanonic67a4702013-08-19 13:18:09 -03009671 DRM_DEBUG_KMS("Enabling package C8+\n");
9672
Ville Syrjäläc2699522015-08-27 23:55:59 +03009673 if (HAS_PCH_LPT_LP(dev)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009674 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9675 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9676 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9677 }
9678
9679 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009680 hsw_disable_lcpll(dev_priv, true, true);
9681}
9682
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009683void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009684{
Chris Wilson91c8a322016-07-05 10:40:23 +01009685 struct drm_device *dev = &dev_priv->drm;
Paulo Zanonic67a4702013-08-19 13:18:09 -03009686 uint32_t val;
9687
Paulo Zanonic67a4702013-08-19 13:18:09 -03009688 DRM_DEBUG_KMS("Disabling package C8+\n");
9689
9690 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009691 lpt_init_pch_refclk(dev);
9692
Ville Syrjäläc2699522015-08-27 23:55:59 +03009693 if (HAS_PCH_LPT_LP(dev)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009694 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9695 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9696 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9697 }
Paulo Zanonic67a4702013-08-19 13:18:09 -03009698}
9699
Imre Deak324513c2016-06-13 16:44:36 +03009700static void bxt_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309701{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009702 struct drm_device *dev = old_state->dev;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009703 struct intel_atomic_state *old_intel_state =
9704 to_intel_atomic_state(old_state);
9705 unsigned int req_cdclk = old_intel_state->dev_cdclk;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309706
Imre Deak324513c2016-06-13 16:44:36 +03009707 bxt_set_cdclk(to_i915(dev), req_cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309708}
9709
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009710/* compute the max rate for new configuration */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009711static int ilk_max_pixel_rate(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009712{
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009713 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Chris Wilsonfac5e232016-07-04 11:34:36 +01009714 struct drm_i915_private *dev_priv = to_i915(state->dev);
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009715 struct drm_crtc *crtc;
9716 struct drm_crtc_state *cstate;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009717 struct intel_crtc_state *crtc_state;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009718 unsigned max_pixel_rate = 0, i;
9719 enum pipe pipe;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009720
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009721 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
9722 sizeof(intel_state->min_pixclk));
9723
9724 for_each_crtc_in_state(state, crtc, cstate, i) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009725 int pixel_rate;
9726
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009727 crtc_state = to_intel_crtc_state(cstate);
9728 if (!crtc_state->base.enable) {
9729 intel_state->min_pixclk[i] = 0;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009730 continue;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009731 }
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009732
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009733 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009734
9735 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009736 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009737 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9738
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009739 intel_state->min_pixclk[i] = pixel_rate;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009740 }
9741
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009742 for_each_pipe(dev_priv, pipe)
9743 max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
9744
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009745 return max_pixel_rate;
9746}
9747
9748static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9749{
Chris Wilsonfac5e232016-07-04 11:34:36 +01009750 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009751 uint32_t val, data;
9752 int ret;
9753
9754 if (WARN((I915_READ(LCPLL_CTL) &
9755 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9756 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9757 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9758 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9759 "trying to change cdclk frequency with cdclk not enabled\n"))
9760 return;
9761
9762 mutex_lock(&dev_priv->rps.hw_lock);
9763 ret = sandybridge_pcode_write(dev_priv,
9764 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9765 mutex_unlock(&dev_priv->rps.hw_lock);
9766 if (ret) {
9767 DRM_ERROR("failed to inform pcode about cdclk change\n");
9768 return;
9769 }
9770
9771 val = I915_READ(LCPLL_CTL);
9772 val |= LCPLL_CD_SOURCE_FCLK;
9773 I915_WRITE(LCPLL_CTL, val);
9774
Tvrtko Ursulin5ba00172016-03-03 14:36:45 +00009775 if (wait_for_us(I915_READ(LCPLL_CTL) &
9776 LCPLL_CD_SOURCE_FCLK_DONE, 1))
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009777 DRM_ERROR("Switching to FCLK failed\n");
9778
9779 val = I915_READ(LCPLL_CTL);
9780 val &= ~LCPLL_CLK_FREQ_MASK;
9781
9782 switch (cdclk) {
9783 case 450000:
9784 val |= LCPLL_CLK_FREQ_450;
9785 data = 0;
9786 break;
9787 case 540000:
9788 val |= LCPLL_CLK_FREQ_54O_BDW;
9789 data = 1;
9790 break;
9791 case 337500:
9792 val |= LCPLL_CLK_FREQ_337_5_BDW;
9793 data = 2;
9794 break;
9795 case 675000:
9796 val |= LCPLL_CLK_FREQ_675_BDW;
9797 data = 3;
9798 break;
9799 default:
9800 WARN(1, "invalid cdclk frequency\n");
9801 return;
9802 }
9803
9804 I915_WRITE(LCPLL_CTL, val);
9805
9806 val = I915_READ(LCPLL_CTL);
9807 val &= ~LCPLL_CD_SOURCE_FCLK;
9808 I915_WRITE(LCPLL_CTL, val);
9809
Tvrtko Ursulin5ba00172016-03-03 14:36:45 +00009810 if (wait_for_us((I915_READ(LCPLL_CTL) &
9811 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009812 DRM_ERROR("Switching back to LCPLL failed\n");
9813
9814 mutex_lock(&dev_priv->rps.hw_lock);
9815 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9816 mutex_unlock(&dev_priv->rps.hw_lock);
9817
Ville Syrjälä7f1052a2016-04-26 19:46:32 +03009818 I915_WRITE(CDCLK_FREQ, DIV_ROUND_CLOSEST(cdclk, 1000) - 1);
9819
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009820 intel_update_cdclk(dev);
9821
9822 WARN(cdclk != dev_priv->cdclk_freq,
9823 "cdclk requested %d kHz but got %d kHz\n",
9824 cdclk, dev_priv->cdclk_freq);
9825}
9826
Ville Syrjälä587c7912016-05-11 22:44:41 +03009827static int broadwell_calc_cdclk(int max_pixclk)
9828{
9829 if (max_pixclk > 540000)
9830 return 675000;
9831 else if (max_pixclk > 450000)
9832 return 540000;
9833 else if (max_pixclk > 337500)
9834 return 450000;
9835 else
9836 return 337500;
9837}
9838
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009839static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009840{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009841 struct drm_i915_private *dev_priv = to_i915(state->dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009842 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009843 int max_pixclk = ilk_max_pixel_rate(state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009844 int cdclk;
9845
9846 /*
9847 * FIXME should also account for plane ratio
9848 * once 64bpp pixel formats are supported.
9849 */
Ville Syrjälä587c7912016-05-11 22:44:41 +03009850 cdclk = broadwell_calc_cdclk(max_pixclk);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009851
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009852 if (cdclk > dev_priv->max_cdclk_freq) {
Maarten Lankhorst63ba5342015-11-24 11:29:03 +01009853 DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9854 cdclk, dev_priv->max_cdclk_freq);
9855 return -EINVAL;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009856 }
9857
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009858 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
9859 if (!intel_state->active_crtcs)
Ville Syrjälä587c7912016-05-11 22:44:41 +03009860 intel_state->dev_cdclk = broadwell_calc_cdclk(0);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009861
9862 return 0;
9863}
9864
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009865static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009866{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009867 struct drm_device *dev = old_state->dev;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009868 struct intel_atomic_state *old_intel_state =
9869 to_intel_atomic_state(old_state);
9870 unsigned req_cdclk = old_intel_state->dev_cdclk;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009871
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009872 broadwell_set_cdclk(dev, req_cdclk);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009873}
9874
Clint Taylorc89e39f2016-05-13 23:41:21 +03009875static int skl_modeset_calc_cdclk(struct drm_atomic_state *state)
9876{
9877 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
9878 struct drm_i915_private *dev_priv = to_i915(state->dev);
9879 const int max_pixclk = ilk_max_pixel_rate(state);
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03009880 int vco = intel_state->cdclk_pll_vco;
Clint Taylorc89e39f2016-05-13 23:41:21 +03009881 int cdclk;
9882
9883 /*
9884 * FIXME should also account for plane ratio
9885 * once 64bpp pixel formats are supported.
9886 */
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03009887 cdclk = skl_calc_cdclk(max_pixclk, vco);
Clint Taylorc89e39f2016-05-13 23:41:21 +03009888
9889 /*
9890 * FIXME move the cdclk caclulation to
9891 * compute_config() so we can fail gracegully.
9892 */
9893 if (cdclk > dev_priv->max_cdclk_freq) {
9894 DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9895 cdclk, dev_priv->max_cdclk_freq);
9896 cdclk = dev_priv->max_cdclk_freq;
9897 }
9898
9899 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
9900 if (!intel_state->active_crtcs)
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03009901 intel_state->dev_cdclk = skl_calc_cdclk(0, vco);
Clint Taylorc89e39f2016-05-13 23:41:21 +03009902
9903 return 0;
9904}
9905
9906static void skl_modeset_commit_cdclk(struct drm_atomic_state *old_state)
9907{
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03009908 struct drm_i915_private *dev_priv = to_i915(old_state->dev);
9909 struct intel_atomic_state *intel_state = to_intel_atomic_state(old_state);
9910 unsigned int req_cdclk = intel_state->dev_cdclk;
9911 unsigned int req_vco = intel_state->cdclk_pll_vco;
Clint Taylorc89e39f2016-05-13 23:41:21 +03009912
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03009913 skl_set_cdclk(dev_priv, req_cdclk, req_vco);
Clint Taylorc89e39f2016-05-13 23:41:21 +03009914}
9915
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009916static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9917 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009918{
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03009919 if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
Mika Kaholaaf3997b2016-02-05 13:29:28 +02009920 if (!intel_ddi_pll_select(crtc, crtc_state))
9921 return -EINVAL;
9922 }
Daniel Vetter716c2e52014-06-25 22:02:02 +03009923
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009924 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02009925
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009926 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009927}
9928
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309929static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9930 enum port port,
9931 struct intel_crtc_state *pipe_config)
9932{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009933 enum intel_dpll_id id;
9934
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309935 switch (port) {
9936 case PORT_A:
9937 pipe_config->ddi_pll_sel = SKL_DPLL0;
Imre Deak08250c42016-03-14 19:55:34 +02009938 id = DPLL_ID_SKL_DPLL0;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309939 break;
9940 case PORT_B:
9941 pipe_config->ddi_pll_sel = SKL_DPLL1;
Imre Deak08250c42016-03-14 19:55:34 +02009942 id = DPLL_ID_SKL_DPLL1;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309943 break;
9944 case PORT_C:
9945 pipe_config->ddi_pll_sel = SKL_DPLL2;
Imre Deak08250c42016-03-14 19:55:34 +02009946 id = DPLL_ID_SKL_DPLL2;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309947 break;
9948 default:
9949 DRM_ERROR("Incorrect port type\n");
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009950 return;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309951 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009952
9953 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309954}
9955
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009956static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9957 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009958 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009959{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009960 enum intel_dpll_id id;
Ander Conselvan de Oliveiraa3c988e2016-03-08 17:46:27 +02009961 u32 temp;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009962
9963 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9964 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9965
9966 switch (pipe_config->ddi_pll_sel) {
Damien Lespiau3148ade2014-11-21 16:14:56 +00009967 case SKL_DPLL0:
Ander Conselvan de Oliveiraa3c988e2016-03-08 17:46:27 +02009968 id = DPLL_ID_SKL_DPLL0;
9969 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009970 case SKL_DPLL1:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009971 id = DPLL_ID_SKL_DPLL1;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009972 break;
9973 case SKL_DPLL2:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009974 id = DPLL_ID_SKL_DPLL2;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009975 break;
9976 case SKL_DPLL3:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009977 id = DPLL_ID_SKL_DPLL3;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009978 break;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009979 default:
9980 MISSING_CASE(pipe_config->ddi_pll_sel);
9981 return;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009982 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009983
9984 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009985}
9986
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009987static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9988 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009989 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009990{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009991 enum intel_dpll_id id;
9992
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009993 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9994
9995 switch (pipe_config->ddi_pll_sel) {
9996 case PORT_CLK_SEL_WRPLL1:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009997 id = DPLL_ID_WRPLL1;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009998 break;
9999 case PORT_CLK_SEL_WRPLL2:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010000 id = DPLL_ID_WRPLL2;
Damien Lespiau7d2c8172014-07-29 18:06:18 +010010001 break;
Maarten Lankhorst00490c22015-11-16 14:42:12 +010010002 case PORT_CLK_SEL_SPLL:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010003 id = DPLL_ID_SPLL;
Ville Syrjälä79bd23d2015-12-01 23:32:07 +020010004 break;
Ander Conselvan de Oliveira9d16da62016-03-08 17:46:26 +020010005 case PORT_CLK_SEL_LCPLL_810:
10006 id = DPLL_ID_LCPLL_810;
10007 break;
10008 case PORT_CLK_SEL_LCPLL_1350:
10009 id = DPLL_ID_LCPLL_1350;
10010 break;
10011 case PORT_CLK_SEL_LCPLL_2700:
10012 id = DPLL_ID_LCPLL_2700;
10013 break;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010014 default:
10015 MISSING_CASE(pipe_config->ddi_pll_sel);
10016 /* fall through */
10017 case PORT_CLK_SEL_NONE:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010018 return;
Damien Lespiau7d2c8172014-07-29 18:06:18 +010010019 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010020
10021 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Damien Lespiau7d2c8172014-07-29 18:06:18 +010010022}
10023
Jani Nikulacf304292016-03-18 17:05:41 +020010024static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
10025 struct intel_crtc_state *pipe_config,
10026 unsigned long *power_domain_mask)
10027{
10028 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010029 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikulacf304292016-03-18 17:05:41 +020010030 enum intel_display_power_domain power_domain;
10031 u32 tmp;
10032
Imre Deakd9a7bc62016-05-12 16:18:50 +030010033 /*
10034 * The pipe->transcoder mapping is fixed with the exception of the eDP
10035 * transcoder handled below.
10036 */
Jani Nikulacf304292016-03-18 17:05:41 +020010037 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
10038
10039 /*
10040 * XXX: Do intel_display_power_get_if_enabled before reading this (for
10041 * consistency and less surprising code; it's in always on power).
10042 */
10043 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
10044 if (tmp & TRANS_DDI_FUNC_ENABLE) {
10045 enum pipe trans_edp_pipe;
10046 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
10047 default:
10048 WARN(1, "unknown pipe linked to edp transcoder\n");
10049 case TRANS_DDI_EDP_INPUT_A_ONOFF:
10050 case TRANS_DDI_EDP_INPUT_A_ON:
10051 trans_edp_pipe = PIPE_A;
10052 break;
10053 case TRANS_DDI_EDP_INPUT_B_ONOFF:
10054 trans_edp_pipe = PIPE_B;
10055 break;
10056 case TRANS_DDI_EDP_INPUT_C_ONOFF:
10057 trans_edp_pipe = PIPE_C;
10058 break;
10059 }
10060
10061 if (trans_edp_pipe == crtc->pipe)
10062 pipe_config->cpu_transcoder = TRANSCODER_EDP;
10063 }
10064
10065 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
10066 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
10067 return false;
10068 *power_domain_mask |= BIT(power_domain);
10069
10070 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
10071
10072 return tmp & PIPECONF_ENABLE;
10073}
10074
Jani Nikula4d1de972016-03-18 17:05:42 +020010075static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
10076 struct intel_crtc_state *pipe_config,
10077 unsigned long *power_domain_mask)
10078{
10079 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010080 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikula4d1de972016-03-18 17:05:42 +020010081 enum intel_display_power_domain power_domain;
10082 enum port port;
10083 enum transcoder cpu_transcoder;
10084 u32 tmp;
10085
Jani Nikula4d1de972016-03-18 17:05:42 +020010086 for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
10087 if (port == PORT_A)
10088 cpu_transcoder = TRANSCODER_DSI_A;
10089 else
10090 cpu_transcoder = TRANSCODER_DSI_C;
10091
10092 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
10093 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
10094 continue;
10095 *power_domain_mask |= BIT(power_domain);
10096
Imre Deakdb18b6a2016-03-24 12:41:40 +020010097 /*
10098 * The PLL needs to be enabled with a valid divider
10099 * configuration, otherwise accessing DSI registers will hang
10100 * the machine. See BSpec North Display Engine
10101 * registers/MIPI[BXT]. We can break out here early, since we
10102 * need the same DSI PLL to be enabled for both DSI ports.
10103 */
10104 if (!intel_dsi_pll_is_enabled(dev_priv))
10105 break;
10106
Jani Nikula4d1de972016-03-18 17:05:42 +020010107 /* XXX: this works for video mode only */
10108 tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
10109 if (!(tmp & DPI_ENABLE))
10110 continue;
10111
10112 tmp = I915_READ(MIPI_CTRL(port));
10113 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
10114 continue;
10115
10116 pipe_config->cpu_transcoder = cpu_transcoder;
Jani Nikula4d1de972016-03-18 17:05:42 +020010117 break;
10118 }
10119
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +030010120 return transcoder_is_dsi(pipe_config->cpu_transcoder);
Jani Nikula4d1de972016-03-18 17:05:42 +020010121}
10122
Daniel Vetter26804af2014-06-25 22:01:55 +030010123static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010124 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +030010125{
10126 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010127 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030010128 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +030010129 enum port port;
10130 uint32_t tmp;
10131
10132 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
10133
10134 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
10135
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070010136 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +000010137 skylake_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M3760b592014-08-22 09:49:11 +053010138 else if (IS_BROXTON(dev))
10139 bxt_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +000010140 else
10141 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +030010142
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010143 pll = pipe_config->shared_dpll;
10144 if (pll) {
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020010145 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
10146 &pipe_config->dpll_hw_state));
Daniel Vetterd452c5b2014-07-04 11:27:39 -030010147 }
10148
Daniel Vetter26804af2014-06-25 22:01:55 +030010149 /*
10150 * Haswell has only FDI/PCH transcoder A. It is which is connected to
10151 * DDI E. So just check whether this pipe is wired to DDI E and whether
10152 * the PCH transcoder is on.
10153 */
Damien Lespiauca370452013-12-03 13:56:24 +000010154 if (INTEL_INFO(dev)->gen < 9 &&
10155 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +030010156 pipe_config->has_pch_encoder = true;
10157
10158 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
10159 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
10160 FDI_DP_PORT_WIDTH_SHIFT) + 1;
10161
10162 ironlake_get_fdi_m_n_config(crtc, pipe_config);
10163 }
10164}
10165
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010166static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010167 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010168{
10169 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010170 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak17290502016-02-12 18:55:11 +020010171 enum intel_display_power_domain power_domain;
10172 unsigned long power_domain_mask;
Jani Nikulacf304292016-03-18 17:05:41 +020010173 bool active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010174
Imre Deak17290502016-02-12 18:55:11 +020010175 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
10176 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +020010177 return false;
Imre Deak17290502016-02-12 18:55:11 +020010178 power_domain_mask = BIT(power_domain);
10179
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010180 pipe_config->shared_dpll = NULL;
Daniel Vetterc0d43d62013-06-07 23:11:08 +020010181
Jani Nikulacf304292016-03-18 17:05:41 +020010182 active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
Daniel Vettereccb1402013-05-22 00:50:22 +020010183
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +030010184 if (IS_BROXTON(dev_priv) &&
10185 bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
10186 WARN_ON(active);
10187 active = true;
Jani Nikula4d1de972016-03-18 17:05:42 +020010188 }
10189
Jani Nikulacf304292016-03-18 17:05:41 +020010190 if (!active)
Imre Deak17290502016-02-12 18:55:11 +020010191 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010192
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +030010193 if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
Jani Nikula4d1de972016-03-18 17:05:42 +020010194 haswell_get_ddi_port_state(crtc, pipe_config);
10195 intel_get_pipe_timings(crtc, pipe_config);
10196 }
Daniel Vetter627eb5a2013-04-29 19:33:42 +020010197
Jani Nikulabc58be62016-03-18 17:05:39 +020010198 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010199
Lionel Landwerlin05dc6982016-03-16 10:57:15 +000010200 pipe_config->gamma_mode =
10201 I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
10202
Chandra Kondurua1b22782015-04-07 15:28:45 -070010203 if (INTEL_INFO(dev)->gen >= 9) {
10204 skl_init_scalers(dev, crtc, pipe_config);
10205 }
10206
Chandra Konduruaf99ced2015-05-11 14:35:47 -070010207 if (INTEL_INFO(dev)->gen >= 9) {
10208 pipe_config->scaler_state.scaler_id = -1;
10209 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
10210 }
10211
Imre Deak17290502016-02-12 18:55:11 +020010212 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
10213 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
10214 power_domain_mask |= BIT(power_domain);
Rodrigo Vivi1c132b42015-09-02 15:19:26 -070010215 if (INTEL_INFO(dev)->gen >= 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +000010216 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -080010217 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -070010218 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesbd2e2442014-11-13 17:51:47 +000010219 }
Daniel Vetter88adfff2013-03-28 10:42:01 +010010220
Jesse Barnese59150d2014-01-07 13:30:45 -080010221 if (IS_HASWELL(dev))
10222 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
10223 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030010224
Jani Nikula4d1de972016-03-18 17:05:42 +020010225 if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
10226 !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
Clint Taylorebb69c92014-09-30 10:30:22 -070010227 pipe_config->pixel_multiplier =
10228 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
10229 } else {
10230 pipe_config->pixel_multiplier = 1;
10231 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020010232
Imre Deak17290502016-02-12 18:55:11 +020010233out:
10234 for_each_power_domain(power_domain, power_domain_mask)
10235 intel_display_power_put(dev_priv, power_domain);
10236
Jani Nikulacf304292016-03-18 17:05:41 +020010237 return active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010238}
10239
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010240static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
10241 const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +010010242{
10243 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010244 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson560b85b2010-08-07 11:01:38 +010010245 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +030010246 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +010010247
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010248 if (plane_state && plane_state->visible) {
10249 unsigned int width = plane_state->base.crtc_w;
10250 unsigned int height = plane_state->base.crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +030010251 unsigned int stride = roundup_pow_of_two(width) * 4;
10252
10253 switch (stride) {
10254 default:
10255 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
10256 width, stride);
10257 stride = 256;
10258 /* fallthrough */
10259 case 256:
10260 case 512:
10261 case 1024:
10262 case 2048:
10263 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010264 }
10265
Ville Syrjälädc41c152014-08-13 11:57:05 +030010266 cntl |= CURSOR_ENABLE |
10267 CURSOR_GAMMA_ENABLE |
10268 CURSOR_FORMAT_ARGB |
10269 CURSOR_STRIDE(stride);
10270
10271 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010272 }
Chris Wilson560b85b2010-08-07 11:01:38 +010010273
Ville Syrjälädc41c152014-08-13 11:57:05 +030010274 if (intel_crtc->cursor_cntl != 0 &&
10275 (intel_crtc->cursor_base != base ||
10276 intel_crtc->cursor_size != size ||
10277 intel_crtc->cursor_cntl != cntl)) {
10278 /* On these chipsets we can only modify the base/size/stride
10279 * whilst the cursor is disabled.
10280 */
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010281 I915_WRITE(CURCNTR(PIPE_A), 0);
10282 POSTING_READ(CURCNTR(PIPE_A));
Ville Syrjälädc41c152014-08-13 11:57:05 +030010283 intel_crtc->cursor_cntl = 0;
10284 }
10285
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010286 if (intel_crtc->cursor_base != base) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010287 I915_WRITE(CURBASE(PIPE_A), base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010288 intel_crtc->cursor_base = base;
10289 }
Ville Syrjälädc41c152014-08-13 11:57:05 +030010290
10291 if (intel_crtc->cursor_size != size) {
10292 I915_WRITE(CURSIZE, size);
10293 intel_crtc->cursor_size = size;
10294 }
10295
Chris Wilson4b0e3332014-05-30 16:35:26 +030010296 if (intel_crtc->cursor_cntl != cntl) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010297 I915_WRITE(CURCNTR(PIPE_A), cntl);
10298 POSTING_READ(CURCNTR(PIPE_A));
Chris Wilson4b0e3332014-05-30 16:35:26 +030010299 intel_crtc->cursor_cntl = cntl;
10300 }
Chris Wilson560b85b2010-08-07 11:01:38 +010010301}
10302
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010303static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
10304 const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +010010305{
10306 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010307 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson560b85b2010-08-07 11:01:38 +010010308 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10309 int pipe = intel_crtc->pipe;
Ville Syrjälä663f3122015-12-14 13:16:48 +020010310 uint32_t cntl = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +010010311
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010312 if (plane_state && plane_state->visible) {
Chris Wilson4b0e3332014-05-30 16:35:26 +030010313 cntl = MCURSOR_GAMMA_ENABLE;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010314 switch (plane_state->base.crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010315 case 64:
10316 cntl |= CURSOR_MODE_64_ARGB_AX;
10317 break;
10318 case 128:
10319 cntl |= CURSOR_MODE_128_ARGB_AX;
10320 break;
10321 case 256:
10322 cntl |= CURSOR_MODE_256_ARGB_AX;
10323 break;
10324 default:
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010325 MISSING_CASE(plane_state->base.crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010326 return;
Chris Wilson560b85b2010-08-07 11:01:38 +010010327 }
Chris Wilson4b0e3332014-05-30 16:35:26 +030010328 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010329
Bob Paauwefc6f93b2015-08-31 14:03:30 -070010330 if (HAS_DDI(dev))
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010331 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010332
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010333 if (plane_state->base.rotation == BIT(DRM_ROTATE_180))
10334 cntl |= CURSOR_ROTATE_180;
10335 }
Ville Syrjälä4398ad42014-10-23 07:41:34 -070010336
Chris Wilson4b0e3332014-05-30 16:35:26 +030010337 if (intel_crtc->cursor_cntl != cntl) {
10338 I915_WRITE(CURCNTR(pipe), cntl);
10339 POSTING_READ(CURCNTR(pipe));
10340 intel_crtc->cursor_cntl = cntl;
10341 }
10342
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010343 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010344 I915_WRITE(CURBASE(pipe), base);
10345 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010346
10347 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010348}
10349
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010350/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +010010351static void intel_crtc_update_cursor(struct drm_crtc *crtc,
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010352 const struct intel_plane_state *plane_state)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010353{
10354 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010355 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010356 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10357 int pipe = intel_crtc->pipe;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010358 u32 base = intel_crtc->cursor_addr;
10359 u32 pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010360
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010361 if (plane_state) {
10362 int x = plane_state->base.crtc_x;
10363 int y = plane_state->base.crtc_y;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010364
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010365 if (x < 0) {
10366 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10367 x = -x;
10368 }
10369 pos |= x << CURSOR_X_SHIFT;
10370
10371 if (y < 0) {
10372 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10373 y = -y;
10374 }
10375 pos |= y << CURSOR_Y_SHIFT;
10376
10377 /* ILK+ do this automagically */
10378 if (HAS_GMCH_DISPLAY(dev) &&
10379 plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
10380 base += (plane_state->base.crtc_h *
10381 plane_state->base.crtc_w - 1) * 4;
10382 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010383 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010384
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010385 I915_WRITE(CURPOS(pipe), pos);
10386
Ville Syrjälä8ac54662014-08-12 19:39:54 +030010387 if (IS_845G(dev) || IS_I865G(dev))
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010388 i845_update_cursor(crtc, base, plane_state);
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010389 else
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010390 i9xx_update_cursor(crtc, base, plane_state);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010391}
10392
Ville Syrjälädc41c152014-08-13 11:57:05 +030010393static bool cursor_size_ok(struct drm_device *dev,
10394 uint32_t width, uint32_t height)
10395{
10396 if (width == 0 || height == 0)
10397 return false;
10398
10399 /*
10400 * 845g/865g are special in that they are only limited by
10401 * the width of their cursors, the height is arbitrary up to
10402 * the precision of the register. Everything else requires
10403 * square cursors, limited to a few power-of-two sizes.
10404 */
10405 if (IS_845G(dev) || IS_I865G(dev)) {
10406 if ((width & 63) != 0)
10407 return false;
10408
10409 if (width > (IS_845G(dev) ? 64 : 512))
10410 return false;
10411
10412 if (height > 1023)
10413 return false;
10414 } else {
10415 switch (width | height) {
10416 case 256:
10417 case 128:
10418 if (IS_GEN2(dev))
10419 return false;
10420 case 64:
10421 break;
10422 default:
10423 return false;
10424 }
10425 }
10426
10427 return true;
10428}
10429
Jesse Barnes79e53942008-11-07 14:24:08 -080010430/* VESA 640x480x72Hz mode to set on the pipe */
10431static struct drm_display_mode load_detect_mode = {
10432 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10433 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10434};
10435
Daniel Vettera8bb6812014-02-10 18:00:39 +010010436struct drm_framebuffer *
10437__intel_framebuffer_create(struct drm_device *dev,
10438 struct drm_mode_fb_cmd2 *mode_cmd,
10439 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +010010440{
10441 struct intel_framebuffer *intel_fb;
10442 int ret;
10443
10444 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010445 if (!intel_fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010446 return ERR_PTR(-ENOMEM);
Chris Wilsond2dff872011-04-19 08:36:26 +010010447
10448 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010449 if (ret)
10450 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +010010451
10452 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010453
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010454err:
10455 kfree(intel_fb);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010456 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +010010457}
10458
Daniel Vetterb5ea6422014-03-02 21:18:00 +010010459static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +010010460intel_framebuffer_create(struct drm_device *dev,
10461 struct drm_mode_fb_cmd2 *mode_cmd,
10462 struct drm_i915_gem_object *obj)
10463{
10464 struct drm_framebuffer *fb;
10465 int ret;
10466
10467 ret = i915_mutex_lock_interruptible(dev);
10468 if (ret)
10469 return ERR_PTR(ret);
10470 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10471 mutex_unlock(&dev->struct_mutex);
10472
10473 return fb;
10474}
10475
Chris Wilsond2dff872011-04-19 08:36:26 +010010476static u32
10477intel_framebuffer_pitch_for_width(int width, int bpp)
10478{
10479 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10480 return ALIGN(pitch, 64);
10481}
10482
10483static u32
10484intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10485{
10486 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +020010487 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +010010488}
10489
10490static struct drm_framebuffer *
10491intel_framebuffer_create_for_mode(struct drm_device *dev,
10492 struct drm_display_mode *mode,
10493 int depth, int bpp)
10494{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010495 struct drm_framebuffer *fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010496 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +000010497 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +010010498
Dave Gordond37cd8a2016-04-22 19:14:32 +010010499 obj = i915_gem_object_create(dev,
Chris Wilsond2dff872011-04-19 08:36:26 +010010500 intel_framebuffer_size_for_mode(mode, bpp));
Chris Wilsonfe3db792016-04-25 13:32:13 +010010501 if (IS_ERR(obj))
10502 return ERR_CAST(obj);
Chris Wilsond2dff872011-04-19 08:36:26 +010010503
10504 mode_cmd.width = mode->hdisplay;
10505 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010506 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10507 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +000010508 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +010010509
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010510 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
10511 if (IS_ERR(fb))
10512 drm_gem_object_unreference_unlocked(&obj->base);
10513
10514 return fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010515}
10516
10517static struct drm_framebuffer *
10518mode_fits_in_fbdev(struct drm_device *dev,
10519 struct drm_display_mode *mode)
10520{
Daniel Vetter06957262015-08-10 13:34:08 +020010521#ifdef CONFIG_DRM_FBDEV_EMULATION
Chris Wilsonfac5e232016-07-04 11:34:36 +010010522 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsond2dff872011-04-19 08:36:26 +010010523 struct drm_i915_gem_object *obj;
10524 struct drm_framebuffer *fb;
10525
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010526 if (!dev_priv->fbdev)
10527 return NULL;
10528
10529 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010530 return NULL;
10531
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010532 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010533 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +010010534
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010535 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010536 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10537 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +010010538 return NULL;
10539
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010540 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +010010541 return NULL;
10542
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010543 drm_framebuffer_reference(fb);
Chris Wilsond2dff872011-04-19 08:36:26 +010010544 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +020010545#else
10546 return NULL;
10547#endif
Chris Wilsond2dff872011-04-19 08:36:26 +010010548}
10549
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010550static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10551 struct drm_crtc *crtc,
10552 struct drm_display_mode *mode,
10553 struct drm_framebuffer *fb,
10554 int x, int y)
10555{
10556 struct drm_plane_state *plane_state;
10557 int hdisplay, vdisplay;
10558 int ret;
10559
10560 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10561 if (IS_ERR(plane_state))
10562 return PTR_ERR(plane_state);
10563
10564 if (mode)
10565 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10566 else
10567 hdisplay = vdisplay = 0;
10568
10569 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10570 if (ret)
10571 return ret;
10572 drm_atomic_set_fb_for_plane(plane_state, fb);
10573 plane_state->crtc_x = 0;
10574 plane_state->crtc_y = 0;
10575 plane_state->crtc_w = hdisplay;
10576 plane_state->crtc_h = vdisplay;
10577 plane_state->src_x = x << 16;
10578 plane_state->src_y = y << 16;
10579 plane_state->src_w = hdisplay << 16;
10580 plane_state->src_h = vdisplay << 16;
10581
10582 return 0;
10583}
10584
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010585bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +010010586 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -050010587 struct intel_load_detect_pipe *old,
10588 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010589{
10590 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010591 struct intel_encoder *intel_encoder =
10592 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -080010593 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +010010594 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -080010595 struct drm_crtc *crtc = NULL;
10596 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +020010597 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -050010598 struct drm_mode_config *config = &dev->mode_config;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010599 struct drm_atomic_state *state = NULL, *restore_state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010600 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010601 struct intel_crtc_state *crtc_state;
Rob Clark51fd3712013-11-19 12:10:12 -050010602 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010603
Chris Wilsond2dff872011-04-19 08:36:26 +010010604 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010605 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010606 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010607
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010608 old->restore_state = NULL;
10609
Rob Clark51fd3712013-11-19 12:10:12 -050010610retry:
10611 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10612 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010613 goto fail;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020010614
Jesse Barnes79e53942008-11-07 14:24:08 -080010615 /*
10616 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +010010617 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010618 * - if the connector already has an assigned crtc, use it (but make
10619 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +010010620 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010621 * - try to find the first unused crtc that can drive this connector,
10622 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -080010623 */
10624
10625 /* See if we already have a CRTC for this connector */
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010626 if (connector->state->crtc) {
10627 crtc = connector->state->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +010010628
Rob Clark51fd3712013-11-19 12:10:12 -050010629 ret = drm_modeset_lock(&crtc->mutex, ctx);
10630 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010631 goto fail;
Chris Wilson8261b192011-04-19 23:18:09 +010010632
10633 /* Make sure the crtc and connector are running */
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010634 goto found;
Jesse Barnes79e53942008-11-07 14:24:08 -080010635 }
10636
10637 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010638 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010639 i++;
10640 if (!(encoder->possible_crtcs & (1 << i)))
10641 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010642
10643 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
10644 if (ret)
10645 goto fail;
10646
10647 if (possible_crtc->state->enable) {
10648 drm_modeset_unlock(&possible_crtc->mutex);
Ville Syrjäläa4592492014-08-11 13:15:36 +030010649 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010650 }
Ville Syrjäläa4592492014-08-11 13:15:36 +030010651
10652 crtc = possible_crtc;
10653 break;
Jesse Barnes79e53942008-11-07 14:24:08 -080010654 }
10655
10656 /*
10657 * If we didn't find an unused CRTC, don't use any.
10658 */
10659 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +010010660 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010661 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010662 }
10663
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010664found:
10665 intel_crtc = to_intel_crtc(crtc);
10666
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010667 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10668 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010669 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010670
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010671 state = drm_atomic_state_alloc(dev);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010672 restore_state = drm_atomic_state_alloc(dev);
10673 if (!state || !restore_state) {
10674 ret = -ENOMEM;
10675 goto fail;
10676 }
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010677
10678 state->acquire_ctx = ctx;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010679 restore_state->acquire_ctx = ctx;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010680
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010681 connector_state = drm_atomic_get_connector_state(state, connector);
10682 if (IS_ERR(connector_state)) {
10683 ret = PTR_ERR(connector_state);
10684 goto fail;
10685 }
10686
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010687 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
10688 if (ret)
10689 goto fail;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010690
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010691 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10692 if (IS_ERR(crtc_state)) {
10693 ret = PTR_ERR(crtc_state);
10694 goto fail;
10695 }
10696
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010697 crtc_state->base.active = crtc_state->base.enable = true;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010698
Chris Wilson64927112011-04-20 07:25:26 +010010699 if (!mode)
10700 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -080010701
Chris Wilsond2dff872011-04-19 08:36:26 +010010702 /* We need a framebuffer large enough to accommodate all accesses
10703 * that the plane may generate whilst we perform load detection.
10704 * We can not rely on the fbcon either being present (we get called
10705 * during its initialisation to detect all boot displays, or it may
10706 * not even exist) or that it is large enough to satisfy the
10707 * requested mode.
10708 */
Daniel Vetter94352cf2012-07-05 22:51:56 +020010709 fb = mode_fits_in_fbdev(dev, mode);
10710 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010711 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010712 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
Chris Wilsond2dff872011-04-19 08:36:26 +010010713 } else
10714 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010715 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010716 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010717 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010718 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010719
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010720 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10721 if (ret)
10722 goto fail;
10723
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010724 drm_framebuffer_unreference(fb);
10725
10726 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
10727 if (ret)
10728 goto fail;
10729
10730 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
10731 if (!ret)
10732 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
10733 if (!ret)
10734 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
10735 if (ret) {
10736 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
10737 goto fail;
10738 }
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030010739
Maarten Lankhorst3ba86072016-02-29 09:18:57 +010010740 ret = drm_atomic_commit(state);
10741 if (ret) {
Chris Wilson64927112011-04-20 07:25:26 +010010742 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010743 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010744 }
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010745
10746 old->restore_state = restore_state;
Chris Wilson71731882011-04-19 23:10:58 +010010747
Jesse Barnes79e53942008-11-07 14:24:08 -080010748 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010749 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +010010750 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010751
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010752fail:
Ander Conselvan de Oliveirae5d958e2015-04-21 17:12:57 +030010753 drm_atomic_state_free(state);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010754 drm_atomic_state_free(restore_state);
10755 restore_state = state = NULL;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010756
Rob Clark51fd3712013-11-19 12:10:12 -050010757 if (ret == -EDEADLK) {
10758 drm_modeset_backoff(ctx);
10759 goto retry;
10760 }
10761
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010762 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -080010763}
10764
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010765void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020010766 struct intel_load_detect_pipe *old,
10767 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010768{
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010769 struct intel_encoder *intel_encoder =
10770 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +010010771 struct drm_encoder *encoder = &intel_encoder->base;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010772 struct drm_atomic_state *state = old->restore_state;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010773 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080010774
Chris Wilsond2dff872011-04-19 08:36:26 +010010775 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010776 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010777 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010778
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010779 if (!state)
Chris Wilson0622a532011-04-21 09:32:11 +010010780 return;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010781
10782 ret = drm_atomic_commit(state);
10783 if (ret) {
10784 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
10785 drm_atomic_state_free(state);
Jesse Barnes79e53942008-11-07 14:24:08 -080010786 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010787}
10788
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010789static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010790 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010791{
Chris Wilsonfac5e232016-07-04 11:34:36 +010010792 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010793 u32 dpll = pipe_config->dpll_hw_state.dpll;
10794
10795 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +020010796 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010797 else if (HAS_PCH_SPLIT(dev))
10798 return 120000;
10799 else if (!IS_GEN2(dev))
10800 return 96000;
10801 else
10802 return 48000;
10803}
10804
Jesse Barnes79e53942008-11-07 14:24:08 -080010805/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010806static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010807 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -080010808{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010809 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010810 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010811 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010812 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -080010813 u32 fp;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +030010814 struct dpll clock;
Imre Deakdccbea32015-06-22 23:35:51 +030010815 int port_clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010816 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -080010817
10818 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +030010819 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010820 else
Ville Syrjälä293623f2013-09-13 16:18:46 +030010821 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010822
10823 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010824 if (IS_PINEVIEW(dev)) {
10825 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10826 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +080010827 } else {
10828 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10829 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10830 }
10831
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010832 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010833 if (IS_PINEVIEW(dev))
10834 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10835 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +080010836 else
10837 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -080010838 DPLL_FPA01_P1_POST_DIV_SHIFT);
10839
10840 switch (dpll & DPLL_MODE_MASK) {
10841 case DPLLB_MODE_DAC_SERIAL:
10842 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10843 5 : 10;
10844 break;
10845 case DPLLB_MODE_LVDS:
10846 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10847 7 : 14;
10848 break;
10849 default:
Zhao Yakui28c97732009-10-09 11:39:41 +080010850 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -080010851 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010852 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010853 }
10854
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010855 if (IS_PINEVIEW(dev))
Imre Deakdccbea32015-06-22 23:35:51 +030010856 port_clock = pnv_calc_dpll_params(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010857 else
Imre Deakdccbea32015-06-22 23:35:51 +030010858 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010859 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +020010860 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010861 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -080010862
10863 if (is_lvds) {
10864 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10865 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010866
10867 if (lvds & LVDS_CLKB_POWER_UP)
10868 clock.p2 = 7;
10869 else
10870 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -080010871 } else {
10872 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10873 clock.p1 = 2;
10874 else {
10875 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10876 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10877 }
10878 if (dpll & PLL_P2_DIVIDE_BY_4)
10879 clock.p2 = 4;
10880 else
10881 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -080010882 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010883
Imre Deakdccbea32015-06-22 23:35:51 +030010884 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010885 }
10886
Ville Syrjälä18442d02013-09-13 16:00:08 +030010887 /*
10888 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +010010889 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +030010890 * encoder's get_config() function.
10891 */
Imre Deakdccbea32015-06-22 23:35:51 +030010892 pipe_config->port_clock = port_clock;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010893}
10894
Ville Syrjälä6878da02013-09-13 15:59:11 +030010895int intel_dotclock_calculate(int link_freq,
10896 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010897{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010898 /*
10899 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010900 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010901 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010902 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010903 *
10904 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010905 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -080010906 */
10907
Ville Syrjälä6878da02013-09-13 15:59:11 +030010908 if (!m_n->link_n)
10909 return 0;
10910
10911 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10912}
10913
Ville Syrjälä18442d02013-09-13 16:00:08 +030010914static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010915 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +030010916{
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020010917 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä18442d02013-09-13 16:00:08 +030010918
10919 /* read out port_clock from the DPLL */
10920 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +030010921
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010922 /*
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020010923 * In case there is an active pipe without active ports,
10924 * we may need some idea for the dotclock anyway.
10925 * Calculate one based on the FDI configuration.
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010926 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010927 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä21a727b2016-02-17 21:41:10 +020010928 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjälä18442d02013-09-13 16:00:08 +030010929 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -080010930}
10931
10932/** Returns the currently programmed mode of the given pipe. */
10933struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10934 struct drm_crtc *crtc)
10935{
Chris Wilsonfac5e232016-07-04 11:34:36 +010010936 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080010937 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010938 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010939 struct drm_display_mode *mode;
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010940 struct intel_crtc_state *pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -020010941 int htot = I915_READ(HTOTAL(cpu_transcoder));
10942 int hsync = I915_READ(HSYNC(cpu_transcoder));
10943 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10944 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +030010945 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -080010946
10947 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10948 if (!mode)
10949 return NULL;
10950
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010951 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
10952 if (!pipe_config) {
10953 kfree(mode);
10954 return NULL;
10955 }
10956
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010957 /*
10958 * Construct a pipe_config sufficient for getting the clock info
10959 * back out of crtc_clock_get.
10960 *
10961 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10962 * to use a real value here instead.
10963 */
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010964 pipe_config->cpu_transcoder = (enum transcoder) pipe;
10965 pipe_config->pixel_multiplier = 1;
10966 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10967 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10968 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
10969 i9xx_crtc_clock_get(intel_crtc, pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010970
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010971 mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -080010972 mode->hdisplay = (htot & 0xffff) + 1;
10973 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10974 mode->hsync_start = (hsync & 0xffff) + 1;
10975 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10976 mode->vdisplay = (vtot & 0xffff) + 1;
10977 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10978 mode->vsync_start = (vsync & 0xffff) + 1;
10979 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10980
10981 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -080010982
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010983 kfree(pipe_config);
10984
Jesse Barnes79e53942008-11-07 14:24:08 -080010985 return mode;
10986}
10987
10988static void intel_crtc_destroy(struct drm_crtc *crtc)
10989{
10990 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010991 struct drm_device *dev = crtc->dev;
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020010992 struct intel_flip_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +020010993
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010994 spin_lock_irq(&dev->event_lock);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010995 work = intel_crtc->flip_work;
10996 intel_crtc->flip_work = NULL;
10997 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010998
Daniel Vetter5a21b662016-05-24 17:13:53 +020010999 if (work) {
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011000 cancel_work_sync(&work->mmio_work);
11001 cancel_work_sync(&work->unpin_work);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011002 kfree(work);
Daniel Vetter67e77c52010-08-20 22:26:30 +020011003 }
Jesse Barnes79e53942008-11-07 14:24:08 -080011004
11005 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020011006
Jesse Barnes79e53942008-11-07 14:24:08 -080011007 kfree(intel_crtc);
11008}
11009
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011010static void intel_unpin_work_fn(struct work_struct *__work)
11011{
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011012 struct intel_flip_work *work =
11013 container_of(__work, struct intel_flip_work, unpin_work);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011014 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
11015 struct drm_device *dev = crtc->base.dev;
11016 struct drm_plane *primary = crtc->base.primary;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011017
Daniel Vetter5a21b662016-05-24 17:13:53 +020011018 if (is_mmio_work(work))
11019 flush_work(&work->mmio_work);
11020
11021 mutex_lock(&dev->struct_mutex);
11022 intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
11023 drm_gem_object_unreference(&work->pending_flip_obj->base);
11024
11025 if (work->flip_queued_req)
11026 i915_gem_request_assign(&work->flip_queued_req, NULL);
11027 mutex_unlock(&dev->struct_mutex);
11028
11029 intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
11030 intel_fbc_post_update(crtc);
11031 drm_framebuffer_unreference(work->old_fb);
11032
11033 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
11034 atomic_dec(&crtc->unpin_work_count);
11035
11036 kfree(work);
11037}
11038
11039/* Is 'a' after or equal to 'b'? */
11040static bool g4x_flip_count_after_eq(u32 a, u32 b)
11041{
11042 return !((a - b) & 0x80000000);
11043}
11044
11045static bool __pageflip_finished_cs(struct intel_crtc *crtc,
11046 struct intel_flip_work *work)
11047{
11048 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010011049 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011050 unsigned reset_counter;
11051
11052 reset_counter = i915_reset_counter(&dev_priv->gpu_error);
11053 if (crtc->reset_counter != reset_counter)
11054 return true;
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011055
Maarten Lankhorst143f73b2016-05-17 15:07:54 +020011056 /*
Daniel Vetter5a21b662016-05-24 17:13:53 +020011057 * The relevant registers doen't exist on pre-ctg.
11058 * As the flip done interrupt doesn't trigger for mmio
11059 * flips on gmch platforms, a flip count check isn't
11060 * really needed there. But since ctg has the registers,
11061 * include it in the check anyway.
Maarten Lankhorst143f73b2016-05-17 15:07:54 +020011062 */
Daniel Vetter5a21b662016-05-24 17:13:53 +020011063 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
11064 return true;
Maarten Lankhorst143f73b2016-05-17 15:07:54 +020011065
Daniel Vetter5a21b662016-05-24 17:13:53 +020011066 /*
11067 * BDW signals flip done immediately if the plane
11068 * is disabled, even if the plane enable is already
11069 * armed to occur at the next vblank :(
11070 */
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020011071
Daniel Vetter5a21b662016-05-24 17:13:53 +020011072 /*
11073 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
11074 * used the same base address. In that case the mmio flip might
11075 * have completed, but the CS hasn't even executed the flip yet.
11076 *
11077 * A flip count check isn't enough as the CS might have updated
11078 * the base address just after start of vblank, but before we
11079 * managed to process the interrupt. This means we'd complete the
11080 * CS flip too soon.
11081 *
11082 * Combining both checks should get us a good enough result. It may
11083 * still happen that the CS flip has been executed, but has not
11084 * yet actually completed. But in case the base address is the same
11085 * anyway, we don't really care.
11086 */
11087 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
11088 crtc->flip_work->gtt_offset &&
11089 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
11090 crtc->flip_work->flip_count);
11091}
Maarten Lankhorst143f73b2016-05-17 15:07:54 +020011092
Daniel Vetter5a21b662016-05-24 17:13:53 +020011093static bool
11094__pageflip_finished_mmio(struct intel_crtc *crtc,
11095 struct intel_flip_work *work)
11096{
11097 /*
11098 * MMIO work completes when vblank is different from
11099 * flip_queued_vblank.
11100 *
11101 * Reset counter value doesn't matter, this is handled by
11102 * i915_wait_request finishing early, so no need to handle
11103 * reset here.
11104 */
11105 return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011106}
11107
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011108
11109static bool pageflip_finished(struct intel_crtc *crtc,
11110 struct intel_flip_work *work)
11111{
11112 if (!atomic_read(&work->pending))
11113 return false;
11114
11115 smp_rmb();
11116
Daniel Vetter5a21b662016-05-24 17:13:53 +020011117 if (is_mmio_work(work))
11118 return __pageflip_finished_mmio(crtc, work);
11119 else
11120 return __pageflip_finished_cs(crtc, work);
11121}
11122
11123void intel_finish_page_flip_cs(struct drm_i915_private *dev_priv, int pipe)
11124{
Chris Wilson91c8a322016-07-05 10:40:23 +010011125 struct drm_device *dev = &dev_priv->drm;
Daniel Vetter5a21b662016-05-24 17:13:53 +020011126 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11127 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11128 struct intel_flip_work *work;
11129 unsigned long flags;
11130
11131 /* Ignore early vblank irqs */
11132 if (!crtc)
11133 return;
11134
Daniel Vetterf3260382014-09-15 14:55:23 +020011135 /*
Daniel Vetter5a21b662016-05-24 17:13:53 +020011136 * This is called both by irq handlers and the reset code (to complete
11137 * lost pageflips) so needs the full irqsave spinlocks.
Chris Wilsone7d841c2012-12-03 11:36:30 +000011138 */
Daniel Vetter5a21b662016-05-24 17:13:53 +020011139 spin_lock_irqsave(&dev->event_lock, flags);
11140 work = intel_crtc->flip_work;
11141
11142 if (work != NULL &&
11143 !is_mmio_work(work) &&
11144 pageflip_finished(intel_crtc, work))
11145 page_flip_completed(intel_crtc);
11146
11147 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011148}
11149
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011150void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
Chris Wilsone7d841c2012-12-03 11:36:30 +000011151{
Chris Wilson91c8a322016-07-05 10:40:23 +010011152 struct drm_device *dev = &dev_priv->drm;
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011153 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11154 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11155 struct intel_flip_work *work;
11156 unsigned long flags;
11157
11158 /* Ignore early vblank irqs */
11159 if (!crtc)
11160 return;
11161
11162 /*
11163 * This is called both by irq handlers and the reset code (to complete
11164 * lost pageflips) so needs the full irqsave spinlocks.
11165 */
11166 spin_lock_irqsave(&dev->event_lock, flags);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011167 work = intel_crtc->flip_work;
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011168
Daniel Vetter5a21b662016-05-24 17:13:53 +020011169 if (work != NULL &&
11170 is_mmio_work(work) &&
11171 pageflip_finished(intel_crtc, work))
11172 page_flip_completed(intel_crtc);
Maarten Lankhorst68858432016-05-17 15:07:52 +020011173
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011174 spin_unlock_irqrestore(&dev->event_lock, flags);
11175}
11176
Daniel Vetter5a21b662016-05-24 17:13:53 +020011177static inline void intel_mark_page_flip_active(struct intel_crtc *crtc,
11178 struct intel_flip_work *work)
11179{
11180 work->flip_queued_vblank = intel_crtc_get_vblank_counter(crtc);
11181
11182 /* Ensure that the work item is consistent when activating it ... */
11183 smp_mb__before_atomic();
11184 atomic_set(&work->pending, 1);
11185}
11186
11187static int intel_gen2_queue_flip(struct drm_device *dev,
11188 struct drm_crtc *crtc,
11189 struct drm_framebuffer *fb,
11190 struct drm_i915_gem_object *obj,
11191 struct drm_i915_gem_request *req,
11192 uint32_t flags)
11193{
11194 struct intel_engine_cs *engine = req->engine;
11195 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11196 u32 flip_mask;
11197 int ret;
11198
11199 ret = intel_ring_begin(req, 6);
11200 if (ret)
11201 return ret;
11202
11203 /* Can't queue multiple flips, so wait for the previous
11204 * one to finish before executing the next.
11205 */
11206 if (intel_crtc->plane)
11207 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11208 else
11209 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
11210 intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
11211 intel_ring_emit(engine, MI_NOOP);
11212 intel_ring_emit(engine, MI_DISPLAY_FLIP |
11213 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11214 intel_ring_emit(engine, fb->pitches[0]);
11215 intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
11216 intel_ring_emit(engine, 0); /* aux display base address, unused */
11217
11218 return 0;
11219}
11220
11221static int intel_gen3_queue_flip(struct drm_device *dev,
11222 struct drm_crtc *crtc,
11223 struct drm_framebuffer *fb,
11224 struct drm_i915_gem_object *obj,
11225 struct drm_i915_gem_request *req,
11226 uint32_t flags)
11227{
11228 struct intel_engine_cs *engine = req->engine;
11229 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11230 u32 flip_mask;
11231 int ret;
11232
11233 ret = intel_ring_begin(req, 6);
11234 if (ret)
11235 return ret;
11236
11237 if (intel_crtc->plane)
11238 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11239 else
11240 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
11241 intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
11242 intel_ring_emit(engine, MI_NOOP);
11243 intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 |
11244 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11245 intel_ring_emit(engine, fb->pitches[0]);
11246 intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
11247 intel_ring_emit(engine, MI_NOOP);
11248
11249 return 0;
11250}
11251
11252static int intel_gen4_queue_flip(struct drm_device *dev,
11253 struct drm_crtc *crtc,
11254 struct drm_framebuffer *fb,
11255 struct drm_i915_gem_object *obj,
11256 struct drm_i915_gem_request *req,
11257 uint32_t flags)
11258{
11259 struct intel_engine_cs *engine = req->engine;
Chris Wilsonfac5e232016-07-04 11:34:36 +010011260 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011261 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11262 uint32_t pf, pipesrc;
11263 int ret;
11264
11265 ret = intel_ring_begin(req, 4);
11266 if (ret)
11267 return ret;
11268
11269 /* i965+ uses the linear or tiled offsets from the
11270 * Display Registers (which do not change across a page-flip)
11271 * so we need only reprogram the base address.
11272 */
11273 intel_ring_emit(engine, MI_DISPLAY_FLIP |
11274 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11275 intel_ring_emit(engine, fb->pitches[0]);
11276 intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset |
11277 obj->tiling_mode);
11278
11279 /* XXX Enabling the panel-fitter across page-flip is so far
11280 * untested on non-native modes, so ignore it for now.
11281 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
11282 */
11283 pf = 0;
11284 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
11285 intel_ring_emit(engine, pf | pipesrc);
11286
11287 return 0;
11288}
11289
11290static int intel_gen6_queue_flip(struct drm_device *dev,
11291 struct drm_crtc *crtc,
11292 struct drm_framebuffer *fb,
11293 struct drm_i915_gem_object *obj,
11294 struct drm_i915_gem_request *req,
11295 uint32_t flags)
11296{
11297 struct intel_engine_cs *engine = req->engine;
Chris Wilsonfac5e232016-07-04 11:34:36 +010011298 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011299 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11300 uint32_t pf, pipesrc;
11301 int ret;
11302
11303 ret = intel_ring_begin(req, 4);
11304 if (ret)
11305 return ret;
11306
11307 intel_ring_emit(engine, MI_DISPLAY_FLIP |
11308 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11309 intel_ring_emit(engine, fb->pitches[0] | obj->tiling_mode);
11310 intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
11311
11312 /* Contrary to the suggestions in the documentation,
11313 * "Enable Panel Fitter" does not seem to be required when page
11314 * flipping with a non-native mode, and worse causes a normal
11315 * modeset to fail.
11316 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11317 */
11318 pf = 0;
11319 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
11320 intel_ring_emit(engine, pf | pipesrc);
11321
11322 return 0;
11323}
11324
11325static int intel_gen7_queue_flip(struct drm_device *dev,
11326 struct drm_crtc *crtc,
11327 struct drm_framebuffer *fb,
11328 struct drm_i915_gem_object *obj,
11329 struct drm_i915_gem_request *req,
11330 uint32_t flags)
11331{
11332 struct intel_engine_cs *engine = req->engine;
11333 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11334 uint32_t plane_bit = 0;
11335 int len, ret;
11336
11337 switch (intel_crtc->plane) {
11338 case PLANE_A:
11339 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11340 break;
11341 case PLANE_B:
11342 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11343 break;
11344 case PLANE_C:
11345 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11346 break;
11347 default:
11348 WARN_ONCE(1, "unknown plane in flip command\n");
11349 return -ENODEV;
11350 }
11351
11352 len = 4;
11353 if (engine->id == RCS) {
11354 len += 6;
11355 /*
11356 * On Gen 8, SRM is now taking an extra dword to accommodate
11357 * 48bits addresses, and we need a NOOP for the batch size to
11358 * stay even.
11359 */
11360 if (IS_GEN8(dev))
11361 len += 2;
11362 }
11363
11364 /*
11365 * BSpec MI_DISPLAY_FLIP for IVB:
11366 * "The full packet must be contained within the same cache line."
11367 *
11368 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11369 * cacheline, if we ever start emitting more commands before
11370 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11371 * then do the cacheline alignment, and finally emit the
11372 * MI_DISPLAY_FLIP.
11373 */
11374 ret = intel_ring_cacheline_align(req);
11375 if (ret)
11376 return ret;
11377
11378 ret = intel_ring_begin(req, len);
11379 if (ret)
11380 return ret;
11381
11382 /* Unmask the flip-done completion message. Note that the bspec says that
11383 * we should do this for both the BCS and RCS, and that we must not unmask
11384 * more than one flip event at any time (or ensure that one flip message
11385 * can be sent by waiting for flip-done prior to queueing new flips).
11386 * Experimentation says that BCS works despite DERRMR masking all
11387 * flip-done completion events and that unmasking all planes at once
11388 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11389 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11390 */
11391 if (engine->id == RCS) {
11392 intel_ring_emit(engine, MI_LOAD_REGISTER_IMM(1));
11393 intel_ring_emit_reg(engine, DERRMR);
11394 intel_ring_emit(engine, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
11395 DERRMR_PIPEB_PRI_FLIP_DONE |
11396 DERRMR_PIPEC_PRI_FLIP_DONE));
11397 if (IS_GEN8(dev))
11398 intel_ring_emit(engine, MI_STORE_REGISTER_MEM_GEN8 |
11399 MI_SRM_LRM_GLOBAL_GTT);
11400 else
11401 intel_ring_emit(engine, MI_STORE_REGISTER_MEM |
11402 MI_SRM_LRM_GLOBAL_GTT);
11403 intel_ring_emit_reg(engine, DERRMR);
11404 intel_ring_emit(engine, engine->scratch.gtt_offset + 256);
11405 if (IS_GEN8(dev)) {
11406 intel_ring_emit(engine, 0);
11407 intel_ring_emit(engine, MI_NOOP);
11408 }
11409 }
11410
11411 intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 | plane_bit);
11412 intel_ring_emit(engine, (fb->pitches[0] | obj->tiling_mode));
11413 intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
11414 intel_ring_emit(engine, (MI_NOOP));
11415
11416 return 0;
11417}
11418
11419static bool use_mmio_flip(struct intel_engine_cs *engine,
11420 struct drm_i915_gem_object *obj)
11421{
Chris Wilsonc37efb92016-06-17 08:28:47 +010011422 struct reservation_object *resv;
11423
Daniel Vetter5a21b662016-05-24 17:13:53 +020011424 /*
11425 * This is not being used for older platforms, because
11426 * non-availability of flip done interrupt forces us to use
11427 * CS flips. Older platforms derive flip done using some clever
11428 * tricks involving the flip_pending status bits and vblank irqs.
11429 * So using MMIO flips there would disrupt this mechanism.
11430 */
11431
11432 if (engine == NULL)
11433 return true;
11434
11435 if (INTEL_GEN(engine->i915) < 5)
11436 return false;
11437
11438 if (i915.use_mmio_flip < 0)
11439 return false;
11440 else if (i915.use_mmio_flip > 0)
11441 return true;
11442 else if (i915.enable_execlists)
11443 return true;
Chris Wilsonc37efb92016-06-17 08:28:47 +010011444
11445 resv = i915_gem_object_get_dmabuf_resv(obj);
11446 if (resv && !reservation_object_test_signaled_rcu(resv, false))
Daniel Vetter5a21b662016-05-24 17:13:53 +020011447 return true;
Chris Wilsonc37efb92016-06-17 08:28:47 +010011448
11449 return engine != i915_gem_request_get_engine(obj->last_write_req);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011450}
11451
11452static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
11453 unsigned int rotation,
11454 struct intel_flip_work *work)
11455{
11456 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010011457 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011458 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
11459 const enum pipe pipe = intel_crtc->pipe;
11460 u32 ctl, stride, tile_height;
11461
11462 ctl = I915_READ(PLANE_CTL(pipe, 0));
11463 ctl &= ~PLANE_CTL_TILED_MASK;
11464 switch (fb->modifier[0]) {
11465 case DRM_FORMAT_MOD_NONE:
11466 break;
11467 case I915_FORMAT_MOD_X_TILED:
11468 ctl |= PLANE_CTL_TILED_X;
11469 break;
11470 case I915_FORMAT_MOD_Y_TILED:
11471 ctl |= PLANE_CTL_TILED_Y;
11472 break;
11473 case I915_FORMAT_MOD_Yf_TILED:
11474 ctl |= PLANE_CTL_TILED_YF;
11475 break;
11476 default:
11477 MISSING_CASE(fb->modifier[0]);
11478 }
11479
11480 /*
11481 * The stride is either expressed as a multiple of 64 bytes chunks for
11482 * linear buffers or in number of tiles for tiled buffers.
11483 */
11484 if (intel_rotation_90_or_270(rotation)) {
11485 /* stride = Surface height in tiles */
11486 tile_height = intel_tile_height(dev_priv, fb->modifier[0], 0);
11487 stride = DIV_ROUND_UP(fb->height, tile_height);
11488 } else {
11489 stride = fb->pitches[0] /
11490 intel_fb_stride_alignment(dev_priv, fb->modifier[0],
11491 fb->pixel_format);
11492 }
11493
11494 /*
11495 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11496 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11497 */
11498 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11499 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11500
11501 I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
11502 POSTING_READ(PLANE_SURF(pipe, 0));
11503}
11504
11505static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
11506 struct intel_flip_work *work)
11507{
11508 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010011509 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011510 struct intel_framebuffer *intel_fb =
11511 to_intel_framebuffer(intel_crtc->base.primary->fb);
11512 struct drm_i915_gem_object *obj = intel_fb->obj;
11513 i915_reg_t reg = DSPCNTR(intel_crtc->plane);
11514 u32 dspcntr;
11515
11516 dspcntr = I915_READ(reg);
11517
11518 if (obj->tiling_mode != I915_TILING_NONE)
11519 dspcntr |= DISPPLANE_TILED;
11520 else
11521 dspcntr &= ~DISPPLANE_TILED;
11522
11523 I915_WRITE(reg, dspcntr);
11524
11525 I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
11526 POSTING_READ(DSPSURF(intel_crtc->plane));
11527}
11528
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011529static void intel_mmio_flip_work_func(struct work_struct *w)
Damien Lespiauff944562014-11-20 14:58:16 +000011530{
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011531 struct intel_flip_work *work =
11532 container_of(w, struct intel_flip_work, mmio_work);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011533 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
11534 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
11535 struct intel_framebuffer *intel_fb =
11536 to_intel_framebuffer(crtc->base.primary->fb);
11537 struct drm_i915_gem_object *obj = intel_fb->obj;
Chris Wilsonc37efb92016-06-17 08:28:47 +010011538 struct reservation_object *resv;
Daniel Vetter5a21b662016-05-24 17:13:53 +020011539
11540 if (work->flip_queued_req)
11541 WARN_ON(__i915_wait_request(work->flip_queued_req,
11542 false, NULL,
11543 &dev_priv->rps.mmioflips));
11544
11545 /* For framebuffer backed by dmabuf, wait for fence */
Chris Wilsonc37efb92016-06-17 08:28:47 +010011546 resv = i915_gem_object_get_dmabuf_resv(obj);
11547 if (resv)
11548 WARN_ON(reservation_object_wait_timeout_rcu(resv, false, false,
Daniel Vetter5a21b662016-05-24 17:13:53 +020011549 MAX_SCHEDULE_TIMEOUT) < 0);
11550
11551 intel_pipe_update_start(crtc);
11552
11553 if (INTEL_GEN(dev_priv) >= 9)
11554 skl_do_mmio_flip(crtc, work->rotation, work);
11555 else
11556 /* use_mmio_flip() retricts MMIO flips to ilk+ */
11557 ilk_do_mmio_flip(crtc, work);
11558
11559 intel_pipe_update_end(crtc, work);
11560}
11561
11562static int intel_default_queue_flip(struct drm_device *dev,
11563 struct drm_crtc *crtc,
11564 struct drm_framebuffer *fb,
11565 struct drm_i915_gem_object *obj,
11566 struct drm_i915_gem_request *req,
11567 uint32_t flags)
11568{
11569 return -ENODEV;
11570}
11571
11572static bool __pageflip_stall_check_cs(struct drm_i915_private *dev_priv,
11573 struct intel_crtc *intel_crtc,
11574 struct intel_flip_work *work)
11575{
11576 u32 addr, vblank;
11577
11578 if (!atomic_read(&work->pending))
11579 return false;
11580
11581 smp_rmb();
11582
11583 vblank = intel_crtc_get_vblank_counter(intel_crtc);
11584 if (work->flip_ready_vblank == 0) {
11585 if (work->flip_queued_req &&
Chris Wilsonf69a02c2016-07-01 17:23:16 +010011586 !i915_gem_request_completed(work->flip_queued_req))
Daniel Vetter5a21b662016-05-24 17:13:53 +020011587 return false;
11588
11589 work->flip_ready_vblank = vblank;
11590 }
11591
11592 if (vblank - work->flip_ready_vblank < 3)
11593 return false;
11594
11595 /* Potential stall - if we see that the flip has happened,
11596 * assume a missed interrupt. */
11597 if (INTEL_GEN(dev_priv) >= 4)
11598 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11599 else
11600 addr = I915_READ(DSPADDR(intel_crtc->plane));
11601
11602 /* There is a potential issue here with a false positive after a flip
11603 * to the same address. We could address this by checking for a
11604 * non-incrementing frame counter.
11605 */
11606 return addr == work->gtt_offset;
11607}
11608
11609void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
11610{
Chris Wilson91c8a322016-07-05 10:40:23 +010011611 struct drm_device *dev = &dev_priv->drm;
Daniel Vetter5a21b662016-05-24 17:13:53 +020011612 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Maarten Lankhorst143f73b2016-05-17 15:07:54 +020011613 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011614 struct intel_flip_work *work;
11615
11616 WARN_ON(!in_interrupt());
11617
11618 if (crtc == NULL)
11619 return;
11620
11621 spin_lock(&dev->event_lock);
11622 work = intel_crtc->flip_work;
11623
11624 if (work != NULL && !is_mmio_work(work) &&
11625 __pageflip_stall_check_cs(dev_priv, intel_crtc, work)) {
11626 WARN_ONCE(1,
11627 "Kicking stuck page flip: queued at %d, now %d\n",
11628 work->flip_queued_vblank, intel_crtc_get_vblank_counter(intel_crtc));
11629 page_flip_completed(intel_crtc);
11630 work = NULL;
11631 }
11632
11633 if (work != NULL && !is_mmio_work(work) &&
11634 intel_crtc_get_vblank_counter(intel_crtc) - work->flip_queued_vblank > 1)
11635 intel_queue_rps_boost_for_request(work->flip_queued_req);
11636 spin_unlock(&dev->event_lock);
11637}
11638
11639static int intel_crtc_page_flip(struct drm_crtc *crtc,
11640 struct drm_framebuffer *fb,
11641 struct drm_pending_vblank_event *event,
11642 uint32_t page_flip_flags)
11643{
Maarten Lankhorst143f73b2016-05-17 15:07:54 +020011644 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010011645 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011646 struct drm_framebuffer *old_fb = crtc->primary->fb;
11647 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
11648 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11649 struct drm_plane *primary = crtc->primary;
11650 enum pipe pipe = intel_crtc->pipe;
11651 struct intel_flip_work *work;
11652 struct intel_engine_cs *engine;
11653 bool mmio_flip;
11654 struct drm_i915_gem_request *request = NULL;
11655 int ret;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011656
Daniel Vetter5a21b662016-05-24 17:13:53 +020011657 /*
11658 * drm_mode_page_flip_ioctl() should already catch this, but double
11659 * check to be safe. In the future we may enable pageflipping from
11660 * a disabled primary plane.
11661 */
11662 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11663 return -EBUSY;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020011664
Daniel Vetter5a21b662016-05-24 17:13:53 +020011665 /* Can't change pixel format via MI display flips. */
11666 if (fb->pixel_format != crtc->primary->fb->pixel_format)
11667 return -EINVAL;
Maarten Lankhorst143f73b2016-05-17 15:07:54 +020011668
Daniel Vetter5a21b662016-05-24 17:13:53 +020011669 /*
11670 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11671 * Note that pitch changes could also affect these register.
11672 */
11673 if (INTEL_INFO(dev)->gen > 3 &&
11674 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11675 fb->pitches[0] != crtc->primary->fb->pitches[0]))
11676 return -EINVAL;
Maarten Lankhorst143f73b2016-05-17 15:07:54 +020011677
Daniel Vetter5a21b662016-05-24 17:13:53 +020011678 if (i915_terminally_wedged(&dev_priv->gpu_error))
11679 goto out_hang;
Maarten Lankhorst143f73b2016-05-17 15:07:54 +020011680
Daniel Vetter5a21b662016-05-24 17:13:53 +020011681 work = kzalloc(sizeof(*work), GFP_KERNEL);
11682 if (work == NULL)
11683 return -ENOMEM;
11684
11685 work->event = event;
11686 work->crtc = crtc;
11687 work->old_fb = old_fb;
11688 INIT_WORK(&work->unpin_work, intel_unpin_work_fn);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011689
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020011690 ret = drm_crtc_vblank_get(crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011691 if (ret)
11692 goto free_work;
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020011693
Daniel Vetter5a21b662016-05-24 17:13:53 +020011694 /* We borrow the event spin lock for protecting flip_work */
11695 spin_lock_irq(&dev->event_lock);
11696 if (intel_crtc->flip_work) {
11697 /* Before declaring the flip queue wedged, check if
11698 * the hardware completed the operation behind our backs.
11699 */
11700 if (pageflip_finished(intel_crtc, intel_crtc->flip_work)) {
11701 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11702 page_flip_completed(intel_crtc);
11703 } else {
11704 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
11705 spin_unlock_irq(&dev->event_lock);
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020011706
Daniel Vetter5a21b662016-05-24 17:13:53 +020011707 drm_crtc_vblank_put(crtc);
11708 kfree(work);
11709 return -EBUSY;
11710 }
11711 }
11712 intel_crtc->flip_work = work;
11713 spin_unlock_irq(&dev->event_lock);
Alex Goinsfd8e0582015-11-25 18:43:38 -080011714
Daniel Vetter5a21b662016-05-24 17:13:53 +020011715 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11716 flush_workqueue(dev_priv->wq);
11717
11718 /* Reference the objects for the scheduled work. */
11719 drm_framebuffer_reference(work->old_fb);
11720 drm_gem_object_reference(&obj->base);
11721
11722 crtc->primary->fb = fb;
11723 update_state_fb(crtc->primary);
Maarten Lankhorstfaf68d92016-06-14 14:24:20 +020011724
11725 intel_fbc_pre_update(intel_crtc, intel_crtc->config,
11726 to_intel_plane_state(primary->state));
Daniel Vetter5a21b662016-05-24 17:13:53 +020011727
11728 work->pending_flip_obj = obj;
11729
11730 ret = i915_mutex_lock_interruptible(dev);
11731 if (ret)
11732 goto cleanup;
11733
11734 intel_crtc->reset_counter = i915_reset_counter(&dev_priv->gpu_error);
11735 if (__i915_reset_in_progress_or_wedged(intel_crtc->reset_counter)) {
11736 ret = -EIO;
11737 goto cleanup;
11738 }
11739
11740 atomic_inc(&intel_crtc->unpin_work_count);
11741
11742 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
11743 work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
11744
11745 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
11746 engine = &dev_priv->engine[BCS];
11747 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
11748 /* vlv: DISPLAY_FLIP fails to change tiling */
11749 engine = NULL;
11750 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
11751 engine = &dev_priv->engine[BCS];
11752 } else if (INTEL_INFO(dev)->gen >= 7) {
11753 engine = i915_gem_request_get_engine(obj->last_write_req);
11754 if (engine == NULL || engine->id != RCS)
11755 engine = &dev_priv->engine[BCS];
11756 } else {
11757 engine = &dev_priv->engine[RCS];
11758 }
11759
11760 mmio_flip = use_mmio_flip(engine, obj);
11761
11762 /* When using CS flips, we want to emit semaphores between rings.
11763 * However, when using mmio flips we will create a task to do the
11764 * synchronisation, so all we want here is to pin the framebuffer
11765 * into the display plane and skip any waits.
11766 */
11767 if (!mmio_flip) {
11768 ret = i915_gem_object_sync(obj, engine, &request);
11769 if (!ret && !request) {
11770 request = i915_gem_request_alloc(engine, NULL);
11771 ret = PTR_ERR_OR_ZERO(request);
Maarten Lankhorst143f73b2016-05-17 15:07:54 +020011772 }
Sourab Gupta84c33a62014-06-02 16:47:17 +053011773
Daniel Vetter5a21b662016-05-24 17:13:53 +020011774 if (ret)
11775 goto cleanup_pending;
Maarten Lankhorst143f73b2016-05-17 15:07:54 +020011776 }
11777
Daniel Vetter5a21b662016-05-24 17:13:53 +020011778 ret = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
11779 if (ret)
11780 goto cleanup_pending;
Maarten Lankhorst143f73b2016-05-17 15:07:54 +020011781
Daniel Vetter5a21b662016-05-24 17:13:53 +020011782 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary),
11783 obj, 0);
11784 work->gtt_offset += intel_crtc->dspaddr_offset;
11785 work->rotation = crtc->primary->state->rotation;
11786
11787 if (mmio_flip) {
11788 INIT_WORK(&work->mmio_work, intel_mmio_flip_work_func);
11789
11790 i915_gem_request_assign(&work->flip_queued_req,
11791 obj->last_write_req);
11792
11793 schedule_work(&work->mmio_work);
11794 } else {
11795 i915_gem_request_assign(&work->flip_queued_req, request);
11796 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
11797 page_flip_flags);
11798 if (ret)
11799 goto cleanup_unpin;
11800
11801 intel_mark_page_flip_active(intel_crtc, work);
11802
11803 i915_add_request_no_flush(request);
Maarten Lankhorst143f73b2016-05-17 15:07:54 +020011804 }
11805
Daniel Vetter5a21b662016-05-24 17:13:53 +020011806 i915_gem_track_fb(intel_fb_obj(old_fb), obj,
11807 to_intel_plane(primary)->frontbuffer_bit);
11808 mutex_unlock(&dev->struct_mutex);
11809
11810 intel_frontbuffer_flip_prepare(dev,
11811 to_intel_plane(primary)->frontbuffer_bit);
11812
11813 trace_i915_flip_request(intel_crtc->plane, obj);
11814
11815 return 0;
11816
11817cleanup_unpin:
11818 intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
11819cleanup_pending:
11820 if (!IS_ERR_OR_NULL(request))
11821 i915_add_request_no_flush(request);
11822 atomic_dec(&intel_crtc->unpin_work_count);
11823 mutex_unlock(&dev->struct_mutex);
11824cleanup:
11825 crtc->primary->fb = old_fb;
11826 update_state_fb(crtc->primary);
11827
11828 drm_gem_object_unreference_unlocked(&obj->base);
11829 drm_framebuffer_unreference(work->old_fb);
11830
11831 spin_lock_irq(&dev->event_lock);
11832 intel_crtc->flip_work = NULL;
11833 spin_unlock_irq(&dev->event_lock);
11834
11835 drm_crtc_vblank_put(crtc);
11836free_work:
11837 kfree(work);
11838
11839 if (ret == -EIO) {
11840 struct drm_atomic_state *state;
11841 struct drm_plane_state *plane_state;
11842
11843out_hang:
11844 state = drm_atomic_state_alloc(dev);
11845 if (!state)
11846 return -ENOMEM;
11847 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
11848
11849retry:
11850 plane_state = drm_atomic_get_plane_state(state, primary);
11851 ret = PTR_ERR_OR_ZERO(plane_state);
11852 if (!ret) {
11853 drm_atomic_set_fb_for_plane(plane_state, fb);
11854
11855 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
11856 if (!ret)
11857 ret = drm_atomic_commit(state);
11858 }
11859
11860 if (ret == -EDEADLK) {
11861 drm_modeset_backoff(state->acquire_ctx);
11862 drm_atomic_state_clear(state);
11863 goto retry;
11864 }
11865
11866 if (ret)
11867 drm_atomic_state_free(state);
11868
11869 if (ret == 0 && event) {
11870 spin_lock_irq(&dev->event_lock);
11871 drm_crtc_send_vblank_event(crtc, event);
11872 spin_unlock_irq(&dev->event_lock);
11873 }
11874 }
11875 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011876}
11877
Daniel Vetter5a21b662016-05-24 17:13:53 +020011878
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011879/**
11880 * intel_wm_need_update - Check whether watermarks need updating
11881 * @plane: drm plane
11882 * @state: new plane state
11883 *
11884 * Check current plane state versus the new one to determine whether
11885 * watermarks need to be recalculated.
11886 *
11887 * Returns true or false.
11888 */
11889static bool intel_wm_need_update(struct drm_plane *plane,
11890 struct drm_plane_state *state)
11891{
Matt Roperd21fbe82015-09-24 15:53:12 -070011892 struct intel_plane_state *new = to_intel_plane_state(state);
11893 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
11894
11895 /* Update watermarks on tiling or size changes. */
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010011896 if (new->visible != cur->visible)
11897 return true;
11898
11899 if (!cur->base.fb || !new->base.fb)
11900 return false;
11901
11902 if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
11903 cur->base.rotation != new->base.rotation ||
Matt Roperd21fbe82015-09-24 15:53:12 -070011904 drm_rect_width(&new->src) != drm_rect_width(&cur->src) ||
11905 drm_rect_height(&new->src) != drm_rect_height(&cur->src) ||
11906 drm_rect_width(&new->dst) != drm_rect_width(&cur->dst) ||
11907 drm_rect_height(&new->dst) != drm_rect_height(&cur->dst))
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011908 return true;
11909
11910 return false;
11911}
11912
Matt Roperd21fbe82015-09-24 15:53:12 -070011913static bool needs_scaling(struct intel_plane_state *state)
11914{
11915 int src_w = drm_rect_width(&state->src) >> 16;
11916 int src_h = drm_rect_height(&state->src) >> 16;
11917 int dst_w = drm_rect_width(&state->dst);
11918 int dst_h = drm_rect_height(&state->dst);
11919
11920 return (src_w != dst_w || src_h != dst_h);
11921}
11922
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011923int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
11924 struct drm_plane_state *plane_state)
11925{
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010011926 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011927 struct drm_crtc *crtc = crtc_state->crtc;
11928 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11929 struct drm_plane *plane = plane_state->plane;
11930 struct drm_device *dev = crtc->dev;
Matt Ropered4a6a72016-02-23 17:20:13 -080011931 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011932 struct intel_plane_state *old_plane_state =
11933 to_intel_plane_state(plane->state);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011934 bool mode_changed = needs_modeset(crtc_state);
11935 bool was_crtc_enabled = crtc->state->active;
11936 bool is_crtc_enabled = crtc_state->active;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011937 bool turn_off, turn_on, visible, was_visible;
11938 struct drm_framebuffer *fb = plane_state->fb;
Ville Syrjälä78108b72016-05-27 20:59:19 +030011939 int ret;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011940
Chris Wilson84114992016-07-02 15:36:06 +010011941 if (INTEL_GEN(dev) >= 9 && plane->type != DRM_PLANE_TYPE_CURSOR) {
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011942 ret = skl_update_scaler_plane(
11943 to_intel_crtc_state(crtc_state),
11944 to_intel_plane_state(plane_state));
11945 if (ret)
11946 return ret;
11947 }
11948
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011949 was_visible = old_plane_state->visible;
11950 visible = to_intel_plane_state(plane_state)->visible;
11951
11952 if (!was_crtc_enabled && WARN_ON(was_visible))
11953 was_visible = false;
11954
Maarten Lankhorst35c08f42015-12-03 14:31:07 +010011955 /*
11956 * Visibility is calculated as if the crtc was on, but
11957 * after scaler setup everything depends on it being off
11958 * when the crtc isn't active.
Ville Syrjäläf818ffe2016-04-29 17:31:18 +030011959 *
11960 * FIXME this is wrong for watermarks. Watermarks should also
11961 * be computed as if the pipe would be active. Perhaps move
11962 * per-plane wm computation to the .check_plane() hook, and
11963 * only combine the results from all planes in the current place?
Maarten Lankhorst35c08f42015-12-03 14:31:07 +010011964 */
11965 if (!is_crtc_enabled)
11966 to_intel_plane_state(plane_state)->visible = visible = false;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011967
11968 if (!was_visible && !visible)
11969 return 0;
11970
Maarten Lankhorste8861672016-02-24 11:24:26 +010011971 if (fb != old_plane_state->base.fb)
11972 pipe_config->fb_changed = true;
11973
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011974 turn_off = was_visible && (!visible || mode_changed);
11975 turn_on = visible && (!was_visible || mode_changed);
11976
Ville Syrjälä72660ce2016-05-27 20:59:20 +030011977 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
Ville Syrjälä78108b72016-05-27 20:59:19 +030011978 intel_crtc->base.base.id,
11979 intel_crtc->base.name,
Ville Syrjälä72660ce2016-05-27 20:59:20 +030011980 plane->base.id, plane->name,
11981 fb ? fb->base.id : -1);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011982
Ville Syrjälä72660ce2016-05-27 20:59:20 +030011983 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
11984 plane->base.id, plane->name,
11985 was_visible, visible,
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011986 turn_off, turn_on, mode_changed);
11987
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011988 if (turn_on) {
11989 pipe_config->update_wm_pre = true;
11990
11991 /* must disable cxsr around plane enable/disable */
11992 if (plane->type != DRM_PLANE_TYPE_CURSOR)
11993 pipe_config->disable_cxsr = true;
11994 } else if (turn_off) {
11995 pipe_config->update_wm_post = true;
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010011996
Ville Syrjälä852eb002015-06-24 22:00:07 +030011997 /* must disable cxsr around plane enable/disable */
Maarten Lankhorste8861672016-02-24 11:24:26 +010011998 if (plane->type != DRM_PLANE_TYPE_CURSOR)
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010011999 pipe_config->disable_cxsr = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030012000 } else if (intel_wm_need_update(plane, plane_state)) {
Ville Syrjäläcaed3612016-03-09 19:07:25 +020012001 /* FIXME bollocks */
12002 pipe_config->update_wm_pre = true;
12003 pipe_config->update_wm_post = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030012004 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012005
Matt Ropered4a6a72016-02-23 17:20:13 -080012006 /* Pre-gen9 platforms need two-step watermark updates */
Ville Syrjäläcaed3612016-03-09 19:07:25 +020012007 if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
12008 INTEL_INFO(dev)->gen < 9 && dev_priv->display.optimize_watermarks)
Matt Ropered4a6a72016-02-23 17:20:13 -080012009 to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
12010
Rodrigo Vivi8be6ca82015-08-24 16:38:23 -070012011 if (visible || was_visible)
Maarten Lankhorstcd202f62016-03-09 10:35:44 +010012012 pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030012013
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +010012014 /*
12015 * WaCxSRDisabledForSpriteScaling:ivb
12016 *
12017 * cstate->update_wm was already set above, so this flag will
12018 * take effect when we commit and program watermarks.
12019 */
12020 if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev) &&
12021 needs_scaling(to_intel_plane_state(plane_state)) &&
12022 !needs_scaling(old_plane_state))
12023 pipe_config->disable_lp_wm = true;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012024
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012025 return 0;
12026}
12027
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012028static bool encoders_cloneable(const struct intel_encoder *a,
12029 const struct intel_encoder *b)
12030{
12031 /* masks could be asymmetric, so check both ways */
12032 return a == b || (a->cloneable & (1 << b->type) &&
12033 b->cloneable & (1 << a->type));
12034}
12035
12036static bool check_single_encoder_cloning(struct drm_atomic_state *state,
12037 struct intel_crtc *crtc,
12038 struct intel_encoder *encoder)
12039{
12040 struct intel_encoder *source_encoder;
12041 struct drm_connector *connector;
12042 struct drm_connector_state *connector_state;
12043 int i;
12044
12045 for_each_connector_in_state(state, connector, connector_state, i) {
12046 if (connector_state->crtc != &crtc->base)
12047 continue;
12048
12049 source_encoder =
12050 to_intel_encoder(connector_state->best_encoder);
12051 if (!encoders_cloneable(encoder, source_encoder))
12052 return false;
12053 }
12054
12055 return true;
12056}
12057
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012058static int intel_crtc_atomic_check(struct drm_crtc *crtc,
12059 struct drm_crtc_state *crtc_state)
12060{
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020012061 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010012062 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012063 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020012064 struct intel_crtc_state *pipe_config =
12065 to_intel_crtc_state(crtc_state);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012066 struct drm_atomic_state *state = crtc_state->state;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012067 int ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012068 bool mode_changed = needs_modeset(crtc_state);
12069
Ville Syrjälä852eb002015-06-24 22:00:07 +030012070 if (mode_changed && !crtc_state->active)
Ville Syrjäläcaed3612016-03-09 19:07:25 +020012071 pipe_config->update_wm_post = true;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020012072
Maarten Lankhorstad421372015-06-15 12:33:42 +020012073 if (mode_changed && crtc_state->enable &&
12074 dev_priv->display.crtc_compute_clock &&
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012075 !WARN_ON(pipe_config->shared_dpll)) {
Maarten Lankhorstad421372015-06-15 12:33:42 +020012076 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
12077 pipe_config);
12078 if (ret)
12079 return ret;
12080 }
12081
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000012082 if (crtc_state->color_mgmt_changed) {
12083 ret = intel_color_check(crtc, crtc_state);
12084 if (ret)
12085 return ret;
Lionel Landwerlined2eebb2016-05-25 14:30:41 +010012086
12087 /*
12088 * Changing color management on Intel hardware is
12089 * handled as part of planes update.
12090 */
12091 crtc_state->planes_changed = true;
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000012092 }
12093
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020012094 ret = 0;
Matt Roper86c8bbb2015-09-24 15:53:16 -070012095 if (dev_priv->display.compute_pipe_wm) {
Maarten Lankhorste3bddde2016-03-01 11:07:22 +010012096 ret = dev_priv->display.compute_pipe_wm(pipe_config);
Matt Ropered4a6a72016-02-23 17:20:13 -080012097 if (ret) {
12098 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
Matt Roper86c8bbb2015-09-24 15:53:16 -070012099 return ret;
Matt Ropered4a6a72016-02-23 17:20:13 -080012100 }
12101 }
12102
12103 if (dev_priv->display.compute_intermediate_wm &&
12104 !to_intel_atomic_state(state)->skip_intermediate_wm) {
12105 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
12106 return 0;
12107
12108 /*
12109 * Calculate 'intermediate' watermarks that satisfy both the
12110 * old state and the new state. We can program these
12111 * immediately.
12112 */
12113 ret = dev_priv->display.compute_intermediate_wm(crtc->dev,
12114 intel_crtc,
12115 pipe_config);
12116 if (ret) {
12117 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
12118 return ret;
12119 }
Ville Syrjäläe3d54572016-05-13 10:10:42 -070012120 } else if (dev_priv->display.compute_intermediate_wm) {
12121 if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
12122 pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
Matt Roper86c8bbb2015-09-24 15:53:16 -070012123 }
12124
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020012125 if (INTEL_INFO(dev)->gen >= 9) {
12126 if (mode_changed)
12127 ret = skl_update_scaler_crtc(pipe_config);
12128
12129 if (!ret)
12130 ret = intel_atomic_setup_scalers(dev, intel_crtc,
12131 pipe_config);
12132 }
12133
12134 return ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012135}
12136
Jani Nikula65b38e02015-04-13 11:26:56 +030012137static const struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012138 .mode_set_base_atomic = intel_pipe_set_base_atomic,
Daniel Vetter5a21b662016-05-24 17:13:53 +020012139 .atomic_begin = intel_begin_crtc_commit,
12140 .atomic_flush = intel_finish_crtc_commit,
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012141 .atomic_check = intel_crtc_atomic_check,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012142};
12143
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020012144static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
12145{
12146 struct intel_connector *connector;
12147
12148 for_each_intel_connector(dev, connector) {
Daniel Vetter8863dc72016-05-06 15:39:03 +020012149 if (connector->base.state->crtc)
12150 drm_connector_unreference(&connector->base);
12151
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020012152 if (connector->base.encoder) {
12153 connector->base.state->best_encoder =
12154 connector->base.encoder;
12155 connector->base.state->crtc =
12156 connector->base.encoder->crtc;
Daniel Vetter8863dc72016-05-06 15:39:03 +020012157
12158 drm_connector_reference(&connector->base);
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020012159 } else {
12160 connector->base.state->best_encoder = NULL;
12161 connector->base.state->crtc = NULL;
12162 }
12163 }
12164}
12165
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012166static void
Robin Schroereba905b2014-05-18 02:24:50 +020012167connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012168 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012169{
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012170 int bpp = pipe_config->pipe_bpp;
12171
12172 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
12173 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030012174 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012175
12176 /* Don't use an invalid EDID bpc value */
12177 if (connector->base.display_info.bpc &&
12178 connector->base.display_info.bpc * 3 < bpp) {
12179 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
12180 bpp, connector->base.display_info.bpc*3);
12181 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
12182 }
12183
Mario Kleiner196f9542016-07-06 12:05:45 +020012184 /* Clamp bpp to 8 on screens without EDID 1.4 */
12185 if (connector->base.display_info.bpc == 0 && bpp > 24) {
12186 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
12187 bpp);
12188 pipe_config->pipe_bpp = 24;
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012189 }
12190}
12191
12192static int
12193compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012194 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012195{
12196 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012197 struct drm_atomic_state *state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012198 struct drm_connector *connector;
12199 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012200 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012201
Wayne Boyer666a4532015-12-09 12:29:35 -080012202 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012203 bpp = 10*3;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012204 else if (INTEL_INFO(dev)->gen >= 5)
12205 bpp = 12*3;
12206 else
12207 bpp = 8*3;
12208
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012209
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012210 pipe_config->pipe_bpp = bpp;
12211
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012212 state = pipe_config->base.state;
12213
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012214 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012215 for_each_connector_in_state(state, connector, connector_state, i) {
12216 if (connector_state->crtc != &crtc->base)
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012217 continue;
12218
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012219 connected_sink_compute_bpp(to_intel_connector(connector),
12220 pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012221 }
12222
12223 return bpp;
12224}
12225
Daniel Vetter644db712013-09-19 14:53:58 +020012226static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
12227{
12228 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
12229 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010012230 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020012231 mode->crtc_hdisplay, mode->crtc_hsync_start,
12232 mode->crtc_hsync_end, mode->crtc_htotal,
12233 mode->crtc_vdisplay, mode->crtc_vsync_start,
12234 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
12235}
12236
Daniel Vetterc0b03412013-05-28 12:05:54 +020012237static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012238 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012239 const char *context)
12240{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012241 struct drm_device *dev = crtc->base.dev;
12242 struct drm_plane *plane;
12243 struct intel_plane *intel_plane;
12244 struct intel_plane_state *state;
12245 struct drm_framebuffer *fb;
12246
Ville Syrjälä78108b72016-05-27 20:59:19 +030012247 DRM_DEBUG_KMS("[CRTC:%d:%s]%s config %p for pipe %c\n",
12248 crtc->base.base.id, crtc->base.name,
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012249 context, pipe_config, pipe_name(crtc->pipe));
Daniel Vetterc0b03412013-05-28 12:05:54 +020012250
Jani Nikulada205632016-03-15 21:51:10 +020012251 DRM_DEBUG_KMS("cpu_transcoder: %s\n", transcoder_name(pipe_config->cpu_transcoder));
Daniel Vetterc0b03412013-05-28 12:05:54 +020012252 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
12253 pipe_config->pipe_bpp, pipe_config->dither);
12254 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
12255 pipe_config->has_pch_encoder,
12256 pipe_config->fdi_lanes,
12257 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
12258 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
12259 pipe_config->fdi_m_n.tu);
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012260 DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
Ville Syrjälä37a56502016-06-22 21:57:04 +030012261 intel_crtc_has_dp_encoder(pipe_config),
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012262 pipe_config->lane_count,
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012263 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
12264 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
12265 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012266
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012267 DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
Ville Syrjälä37a56502016-06-22 21:57:04 +030012268 intel_crtc_has_dp_encoder(pipe_config),
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012269 pipe_config->lane_count,
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012270 pipe_config->dp_m2_n2.gmch_m,
12271 pipe_config->dp_m2_n2.gmch_n,
12272 pipe_config->dp_m2_n2.link_m,
12273 pipe_config->dp_m2_n2.link_n,
12274 pipe_config->dp_m2_n2.tu);
12275
Daniel Vetter55072d12014-11-20 16:10:28 +010012276 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
12277 pipe_config->has_audio,
12278 pipe_config->has_infoframe);
12279
Daniel Vetterc0b03412013-05-28 12:05:54 +020012280 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012281 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012282 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012283 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
12284 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030012285 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030012286 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
12287 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Tvrtko Ursulin0ec463d2015-05-13 16:51:08 +010012288 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
12289 crtc->num_scalers,
12290 pipe_config->scaler_state.scaler_users,
12291 pipe_config->scaler_state.scaler_id);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012292 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
12293 pipe_config->gmch_pfit.control,
12294 pipe_config->gmch_pfit.pgm_ratios,
12295 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012296 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020012297 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012298 pipe_config->pch_pfit.size,
12299 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012300 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030012301 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012302
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012303 if (IS_BROXTON(dev)) {
Imre Deak05712c12015-06-18 17:25:54 +030012304 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012305 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
Imre Deakc8453332015-06-18 17:25:55 +030012306 "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012307 pipe_config->ddi_pll_sel,
12308 pipe_config->dpll_hw_state.ebb0,
Imre Deak05712c12015-06-18 17:25:54 +030012309 pipe_config->dpll_hw_state.ebb4,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012310 pipe_config->dpll_hw_state.pll0,
12311 pipe_config->dpll_hw_state.pll1,
12312 pipe_config->dpll_hw_state.pll2,
12313 pipe_config->dpll_hw_state.pll3,
12314 pipe_config->dpll_hw_state.pll6,
12315 pipe_config->dpll_hw_state.pll8,
Imre Deak05712c12015-06-18 17:25:54 +030012316 pipe_config->dpll_hw_state.pll9,
Imre Deakc8453332015-06-18 17:25:55 +030012317 pipe_config->dpll_hw_state.pll10,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012318 pipe_config->dpll_hw_state.pcsdw12);
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070012319 } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012320 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
12321 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
12322 pipe_config->ddi_pll_sel,
12323 pipe_config->dpll_hw_state.ctrl1,
12324 pipe_config->dpll_hw_state.cfgcr1,
12325 pipe_config->dpll_hw_state.cfgcr2);
12326 } else if (HAS_DDI(dev)) {
Ville Syrjälä1260f072016-02-17 21:41:08 +020012327 DRM_DEBUG_KMS("ddi_pll_sel: 0x%x; dpll_hw_state: wrpll: 0x%x spll: 0x%x\n",
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012328 pipe_config->ddi_pll_sel,
Maarten Lankhorst00490c22015-11-16 14:42:12 +010012329 pipe_config->dpll_hw_state.wrpll,
12330 pipe_config->dpll_hw_state.spll);
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012331 } else {
12332 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
12333 "fp0: 0x%x, fp1: 0x%x\n",
12334 pipe_config->dpll_hw_state.dpll,
12335 pipe_config->dpll_hw_state.dpll_md,
12336 pipe_config->dpll_hw_state.fp0,
12337 pipe_config->dpll_hw_state.fp1);
12338 }
12339
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012340 DRM_DEBUG_KMS("planes on this crtc\n");
12341 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
12342 intel_plane = to_intel_plane(plane);
12343 if (intel_plane->pipe != crtc->pipe)
12344 continue;
12345
12346 state = to_intel_plane_state(plane->state);
12347 fb = state->base.fb;
12348 if (!fb) {
Ville Syrjälä1d577e02016-05-27 20:59:25 +030012349 DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
12350 plane->base.id, plane->name, state->scaler_id);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012351 continue;
12352 }
12353
Ville Syrjälä1d577e02016-05-27 20:59:25 +030012354 DRM_DEBUG_KMS("[PLANE:%d:%s] enabled",
12355 plane->base.id, plane->name);
12356 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = %s",
12357 fb->base.id, fb->width, fb->height,
12358 drm_get_format_name(fb->pixel_format));
12359 DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
12360 state->scaler_id,
12361 state->src.x1 >> 16, state->src.y1 >> 16,
12362 drm_rect_width(&state->src) >> 16,
12363 drm_rect_height(&state->src) >> 16,
12364 state->dst.x1, state->dst.y1,
12365 drm_rect_width(&state->dst),
12366 drm_rect_height(&state->dst));
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012367 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020012368}
12369
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012370static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012371{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012372 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012373 struct drm_connector *connector;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012374 unsigned int used_ports = 0;
12375
12376 /*
12377 * Walk the connector list instead of the encoder
12378 * list to detect the problem on ddi platforms
12379 * where there's just one encoder per digital port.
12380 */
Ville Syrjälä0bff4852015-12-10 18:22:31 +020012381 drm_for_each_connector(connector, dev) {
12382 struct drm_connector_state *connector_state;
12383 struct intel_encoder *encoder;
12384
12385 connector_state = drm_atomic_get_existing_connector_state(state, connector);
12386 if (!connector_state)
12387 connector_state = connector->state;
12388
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012389 if (!connector_state->best_encoder)
12390 continue;
12391
12392 encoder = to_intel_encoder(connector_state->best_encoder);
12393
12394 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012395
12396 switch (encoder->type) {
12397 unsigned int port_mask;
12398 case INTEL_OUTPUT_UNKNOWN:
12399 if (WARN_ON(!HAS_DDI(dev)))
12400 break;
Ville Syrjäläcca05022016-06-22 21:57:06 +030012401 case INTEL_OUTPUT_DP:
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012402 case INTEL_OUTPUT_HDMI:
12403 case INTEL_OUTPUT_EDP:
12404 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12405
12406 /* the same port mustn't appear more than once */
12407 if (used_ports & port_mask)
12408 return false;
12409
12410 used_ports |= port_mask;
12411 default:
12412 break;
12413 }
12414 }
12415
12416 return true;
12417}
12418
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012419static void
12420clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12421{
12422 struct drm_crtc_state tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012423 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012424 struct intel_dpll_hw_state dpll_hw_state;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012425 struct intel_shared_dpll *shared_dpll;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012426 uint32_t ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012427 bool force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012428
Ander Conselvan de Oliveira7546a382015-05-20 09:03:27 +030012429 /* FIXME: before the switch to atomic started, a new pipe_config was
12430 * kzalloc'd. Code that depends on any field being zero should be
12431 * fixed, so that the crtc_state can be safely duplicated. For now,
12432 * only fields that are know to not cause problems are preserved. */
12433
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012434 tmp_state = crtc_state->base;
Chandra Konduru663a3642015-04-07 15:28:41 -070012435 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012436 shared_dpll = crtc_state->shared_dpll;
12437 dpll_hw_state = crtc_state->dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012438 ddi_pll_sel = crtc_state->ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012439 force_thru = crtc_state->pch_pfit.force_thru;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012440
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012441 memset(crtc_state, 0, sizeof *crtc_state);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012442
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012443 crtc_state->base = tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012444 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012445 crtc_state->shared_dpll = shared_dpll;
12446 crtc_state->dpll_hw_state = dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012447 crtc_state->ddi_pll_sel = ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012448 crtc_state->pch_pfit.force_thru = force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012449}
12450
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012451static int
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012452intel_modeset_pipe_config(struct drm_crtc *crtc,
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012453 struct intel_crtc_state *pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +020012454{
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012455 struct drm_atomic_state *state = pipe_config->base.state;
Daniel Vetter7758a112012-07-08 19:40:39 +020012456 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012457 struct drm_connector *connector;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012458 struct drm_connector_state *connector_state;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012459 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012460 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010012461 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020012462
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012463 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020012464
Daniel Vettere143a212013-07-04 12:01:15 +020012465 pipe_config->cpu_transcoder =
12466 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012467
Imre Deak2960bc92013-07-30 13:36:32 +030012468 /*
12469 * Sanitize sync polarity flags based on requested ones. If neither
12470 * positive or negative polarity is requested, treat this as meaning
12471 * negative polarity.
12472 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012473 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012474 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012475 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012476
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012477 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012478 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012479 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012480
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012481 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12482 pipe_config);
12483 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012484 goto fail;
12485
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012486 /*
12487 * Determine the real pipe dimensions. Note that stereo modes can
12488 * increase the actual pipe size due to the frame doubling and
12489 * insertion of additional space for blanks between the frame. This
12490 * is stored in the crtc timings. We use the requested mode to do this
12491 * computation to clearly distinguish it from the adjusted mode, which
12492 * can be changed by the connectors in the below retry loop.
12493 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012494 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080012495 &pipe_config->pipe_src_w,
12496 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012497
Ville Syrjälä253c84c2016-06-22 21:57:01 +030012498 for_each_connector_in_state(state, connector, connector_state, i) {
12499 if (connector_state->crtc != crtc)
12500 continue;
12501
12502 encoder = to_intel_encoder(connector_state->best_encoder);
12503
Ville Syrjäläe25148d2016-06-22 21:57:09 +030012504 if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
12505 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
12506 goto fail;
12507 }
12508
Ville Syrjälä253c84c2016-06-22 21:57:01 +030012509 /*
12510 * Determine output_types before calling the .compute_config()
12511 * hooks so that the hooks can use this information safely.
12512 */
12513 pipe_config->output_types |= 1 << encoder->type;
12514 }
12515
Daniel Vettere29c22c2013-02-21 00:00:16 +010012516encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020012517 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020012518 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020012519 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012520
Daniel Vetter135c81b2013-07-21 21:37:09 +020012521 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012522 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12523 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020012524
Daniel Vetter7758a112012-07-08 19:40:39 +020012525 /* Pass our mode to the connectors and the CRTC to give them a chance to
12526 * adjust it according to limitations or connector properties, and also
12527 * a chance to reject the mode entirely.
12528 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012529 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012530 if (connector_state->crtc != crtc)
12531 continue;
12532
12533 encoder = to_intel_encoder(connector_state->best_encoder);
12534
Daniel Vetterefea6e82013-07-21 21:36:59 +020012535 if (!(encoder->compute_config(encoder, pipe_config))) {
12536 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020012537 goto fail;
12538 }
12539 }
12540
Daniel Vetterff9a6752013-06-01 17:16:21 +020012541 /* Set default port clock if not overwritten by the encoder. Needs to be
12542 * done afterwards in case the encoder adjusts the mode. */
12543 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012544 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010012545 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012546
Daniel Vettera43f6e02013-06-07 23:10:32 +020012547 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010012548 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020012549 DRM_DEBUG_KMS("CRTC fixup failed\n");
12550 goto fail;
12551 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010012552
12553 if (ret == RETRY) {
12554 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12555 ret = -EINVAL;
12556 goto fail;
12557 }
12558
12559 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12560 retry = false;
12561 goto encoder_retry;
12562 }
12563
Daniel Vettere8fa4272015-08-12 11:43:34 +020012564 /* Dithering seems to not pass-through bits correctly when it should, so
12565 * only enable it on 6bpc panels. */
12566 pipe_config->dither = pipe_config->pipe_bpp == 6*3;
Daniel Vetter62f0ace2015-08-26 18:57:26 +020012567 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012568 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012569
Daniel Vetter7758a112012-07-08 19:40:39 +020012570fail:
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012571 return ret;
Daniel Vetter7758a112012-07-08 19:40:39 +020012572}
12573
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012574static void
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020012575intel_modeset_update_crtc_state(struct drm_atomic_state *state)
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012576{
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012577 struct drm_crtc *crtc;
12578 struct drm_crtc_state *crtc_state;
Maarten Lankhorst8a75d152015-07-13 16:30:14 +020012579 int i;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012580
Ville Syrjälä76688512014-01-10 11:28:06 +020012581 /* Double check state. */
Maarten Lankhorst8a75d152015-07-13 16:30:14 +020012582 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst3cb480b2015-06-01 12:49:49 +020012583 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
Maarten Lankhorstfc467a222015-06-01 12:50:07 +020012584
12585 /* Update hwmode for vblank functions */
12586 if (crtc->state->active)
12587 crtc->hwmode = crtc->state->adjusted_mode;
12588 else
12589 crtc->hwmode.crtc_clock = 0;
Maarten Lankhorst61067a52015-09-23 16:29:36 +020012590
12591 /*
12592 * Update legacy state to satisfy fbc code. This can
12593 * be removed when fbc uses the atomic state.
12594 */
12595 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
12596 struct drm_plane_state *plane_state = crtc->primary->state;
12597
12598 crtc->primary->fb = plane_state->fb;
12599 crtc->x = plane_state->src_x >> 16;
12600 crtc->y = plane_state->src_y >> 16;
12601 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012602 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012603}
12604
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012605static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012606{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012607 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012608
12609 if (clock1 == clock2)
12610 return true;
12611
12612 if (!clock1 || !clock2)
12613 return false;
12614
12615 diff = abs(clock1 - clock2);
12616
12617 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12618 return true;
12619
12620 return false;
12621}
12622
Daniel Vetter25c5b262012-07-08 22:08:04 +020012623#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12624 list_for_each_entry((intel_crtc), \
12625 &(dev)->mode_config.crtc_list, \
12626 base.head) \
Jani Nikula95150bd2015-11-24 21:21:56 +020012627 for_each_if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020012628
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012629static bool
12630intel_compare_m_n(unsigned int m, unsigned int n,
12631 unsigned int m2, unsigned int n2,
12632 bool exact)
12633{
12634 if (m == m2 && n == n2)
12635 return true;
12636
12637 if (exact || !m || !n || !m2 || !n2)
12638 return false;
12639
12640 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
12641
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012642 if (n > n2) {
12643 while (n > n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012644 m2 <<= 1;
12645 n2 <<= 1;
12646 }
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012647 } else if (n < n2) {
12648 while (n < n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012649 m <<= 1;
12650 n <<= 1;
12651 }
12652 }
12653
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012654 if (n != n2)
12655 return false;
12656
12657 return intel_fuzzy_clock_check(m, m2);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012658}
12659
12660static bool
12661intel_compare_link_m_n(const struct intel_link_m_n *m_n,
12662 struct intel_link_m_n *m2_n2,
12663 bool adjust)
12664{
12665 if (m_n->tu == m2_n2->tu &&
12666 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
12667 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
12668 intel_compare_m_n(m_n->link_m, m_n->link_n,
12669 m2_n2->link_m, m2_n2->link_n, !adjust)) {
12670 if (adjust)
12671 *m2_n2 = *m_n;
12672
12673 return true;
12674 }
12675
12676 return false;
12677}
12678
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012679static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012680intel_pipe_config_compare(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012681 struct intel_crtc_state *current_config,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012682 struct intel_crtc_state *pipe_config,
12683 bool adjust)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012684{
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012685 bool ret = true;
12686
12687#define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
12688 do { \
12689 if (!adjust) \
12690 DRM_ERROR(fmt, ##__VA_ARGS__); \
12691 else \
12692 DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
12693 } while (0)
12694
Daniel Vetter66e985c2013-06-05 13:34:20 +020012695#define PIPE_CONF_CHECK_X(name) \
12696 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012697 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012698 "(expected 0x%08x, found 0x%08x)\n", \
12699 current_config->name, \
12700 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012701 ret = false; \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012702 }
12703
Daniel Vetter08a24032013-04-19 11:25:34 +020012704#define PIPE_CONF_CHECK_I(name) \
12705 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012706 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter08a24032013-04-19 11:25:34 +020012707 "(expected %i, found %i)\n", \
12708 current_config->name, \
12709 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012710 ret = false; \
12711 }
12712
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012713#define PIPE_CONF_CHECK_P(name) \
12714 if (current_config->name != pipe_config->name) { \
12715 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12716 "(expected %p, found %p)\n", \
12717 current_config->name, \
12718 pipe_config->name); \
12719 ret = false; \
12720 }
12721
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012722#define PIPE_CONF_CHECK_M_N(name) \
12723 if (!intel_compare_link_m_n(&current_config->name, \
12724 &pipe_config->name,\
12725 adjust)) { \
12726 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12727 "(expected tu %i gmch %i/%i link %i/%i, " \
12728 "found tu %i, gmch %i/%i link %i/%i)\n", \
12729 current_config->name.tu, \
12730 current_config->name.gmch_m, \
12731 current_config->name.gmch_n, \
12732 current_config->name.link_m, \
12733 current_config->name.link_n, \
12734 pipe_config->name.tu, \
12735 pipe_config->name.gmch_m, \
12736 pipe_config->name.gmch_n, \
12737 pipe_config->name.link_m, \
12738 pipe_config->name.link_n); \
12739 ret = false; \
12740 }
12741
Daniel Vetter55c561a2016-03-30 11:34:36 +020012742/* This is required for BDW+ where there is only one set of registers for
12743 * switching between high and low RR.
12744 * This macro can be used whenever a comparison has to be made between one
12745 * hw state and multiple sw state variables.
12746 */
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012747#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
12748 if (!intel_compare_link_m_n(&current_config->name, \
12749 &pipe_config->name, adjust) && \
12750 !intel_compare_link_m_n(&current_config->alt_name, \
12751 &pipe_config->name, adjust)) { \
12752 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12753 "(expected tu %i gmch %i/%i link %i/%i, " \
12754 "or tu %i gmch %i/%i link %i/%i, " \
12755 "found tu %i, gmch %i/%i link %i/%i)\n", \
12756 current_config->name.tu, \
12757 current_config->name.gmch_m, \
12758 current_config->name.gmch_n, \
12759 current_config->name.link_m, \
12760 current_config->name.link_n, \
12761 current_config->alt_name.tu, \
12762 current_config->alt_name.gmch_m, \
12763 current_config->alt_name.gmch_n, \
12764 current_config->alt_name.link_m, \
12765 current_config->alt_name.link_n, \
12766 pipe_config->name.tu, \
12767 pipe_config->name.gmch_m, \
12768 pipe_config->name.gmch_n, \
12769 pipe_config->name.link_m, \
12770 pipe_config->name.link_n); \
12771 ret = false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010012772 }
12773
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012774#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12775 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012776 INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012777 "(expected %i, found %i)\n", \
12778 current_config->name & (mask), \
12779 pipe_config->name & (mask)); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012780 ret = false; \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012781 }
12782
Ville Syrjälä5e550652013-09-06 23:29:07 +030012783#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12784 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012785 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012786 "(expected %i, found %i)\n", \
12787 current_config->name, \
12788 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012789 ret = false; \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012790 }
12791
Daniel Vetterbb760062013-06-06 14:55:52 +020012792#define PIPE_CONF_QUIRK(quirk) \
12793 ((current_config->quirks | pipe_config->quirks) & (quirk))
12794
Daniel Vettereccb1402013-05-22 00:50:22 +020012795 PIPE_CONF_CHECK_I(cpu_transcoder);
12796
Daniel Vetter08a24032013-04-19 11:25:34 +020012797 PIPE_CONF_CHECK_I(has_pch_encoder);
12798 PIPE_CONF_CHECK_I(fdi_lanes);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012799 PIPE_CONF_CHECK_M_N(fdi_m_n);
Daniel Vetter08a24032013-04-19 11:25:34 +020012800
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012801 PIPE_CONF_CHECK_I(lane_count);
Imre Deak95a7a2a2016-06-13 16:44:35 +030012802 PIPE_CONF_CHECK_X(lane_lat_optim_mask);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012803
12804 if (INTEL_INFO(dev)->gen < 8) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012805 PIPE_CONF_CHECK_M_N(dp_m_n);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012806
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012807 if (current_config->has_drrs)
12808 PIPE_CONF_CHECK_M_N(dp_m2_n2);
12809 } else
12810 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012811
Ville Syrjälä253c84c2016-06-22 21:57:01 +030012812 PIPE_CONF_CHECK_X(output_types);
Jani Nikulaa65347b2015-11-27 12:21:46 +020012813
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012814 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12815 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12816 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12817 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12818 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12819 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012820
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012821 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12822 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12823 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12824 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12825 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12826 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012827
Daniel Vetterc93f54c2013-06-27 19:47:19 +020012828 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b2014-04-24 23:54:47 +020012829 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012830 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
Wayne Boyer666a4532015-12-09 12:29:35 -080012831 IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012832 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080012833 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020012834
Daniel Vetter9ed109a2014-04-24 23:54:52 +020012835 PIPE_CONF_CHECK_I(has_audio);
12836
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012837 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012838 DRM_MODE_FLAG_INTERLACE);
12839
Daniel Vetterbb760062013-06-06 14:55:52 +020012840 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012841 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012842 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012843 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012844 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012845 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012846 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012847 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012848 DRM_MODE_FLAG_NVSYNC);
12849 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012850
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030012851 PIPE_CONF_CHECK_X(gmch_pfit.control);
Daniel Vettere2ff2d42015-07-15 14:15:50 +020012852 /* pfit ratios are autocomputed by the hw on gen4+ */
12853 if (INTEL_INFO(dev)->gen < 4)
Ville Syrjälä7f7d8dd2016-03-15 16:40:07 +020012854 PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030012855 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
Daniel Vetter99535992014-04-13 12:00:33 +020012856
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020012857 if (!adjust) {
12858 PIPE_CONF_CHECK_I(pipe_src_w);
12859 PIPE_CONF_CHECK_I(pipe_src_h);
12860
12861 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12862 if (current_config->pch_pfit.enabled) {
12863 PIPE_CONF_CHECK_X(pch_pfit.pos);
12864 PIPE_CONF_CHECK_X(pch_pfit.size);
12865 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012866
Maarten Lankhorst7aefe2b2015-09-14 11:30:10 +020012867 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12868 }
Chandra Kondurua1b22782015-04-07 15:28:45 -070012869
Jesse Barnese59150d2014-01-07 13:30:45 -080012870 /* BDW+ don't expose a synchronous way to read the state */
12871 if (IS_HASWELL(dev))
12872 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012873
Ville Syrjälä282740f2013-09-04 18:30:03 +030012874 PIPE_CONF_CHECK_I(double_wide);
12875
Daniel Vetter26804af2014-06-25 22:01:55 +030012876 PIPE_CONF_CHECK_X(ddi_pll_sel);
12877
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012878 PIPE_CONF_CHECK_P(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012879 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020012880 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012881 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12882 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030012883 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Maarten Lankhorst00490c22015-11-16 14:42:12 +010012884 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000012885 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12886 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12887 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012888
Ville Syrjälä47eacba2016-04-12 22:14:35 +030012889 PIPE_CONF_CHECK_X(dsi_pll.ctrl);
12890 PIPE_CONF_CHECK_X(dsi_pll.div);
12891
Ville Syrjälä42571ae2013-09-06 23:29:00 +030012892 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12893 PIPE_CONF_CHECK_I(pipe_bpp);
12894
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012895 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080012896 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030012897
Daniel Vetter66e985c2013-06-05 13:34:20 +020012898#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020012899#undef PIPE_CONF_CHECK_I
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012900#undef PIPE_CONF_CHECK_P
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012901#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030012902#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020012903#undef PIPE_CONF_QUIRK
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012904#undef INTEL_ERR_OR_DBG_KMS
Daniel Vetter627eb5a2013-04-29 19:33:42 +020012905
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012906 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012907}
12908
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020012909static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
12910 const struct intel_crtc_state *pipe_config)
12911{
12912 if (pipe_config->has_pch_encoder) {
Ville Syrjälä21a727b2016-02-17 21:41:10 +020012913 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020012914 &pipe_config->fdi_m_n);
12915 int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
12916
12917 /*
12918 * FDI already provided one idea for the dotclock.
12919 * Yell if the encoder disagrees.
12920 */
12921 WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
12922 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
12923 fdi_dotclock, dotclock);
12924 }
12925}
12926
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012927static void verify_wm_state(struct drm_crtc *crtc,
12928 struct drm_crtc_state *new_state)
Damien Lespiau08db6652014-11-04 17:06:52 +000012929{
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012930 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010012931 struct drm_i915_private *dev_priv = to_i915(dev);
Damien Lespiau08db6652014-11-04 17:06:52 +000012932 struct skl_ddb_allocation hw_ddb, *sw_ddb;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012933 struct skl_ddb_entry *hw_entry, *sw_entry;
12934 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12935 const enum pipe pipe = intel_crtc->pipe;
Damien Lespiau08db6652014-11-04 17:06:52 +000012936 int plane;
12937
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012938 if (INTEL_INFO(dev)->gen < 9 || !new_state->active)
Damien Lespiau08db6652014-11-04 17:06:52 +000012939 return;
12940
12941 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12942 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12943
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012944 /* planes */
12945 for_each_plane(dev_priv, pipe, plane) {
12946 hw_entry = &hw_ddb.plane[pipe][plane];
12947 sw_entry = &sw_ddb->plane[pipe][plane];
Damien Lespiau08db6652014-11-04 17:06:52 +000012948
12949 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12950 continue;
12951
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012952 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12953 "(expected (%u,%u), found (%u,%u))\n",
12954 pipe_name(pipe), plane + 1,
12955 sw_entry->start, sw_entry->end,
12956 hw_entry->start, hw_entry->end);
12957 }
12958
12959 /* cursor */
12960 hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
12961 sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
12962
12963 if (!skl_ddb_entry_equal(hw_entry, sw_entry)) {
Damien Lespiau08db6652014-11-04 17:06:52 +000012964 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12965 "(expected (%u,%u), found (%u,%u))\n",
12966 pipe_name(pipe),
12967 sw_entry->start, sw_entry->end,
12968 hw_entry->start, hw_entry->end);
12969 }
12970}
12971
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012972static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012973verify_connector_state(struct drm_device *dev, struct drm_crtc *crtc)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012974{
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012975 struct drm_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012976
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012977 drm_for_each_connector(connector, dev) {
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012978 struct drm_encoder *encoder = connector->encoder;
12979 struct drm_connector_state *state = connector->state;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012980
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012981 if (state->crtc != crtc)
12982 continue;
12983
Daniel Vetter5a21b662016-05-24 17:13:53 +020012984 intel_connector_verify_state(to_intel_connector(connector));
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012985
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012986 I915_STATE_WARN(state->best_encoder != encoder,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012987 "connector's atomic encoder doesn't match legacy encoder\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012988 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012989}
12990
12991static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012992verify_encoder_state(struct drm_device *dev)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012993{
12994 struct intel_encoder *encoder;
12995 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012996
Damien Lespiaub2784e12014-08-05 11:29:37 +010012997 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012998 bool enabled = false;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012999 enum pipe pipe;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013000
13001 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
13002 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030013003 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013004
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020013005 for_each_intel_connector(dev, connector) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013006 if (connector->base.state->best_encoder != &encoder->base)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013007 continue;
13008 enabled = true;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020013009
13010 I915_STATE_WARN(connector->base.state->crtc !=
13011 encoder->base.crtc,
13012 "connector's crtc doesn't match encoder crtc\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013013 }
Dave Airlie0e32b392014-05-02 14:02:48 +100013014
Rob Clarke2c719b2014-12-15 13:56:32 -050013015 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013016 "encoder's enabled state mismatch "
13017 "(expected %i, found %i)\n",
13018 !!encoder->base.crtc, enabled);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020013019
13020 if (!encoder->base.crtc) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013021 bool active;
13022
13023 active = encoder->get_hw_state(encoder, &pipe);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020013024 I915_STATE_WARN(active,
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013025 "encoder detached but still enabled on pipe %c.\n",
13026 pipe_name(pipe));
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020013027 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013028 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013029}
13030
13031static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013032verify_crtc_state(struct drm_crtc *crtc,
13033 struct drm_crtc_state *old_crtc_state,
13034 struct drm_crtc_state *new_crtc_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013035{
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013036 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010013037 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013038 struct intel_encoder *encoder;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013039 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13040 struct intel_crtc_state *pipe_config, *sw_config;
13041 struct drm_atomic_state *old_state;
13042 bool active;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013043
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013044 old_state = old_crtc_state->state;
Daniel Vetterec2dc6a2016-05-09 16:34:09 +020013045 __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013046 pipe_config = to_intel_crtc_state(old_crtc_state);
13047 memset(pipe_config, 0, sizeof(*pipe_config));
13048 pipe_config->base.crtc = crtc;
13049 pipe_config->base.state = old_state;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013050
Ville Syrjälä78108b72016-05-27 20:59:19 +030013051 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013052
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013053 active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013054
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013055 /* hw state is inconsistent with the pipe quirk */
13056 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
13057 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
13058 active = new_crtc_state->active;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013059
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013060 I915_STATE_WARN(new_crtc_state->active != active,
13061 "crtc active state doesn't match with hw state "
13062 "(expected %i, found %i)\n", new_crtc_state->active, active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013063
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013064 I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
13065 "transitional active state does not match atomic hw state "
13066 "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013067
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013068 for_each_encoder_on_crtc(dev, crtc, encoder) {
13069 enum pipe pipe;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013070
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013071 active = encoder->get_hw_state(encoder, &pipe);
13072 I915_STATE_WARN(active != new_crtc_state->active,
13073 "[ENCODER:%i] active %i with crtc active %i\n",
13074 encoder->base.base.id, active, new_crtc_state->active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013075
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013076 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
13077 "Encoder connected to wrong pipe %c\n",
13078 pipe_name(pipe));
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013079
Ville Syrjälä253c84c2016-06-22 21:57:01 +030013080 if (active) {
13081 pipe_config->output_types |= 1 << encoder->type;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013082 encoder->get_config(encoder, pipe_config);
Ville Syrjälä253c84c2016-06-22 21:57:01 +030013083 }
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013084 }
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013085
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013086 if (!new_crtc_state->active)
13087 return;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013088
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013089 intel_pipe_config_sanity_check(dev_priv, pipe_config);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013090
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013091 sw_config = to_intel_crtc_state(crtc->state);
13092 if (!intel_pipe_config_compare(dev, sw_config,
13093 pipe_config, false)) {
13094 I915_STATE_WARN(1, "pipe state doesn't match!\n");
13095 intel_dump_pipe_config(intel_crtc, pipe_config,
13096 "[hw state]");
13097 intel_dump_pipe_config(intel_crtc, sw_config,
13098 "[sw state]");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013099 }
13100}
13101
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013102static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013103verify_single_dpll_state(struct drm_i915_private *dev_priv,
13104 struct intel_shared_dpll *pll,
13105 struct drm_crtc *crtc,
13106 struct drm_crtc_state *new_state)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013107{
13108 struct intel_dpll_hw_state dpll_hw_state;
13109 unsigned crtc_mask;
13110 bool active;
13111
13112 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
13113
13114 DRM_DEBUG_KMS("%s\n", pll->name);
13115
13116 active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
13117
13118 if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
13119 I915_STATE_WARN(!pll->on && pll->active_mask,
13120 "pll in active use but not on in sw tracking\n");
13121 I915_STATE_WARN(pll->on && !pll->active_mask,
13122 "pll is on but not used by any active crtc\n");
13123 I915_STATE_WARN(pll->on != active,
13124 "pll on state mismatch (expected %i, found %i)\n",
13125 pll->on, active);
13126 }
13127
13128 if (!crtc) {
13129 I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
13130 "more active pll users than references: %x vs %x\n",
13131 pll->active_mask, pll->config.crtc_mask);
13132
13133 return;
13134 }
13135
13136 crtc_mask = 1 << drm_crtc_index(crtc);
13137
13138 if (new_state->active)
13139 I915_STATE_WARN(!(pll->active_mask & crtc_mask),
13140 "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
13141 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
13142 else
13143 I915_STATE_WARN(pll->active_mask & crtc_mask,
13144 "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
13145 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
13146
13147 I915_STATE_WARN(!(pll->config.crtc_mask & crtc_mask),
13148 "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
13149 crtc_mask, pll->config.crtc_mask);
13150
13151 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state,
13152 &dpll_hw_state,
13153 sizeof(dpll_hw_state)),
13154 "pll hw state mismatch\n");
13155}
13156
13157static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013158verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
13159 struct drm_crtc_state *old_crtc_state,
13160 struct drm_crtc_state *new_crtc_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013161{
Chris Wilsonfac5e232016-07-04 11:34:36 +010013162 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013163 struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
13164 struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
13165
13166 if (new_state->shared_dpll)
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013167 verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013168
13169 if (old_state->shared_dpll &&
13170 old_state->shared_dpll != new_state->shared_dpll) {
13171 unsigned crtc_mask = 1 << drm_crtc_index(crtc);
13172 struct intel_shared_dpll *pll = old_state->shared_dpll;
13173
13174 I915_STATE_WARN(pll->active_mask & crtc_mask,
13175 "pll active mismatch (didn't expect pipe %c in active mask)\n",
13176 pipe_name(drm_crtc_index(crtc)));
13177 I915_STATE_WARN(pll->config.crtc_mask & crtc_mask,
13178 "pll enabled crtcs mismatch (found %x in enabled mask)\n",
13179 pipe_name(drm_crtc_index(crtc)));
13180 }
13181}
13182
13183static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013184intel_modeset_verify_crtc(struct drm_crtc *crtc,
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013185 struct drm_crtc_state *old_state,
13186 struct drm_crtc_state *new_state)
13187{
Daniel Vetter5a21b662016-05-24 17:13:53 +020013188 if (!needs_modeset(new_state) &&
13189 !to_intel_crtc_state(new_state)->update_pipe)
13190 return;
13191
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013192 verify_wm_state(crtc, new_state);
Daniel Vetter5a21b662016-05-24 17:13:53 +020013193 verify_connector_state(crtc->dev, crtc);
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013194 verify_crtc_state(crtc, old_state, new_state);
13195 verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013196}
13197
13198static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013199verify_disabled_dpll_state(struct drm_device *dev)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013200{
Chris Wilsonfac5e232016-07-04 11:34:36 +010013201 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013202 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020013203
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013204 for (i = 0; i < dev_priv->num_shared_dpll; i++)
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013205 verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013206}
Daniel Vetter53589012013-06-05 13:34:16 +020013207
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013208static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013209intel_modeset_verify_disabled(struct drm_device *dev)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013210{
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013211 verify_encoder_state(dev);
13212 verify_connector_state(dev, NULL);
13213 verify_disabled_dpll_state(dev);
Daniel Vettere2e1ed42012-07-08 21:14:38 +020013214}
13215
Ville Syrjälä80715b22014-05-15 20:23:23 +030013216static void update_scanline_offset(struct intel_crtc *crtc)
13217{
13218 struct drm_device *dev = crtc->base.dev;
13219
13220 /*
13221 * The scanline counter increments at the leading edge of hsync.
13222 *
13223 * On most platforms it starts counting from vtotal-1 on the
13224 * first active line. That means the scanline counter value is
13225 * always one less than what we would expect. Ie. just after
13226 * start of vblank, which also occurs at start of hsync (on the
13227 * last active line), the scanline counter will read vblank_start-1.
13228 *
13229 * On gen2 the scanline counter starts counting from 1 instead
13230 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
13231 * to keep the value positive), instead of adding one.
13232 *
13233 * On HSW+ the behaviour of the scanline counter depends on the output
13234 * type. For DP ports it behaves like most other platforms, but on HDMI
13235 * there's an extra 1 line difference. So we need to add two instead of
13236 * one to the value.
13237 */
13238 if (IS_GEN2(dev)) {
Ville Syrjälä124abe02015-09-08 13:40:45 +030013239 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030013240 int vtotal;
13241
Ville Syrjälä124abe02015-09-08 13:40:45 +030013242 vtotal = adjusted_mode->crtc_vtotal;
13243 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
Ville Syrjälä80715b22014-05-15 20:23:23 +030013244 vtotal /= 2;
13245
13246 crtc->scanline_offset = vtotal - 1;
13247 } else if (HAS_DDI(dev) &&
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +030013248 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030013249 crtc->scanline_offset = 2;
13250 } else
13251 crtc->scanline_offset = 1;
13252}
13253
Maarten Lankhorstad421372015-06-15 12:33:42 +020013254static void intel_modeset_clear_plls(struct drm_atomic_state *state)
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013255{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030013256 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013257 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstad421372015-06-15 12:33:42 +020013258 struct intel_shared_dpll_config *shared_dpll = NULL;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013259 struct drm_crtc *crtc;
13260 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013261 int i;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013262
13263 if (!dev_priv->display.crtc_compute_clock)
Maarten Lankhorstad421372015-06-15 12:33:42 +020013264 return;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013265
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013266 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013267 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013268 struct intel_shared_dpll *old_dpll =
13269 to_intel_crtc_state(crtc->state)->shared_dpll;
Maarten Lankhorstad421372015-06-15 12:33:42 +020013270
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013271 if (!needs_modeset(crtc_state))
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030013272 continue;
13273
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013274 to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013275
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013276 if (!old_dpll)
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013277 continue;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013278
Maarten Lankhorstad421372015-06-15 12:33:42 +020013279 if (!shared_dpll)
13280 shared_dpll = intel_atomic_get_shared_dpll_state(state);
13281
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013282 intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013283 }
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013284}
13285
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013286/*
13287 * This implements the workaround described in the "notes" section of the mode
13288 * set sequence documentation. When going from no pipes or single pipe to
13289 * multiple pipes, and planes are enabled after the pipe, we need to wait at
13290 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
13291 */
13292static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
13293{
13294 struct drm_crtc_state *crtc_state;
13295 struct intel_crtc *intel_crtc;
13296 struct drm_crtc *crtc;
13297 struct intel_crtc_state *first_crtc_state = NULL;
13298 struct intel_crtc_state *other_crtc_state = NULL;
13299 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
13300 int i;
13301
13302 /* look at all crtc's that are going to be enabled in during modeset */
13303 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13304 intel_crtc = to_intel_crtc(crtc);
13305
13306 if (!crtc_state->active || !needs_modeset(crtc_state))
13307 continue;
13308
13309 if (first_crtc_state) {
13310 other_crtc_state = to_intel_crtc_state(crtc_state);
13311 break;
13312 } else {
13313 first_crtc_state = to_intel_crtc_state(crtc_state);
13314 first_pipe = intel_crtc->pipe;
13315 }
13316 }
13317
13318 /* No workaround needed? */
13319 if (!first_crtc_state)
13320 return 0;
13321
13322 /* w/a possibly needed, check how many crtc's are already enabled. */
13323 for_each_intel_crtc(state->dev, intel_crtc) {
13324 struct intel_crtc_state *pipe_config;
13325
13326 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
13327 if (IS_ERR(pipe_config))
13328 return PTR_ERR(pipe_config);
13329
13330 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
13331
13332 if (!pipe_config->base.active ||
13333 needs_modeset(&pipe_config->base))
13334 continue;
13335
13336 /* 2 or more enabled crtcs means no need for w/a */
13337 if (enabled_pipe != INVALID_PIPE)
13338 return 0;
13339
13340 enabled_pipe = intel_crtc->pipe;
13341 }
13342
13343 if (enabled_pipe != INVALID_PIPE)
13344 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
13345 else if (other_crtc_state)
13346 other_crtc_state->hsw_workaround_pipe = first_pipe;
13347
13348 return 0;
13349}
13350
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013351static int intel_modeset_all_pipes(struct drm_atomic_state *state)
13352{
13353 struct drm_crtc *crtc;
13354 struct drm_crtc_state *crtc_state;
13355 int ret = 0;
13356
13357 /* add all active pipes to the state */
13358 for_each_crtc(state->dev, crtc) {
13359 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13360 if (IS_ERR(crtc_state))
13361 return PTR_ERR(crtc_state);
13362
13363 if (!crtc_state->active || needs_modeset(crtc_state))
13364 continue;
13365
13366 crtc_state->mode_changed = true;
13367
13368 ret = drm_atomic_add_affected_connectors(state, crtc);
13369 if (ret)
13370 break;
13371
13372 ret = drm_atomic_add_affected_planes(state, crtc);
13373 if (ret)
13374 break;
13375 }
13376
13377 return ret;
13378}
13379
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013380static int intel_modeset_checks(struct drm_atomic_state *state)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013381{
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013382 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Chris Wilsonfac5e232016-07-04 11:34:36 +010013383 struct drm_i915_private *dev_priv = to_i915(state->dev);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013384 struct drm_crtc *crtc;
13385 struct drm_crtc_state *crtc_state;
13386 int ret = 0, i;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013387
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013388 if (!check_digital_port_conflicts(state)) {
13389 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
13390 return -EINVAL;
13391 }
13392
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013393 intel_state->modeset = true;
13394 intel_state->active_crtcs = dev_priv->active_crtcs;
13395
13396 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13397 if (crtc_state->active)
13398 intel_state->active_crtcs |= 1 << i;
13399 else
13400 intel_state->active_crtcs &= ~(1 << i);
Matt Roper8b4a7d02016-05-12 07:06:00 -070013401
13402 if (crtc_state->active != crtc->state->active)
13403 intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013404 }
13405
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013406 /*
13407 * See if the config requires any additional preparation, e.g.
13408 * to adjust global state with pipes off. We need to do this
13409 * here so we can get the modeset_pipe updated config for the new
13410 * mode set on this crtc. For other crtcs we need to use the
13411 * adjusted_mode bits in the crtc directly.
13412 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013413 if (dev_priv->display.modeset_calc_cdclk) {
Clint Taylorc89e39f2016-05-13 23:41:21 +030013414 if (!intel_state->cdclk_pll_vco)
Ville Syrjälä63911d72016-05-13 23:41:32 +030013415 intel_state->cdclk_pll_vco = dev_priv->cdclk_pll.vco;
Ville Syrjäläb2045352016-05-13 23:41:27 +030013416 if (!intel_state->cdclk_pll_vco)
13417 intel_state->cdclk_pll_vco = dev_priv->skl_preferred_vco_freq;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013418
Clint Taylorc89e39f2016-05-13 23:41:21 +030013419 ret = dev_priv->display.modeset_calc_cdclk(state);
13420 if (ret < 0)
13421 return ret;
13422
13423 if (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
Ville Syrjälä63911d72016-05-13 23:41:32 +030013424 intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco)
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013425 ret = intel_modeset_all_pipes(state);
13426
13427 if (ret < 0)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013428 return ret;
Maarten Lankhorste8788cb2016-02-16 10:25:11 +010013429
13430 DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
13431 intel_state->cdclk, intel_state->dev_cdclk);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013432 } else
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013433 to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013434
Maarten Lankhorstad421372015-06-15 12:33:42 +020013435 intel_modeset_clear_plls(state);
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013436
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013437 if (IS_HASWELL(dev_priv))
Maarten Lankhorstad421372015-06-15 12:33:42 +020013438 return haswell_mode_set_planes_workaround(state);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013439
Maarten Lankhorstad421372015-06-15 12:33:42 +020013440 return 0;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013441}
13442
Matt Roperaa363132015-09-24 15:53:18 -070013443/*
13444 * Handle calculation of various watermark data at the end of the atomic check
13445 * phase. The code here should be run after the per-crtc and per-plane 'check'
13446 * handlers to ensure that all derived state has been updated.
13447 */
Matt Roper55994c22016-05-12 07:06:08 -070013448static int calc_watermark_data(struct drm_atomic_state *state)
Matt Roperaa363132015-09-24 15:53:18 -070013449{
13450 struct drm_device *dev = state->dev;
Matt Roper98d39492016-05-12 07:06:03 -070013451 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roper98d39492016-05-12 07:06:03 -070013452
13453 /* Is there platform-specific watermark information to calculate? */
13454 if (dev_priv->display.compute_global_watermarks)
Matt Roper55994c22016-05-12 07:06:08 -070013455 return dev_priv->display.compute_global_watermarks(state);
13456
13457 return 0;
Matt Roperaa363132015-09-24 15:53:18 -070013458}
13459
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013460/**
13461 * intel_atomic_check - validate state object
13462 * @dev: drm device
13463 * @state: state to validate
13464 */
13465static int intel_atomic_check(struct drm_device *dev,
13466 struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020013467{
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013468 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roperaa363132015-09-24 15:53:18 -070013469 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013470 struct drm_crtc *crtc;
13471 struct drm_crtc_state *crtc_state;
13472 int ret, i;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013473 bool any_ms = false;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013474
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013475 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020013476 if (ret)
13477 return ret;
13478
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013479 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013480 struct intel_crtc_state *pipe_config =
13481 to_intel_crtc_state(crtc_state);
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013482
13483 /* Catch I915_MODE_FLAG_INHERITED */
13484 if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
13485 crtc_state->mode_changed = true;
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013486
Daniel Vetter26495482015-07-15 14:15:52 +020013487 if (!needs_modeset(crtc_state))
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013488 continue;
13489
Daniel Vetteraf4a8792016-05-09 09:31:25 +020013490 if (!crtc_state->enable) {
13491 any_ms = true;
13492 continue;
13493 }
13494
Daniel Vetter26495482015-07-15 14:15:52 +020013495 /* FIXME: For only active_changed we shouldn't need to do any
13496 * state recomputation at all. */
13497
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013498 ret = drm_atomic_add_affected_connectors(state, crtc);
13499 if (ret)
13500 return ret;
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013501
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013502 ret = intel_modeset_pipe_config(crtc, pipe_config);
Maarten Lankhorst25aa1c32016-05-03 10:30:38 +020013503 if (ret) {
13504 intel_dump_pipe_config(to_intel_crtc(crtc),
13505 pipe_config, "[failed]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013506 return ret;
Maarten Lankhorst25aa1c32016-05-03 10:30:38 +020013507 }
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013508
Jani Nikula73831232015-11-19 10:26:30 +020013509 if (i915.fastboot &&
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013510 intel_pipe_config_compare(dev,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013511 to_intel_crtc_state(crtc->state),
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013512 pipe_config, true)) {
Daniel Vetter26495482015-07-15 14:15:52 +020013513 crtc_state->mode_changed = false;
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013514 to_intel_crtc_state(crtc_state)->update_pipe = true;
Daniel Vetter26495482015-07-15 14:15:52 +020013515 }
13516
Daniel Vetteraf4a8792016-05-09 09:31:25 +020013517 if (needs_modeset(crtc_state))
Daniel Vetter26495482015-07-15 14:15:52 +020013518 any_ms = true;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013519
Daniel Vetteraf4a8792016-05-09 09:31:25 +020013520 ret = drm_atomic_add_affected_planes(state, crtc);
13521 if (ret)
13522 return ret;
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013523
Daniel Vetter26495482015-07-15 14:15:52 +020013524 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
13525 needs_modeset(crtc_state) ?
13526 "[modeset]" : "[fastset]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013527 }
13528
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013529 if (any_ms) {
13530 ret = intel_modeset_checks(state);
13531
13532 if (ret)
13533 return ret;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013534 } else
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013535 intel_state->cdclk = dev_priv->cdclk_freq;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013536
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013537 ret = drm_atomic_helper_check_planes(dev, state);
Matt Roperaa363132015-09-24 15:53:18 -070013538 if (ret)
13539 return ret;
13540
Paulo Zanonif51be2e2016-01-19 11:35:50 -020013541 intel_fbc_choose_crtc(dev_priv, state);
Matt Roper55994c22016-05-12 07:06:08 -070013542 return calc_watermark_data(state);
Daniel Vettera6778b32012-07-02 09:56:42 +020013543}
13544
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013545static int intel_atomic_prepare_commit(struct drm_device *dev,
13546 struct drm_atomic_state *state,
Maarten Lankhorst81072bf2016-04-26 16:11:45 +020013547 bool nonblock)
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013548{
Chris Wilsonfac5e232016-07-04 11:34:36 +010013549 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013550 struct drm_plane_state *plane_state;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013551 struct drm_crtc_state *crtc_state;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013552 struct drm_plane *plane;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013553 struct drm_crtc *crtc;
13554 int i, ret;
13555
Daniel Vetter5a21b662016-05-24 17:13:53 +020013556 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13557 if (state->legacy_cursor_update)
13558 continue;
13559
13560 ret = intel_crtc_wait_for_pending_flips(crtc);
13561 if (ret)
13562 return ret;
13563
13564 if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
13565 flush_workqueue(dev_priv->wq);
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020013566 }
13567
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013568 ret = mutex_lock_interruptible(&dev->struct_mutex);
13569 if (ret)
13570 return ret;
13571
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013572 ret = drm_atomic_helper_prepare_planes(dev, state);
Chris Wilsonf7e58382016-04-13 17:35:07 +010013573 mutex_unlock(&dev->struct_mutex);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013574
Dave Airlie21daaee2016-05-05 09:56:30 +100013575 if (!ret && !nonblock) {
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013576 for_each_plane_in_state(state, plane, plane_state, i) {
13577 struct intel_plane_state *intel_plane_state =
13578 to_intel_plane_state(plane_state);
13579
13580 if (!intel_plane_state->wait_req)
13581 continue;
13582
13583 ret = __i915_wait_request(intel_plane_state->wait_req,
Chris Wilson299259a2016-04-13 17:35:06 +010013584 true, NULL, NULL);
Chris Wilsonf7e58382016-04-13 17:35:07 +010013585 if (ret) {
Chris Wilsonf4457ae2016-04-13 17:35:08 +010013586 /* Any hang should be swallowed by the wait */
13587 WARN_ON(ret == -EIO);
Chris Wilsonf7e58382016-04-13 17:35:07 +010013588 mutex_lock(&dev->struct_mutex);
13589 drm_atomic_helper_cleanup_planes(dev, state);
13590 mutex_unlock(&dev->struct_mutex);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013591 break;
Chris Wilsonf7e58382016-04-13 17:35:07 +010013592 }
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013593 }
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013594 }
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013595
13596 return ret;
13597}
13598
Maarten Lankhorsta2991412016-05-17 15:07:48 +020013599u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
13600{
13601 struct drm_device *dev = crtc->base.dev;
13602
13603 if (!dev->max_vblank_count)
13604 return drm_accurate_vblank_count(&crtc->base);
13605
13606 return dev->driver->get_vblank_counter(dev, crtc->pipe);
13607}
13608
Daniel Vetter5a21b662016-05-24 17:13:53 +020013609static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
13610 struct drm_i915_private *dev_priv,
13611 unsigned crtc_mask)
Maarten Lankhorste8861672016-02-24 11:24:26 +010013612{
Daniel Vetter5a21b662016-05-24 17:13:53 +020013613 unsigned last_vblank_count[I915_MAX_PIPES];
13614 enum pipe pipe;
13615 int ret;
Maarten Lankhorste8861672016-02-24 11:24:26 +010013616
Daniel Vetter5a21b662016-05-24 17:13:53 +020013617 if (!crtc_mask)
13618 return;
Maarten Lankhorste8861672016-02-24 11:24:26 +010013619
Daniel Vetter5a21b662016-05-24 17:13:53 +020013620 for_each_pipe(dev_priv, pipe) {
13621 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Maarten Lankhorste8861672016-02-24 11:24:26 +010013622
Daniel Vetter5a21b662016-05-24 17:13:53 +020013623 if (!((1 << pipe) & crtc_mask))
Maarten Lankhorste8861672016-02-24 11:24:26 +010013624 continue;
13625
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020013626 ret = drm_crtc_vblank_get(crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +020013627 if (WARN_ON(ret != 0)) {
13628 crtc_mask &= ~(1 << pipe);
13629 continue;
13630 }
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020013631
Daniel Vetter5a21b662016-05-24 17:13:53 +020013632 last_vblank_count[pipe] = drm_crtc_vblank_count(crtc);
13633 }
13634
13635 for_each_pipe(dev_priv, pipe) {
13636 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13637 long lret;
13638
13639 if (!((1 << pipe) & crtc_mask))
13640 continue;
13641
13642 lret = wait_event_timeout(dev->vblank[pipe].queue,
13643 last_vblank_count[pipe] !=
13644 drm_crtc_vblank_count(crtc),
13645 msecs_to_jiffies(50));
13646
13647 WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
13648
13649 drm_crtc_vblank_put(crtc);
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020013650 }
13651}
13652
Daniel Vetter5a21b662016-05-24 17:13:53 +020013653static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013654{
Daniel Vetter5a21b662016-05-24 17:13:53 +020013655 /* fb updated, need to unpin old fb */
13656 if (crtc_state->fb_changed)
13657 return true;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013658
Daniel Vetter5a21b662016-05-24 17:13:53 +020013659 /* wm changes, need vblank before final wm's */
13660 if (crtc_state->update_wm_post)
13661 return true;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013662
Daniel Vetter5a21b662016-05-24 17:13:53 +020013663 /*
13664 * cxsr is re-enabled after vblank.
13665 * This is already handled by crtc_state->update_wm_post,
13666 * but added for clarity.
13667 */
13668 if (crtc_state->disable_cxsr)
13669 return true;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013670
Daniel Vetter5a21b662016-05-24 17:13:53 +020013671 return false;
Maarten Lankhorste8861672016-02-24 11:24:26 +010013672}
13673
Daniel Vetter94f05022016-06-14 18:01:00 +020013674static void intel_atomic_commit_tail(struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020013675{
Daniel Vetter94f05022016-06-14 18:01:00 +020013676 struct drm_device *dev = state->dev;
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013677 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Chris Wilsonfac5e232016-07-04 11:34:36 +010013678 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013679 struct drm_crtc_state *old_crtc_state;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013680 struct drm_crtc *crtc;
Daniel Vetter5a21b662016-05-24 17:13:53 +020013681 struct intel_crtc_state *intel_cstate;
Daniel Vetter94f05022016-06-14 18:01:00 +020013682 struct drm_plane *plane;
13683 struct drm_plane_state *plane_state;
Daniel Vetter5a21b662016-05-24 17:13:53 +020013684 bool hw_check = intel_state->modeset;
13685 unsigned long put_domains[I915_MAX_PIPES] = {};
13686 unsigned crtc_vblank_mask = 0;
Daniel Vetter94f05022016-06-14 18:01:00 +020013687 int i, ret;
Daniel Vettera6778b32012-07-02 09:56:42 +020013688
Daniel Vetter94f05022016-06-14 18:01:00 +020013689 for_each_plane_in_state(state, plane, plane_state, i) {
13690 struct intel_plane_state *intel_plane_state =
13691 to_intel_plane_state(plane_state);
Daniel Vetterea0000f2016-06-13 16:13:46 +020013692
Daniel Vetter94f05022016-06-14 18:01:00 +020013693 if (!intel_plane_state->wait_req)
13694 continue;
13695
13696 ret = __i915_wait_request(intel_plane_state->wait_req,
13697 true, NULL, NULL);
13698 /* EIO should be eaten, and we can't get interrupted in the
13699 * worker, and blocking commits have waited already. */
13700 WARN_ON(ret);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013701 }
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013702
Daniel Vetterea0000f2016-06-13 16:13:46 +020013703 drm_atomic_helper_wait_for_dependencies(state);
13704
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013705 if (intel_state->modeset) {
13706 memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
13707 sizeof(intel_state->min_pixclk));
13708 dev_priv->active_crtcs = intel_state->active_crtcs;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013709 dev_priv->atomic_cdclk_freq = intel_state->cdclk;
Daniel Vetter5a21b662016-05-24 17:13:53 +020013710
13711 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013712 }
13713
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013714 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013715 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13716
Daniel Vetter5a21b662016-05-24 17:13:53 +020013717 if (needs_modeset(crtc->state) ||
13718 to_intel_crtc_state(crtc->state)->update_pipe) {
13719 hw_check = true;
13720
13721 put_domains[to_intel_crtc(crtc)->pipe] =
13722 modeset_get_crtc_power_domains(crtc,
13723 to_intel_crtc_state(crtc->state));
13724 }
13725
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013726 if (!needs_modeset(crtc->state))
13727 continue;
13728
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013729 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
Daniel Vetter460da9162013-03-27 00:44:51 +010013730
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013731 if (old_crtc_state->active) {
13732 intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013733 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013734 intel_crtc->active = false;
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -020013735 intel_fbc_disable(intel_crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013736 intel_disable_shared_dpll(intel_crtc);
Ville Syrjälä9bbc8258a2015-11-20 22:09:20 +020013737
13738 /*
13739 * Underruns don't always raise
13740 * interrupts, so check manually.
13741 */
13742 intel_check_cpu_fifo_underruns(dev_priv);
13743 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorstb9001112015-11-19 16:07:16 +010013744
13745 if (!crtc->state->active)
13746 intel_update_watermarks(crtc);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013747 }
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010013748 }
Daniel Vetter7758a112012-07-08 19:40:39 +020013749
Daniel Vetterea9d7582012-07-10 10:42:52 +020013750 /* Only after disabling all output pipelines that will be changed can we
13751 * update the the output configuration. */
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013752 intel_modeset_update_crtc_state(state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020013753
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013754 if (intel_state->modeset) {
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013755 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013756
13757 if (dev_priv->display.modeset_commit_cdclk &&
Clint Taylorc89e39f2016-05-13 23:41:21 +030013758 (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
Ville Syrjälä63911d72016-05-13 23:41:32 +030013759 intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco))
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013760 dev_priv->display.modeset_commit_cdclk(state);
Maarten Lankhorstf6d19732016-03-23 14:58:07 +010013761
Lyudef4033722016-08-17 15:55:54 -040013762 /*
13763 * SKL workaround: bspec recommends we disable the SAGV when we
13764 * have more then one pipe enabled
13765 */
13766 if (IS_SKYLAKE(dev_priv) && !skl_can_enable_sagv(state))
13767 skl_disable_sagv(dev_priv);
13768
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013769 intel_modeset_verify_disabled(dev);
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013770 }
Daniel Vetter47fab732012-10-26 10:58:18 +020013771
Daniel Vettera6778b32012-07-02 09:56:42 +020013772 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013773 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013774 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13775 bool modeset = needs_modeset(crtc->state);
Daniel Vetter5a21b662016-05-24 17:13:53 +020013776 struct intel_crtc_state *pipe_config =
13777 to_intel_crtc_state(crtc->state);
Patrik Jakobsson9f836f92015-11-16 16:20:01 +010013778
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013779 if (modeset && crtc->state->active) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013780 update_scanline_offset(to_intel_crtc(crtc));
13781 dev_priv->display.crtc_enable(crtc);
13782 }
13783
Daniel Vetter1f7528c2016-06-13 16:13:45 +020013784 /* Complete events for now disable pipes here. */
13785 if (modeset && !crtc->state->active && crtc->state->event) {
13786 spin_lock_irq(&dev->event_lock);
13787 drm_crtc_send_vblank_event(crtc, crtc->state->event);
13788 spin_unlock_irq(&dev->event_lock);
13789
13790 crtc->state->event = NULL;
13791 }
13792
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013793 if (!modeset)
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013794 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013795
Daniel Vetter5a21b662016-05-24 17:13:53 +020013796 if (crtc->state->active &&
13797 drm_atomic_get_existing_plane_state(state, crtc->primary))
Maarten Lankhorstfaf68d92016-06-14 14:24:20 +020013798 intel_fbc_enable(intel_crtc, pipe_config, to_intel_plane_state(crtc->primary->state));
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013799
Daniel Vetter1f7528c2016-06-13 16:13:45 +020013800 if (crtc->state->active)
Daniel Vetter5a21b662016-05-24 17:13:53 +020013801 drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013802
Daniel Vetter5a21b662016-05-24 17:13:53 +020013803 if (pipe_config->base.active && needs_vblank_wait(pipe_config))
13804 crtc_vblank_mask |= 1 << i;
Matt Ropered4a6a72016-02-23 17:20:13 -080013805 }
13806
Daniel Vetter94f05022016-06-14 18:01:00 +020013807 /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
13808 * already, but still need the state for the delayed optimization. To
13809 * fix this:
13810 * - wrap the optimization/post_plane_update stuff into a per-crtc work.
13811 * - schedule that vblank worker _before_ calling hw_done
13812 * - at the start of commit_tail, cancel it _synchrously
13813 * - switch over to the vblank wait helper in the core after that since
13814 * we don't need out special handling any more.
13815 */
Daniel Vetter5a21b662016-05-24 17:13:53 +020013816 if (!state->legacy_cursor_update)
13817 intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
13818
13819 /*
13820 * Now that the vblank has passed, we can go ahead and program the
13821 * optimal watermarks on platforms that need two-step watermark
13822 * programming.
13823 *
13824 * TODO: Move this (and other cleanup) to an async worker eventually.
13825 */
13826 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
13827 intel_cstate = to_intel_crtc_state(crtc->state);
13828
13829 if (dev_priv->display.optimize_watermarks)
13830 dev_priv->display.optimize_watermarks(intel_cstate);
13831 }
13832
13833 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
13834 intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
13835
13836 if (put_domains[i])
13837 modeset_put_power_domains(dev_priv, put_domains[i]);
13838
13839 intel_modeset_verify_crtc(crtc, old_crtc_state, crtc->state);
13840 }
13841
Lyudef4033722016-08-17 15:55:54 -040013842 if (IS_SKYLAKE(dev_priv) && intel_state->modeset &&
13843 skl_can_enable_sagv(state))
13844 skl_enable_sagv(dev_priv);
13845
Daniel Vetter94f05022016-06-14 18:01:00 +020013846 drm_atomic_helper_commit_hw_done(state);
13847
Daniel Vetter5a21b662016-05-24 17:13:53 +020013848 if (intel_state->modeset)
13849 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
13850
13851 mutex_lock(&dev->struct_mutex);
13852 drm_atomic_helper_cleanup_planes(dev, state);
13853 mutex_unlock(&dev->struct_mutex);
13854
Daniel Vetterea0000f2016-06-13 16:13:46 +020013855 drm_atomic_helper_commit_cleanup_done(state);
13856
Maarten Lankhorstee165b12015-08-05 12:37:00 +020013857 drm_atomic_state_free(state);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013858
Mika Kuoppala75714942015-12-16 09:26:48 +020013859 /* As one of the primary mmio accessors, KMS has a high likelihood
13860 * of triggering bugs in unclaimed access. After we finish
13861 * modesetting, see if an error has been flagged, and if so
13862 * enable debugging for the next modeset - and hope we catch
13863 * the culprit.
13864 *
13865 * XXX note that we assume display power is on at this point.
13866 * This might hold true now but we need to add pm helper to check
13867 * unclaimed only when the hardware is on, as atomic commits
13868 * can happen also when the device is completely off.
13869 */
13870 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
Daniel Vetter94f05022016-06-14 18:01:00 +020013871}
13872
13873static void intel_atomic_commit_work(struct work_struct *work)
13874{
13875 struct drm_atomic_state *state = container_of(work,
13876 struct drm_atomic_state,
13877 commit_work);
13878 intel_atomic_commit_tail(state);
13879}
13880
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020013881static void intel_atomic_track_fbs(struct drm_atomic_state *state)
13882{
13883 struct drm_plane_state *old_plane_state;
13884 struct drm_plane *plane;
13885 struct drm_i915_gem_object *obj, *old_obj;
13886 struct intel_plane *intel_plane;
13887 int i;
13888
13889 mutex_lock(&state->dev->struct_mutex);
13890 for_each_plane_in_state(state, plane, old_plane_state, i) {
13891 obj = intel_fb_obj(plane->state->fb);
13892 old_obj = intel_fb_obj(old_plane_state->fb);
13893 intel_plane = to_intel_plane(plane);
13894
13895 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
13896 }
13897 mutex_unlock(&state->dev->struct_mutex);
13898}
13899
Daniel Vetter94f05022016-06-14 18:01:00 +020013900/**
13901 * intel_atomic_commit - commit validated state object
13902 * @dev: DRM device
13903 * @state: the top-level driver state object
13904 * @nonblock: nonblocking commit
13905 *
13906 * This function commits a top-level state object that has been validated
13907 * with drm_atomic_helper_check().
13908 *
13909 * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
13910 * nonblocking commits are only safe for pure plane updates. Everything else
13911 * should work though.
13912 *
13913 * RETURNS
13914 * Zero for success or -errno.
13915 */
13916static int intel_atomic_commit(struct drm_device *dev,
13917 struct drm_atomic_state *state,
13918 bool nonblock)
13919{
13920 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Chris Wilsonfac5e232016-07-04 11:34:36 +010013921 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter94f05022016-06-14 18:01:00 +020013922 int ret = 0;
13923
13924 if (intel_state->modeset && nonblock) {
13925 DRM_DEBUG_KMS("nonblocking commit for modeset not yet implemented.\n");
13926 return -EINVAL;
13927 }
13928
13929 ret = drm_atomic_helper_setup_commit(state, nonblock);
13930 if (ret)
13931 return ret;
13932
13933 INIT_WORK(&state->commit_work, intel_atomic_commit_work);
13934
13935 ret = intel_atomic_prepare_commit(dev, state, nonblock);
13936 if (ret) {
13937 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
13938 return ret;
13939 }
13940
13941 drm_atomic_helper_swap_state(state, true);
13942 dev_priv->wm.distrust_bios_wm = false;
13943 dev_priv->wm.skl_results = intel_state->wm_results;
13944 intel_shared_dpll_commit(state);
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020013945 intel_atomic_track_fbs(state);
Daniel Vetter94f05022016-06-14 18:01:00 +020013946
13947 if (nonblock)
13948 queue_work(system_unbound_wq, &state->commit_work);
13949 else
13950 intel_atomic_commit_tail(state);
Mika Kuoppala75714942015-12-16 09:26:48 +020013951
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013952 return 0;
Daniel Vetterf30da182013-04-11 20:22:50 +020013953}
13954
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013955void intel_crtc_restore_mode(struct drm_crtc *crtc)
13956{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013957 struct drm_device *dev = crtc->dev;
13958 struct drm_atomic_state *state;
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013959 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013960 int ret;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013961
13962 state = drm_atomic_state_alloc(dev);
13963 if (!state) {
Ville Syrjälä78108b72016-05-27 20:59:19 +030013964 DRM_DEBUG_KMS("[CRTC:%d:%s] crtc restore failed, out of memory",
13965 crtc->base.id, crtc->name);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013966 return;
13967 }
13968
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013969 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013970
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013971retry:
13972 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13973 ret = PTR_ERR_OR_ZERO(crtc_state);
13974 if (!ret) {
13975 if (!crtc_state->active)
13976 goto out;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013977
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013978 crtc_state->mode_changed = true;
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013979 ret = drm_atomic_commit(state);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013980 }
13981
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013982 if (ret == -EDEADLK) {
13983 drm_atomic_state_clear(state);
13984 drm_modeset_backoff(state->acquire_ctx);
13985 goto retry;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013986 }
13987
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013988 if (ret)
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013989out:
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013990 drm_atomic_state_free(state);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013991}
13992
Daniel Vetter25c5b262012-07-08 22:08:04 +020013993#undef for_each_intel_crtc_masked
13994
Bob Paauwefa959862016-07-15 14:59:02 +010013995/*
13996 * FIXME: Remove this once i915 is fully DRIVER_ATOMIC by calling
13997 * drm_atomic_helper_legacy_gamma_set() directly.
13998 */
13999static int intel_atomic_legacy_gamma_set(struct drm_crtc *crtc,
14000 u16 *red, u16 *green, u16 *blue,
14001 uint32_t size)
14002{
14003 struct drm_device *dev = crtc->dev;
14004 struct drm_mode_config *config = &dev->mode_config;
14005 struct drm_crtc_state *state;
14006 int ret;
14007
14008 ret = drm_atomic_helper_legacy_gamma_set(crtc, red, green, blue, size);
14009 if (ret)
14010 return ret;
14011
14012 /*
14013 * Make sure we update the legacy properties so this works when
14014 * atomic is not enabled.
14015 */
14016
14017 state = crtc->state;
14018
14019 drm_object_property_set_value(&crtc->base,
14020 config->degamma_lut_property,
14021 (state->degamma_lut) ?
14022 state->degamma_lut->base.id : 0);
14023
14024 drm_object_property_set_value(&crtc->base,
14025 config->ctm_property,
14026 (state->ctm) ?
14027 state->ctm->base.id : 0);
14028
14029 drm_object_property_set_value(&crtc->base,
14030 config->gamma_lut_property,
14031 (state->gamma_lut) ?
14032 state->gamma_lut->base.id : 0);
14033
14034 return 0;
14035}
14036
Chris Wilsonf6e5b162011-04-12 18:06:51 +010014037static const struct drm_crtc_funcs intel_crtc_funcs = {
Bob Paauwefa959862016-07-15 14:59:02 +010014038 .gamma_set = intel_atomic_legacy_gamma_set,
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020014039 .set_config = drm_atomic_helper_set_config,
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000014040 .set_property = drm_atomic_helper_crtc_set_property,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010014041 .destroy = intel_crtc_destroy,
Chris Wilson527b6ab2016-06-24 13:44:03 +010014042 .page_flip = intel_crtc_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080014043 .atomic_duplicate_state = intel_crtc_duplicate_state,
14044 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010014045};
14046
Matt Roper6beb8c232014-12-01 15:40:14 -080014047/**
14048 * intel_prepare_plane_fb - Prepare fb for usage on plane
14049 * @plane: drm plane to prepare for
14050 * @fb: framebuffer to prepare for presentation
14051 *
14052 * Prepares a framebuffer for usage on a display plane. Generally this
14053 * involves pinning the underlying object and updating the frontbuffer tracking
14054 * bits. Some older platforms need special physical address handling for
14055 * cursor planes.
14056 *
Maarten Lankhorstf9356752015-08-18 13:40:05 +020014057 * Must be called with struct_mutex held.
14058 *
Matt Roper6beb8c232014-12-01 15:40:14 -080014059 * Returns 0 on success, negative error code on failure.
14060 */
14061int
14062intel_prepare_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000014063 const struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070014064{
14065 struct drm_device *dev = plane->dev;
Maarten Lankhorst844f9112015-09-02 10:42:40 +020014066 struct drm_framebuffer *fb = new_state->fb;
Matt Roper6beb8c232014-12-01 15:40:14 -080014067 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014068 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
Chris Wilsonc37efb92016-06-17 08:28:47 +010014069 struct reservation_object *resv;
Matt Roper6beb8c232014-12-01 15:40:14 -080014070 int ret = 0;
Matt Roper465c1202014-05-29 08:06:54 -070014071
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014072 if (!obj && !old_obj)
Matt Roper465c1202014-05-29 08:06:54 -070014073 return 0;
14074
Maarten Lankhorst5008e872015-08-18 13:40:05 +020014075 if (old_obj) {
14076 struct drm_crtc_state *crtc_state =
14077 drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
14078
14079 /* Big Hammer, we also need to ensure that any pending
14080 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
14081 * current scanout is retired before unpinning the old
14082 * framebuffer. Note that we rely on userspace rendering
14083 * into the buffer attached to the pipe they are waiting
14084 * on. If not, userspace generates a GPU hang with IPEHR
14085 * point to the MI_WAIT_FOR_EVENT.
14086 *
14087 * This should only fail upon a hung GPU, in which case we
14088 * can safely continue.
14089 */
14090 if (needs_modeset(crtc_state))
14091 ret = i915_gem_object_wait_rendering(old_obj, true);
Chris Wilsonf4457ae2016-04-13 17:35:08 +010014092 if (ret) {
14093 /* GPU hangs should have been swallowed by the wait */
14094 WARN_ON(ret == -EIO);
Maarten Lankhorstf9356752015-08-18 13:40:05 +020014095 return ret;
Chris Wilsonf4457ae2016-04-13 17:35:08 +010014096 }
Maarten Lankhorst5008e872015-08-18 13:40:05 +020014097 }
14098
Chris Wilsonc37efb92016-06-17 08:28:47 +010014099 if (!obj)
14100 return 0;
14101
Daniel Vetter5a21b662016-05-24 17:13:53 +020014102 /* For framebuffer backed by dmabuf, wait for fence */
Chris Wilsonc37efb92016-06-17 08:28:47 +010014103 resv = i915_gem_object_get_dmabuf_resv(obj);
14104 if (resv) {
Daniel Vetter5a21b662016-05-24 17:13:53 +020014105 long lret;
14106
Chris Wilsonc37efb92016-06-17 08:28:47 +010014107 lret = reservation_object_wait_timeout_rcu(resv, false, true,
Daniel Vetter5a21b662016-05-24 17:13:53 +020014108 MAX_SCHEDULE_TIMEOUT);
14109 if (lret == -ERESTARTSYS)
14110 return lret;
14111
14112 WARN(lret < 0, "waiting returns %li\n", lret);
14113 }
14114
Chris Wilsonc37efb92016-06-17 08:28:47 +010014115 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
Matt Roper6beb8c232014-12-01 15:40:14 -080014116 INTEL_INFO(dev)->cursor_needs_physical) {
14117 int align = IS_I830(dev) ? 16 * 1024 : 256;
14118 ret = i915_gem_object_attach_phys(obj, align);
14119 if (ret)
14120 DRM_DEBUG_KMS("failed to attach phys object\n");
14121 } else {
Ville Syrjälä3465c582016-02-15 22:54:43 +020014122 ret = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
Matt Roper6beb8c232014-12-01 15:40:14 -080014123 }
14124
Chris Wilsonc37efb92016-06-17 08:28:47 +010014125 if (ret == 0) {
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020014126 struct intel_plane_state *plane_state =
14127 to_intel_plane_state(new_state);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020014128
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020014129 i915_gem_request_assign(&plane_state->wait_req,
14130 obj->last_write_req);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020014131 }
Matt Roper6beb8c232014-12-01 15:40:14 -080014132
Matt Roper6beb8c232014-12-01 15:40:14 -080014133 return ret;
14134}
14135
Matt Roper38f3ce32014-12-02 07:45:25 -080014136/**
14137 * intel_cleanup_plane_fb - Cleans up an fb after plane use
14138 * @plane: drm plane to clean up for
14139 * @fb: old framebuffer that was on plane
14140 *
14141 * Cleans up a framebuffer that has just been removed from a plane.
Maarten Lankhorstf9356752015-08-18 13:40:05 +020014142 *
14143 * Must be called with struct_mutex held.
Matt Roper38f3ce32014-12-02 07:45:25 -080014144 */
14145void
14146intel_cleanup_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000014147 const struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080014148{
14149 struct drm_device *dev = plane->dev;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020014150 struct intel_plane_state *old_intel_state;
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014151 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
14152 struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
Matt Roper38f3ce32014-12-02 07:45:25 -080014153
Maarten Lankhorst7580d772015-08-18 13:40:06 +020014154 old_intel_state = to_intel_plane_state(old_state);
14155
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014156 if (!obj && !old_obj)
Matt Roper38f3ce32014-12-02 07:45:25 -080014157 return;
14158
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014159 if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
14160 !INTEL_INFO(dev)->cursor_needs_physical))
Ville Syrjälä3465c582016-02-15 22:54:43 +020014161 intel_unpin_fb_obj(old_state->fb, old_state->rotation);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014162
Maarten Lankhorst7580d772015-08-18 13:40:06 +020014163 i915_gem_request_assign(&old_intel_state->wait_req, NULL);
Matt Roper465c1202014-05-29 08:06:54 -070014164}
14165
Chandra Konduru6156a452015-04-27 13:48:39 -070014166int
14167skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
14168{
14169 int max_scale;
Chandra Konduru6156a452015-04-27 13:48:39 -070014170 int crtc_clock, cdclk;
14171
Maarten Lankhorstbf8a0af2015-11-24 11:29:02 +010014172 if (!intel_crtc || !crtc_state->base.enable)
Chandra Konduru6156a452015-04-27 13:48:39 -070014173 return DRM_PLANE_HELPER_NO_SCALING;
14174
Chandra Konduru6156a452015-04-27 13:48:39 -070014175 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014176 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
Chandra Konduru6156a452015-04-27 13:48:39 -070014177
Tvrtko Ursulin54bf1ce2015-10-20 17:17:07 +010014178 if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
Chandra Konduru6156a452015-04-27 13:48:39 -070014179 return DRM_PLANE_HELPER_NO_SCALING;
14180
14181 /*
14182 * skl max scale is lower of:
14183 * close to 3 but not 3, -1 is for that purpose
14184 * or
14185 * cdclk/crtc_clock
14186 */
14187 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
14188
14189 return max_scale;
14190}
14191
Matt Roper465c1202014-05-29 08:06:54 -070014192static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030014193intel_check_primary_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014194 struct intel_crtc_state *crtc_state,
Gustavo Padovan3c692a42014-09-05 17:04:49 -030014195 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070014196{
Matt Roper2b875c22014-12-01 15:40:13 -080014197 struct drm_crtc *crtc = state->base.crtc;
14198 struct drm_framebuffer *fb = state->base.fb;
Chandra Konduru6156a452015-04-27 13:48:39 -070014199 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014200 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
14201 bool can_position = false;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030014202
Ville Syrjälä693bdc22016-01-15 20:46:53 +020014203 if (INTEL_INFO(plane->dev)->gen >= 9) {
14204 /* use scaler when colorkey is not required */
14205 if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
14206 min_scale = 1;
14207 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
14208 }
Sonika Jindald8106362015-04-10 14:37:28 +053014209 can_position = true;
Chandra Konduru6156a452015-04-27 13:48:39 -070014210 }
Sonika Jindald8106362015-04-10 14:37:28 +053014211
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014212 return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
14213 &state->dst, &state->clip,
Ville Syrjälä9b8b0132016-06-17 17:13:10 +030014214 state->base.rotation,
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014215 min_scale, max_scale,
14216 can_position, true,
14217 &state->visible);
Matt Roper465c1202014-05-29 08:06:54 -070014218}
14219
Daniel Vetter5a21b662016-05-24 17:13:53 +020014220static void intel_begin_crtc_commit(struct drm_crtc *crtc,
14221 struct drm_crtc_state *old_crtc_state)
14222{
14223 struct drm_device *dev = crtc->dev;
14224 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14225 struct intel_crtc_state *old_intel_state =
14226 to_intel_crtc_state(old_crtc_state);
14227 bool modeset = needs_modeset(crtc->state);
14228
14229 /* Perform vblank evasion around commit operation */
14230 intel_pipe_update_start(intel_crtc);
14231
14232 if (modeset)
14233 return;
14234
14235 if (crtc->state->color_mgmt_changed || to_intel_crtc_state(crtc->state)->update_pipe) {
14236 intel_color_set_csc(crtc->state);
14237 intel_color_load_luts(crtc->state);
14238 }
14239
14240 if (to_intel_crtc_state(crtc->state)->update_pipe)
14241 intel_update_pipe_config(intel_crtc, old_intel_state);
14242 else if (INTEL_INFO(dev)->gen >= 9)
14243 skl_detach_scalers(intel_crtc);
14244}
14245
14246static void intel_finish_crtc_commit(struct drm_crtc *crtc,
14247 struct drm_crtc_state *old_crtc_state)
14248{
14249 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14250
14251 intel_pipe_update_end(intel_crtc, NULL);
14252}
14253
Matt Ropercf4c7c12014-12-04 10:27:42 -080014254/**
Matt Roper4a3b8762014-12-23 10:41:51 -080014255 * intel_plane_destroy - destroy a plane
14256 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080014257 *
Matt Roper4a3b8762014-12-23 10:41:51 -080014258 * Common destruction function for all types of planes (primary, cursor,
14259 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080014260 */
Matt Roper4a3b8762014-12-23 10:41:51 -080014261void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070014262{
Ville Syrjälä69ae5612016-05-27 20:59:22 +030014263 if (!plane)
14264 return;
14265
Matt Roper465c1202014-05-29 08:06:54 -070014266 drm_plane_cleanup(plane);
Ville Syrjälä69ae5612016-05-27 20:59:22 +030014267 kfree(to_intel_plane(plane));
Matt Roper465c1202014-05-29 08:06:54 -070014268}
14269
Matt Roper65a3fea2015-01-21 16:35:42 -080014270const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070014271 .update_plane = drm_atomic_helper_update_plane,
14272 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070014273 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080014274 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080014275 .atomic_get_property = intel_plane_atomic_get_property,
14276 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080014277 .atomic_duplicate_state = intel_plane_duplicate_state,
14278 .atomic_destroy_state = intel_plane_destroy_state,
14279
Matt Roper465c1202014-05-29 08:06:54 -070014280};
14281
14282static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
14283 int pipe)
14284{
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014285 struct intel_plane *primary = NULL;
14286 struct intel_plane_state *state = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070014287 const uint32_t *intel_primary_formats;
Thierry Reding45e37432015-08-12 16:54:28 +020014288 unsigned int num_formats;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014289 int ret;
Matt Roper465c1202014-05-29 08:06:54 -070014290
14291 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014292 if (!primary)
14293 goto fail;
Matt Roper465c1202014-05-29 08:06:54 -070014294
Matt Roper8e7d6882015-01-21 16:35:41 -080014295 state = intel_create_plane_state(&primary->base);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014296 if (!state)
14297 goto fail;
Matt Roper8e7d6882015-01-21 16:35:41 -080014298 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080014299
Matt Roper465c1202014-05-29 08:06:54 -070014300 primary->can_scale = false;
14301 primary->max_downscale = 1;
Chandra Konduru6156a452015-04-27 13:48:39 -070014302 if (INTEL_INFO(dev)->gen >= 9) {
14303 primary->can_scale = true;
Chandra Konduruaf99ced2015-05-11 14:35:47 -070014304 state->scaler_id = -1;
Chandra Konduru6156a452015-04-27 13:48:39 -070014305 }
Matt Roper465c1202014-05-29 08:06:54 -070014306 primary->pipe = pipe;
14307 primary->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030014308 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080014309 primary->check_plane = intel_check_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070014310 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
14311 primary->plane = !pipe;
14312
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014313 if (INTEL_INFO(dev)->gen >= 9) {
14314 intel_primary_formats = skl_primary_formats;
14315 num_formats = ARRAY_SIZE(skl_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010014316
14317 primary->update_plane = skylake_update_primary_plane;
14318 primary->disable_plane = skylake_disable_primary_plane;
14319 } else if (HAS_PCH_SPLIT(dev)) {
14320 intel_primary_formats = i965_primary_formats;
14321 num_formats = ARRAY_SIZE(i965_primary_formats);
14322
14323 primary->update_plane = ironlake_update_primary_plane;
14324 primary->disable_plane = i9xx_disable_primary_plane;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014325 } else if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau568db4f2015-05-12 16:13:18 +010014326 intel_primary_formats = i965_primary_formats;
14327 num_formats = ARRAY_SIZE(i965_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010014328
14329 primary->update_plane = i9xx_update_primary_plane;
14330 primary->disable_plane = i9xx_disable_primary_plane;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014331 } else {
14332 intel_primary_formats = i8xx_primary_formats;
14333 num_formats = ARRAY_SIZE(i8xx_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010014334
14335 primary->update_plane = i9xx_update_primary_plane;
14336 primary->disable_plane = i9xx_disable_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070014337 }
14338
Ville Syrjälä38573dc2016-05-27 20:59:23 +030014339 if (INTEL_INFO(dev)->gen >= 9)
14340 ret = drm_universal_plane_init(dev, &primary->base, 0,
14341 &intel_plane_funcs,
14342 intel_primary_formats, num_formats,
14343 DRM_PLANE_TYPE_PRIMARY,
14344 "plane 1%c", pipe_name(pipe));
14345 else if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
14346 ret = drm_universal_plane_init(dev, &primary->base, 0,
14347 &intel_plane_funcs,
14348 intel_primary_formats, num_formats,
14349 DRM_PLANE_TYPE_PRIMARY,
14350 "primary %c", pipe_name(pipe));
14351 else
14352 ret = drm_universal_plane_init(dev, &primary->base, 0,
14353 &intel_plane_funcs,
14354 intel_primary_formats, num_formats,
14355 DRM_PLANE_TYPE_PRIMARY,
14356 "plane %c", plane_name(primary->plane));
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014357 if (ret)
14358 goto fail;
Sonika Jindal48404c12014-08-22 14:06:04 +053014359
Sonika Jindal3b7a5112015-04-10 14:37:29 +053014360 if (INTEL_INFO(dev)->gen >= 4)
14361 intel_create_rotation_property(dev, primary);
Sonika Jindal48404c12014-08-22 14:06:04 +053014362
Matt Roperea2c67b2014-12-23 10:41:52 -080014363 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
14364
Matt Roper465c1202014-05-29 08:06:54 -070014365 return &primary->base;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014366
14367fail:
14368 kfree(state);
14369 kfree(primary);
14370
14371 return NULL;
Matt Roper465c1202014-05-29 08:06:54 -070014372}
14373
Sonika Jindal3b7a5112015-04-10 14:37:29 +053014374void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
14375{
14376 if (!dev->mode_config.rotation_property) {
14377 unsigned long flags = BIT(DRM_ROTATE_0) |
14378 BIT(DRM_ROTATE_180);
14379
14380 if (INTEL_INFO(dev)->gen >= 9)
14381 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
14382
14383 dev->mode_config.rotation_property =
14384 drm_mode_create_rotation_property(dev, flags);
14385 }
14386 if (dev->mode_config.rotation_property)
14387 drm_object_attach_property(&plane->base.base,
14388 dev->mode_config.rotation_property,
14389 plane->base.state->rotation);
14390}
14391
Matt Roper3d7d6512014-06-10 08:28:13 -070014392static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030014393intel_check_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014394 struct intel_crtc_state *crtc_state,
Gustavo Padovan852e7872014-09-05 17:22:31 -030014395 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070014396{
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014397 struct drm_crtc *crtc = crtc_state->base.crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080014398 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014399 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Ville Syrjäläb29ec922015-12-18 19:24:39 +020014400 enum pipe pipe = to_intel_plane(plane)->pipe;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014401 unsigned stride;
14402 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030014403
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014404 ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
14405 &state->dst, &state->clip,
Ville Syrjälä9b8b0132016-06-17 17:13:10 +030014406 state->base.rotation,
Gustavo Padovan852e7872014-09-05 17:22:31 -030014407 DRM_PLANE_HELPER_NO_SCALING,
14408 DRM_PLANE_HELPER_NO_SCALING,
14409 true, true, &state->visible);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014410 if (ret)
14411 return ret;
14412
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014413 /* if we want to turn off the cursor ignore width and height */
14414 if (!obj)
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014415 return 0;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014416
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014417 /* Check for which cursor types we support */
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014418 if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
Matt Roperea2c67b2014-12-23 10:41:52 -080014419 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
14420 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014421 return -EINVAL;
14422 }
14423
Matt Roperea2c67b2014-12-23 10:41:52 -080014424 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
14425 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014426 DRM_DEBUG_KMS("buffer is too small\n");
14427 return -ENOMEM;
14428 }
14429
Ville Syrjälä3a656b52015-03-09 21:08:37 +020014430 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014431 DRM_DEBUG_KMS("cursor cannot be tiled\n");
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014432 return -EINVAL;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014433 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014434
Ville Syrjäläb29ec922015-12-18 19:24:39 +020014435 /*
14436 * There's something wrong with the cursor on CHV pipe C.
14437 * If it straddles the left edge of the screen then
14438 * moving it away from the edge or disabling it often
14439 * results in a pipe underrun, and often that can lead to
14440 * dead pipe (constant underrun reported, and it scans
14441 * out just a solid color). To recover from that, the
14442 * display power well must be turned off and on again.
14443 * Refuse the put the cursor into that compromised position.
14444 */
14445 if (IS_CHERRYVIEW(plane->dev) && pipe == PIPE_C &&
14446 state->visible && state->base.crtc_x < 0) {
14447 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
14448 return -EINVAL;
14449 }
14450
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014451 return 0;
Gustavo Padovan852e7872014-09-05 17:22:31 -030014452}
14453
Matt Roperf4a2cf22014-12-01 15:40:12 -080014454static void
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014455intel_disable_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst7fabf5e2015-06-15 12:33:47 +020014456 struct drm_crtc *crtc)
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014457{
Maarten Lankhorstf2858022016-01-07 11:54:09 +010014458 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14459
14460 intel_crtc->cursor_addr = 0;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014461 intel_crtc_update_cursor(crtc, NULL);
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014462}
14463
14464static void
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014465intel_update_cursor_plane(struct drm_plane *plane,
14466 const struct intel_crtc_state *crtc_state,
14467 const struct intel_plane_state *state)
Gustavo Padovan852e7872014-09-05 17:22:31 -030014468{
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014469 struct drm_crtc *crtc = crtc_state->base.crtc;
14470 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roperea2c67b2014-12-23 10:41:52 -080014471 struct drm_device *dev = plane->dev;
Matt Roper2b875c22014-12-01 15:40:13 -080014472 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080014473 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070014474
Matt Roperf4a2cf22014-12-01 15:40:12 -080014475 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080014476 addr = 0;
Matt Roperf4a2cf22014-12-01 15:40:12 -080014477 else if (!INTEL_INFO(dev)->cursor_needs_physical)
Gustavo Padovana912f122014-12-01 15:40:10 -080014478 addr = i915_gem_obj_ggtt_offset(obj);
Matt Roperf4a2cf22014-12-01 15:40:12 -080014479 else
Gustavo Padovana912f122014-12-01 15:40:10 -080014480 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080014481
Gustavo Padovana912f122014-12-01 15:40:10 -080014482 intel_crtc->cursor_addr = addr;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014483 intel_crtc_update_cursor(crtc, state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014484}
Gustavo Padovan852e7872014-09-05 17:22:31 -030014485
Matt Roper3d7d6512014-06-10 08:28:13 -070014486static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
14487 int pipe)
14488{
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014489 struct intel_plane *cursor = NULL;
14490 struct intel_plane_state *state = NULL;
14491 int ret;
Matt Roper3d7d6512014-06-10 08:28:13 -070014492
14493 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014494 if (!cursor)
14495 goto fail;
Matt Roper3d7d6512014-06-10 08:28:13 -070014496
Matt Roper8e7d6882015-01-21 16:35:41 -080014497 state = intel_create_plane_state(&cursor->base);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014498 if (!state)
14499 goto fail;
Matt Roper8e7d6882015-01-21 16:35:41 -080014500 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080014501
Matt Roper3d7d6512014-06-10 08:28:13 -070014502 cursor->can_scale = false;
14503 cursor->max_downscale = 1;
14504 cursor->pipe = pipe;
14505 cursor->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030014506 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080014507 cursor->check_plane = intel_check_cursor_plane;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014508 cursor->update_plane = intel_update_cursor_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014509 cursor->disable_plane = intel_disable_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070014510
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014511 ret = drm_universal_plane_init(dev, &cursor->base, 0,
14512 &intel_plane_funcs,
14513 intel_cursor_formats,
14514 ARRAY_SIZE(intel_cursor_formats),
Ville Syrjälä38573dc2016-05-27 20:59:23 +030014515 DRM_PLANE_TYPE_CURSOR,
14516 "cursor %c", pipe_name(pipe));
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014517 if (ret)
14518 goto fail;
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014519
14520 if (INTEL_INFO(dev)->gen >= 4) {
14521 if (!dev->mode_config.rotation_property)
14522 dev->mode_config.rotation_property =
14523 drm_mode_create_rotation_property(dev,
14524 BIT(DRM_ROTATE_0) |
14525 BIT(DRM_ROTATE_180));
14526 if (dev->mode_config.rotation_property)
14527 drm_object_attach_property(&cursor->base.base,
14528 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080014529 state->base.rotation);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014530 }
14531
Chandra Konduruaf99ced2015-05-11 14:35:47 -070014532 if (INTEL_INFO(dev)->gen >=9)
14533 state->scaler_id = -1;
14534
Matt Roperea2c67b2014-12-23 10:41:52 -080014535 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
14536
Matt Roper3d7d6512014-06-10 08:28:13 -070014537 return &cursor->base;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014538
14539fail:
14540 kfree(state);
14541 kfree(cursor);
14542
14543 return NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070014544}
14545
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014546static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
14547 struct intel_crtc_state *crtc_state)
14548{
14549 int i;
14550 struct intel_scaler *intel_scaler;
14551 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
14552
14553 for (i = 0; i < intel_crtc->num_scalers; i++) {
14554 intel_scaler = &scaler_state->scalers[i];
14555 intel_scaler->in_use = 0;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014556 intel_scaler->mode = PS_SCALER_MODE_DYN;
14557 }
14558
14559 scaler_state->scaler_id = -1;
14560}
14561
Hannes Ederb358d0a2008-12-18 21:18:47 +010014562static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080014563{
Chris Wilsonfac5e232016-07-04 11:34:36 +010014564 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014565 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014566 struct intel_crtc_state *crtc_state = NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070014567 struct drm_plane *primary = NULL;
14568 struct drm_plane *cursor = NULL;
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +000014569 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080014570
Daniel Vetter955382f2013-09-19 14:05:45 +020014571 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080014572 if (intel_crtc == NULL)
14573 return;
14574
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014575 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
14576 if (!crtc_state)
14577 goto fail;
Ander Conselvan de Oliveira550acef2015-04-21 17:13:24 +030014578 intel_crtc->config = crtc_state;
14579 intel_crtc->base.state = &crtc_state->base;
Matt Roper07878242015-02-25 11:43:26 -080014580 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014581
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014582 /* initialize shared scalers */
14583 if (INTEL_INFO(dev)->gen >= 9) {
14584 if (pipe == PIPE_C)
14585 intel_crtc->num_scalers = 1;
14586 else
14587 intel_crtc->num_scalers = SKL_NUM_SCALERS;
14588
14589 skl_init_scalers(dev, intel_crtc, crtc_state);
14590 }
14591
Matt Roper465c1202014-05-29 08:06:54 -070014592 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014593 if (!primary)
14594 goto fail;
14595
14596 cursor = intel_cursor_plane_create(dev, pipe);
14597 if (!cursor)
14598 goto fail;
14599
Matt Roper465c1202014-05-29 08:06:54 -070014600 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Ville Syrjälä4d5d72b72016-05-27 20:59:21 +030014601 cursor, &intel_crtc_funcs,
14602 "pipe %c", pipe_name(pipe));
Matt Roper3d7d6512014-06-10 08:28:13 -070014603 if (ret)
14604 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080014605
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014606 /*
14607 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020014608 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014609 */
Jesse Barnes80824002009-09-10 15:28:06 -070014610 intel_crtc->pipe = pipe;
14611 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010014612 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080014613 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010014614 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070014615 }
14616
Chris Wilson4b0e3332014-05-30 16:35:26 +030014617 intel_crtc->cursor_base = ~0;
14618 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030014619 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030014620
Ville Syrjälä852eb002015-06-24 22:00:07 +030014621 intel_crtc->wm.cxsr_allowed = true;
14622
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080014623 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
14624 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
14625 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
14626 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
14627
Jesse Barnes79e53942008-11-07 14:24:08 -080014628 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020014629
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +000014630 intel_color_init(&intel_crtc->base);
14631
Daniel Vetter87b6b102014-05-15 15:33:46 +020014632 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014633 return;
14634
14635fail:
Ville Syrjälä69ae5612016-05-27 20:59:22 +030014636 intel_plane_destroy(primary);
14637 intel_plane_destroy(cursor);
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014638 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014639 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080014640}
14641
Jesse Barnes752aa882013-10-31 18:55:49 +020014642enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14643{
14644 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014645 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020014646
Rob Clark51fd3712013-11-19 12:10:12 -050014647 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020014648
Ville Syrjäläd3babd32014-11-07 11:16:01 +020014649 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020014650 return INVALID_PIPE;
14651
14652 return to_intel_crtc(encoder->crtc)->pipe;
14653}
14654
Carl Worth08d7b3d2009-04-29 14:43:54 -070014655int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000014656 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070014657{
Carl Worth08d7b3d2009-04-29 14:43:54 -070014658 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040014659 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020014660 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014661
Rob Clark7707e652014-07-17 23:30:04 -040014662 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Chris Wilson71240ed2016-06-24 14:00:24 +010014663 if (!drmmode_crtc)
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030014664 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014665
Rob Clark7707e652014-07-17 23:30:04 -040014666 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020014667 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014668
Daniel Vetterc05422d2009-08-11 16:05:30 +020014669 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014670}
14671
Daniel Vetter66a92782012-07-12 20:08:18 +020014672static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080014673{
Daniel Vetter66a92782012-07-12 20:08:18 +020014674 struct drm_device *dev = encoder->base.dev;
14675 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080014676 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080014677 int entry = 0;
14678
Damien Lespiaub2784e12014-08-05 11:29:37 +010014679 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020014680 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020014681 index_mask |= (1 << entry);
14682
Jesse Barnes79e53942008-11-07 14:24:08 -080014683 entry++;
14684 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010014685
Jesse Barnes79e53942008-11-07 14:24:08 -080014686 return index_mask;
14687}
14688
Chris Wilson4d302442010-12-14 19:21:29 +000014689static bool has_edp_a(struct drm_device *dev)
14690{
Chris Wilsonfac5e232016-07-04 11:34:36 +010014691 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson4d302442010-12-14 19:21:29 +000014692
14693 if (!IS_MOBILE(dev))
14694 return false;
14695
14696 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14697 return false;
14698
Damien Lespiaue3589902014-02-07 19:12:50 +000014699 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000014700 return false;
14701
14702 return true;
14703}
14704
Jesse Barnes84b4e042014-06-25 08:24:29 -070014705static bool intel_crt_present(struct drm_device *dev)
14706{
Chris Wilsonfac5e232016-07-04 11:34:36 +010014707 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes84b4e042014-06-25 08:24:29 -070014708
Damien Lespiau884497e2013-12-03 13:56:23 +000014709 if (INTEL_INFO(dev)->gen >= 9)
14710 return false;
14711
Damien Lespiaucf404ce2014-10-01 20:04:15 +010014712 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
Jesse Barnes84b4e042014-06-25 08:24:29 -070014713 return false;
14714
14715 if (IS_CHERRYVIEW(dev))
14716 return false;
14717
Ville Syrjälä65e472e2015-12-01 23:28:55 +020014718 if (HAS_PCH_LPT_H(dev) && I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
14719 return false;
14720
Ville Syrjälä70ac54d2015-12-01 23:29:56 +020014721 /* DDI E can't be used if DDI A requires 4 lanes */
14722 if (HAS_DDI(dev) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
14723 return false;
14724
Ville Syrjäläe4abb732015-12-01 23:31:33 +020014725 if (!dev_priv->vbt.int_crt_support)
Jesse Barnes84b4e042014-06-25 08:24:29 -070014726 return false;
14727
14728 return true;
14729}
14730
Jesse Barnes79e53942008-11-07 14:24:08 -080014731static void intel_setup_outputs(struct drm_device *dev)
14732{
Chris Wilsonfac5e232016-07-04 11:34:36 +010014733 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson4ef69c72010-09-09 15:14:28 +010014734 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014735 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080014736
Imre Deak97a824e12016-06-21 11:51:47 +030014737 /*
14738 * intel_edp_init_connector() depends on this completing first, to
14739 * prevent the registeration of both eDP and LVDS and the incorrect
14740 * sharing of the PPS.
14741 */
Daniel Vetterc9093352013-06-06 22:22:47 +020014742 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014743
Jesse Barnes84b4e042014-06-25 08:24:29 -070014744 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020014745 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014746
Vandana Kannanc776eb22014-08-19 12:05:01 +053014747 if (IS_BROXTON(dev)) {
14748 /*
14749 * FIXME: Broxton doesn't support port detection via the
14750 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14751 * detect the ports.
14752 */
14753 intel_ddi_init(dev, PORT_A);
14754 intel_ddi_init(dev, PORT_B);
14755 intel_ddi_init(dev, PORT_C);
Shashank Sharmac6c794a2016-03-22 12:01:50 +020014756
14757 intel_dsi_init(dev);
Vandana Kannanc776eb22014-08-19 12:05:01 +053014758 } else if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014759 int found;
14760
Jesse Barnesde31fac2015-03-06 15:53:32 -080014761 /*
14762 * Haswell uses DDI functions to detect digital outputs.
14763 * On SKL pre-D0 the strap isn't connected, so we assume
14764 * it's there.
14765 */
Ville Syrjälä77179402015-09-18 20:03:35 +030014766 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080014767 /* WaIgnoreDDIAStrap: skl */
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070014768 if (found || IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014769 intel_ddi_init(dev, PORT_A);
14770
14771 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14772 * register */
14773 found = I915_READ(SFUSE_STRAP);
14774
14775 if (found & SFUSE_STRAP_DDIB_DETECTED)
14776 intel_ddi_init(dev, PORT_B);
14777 if (found & SFUSE_STRAP_DDIC_DETECTED)
14778 intel_ddi_init(dev, PORT_C);
14779 if (found & SFUSE_STRAP_DDID_DETECTED)
14780 intel_ddi_init(dev, PORT_D);
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014781 /*
14782 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
14783 */
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070014784 if ((IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) &&
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014785 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
14786 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
14787 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
14788 intel_ddi_init(dev, PORT_E);
14789
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014790 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014791 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020014792 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020014793
14794 if (has_edp_a(dev))
14795 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014796
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014797 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080014798 /* PCH SDVOB multiplex with HDMIB */
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014799 found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014800 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014801 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014802 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014803 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014804 }
14805
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014806 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014807 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014808
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014809 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014810 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014811
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014812 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014813 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014814
Daniel Vetter270b3042012-10-27 15:52:05 +020014815 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014816 intel_dp_init(dev, PCH_DP_D, PORT_D);
Wayne Boyer666a4532015-12-09 12:29:35 -080014817 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Ville Syrjälä22f350422016-06-03 12:17:43 +030014818 bool has_edp, has_port;
Chris Wilson457c52d2016-06-01 08:27:50 +010014819
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014820 /*
14821 * The DP_DETECTED bit is the latched state of the DDC
14822 * SDA pin at boot. However since eDP doesn't require DDC
14823 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14824 * eDP ports may have been muxed to an alternate function.
14825 * Thus we can't rely on the DP_DETECTED bit alone to detect
14826 * eDP ports. Consult the VBT as well as DP_DETECTED to
14827 * detect eDP ports.
Ville Syrjälä22f350422016-06-03 12:17:43 +030014828 *
14829 * Sadly the straps seem to be missing sometimes even for HDMI
14830 * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
14831 * and VBT for the presence of the port. Additionally we can't
14832 * trust the port type the VBT declares as we've seen at least
14833 * HDMI ports that the VBT claim are DP or eDP.
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014834 */
Chris Wilson457c52d2016-06-01 08:27:50 +010014835 has_edp = intel_dp_is_edp(dev, PORT_B);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014836 has_port = intel_bios_is_port_present(dev_priv, PORT_B);
14837 if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
Chris Wilson457c52d2016-06-01 08:27:50 +010014838 has_edp &= intel_dp_init(dev, VLV_DP_B, PORT_B);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014839 if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014840 intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014841
Chris Wilson457c52d2016-06-01 08:27:50 +010014842 has_edp = intel_dp_is_edp(dev, PORT_C);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014843 has_port = intel_bios_is_port_present(dev_priv, PORT_C);
14844 if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
Chris Wilson457c52d2016-06-01 08:27:50 +010014845 has_edp &= intel_dp_init(dev, VLV_DP_C, PORT_C);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014846 if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014847 intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053014848
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014849 if (IS_CHERRYVIEW(dev)) {
Ville Syrjälä22f350422016-06-03 12:17:43 +030014850 /*
14851 * eDP not supported on port D,
14852 * so no need to worry about it
14853 */
14854 has_port = intel_bios_is_port_present(dev_priv, PORT_D);
14855 if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014856 intel_dp_init(dev, CHV_DP_D, PORT_D);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014857 if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
14858 intel_hdmi_init(dev, CHV_HDMID, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014859 }
14860
Jani Nikula3cfca972013-08-27 15:12:26 +030014861 intel_dsi_init(dev);
Daniel Vetter09da55d2015-07-07 11:44:32 +020014862 } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014863 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080014864
Paulo Zanonie2debe92013-02-18 19:00:27 -030014865 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014866 DRM_DEBUG_KMS("probing SDVOB\n");
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014867 found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014868 if (!found && IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014869 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014870 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014871 }
Ma Ling27185ae2009-08-24 13:50:23 +080014872
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014873 if (!found && IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014874 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080014875 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014876
14877 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014878
Paulo Zanonie2debe92013-02-18 19:00:27 -030014879 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014880 DRM_DEBUG_KMS("probing SDVOC\n");
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014881 found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014882 }
Ma Ling27185ae2009-08-24 13:50:23 +080014883
Paulo Zanonie2debe92013-02-18 19:00:27 -030014884 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014885
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014886 if (IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014887 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014888 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014889 }
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014890 if (IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014891 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080014892 }
Ma Ling27185ae2009-08-24 13:50:23 +080014893
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014894 if (IS_G4X(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030014895 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014896 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070014897 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014898 intel_dvo_init(dev);
14899
Zhenyu Wang103a1962009-11-27 11:44:36 +080014900 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014901 intel_tv_init(dev);
14902
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -080014903 intel_psr_init(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070014904
Damien Lespiaub2784e12014-08-05 11:29:37 +010014905 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010014906 encoder->base.possible_crtcs = encoder->crtc_mask;
14907 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020014908 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080014909 }
Chris Wilson47356eb2011-01-11 17:06:04 +000014910
Paulo Zanonidde86e22012-12-01 12:04:25 -020014911 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020014912
14913 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014914}
14915
14916static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14917{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014918 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080014919 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080014920
Daniel Vetteref2d6332014-02-10 18:00:38 +010014921 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014922 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010014923 WARN_ON(!intel_fb->obj->framebuffer_references--);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014924 drm_gem_object_unreference(&intel_fb->obj->base);
14925 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080014926 kfree(intel_fb);
14927}
14928
14929static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000014930 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080014931 unsigned int *handle)
14932{
14933 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000014934 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014935
Chris Wilsoncc917ab2015-10-13 14:22:26 +010014936 if (obj->userptr.mm) {
14937 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
14938 return -EINVAL;
14939 }
14940
Chris Wilson05394f32010-11-08 19:18:58 +000014941 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080014942}
14943
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014944static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
14945 struct drm_file *file,
14946 unsigned flags, unsigned color,
14947 struct drm_clip_rect *clips,
14948 unsigned num_clips)
14949{
14950 struct drm_device *dev = fb->dev;
14951 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
14952 struct drm_i915_gem_object *obj = intel_fb->obj;
14953
14954 mutex_lock(&dev->struct_mutex);
Paulo Zanoni74b4ea12015-07-14 16:29:14 -030014955 intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014956 mutex_unlock(&dev->struct_mutex);
14957
14958 return 0;
14959}
14960
Jesse Barnes79e53942008-11-07 14:24:08 -080014961static const struct drm_framebuffer_funcs intel_fb_funcs = {
14962 .destroy = intel_user_framebuffer_destroy,
14963 .create_handle = intel_user_framebuffer_create_handle,
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014964 .dirty = intel_user_framebuffer_dirty,
Jesse Barnes79e53942008-11-07 14:24:08 -080014965};
14966
Damien Lespiaub3218032015-02-27 11:15:18 +000014967static
14968u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14969 uint32_t pixel_format)
14970{
14971 u32 gen = INTEL_INFO(dev)->gen;
14972
14973 if (gen >= 9) {
Ville Syrjäläac484962016-01-20 21:05:26 +020014974 int cpp = drm_format_plane_cpp(pixel_format, 0);
14975
Damien Lespiaub3218032015-02-27 11:15:18 +000014976 /* "The stride in bytes must not exceed the of the size of 8K
14977 * pixels and 32K bytes."
14978 */
Ville Syrjäläac484962016-01-20 21:05:26 +020014979 return min(8192 * cpp, 32768);
Wayne Boyer666a4532015-12-09 12:29:35 -080014980 } else if (gen >= 5 && !IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014981 return 32*1024;
14982 } else if (gen >= 4) {
14983 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14984 return 16*1024;
14985 else
14986 return 32*1024;
14987 } else if (gen >= 3) {
14988 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14989 return 8*1024;
14990 else
14991 return 16*1024;
14992 } else {
14993 /* XXX DSPC is limited to 4k tiled */
14994 return 8*1024;
14995 }
14996}
14997
Daniel Vetterb5ea6422014-03-02 21:18:00 +010014998static int intel_framebuffer_init(struct drm_device *dev,
14999 struct intel_framebuffer *intel_fb,
15000 struct drm_mode_fb_cmd2 *mode_cmd,
15001 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080015002{
Ville Syrjälä7b49f942016-01-12 21:08:32 +020015003 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +000015004 unsigned int aligned_height;
Jesse Barnes79e53942008-11-07 14:24:08 -080015005 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000015006 u32 pitch_limit, stride_alignment;
Jesse Barnes79e53942008-11-07 14:24:08 -080015007
Daniel Vetterdd4916c2013-10-09 21:23:51 +020015008 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
15009
Daniel Vetter2a80ead2015-02-10 17:16:06 +000015010 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
15011 /* Enforce that fb modifier and tiling mode match, but only for
15012 * X-tiled. This is needed for FBC. */
15013 if (!!(obj->tiling_mode == I915_TILING_X) !=
15014 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
15015 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
15016 return -EINVAL;
15017 }
15018 } else {
15019 if (obj->tiling_mode == I915_TILING_X)
15020 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
15021 else if (obj->tiling_mode == I915_TILING_Y) {
15022 DRM_DEBUG("No Y tiling for legacy addfb\n");
15023 return -EINVAL;
15024 }
15025 }
15026
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000015027 /* Passed in modifier sanity checking. */
15028 switch (mode_cmd->modifier[0]) {
15029 case I915_FORMAT_MOD_Y_TILED:
15030 case I915_FORMAT_MOD_Yf_TILED:
15031 if (INTEL_INFO(dev)->gen < 9) {
15032 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
15033 mode_cmd->modifier[0]);
15034 return -EINVAL;
15035 }
15036 case DRM_FORMAT_MOD_NONE:
15037 case I915_FORMAT_MOD_X_TILED:
15038 break;
15039 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070015040 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
15041 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010015042 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015043 }
Chris Wilson57cd6502010-08-08 12:34:44 +010015044
Ville Syrjälä7b49f942016-01-12 21:08:32 +020015045 stride_alignment = intel_fb_stride_alignment(dev_priv,
15046 mode_cmd->modifier[0],
Damien Lespiaub3218032015-02-27 11:15:18 +000015047 mode_cmd->pixel_format);
15048 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
15049 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
15050 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010015051 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015052 }
Chris Wilson57cd6502010-08-08 12:34:44 +010015053
Damien Lespiaub3218032015-02-27 11:15:18 +000015054 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
15055 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010015056 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000015057 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
15058 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000015059 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010015060 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020015061 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015062 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020015063
Daniel Vetter2a80ead2015-02-10 17:16:06 +000015064 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015065 mode_cmd->pitches[0] != obj->stride) {
15066 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
15067 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020015068 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015069 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020015070
Ville Syrjälä57779d02012-10-31 17:50:14 +020015071 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080015072 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020015073 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020015074 case DRM_FORMAT_RGB565:
15075 case DRM_FORMAT_XRGB8888:
15076 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020015077 break;
15078 case DRM_FORMAT_XRGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015079 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000015080 DRM_DEBUG("unsupported pixel format: %s\n",
15081 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020015082 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015083 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020015084 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +020015085 case DRM_FORMAT_ABGR8888:
Wayne Boyer666a4532015-12-09 12:29:35 -080015086 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
15087 INTEL_INFO(dev)->gen < 9) {
Damien Lespiau6c0fd452015-05-19 12:29:16 +010015088 DRM_DEBUG("unsupported pixel format: %s\n",
15089 drm_get_format_name(mode_cmd->pixel_format));
15090 return -EINVAL;
15091 }
15092 break;
15093 case DRM_FORMAT_XBGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020015094 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020015095 case DRM_FORMAT_XBGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015096 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000015097 DRM_DEBUG("unsupported pixel format: %s\n",
15098 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020015099 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015100 }
Jesse Barnesb5626742011-06-24 12:19:27 -070015101 break;
Damien Lespiau75312082015-05-15 19:06:01 +010015102 case DRM_FORMAT_ABGR2101010:
Wayne Boyer666a4532015-12-09 12:29:35 -080015103 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Damien Lespiau75312082015-05-15 19:06:01 +010015104 DRM_DEBUG("unsupported pixel format: %s\n",
15105 drm_get_format_name(mode_cmd->pixel_format));
15106 return -EINVAL;
15107 }
15108 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020015109 case DRM_FORMAT_YUYV:
15110 case DRM_FORMAT_UYVY:
15111 case DRM_FORMAT_YVYU:
15112 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015113 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000015114 DRM_DEBUG("unsupported pixel format: %s\n",
15115 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020015116 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015117 }
Chris Wilson57cd6502010-08-08 12:34:44 +010015118 break;
15119 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000015120 DRM_DEBUG("unsupported pixel format: %s\n",
15121 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010015122 return -EINVAL;
15123 }
15124
Ville Syrjälä90f9a332012-10-31 17:50:19 +020015125 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
15126 if (mode_cmd->offsets[0] != 0)
15127 return -EINVAL;
15128
Damien Lespiauec2c9812015-01-20 12:51:45 +000015129 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +000015130 mode_cmd->pixel_format,
15131 mode_cmd->modifier[0]);
Daniel Vetter53155c02013-10-09 21:55:33 +020015132 /* FIXME drm helper for size checks (especially planar formats)? */
15133 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
15134 return -EINVAL;
15135
Daniel Vetterc7d73f62012-12-13 23:38:38 +010015136 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
15137 intel_fb->obj = obj;
15138
Ville Syrjälä2d7a2152016-02-15 22:54:47 +020015139 intel_fill_fb_info(dev_priv, &intel_fb->base);
15140
Jesse Barnes79e53942008-11-07 14:24:08 -080015141 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
15142 if (ret) {
15143 DRM_ERROR("framebuffer init failed %d\n", ret);
15144 return ret;
15145 }
15146
Ville Syrjälä0b05e1e2016-01-14 15:22:09 +020015147 intel_fb->obj->framebuffer_references++;
15148
Jesse Barnes79e53942008-11-07 14:24:08 -080015149 return 0;
15150}
15151
Jesse Barnes79e53942008-11-07 14:24:08 -080015152static struct drm_framebuffer *
15153intel_user_framebuffer_create(struct drm_device *dev,
15154 struct drm_file *filp,
Ville Syrjälä1eb83452015-11-11 19:11:29 +020015155 const struct drm_mode_fb_cmd2 *user_mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080015156{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020015157 struct drm_framebuffer *fb;
Chris Wilson05394f32010-11-08 19:18:58 +000015158 struct drm_i915_gem_object *obj;
Ville Syrjälä76dc3762015-11-11 19:11:28 +020015159 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
Jesse Barnes79e53942008-11-07 14:24:08 -080015160
Chris Wilsona8ad0bd2016-05-09 11:04:54 +010015161 obj = to_intel_bo(drm_gem_object_lookup(filp, mode_cmd.handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000015162 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010015163 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080015164
Daniel Vetter92907cb2015-11-23 09:04:05 +010015165 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020015166 if (IS_ERR(fb))
15167 drm_gem_object_unreference_unlocked(&obj->base);
15168
15169 return fb;
Jesse Barnes79e53942008-11-07 14:24:08 -080015170}
15171
Daniel Vetter06957262015-08-10 13:34:08 +020015172#ifndef CONFIG_DRM_FBDEV_EMULATION
Daniel Vetter0632fef2013-10-08 17:44:49 +020015173static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020015174{
15175}
15176#endif
15177
Jesse Barnes79e53942008-11-07 14:24:08 -080015178static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080015179 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020015180 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080015181 .atomic_check = intel_atomic_check,
15182 .atomic_commit = intel_atomic_commit,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +020015183 .atomic_state_alloc = intel_atomic_state_alloc,
15184 .atomic_state_clear = intel_atomic_state_clear,
Jesse Barnes79e53942008-11-07 14:24:08 -080015185};
15186
Imre Deak88212942016-03-16 13:38:53 +020015187/**
15188 * intel_init_display_hooks - initialize the display modesetting hooks
15189 * @dev_priv: device private
15190 */
15191void intel_init_display_hooks(struct drm_i915_private *dev_priv)
Jesse Barnese70236a2009-09-21 10:42:27 -070015192{
Imre Deak88212942016-03-16 13:38:53 +020015193 if (INTEL_INFO(dev_priv)->gen >= 9) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000015194 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015195 dev_priv->display.get_initial_plane_config =
15196 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000015197 dev_priv->display.crtc_compute_clock =
15198 haswell_crtc_compute_clock;
15199 dev_priv->display.crtc_enable = haswell_crtc_enable;
15200 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020015201 } else if (HAS_DDI(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010015202 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015203 dev_priv->display.get_initial_plane_config =
15204 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020015205 dev_priv->display.crtc_compute_clock =
15206 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020015207 dev_priv->display.crtc_enable = haswell_crtc_enable;
15208 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020015209 } else if (HAS_PCH_SPLIT(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010015210 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015211 dev_priv->display.get_initial_plane_config =
15212 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020015213 dev_priv->display.crtc_compute_clock =
15214 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020015215 dev_priv->display.crtc_enable = ironlake_crtc_enable;
15216 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +020015217 } else if (IS_CHERRYVIEW(dev_priv)) {
Jesse Barnes89b667f2013-04-18 14:51:36 -070015218 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015219 dev_priv->display.get_initial_plane_config =
15220 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +020015221 dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
15222 dev_priv->display.crtc_enable = valleyview_crtc_enable;
15223 dev_priv->display.crtc_disable = i9xx_crtc_disable;
15224 } else if (IS_VALLEYVIEW(dev_priv)) {
15225 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15226 dev_priv->display.get_initial_plane_config =
15227 i9xx_get_initial_plane_config;
15228 dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070015229 dev_priv->display.crtc_enable = valleyview_crtc_enable;
15230 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +020015231 } else if (IS_G4X(dev_priv)) {
15232 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15233 dev_priv->display.get_initial_plane_config =
15234 i9xx_get_initial_plane_config;
15235 dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
15236 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15237 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +020015238 } else if (IS_PINEVIEW(dev_priv)) {
15239 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15240 dev_priv->display.get_initial_plane_config =
15241 i9xx_get_initial_plane_config;
15242 dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
15243 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15244 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +020015245 } else if (!IS_GEN2(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010015246 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015247 dev_priv->display.get_initial_plane_config =
15248 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020015249 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020015250 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15251 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +020015252 } else {
15253 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15254 dev_priv->display.get_initial_plane_config =
15255 i9xx_get_initial_plane_config;
15256 dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
15257 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15258 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Eric Anholtf564048e2011-03-30 13:01:02 -070015259 }
Jesse Barnese70236a2009-09-21 10:42:27 -070015260
Jesse Barnese70236a2009-09-21 10:42:27 -070015261 /* Returns the core display clock speed */
Imre Deak88212942016-03-16 13:38:53 +020015262 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030015263 dev_priv->display.get_display_clock_speed =
15264 skylake_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015265 else if (IS_BROXTON(dev_priv))
Bob Paauweacd3f3d2015-06-23 14:14:26 -070015266 dev_priv->display.get_display_clock_speed =
15267 broxton_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015268 else if (IS_BROADWELL(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030015269 dev_priv->display.get_display_clock_speed =
15270 broadwell_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015271 else if (IS_HASWELL(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030015272 dev_priv->display.get_display_clock_speed =
15273 haswell_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015274 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070015275 dev_priv->display.get_display_clock_speed =
15276 valleyview_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015277 else if (IS_GEN5(dev_priv))
Ville Syrjäläb37a6432015-03-31 14:11:54 +030015278 dev_priv->display.get_display_clock_speed =
15279 ilk_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015280 else if (IS_I945G(dev_priv) || IS_BROADWATER(dev_priv) ||
15281 IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015282 dev_priv->display.get_display_clock_speed =
15283 i945_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015284 else if (IS_GM45(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030015285 dev_priv->display.get_display_clock_speed =
15286 gm45_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015287 else if (IS_CRESTLINE(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030015288 dev_priv->display.get_display_clock_speed =
15289 i965gm_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015290 else if (IS_PINEVIEW(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030015291 dev_priv->display.get_display_clock_speed =
15292 pnv_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015293 else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030015294 dev_priv->display.get_display_clock_speed =
15295 g33_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015296 else if (IS_I915G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015297 dev_priv->display.get_display_clock_speed =
15298 i915_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015299 else if (IS_I945GM(dev_priv) || IS_845G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015300 dev_priv->display.get_display_clock_speed =
15301 i9xx_misc_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015302 else if (IS_I915GM(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015303 dev_priv->display.get_display_clock_speed =
15304 i915gm_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015305 else if (IS_I865G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015306 dev_priv->display.get_display_clock_speed =
15307 i865_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015308 else if (IS_I85X(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015309 dev_priv->display.get_display_clock_speed =
Ville Syrjälä1b1d2712015-05-22 11:22:31 +030015310 i85x_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030015311 else { /* 830 */
Imre Deak88212942016-03-16 13:38:53 +020015312 WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
Jesse Barnese70236a2009-09-21 10:42:27 -070015313 dev_priv->display.get_display_clock_speed =
15314 i830_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030015315 }
Jesse Barnese70236a2009-09-21 10:42:27 -070015316
Imre Deak88212942016-03-16 13:38:53 +020015317 if (IS_GEN5(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015318 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020015319 } else if (IS_GEN6(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015320 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020015321 } else if (IS_IVYBRIDGE(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015322 /* FIXME: detect B0+ stepping and use auto training */
15323 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020015324 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015325 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Ville Syrjälä445e7802016-05-11 22:44:42 +030015326 }
15327
15328 if (IS_BROADWELL(dev_priv)) {
15329 dev_priv->display.modeset_commit_cdclk =
15330 broadwell_modeset_commit_cdclk;
15331 dev_priv->display.modeset_calc_cdclk =
15332 broadwell_modeset_calc_cdclk;
Imre Deak88212942016-03-16 13:38:53 +020015333 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020015334 dev_priv->display.modeset_commit_cdclk =
15335 valleyview_modeset_commit_cdclk;
15336 dev_priv->display.modeset_calc_cdclk =
15337 valleyview_modeset_calc_cdclk;
Imre Deak88212942016-03-16 13:38:53 +020015338 } else if (IS_BROXTON(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020015339 dev_priv->display.modeset_commit_cdclk =
Imre Deak324513c2016-06-13 16:44:36 +030015340 bxt_modeset_commit_cdclk;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020015341 dev_priv->display.modeset_calc_cdclk =
Imre Deak324513c2016-06-13 16:44:36 +030015342 bxt_modeset_calc_cdclk;
Clint Taylorc89e39f2016-05-13 23:41:21 +030015343 } else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
15344 dev_priv->display.modeset_commit_cdclk =
15345 skl_modeset_commit_cdclk;
15346 dev_priv->display.modeset_calc_cdclk =
15347 skl_modeset_calc_cdclk;
Jesse Barnese70236a2009-09-21 10:42:27 -070015348 }
Daniel Vetter5a21b662016-05-24 17:13:53 +020015349
15350 switch (INTEL_INFO(dev_priv)->gen) {
15351 case 2:
15352 dev_priv->display.queue_flip = intel_gen2_queue_flip;
15353 break;
15354
15355 case 3:
15356 dev_priv->display.queue_flip = intel_gen3_queue_flip;
15357 break;
15358
15359 case 4:
15360 case 5:
15361 dev_priv->display.queue_flip = intel_gen4_queue_flip;
15362 break;
15363
15364 case 6:
15365 dev_priv->display.queue_flip = intel_gen6_queue_flip;
15366 break;
15367 case 7:
15368 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
15369 dev_priv->display.queue_flip = intel_gen7_queue_flip;
15370 break;
15371 case 9:
15372 /* Drop through - unsupported since execlist only. */
15373 default:
15374 /* Default just returns -ENODEV to indicate unsupported */
15375 dev_priv->display.queue_flip = intel_default_queue_flip;
15376 }
Jesse Barnese70236a2009-09-21 10:42:27 -070015377}
15378
Jesse Barnesb690e962010-07-19 13:53:12 -070015379/*
15380 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
15381 * resume, or other times. This quirk makes sure that's the case for
15382 * affected systems.
15383 */
Akshay Joshi0206e352011-08-16 15:34:10 -040015384static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070015385{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015386 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesb690e962010-07-19 13:53:12 -070015387
15388 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015389 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070015390}
15391
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030015392static void quirk_pipeb_force(struct drm_device *dev)
15393{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015394 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030015395
15396 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
15397 DRM_INFO("applying pipe b force quirk\n");
15398}
15399
Keith Packard435793d2011-07-12 14:56:22 -070015400/*
15401 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
15402 */
15403static void quirk_ssc_force_disable(struct drm_device *dev)
15404{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015405 struct drm_i915_private *dev_priv = to_i915(dev);
Keith Packard435793d2011-07-12 14:56:22 -070015406 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015407 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070015408}
15409
Carsten Emde4dca20e2012-03-15 15:56:26 +010015410/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010015411 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
15412 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010015413 */
15414static void quirk_invert_brightness(struct drm_device *dev)
15415{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015416 struct drm_i915_private *dev_priv = to_i915(dev);
Carsten Emde4dca20e2012-03-15 15:56:26 +010015417 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015418 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070015419}
15420
Scot Doyle9c72cc62014-07-03 23:27:50 +000015421/* Some VBT's incorrectly indicate no backlight is present */
15422static void quirk_backlight_present(struct drm_device *dev)
15423{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015424 struct drm_i915_private *dev_priv = to_i915(dev);
Scot Doyle9c72cc62014-07-03 23:27:50 +000015425 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
15426 DRM_INFO("applying backlight present quirk\n");
15427}
15428
Jesse Barnesb690e962010-07-19 13:53:12 -070015429struct intel_quirk {
15430 int device;
15431 int subsystem_vendor;
15432 int subsystem_device;
15433 void (*hook)(struct drm_device *dev);
15434};
15435
Egbert Eich5f85f172012-10-14 15:46:38 +020015436/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
15437struct intel_dmi_quirk {
15438 void (*hook)(struct drm_device *dev);
15439 const struct dmi_system_id (*dmi_id_list)[];
15440};
15441
15442static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
15443{
15444 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
15445 return 1;
15446}
15447
15448static const struct intel_dmi_quirk intel_dmi_quirks[] = {
15449 {
15450 .dmi_id_list = &(const struct dmi_system_id[]) {
15451 {
15452 .callback = intel_dmi_reverse_brightness,
15453 .ident = "NCR Corporation",
15454 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
15455 DMI_MATCH(DMI_PRODUCT_NAME, ""),
15456 },
15457 },
15458 { } /* terminating entry */
15459 },
15460 .hook = quirk_invert_brightness,
15461 },
15462};
15463
Ben Widawskyc43b5632012-04-16 14:07:40 -070015464static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070015465 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
15466 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
15467
Jesse Barnesb690e962010-07-19 13:53:12 -070015468 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
15469 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
15470
Ville Syrjälä5f080c02014-08-15 01:22:06 +030015471 /* 830 needs to leave pipe A & dpll A up */
15472 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
15473
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030015474 /* 830 needs to leave pipe B & dpll B up */
15475 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
15476
Keith Packard435793d2011-07-12 14:56:22 -070015477 /* Lenovo U160 cannot use SSC on LVDS */
15478 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020015479
15480 /* Sony Vaio Y cannot use SSC on LVDS */
15481 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010015482
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010015483 /* Acer Aspire 5734Z must invert backlight brightness */
15484 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
15485
15486 /* Acer/eMachines G725 */
15487 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
15488
15489 /* Acer/eMachines e725 */
15490 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
15491
15492 /* Acer/Packard Bell NCL20 */
15493 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
15494
15495 /* Acer Aspire 4736Z */
15496 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020015497
15498 /* Acer Aspire 5336 */
15499 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000015500
15501 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
15502 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000015503
Scot Doyledfb3d47b2014-08-21 16:08:02 +000015504 /* Acer C720 Chromebook (Core i3 4005U) */
15505 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
15506
jens steinb2a96012014-10-28 20:25:53 +010015507 /* Apple Macbook 2,1 (Core 2 T7400) */
15508 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
15509
Jani Nikula1b9448b2015-11-05 11:49:59 +020015510 /* Apple Macbook 4,1 */
15511 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
15512
Scot Doyled4967d82014-07-03 23:27:52 +000015513 /* Toshiba CB35 Chromebook (Celeron 2955U) */
15514 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000015515
15516 /* HP Chromebook 14 (Celeron 2955U) */
15517 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020015518
15519 /* Dell Chromebook 11 */
15520 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jani Nikula9be64ee2015-10-30 14:50:24 +020015521
15522 /* Dell Chromebook 11 (2015 version) */
15523 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070015524};
15525
15526static void intel_init_quirks(struct drm_device *dev)
15527{
15528 struct pci_dev *d = dev->pdev;
15529 int i;
15530
15531 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
15532 struct intel_quirk *q = &intel_quirks[i];
15533
15534 if (d->device == q->device &&
15535 (d->subsystem_vendor == q->subsystem_vendor ||
15536 q->subsystem_vendor == PCI_ANY_ID) &&
15537 (d->subsystem_device == q->subsystem_device ||
15538 q->subsystem_device == PCI_ANY_ID))
15539 q->hook(dev);
15540 }
Egbert Eich5f85f172012-10-14 15:46:38 +020015541 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
15542 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
15543 intel_dmi_quirks[i].hook(dev);
15544 }
Jesse Barnesb690e962010-07-19 13:53:12 -070015545}
15546
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015547/* Disable the VGA plane that we never use */
15548static void i915_disable_vga(struct drm_device *dev)
15549{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015550 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015551 u8 sr1;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020015552 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015553
Ville Syrjälä2b37c612014-01-22 21:32:38 +020015554 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015555 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070015556 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015557 sr1 = inb(VGA_SR_DATA);
15558 outb(sr1 | 1<<5, VGA_SR_DATA);
15559 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
15560 udelay(300);
15561
Ville Syrjälä01f5a622014-12-16 18:38:37 +020015562 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015563 POSTING_READ(vga_reg);
15564}
15565
Daniel Vetterf8175862012-04-10 15:50:11 +020015566void intel_modeset_init_hw(struct drm_device *dev)
15567{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015568 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010015569
Ville Syrjäläb6283052015-06-03 15:45:07 +030015570 intel_update_cdclk(dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010015571
15572 dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
15573
Daniel Vetterf8175862012-04-10 15:50:11 +020015574 intel_init_clock_gating(dev);
Chris Wilsondc979972016-05-10 14:10:04 +010015575 intel_enable_gt_powersave(dev_priv);
Daniel Vetterf8175862012-04-10 15:50:11 +020015576}
15577
Matt Roperd93c0372015-12-03 11:37:41 -080015578/*
15579 * Calculate what we think the watermarks should be for the state we've read
15580 * out of the hardware and then immediately program those watermarks so that
15581 * we ensure the hardware settings match our internal state.
15582 *
15583 * We can calculate what we think WM's should be by creating a duplicate of the
15584 * current state (which was constructed during hardware readout) and running it
15585 * through the atomic check code to calculate new watermark values in the
15586 * state object.
15587 */
15588static void sanitize_watermarks(struct drm_device *dev)
15589{
15590 struct drm_i915_private *dev_priv = to_i915(dev);
15591 struct drm_atomic_state *state;
15592 struct drm_crtc *crtc;
15593 struct drm_crtc_state *cstate;
15594 struct drm_modeset_acquire_ctx ctx;
15595 int ret;
15596 int i;
15597
15598 /* Only supported on platforms that use atomic watermark design */
Matt Ropered4a6a72016-02-23 17:20:13 -080015599 if (!dev_priv->display.optimize_watermarks)
Matt Roperd93c0372015-12-03 11:37:41 -080015600 return;
15601
15602 /*
15603 * We need to hold connection_mutex before calling duplicate_state so
15604 * that the connector loop is protected.
15605 */
15606 drm_modeset_acquire_init(&ctx, 0);
15607retry:
Matt Roper0cd12622016-01-12 07:13:37 -080015608 ret = drm_modeset_lock_all_ctx(dev, &ctx);
Matt Roperd93c0372015-12-03 11:37:41 -080015609 if (ret == -EDEADLK) {
15610 drm_modeset_backoff(&ctx);
15611 goto retry;
15612 } else if (WARN_ON(ret)) {
Matt Roper0cd12622016-01-12 07:13:37 -080015613 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015614 }
15615
15616 state = drm_atomic_helper_duplicate_state(dev, &ctx);
15617 if (WARN_ON(IS_ERR(state)))
Matt Roper0cd12622016-01-12 07:13:37 -080015618 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015619
Matt Ropered4a6a72016-02-23 17:20:13 -080015620 /*
15621 * Hardware readout is the only time we don't want to calculate
15622 * intermediate watermarks (since we don't trust the current
15623 * watermarks).
15624 */
15625 to_intel_atomic_state(state)->skip_intermediate_wm = true;
15626
Matt Roperd93c0372015-12-03 11:37:41 -080015627 ret = intel_atomic_check(dev, state);
15628 if (ret) {
15629 /*
15630 * If we fail here, it means that the hardware appears to be
15631 * programmed in a way that shouldn't be possible, given our
15632 * understanding of watermark requirements. This might mean a
15633 * mistake in the hardware readout code or a mistake in the
15634 * watermark calculations for a given platform. Raise a WARN
15635 * so that this is noticeable.
15636 *
15637 * If this actually happens, we'll have to just leave the
15638 * BIOS-programmed watermarks untouched and hope for the best.
15639 */
15640 WARN(true, "Could not determine valid watermarks for inherited state\n");
Matt Roper0cd12622016-01-12 07:13:37 -080015641 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015642 }
15643
15644 /* Write calculated watermark values back */
Matt Roperd93c0372015-12-03 11:37:41 -080015645 for_each_crtc_in_state(state, crtc, cstate, i) {
15646 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
15647
Matt Ropered4a6a72016-02-23 17:20:13 -080015648 cs->wm.need_postvbl_update = true;
15649 dev_priv->display.optimize_watermarks(cs);
Matt Roperd93c0372015-12-03 11:37:41 -080015650 }
15651
15652 drm_atomic_state_free(state);
Matt Roper0cd12622016-01-12 07:13:37 -080015653fail:
Matt Roperd93c0372015-12-03 11:37:41 -080015654 drm_modeset_drop_locks(&ctx);
15655 drm_modeset_acquire_fini(&ctx);
15656}
15657
Jesse Barnes79e53942008-11-07 14:24:08 -080015658void intel_modeset_init(struct drm_device *dev)
15659{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +030015660 struct drm_i915_private *dev_priv = to_i915(dev);
15661 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Damien Lespiau1fe47782014-03-03 17:31:47 +000015662 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015663 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080015664 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080015665
15666 drm_mode_config_init(dev);
15667
15668 dev->mode_config.min_width = 0;
15669 dev->mode_config.min_height = 0;
15670
Dave Airlie019d96c2011-09-29 16:20:42 +010015671 dev->mode_config.preferred_depth = 24;
15672 dev->mode_config.prefer_shadow = 1;
15673
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000015674 dev->mode_config.allow_fb_modifiers = true;
15675
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020015676 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080015677
Jesse Barnesb690e962010-07-19 13:53:12 -070015678 intel_init_quirks(dev);
15679
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030015680 intel_init_pm(dev);
15681
Ben Widawskye3c74752013-04-05 13:12:39 -070015682 if (INTEL_INFO(dev)->num_pipes == 0)
15683 return;
15684
Lukas Wunner69f92f62015-07-15 13:57:35 +020015685 /*
15686 * There may be no VBT; and if the BIOS enabled SSC we can
15687 * just keep using it to avoid unnecessary flicker. Whereas if the
15688 * BIOS isn't using it, don't assume it will work even if the VBT
15689 * indicates as much.
15690 */
15691 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
15692 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15693 DREF_SSC1_ENABLE);
15694
15695 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
15696 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
15697 bios_lvds_use_ssc ? "en" : "dis",
15698 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
15699 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
15700 }
15701 }
15702
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015703 if (IS_GEN2(dev)) {
15704 dev->mode_config.max_width = 2048;
15705 dev->mode_config.max_height = 2048;
15706 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070015707 dev->mode_config.max_width = 4096;
15708 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080015709 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015710 dev->mode_config.max_width = 8192;
15711 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080015712 }
Damien Lespiau068be562014-03-28 14:17:49 +000015713
Ville Syrjälädc41c152014-08-13 11:57:05 +030015714 if (IS_845G(dev) || IS_I865G(dev)) {
15715 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
15716 dev->mode_config.cursor_height = 1023;
15717 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000015718 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
15719 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
15720 } else {
15721 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
15722 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
15723 }
15724
Joonas Lahtinen72e96d62016-03-30 16:57:10 +030015725 dev->mode_config.fb_base = ggtt->mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080015726
Zhao Yakui28c97732009-10-09 11:39:41 +080015727 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015728 INTEL_INFO(dev)->num_pipes,
15729 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080015730
Damien Lespiau055e3932014-08-18 13:49:10 +010015731 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015732 intel_crtc_init(dev, pipe);
Damien Lespiau3bdcfc02015-02-28 14:54:09 +000015733 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +000015734 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015735 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030015736 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000015737 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015738 }
Jesse Barnes79e53942008-11-07 14:24:08 -080015739 }
15740
Ville Syrjäläbfa7df02015-09-24 23:29:18 +030015741 intel_update_czclk(dev_priv);
15742 intel_update_cdclk(dev);
15743
Daniel Vettere72f9fb2013-06-05 13:34:06 +020015744 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010015745
Ville Syrjäläb2045352016-05-13 23:41:27 +030015746 if (dev_priv->max_cdclk_freq == 0)
15747 intel_update_max_cdclk(dev);
15748
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015749 /* Just disable it once at startup */
15750 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015751 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000015752
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015753 drm_modeset_lock_all(dev);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015754 intel_modeset_setup_hw_state(dev);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015755 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015756
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015757 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015758 struct intel_initial_plane_config plane_config = {};
15759
Jesse Barnes46f297f2014-03-07 08:57:48 -080015760 if (!crtc->active)
15761 continue;
15762
Jesse Barnes46f297f2014-03-07 08:57:48 -080015763 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080015764 * Note that reserving the BIOS fb up front prevents us
15765 * from stuffing other stolen allocations like the ring
15766 * on top. This prevents some ugliness at boot time, and
15767 * can even allow for smooth boot transitions if the BIOS
15768 * fb is large enough for the active pipe configuration.
15769 */
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015770 dev_priv->display.get_initial_plane_config(crtc,
15771 &plane_config);
15772
15773 /*
15774 * If the fb is shared between multiple heads, we'll
15775 * just get the first one.
15776 */
15777 intel_find_initial_plane_obj(crtc, &plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015778 }
Matt Roperd93c0372015-12-03 11:37:41 -080015779
15780 /*
15781 * Make sure hardware watermarks really match the state we read out.
15782 * Note that we need to do this after reconstructing the BIOS fb's
15783 * since the watermark calculation done here will use pstate->fb.
15784 */
15785 sanitize_watermarks(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010015786}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080015787
Daniel Vetter7fad7982012-07-04 17:51:47 +020015788static void intel_enable_pipe_a(struct drm_device *dev)
15789{
15790 struct intel_connector *connector;
15791 struct drm_connector *crt = NULL;
15792 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015793 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020015794
15795 /* We can't just switch on the pipe A, we need to set things up with a
15796 * proper mode and output configuration. As a gross hack, enable pipe A
15797 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015798 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020015799 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15800 crt = &connector->base;
15801 break;
15802 }
15803 }
15804
15805 if (!crt)
15806 return;
15807
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015808 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020015809 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020015810}
15811
Daniel Vetterfa555832012-10-10 23:14:00 +020015812static bool
15813intel_check_plane_mapping(struct intel_crtc *crtc)
15814{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015815 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010015816 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä649636e2015-09-22 19:50:01 +030015817 u32 val;
Daniel Vetterfa555832012-10-10 23:14:00 +020015818
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015819 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020015820 return true;
15821
Ville Syrjälä649636e2015-09-22 19:50:01 +030015822 val = I915_READ(DSPCNTR(!crtc->plane));
Daniel Vetterfa555832012-10-10 23:14:00 +020015823
15824 if ((val & DISPLAY_PLANE_ENABLE) &&
15825 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15826 return false;
15827
15828 return true;
15829}
15830
Ville Syrjälä02e93c32015-08-26 19:39:19 +030015831static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
15832{
15833 struct drm_device *dev = crtc->base.dev;
15834 struct intel_encoder *encoder;
15835
15836 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15837 return true;
15838
15839 return false;
15840}
15841
Ville Syrjälädd756192016-02-17 21:28:45 +020015842static bool intel_encoder_has_connectors(struct intel_encoder *encoder)
15843{
15844 struct drm_device *dev = encoder->base.dev;
15845 struct intel_connector *connector;
15846
15847 for_each_connector_on_encoder(dev, &encoder->base, connector)
15848 return true;
15849
15850 return false;
15851}
15852
Daniel Vetter24929352012-07-02 20:28:59 +020015853static void intel_sanitize_crtc(struct intel_crtc *crtc)
15854{
15855 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010015856 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikula4d1de972016-03-18 17:05:42 +020015857 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter24929352012-07-02 20:28:59 +020015858
Daniel Vetter24929352012-07-02 20:28:59 +020015859 /* Clear any frame start delays used for debugging left by the BIOS */
Jani Nikula4d1de972016-03-18 17:05:42 +020015860 if (!transcoder_is_dsi(cpu_transcoder)) {
15861 i915_reg_t reg = PIPECONF(cpu_transcoder);
15862
15863 I915_WRITE(reg,
15864 I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15865 }
Daniel Vetter24929352012-07-02 20:28:59 +020015866
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015867 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010015868 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030015869 if (crtc->active) {
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015870 struct intel_plane *plane;
15871
Daniel Vetter96256042015-02-13 21:03:42 +010015872 drm_crtc_vblank_on(&crtc->base);
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015873
15874 /* Disable everything but the primary plane */
15875 for_each_intel_plane_on_crtc(dev, crtc, plane) {
15876 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
15877 continue;
15878
15879 plane->disable_plane(&plane->base, &crtc->base);
15880 }
Daniel Vetter96256042015-02-13 21:03:42 +010015881 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015882
Daniel Vetter24929352012-07-02 20:28:59 +020015883 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020015884 * disable the crtc (and hence change the state) if it is wrong. Note
15885 * that gen4+ has a fixed plane -> pipe mapping. */
15886 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020015887 bool plane;
15888
Ville Syrjälä78108b72016-05-27 20:59:19 +030015889 DRM_DEBUG_KMS("[CRTC:%d:%s] wrong plane connection detected!\n",
15890 crtc->base.base.id, crtc->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015891
15892 /* Pipe has the wrong plane attached and the plane is active.
15893 * Temporarily change the plane mapping and disable everything
15894 * ... */
15895 plane = crtc->plane;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015896 to_intel_plane_state(crtc->base.primary->state)->visible = true;
Daniel Vetter24929352012-07-02 20:28:59 +020015897 crtc->plane = !plane;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015898 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015899 crtc->plane = plane;
Daniel Vetter24929352012-07-02 20:28:59 +020015900 }
Daniel Vetter24929352012-07-02 20:28:59 +020015901
Daniel Vetter7fad7982012-07-04 17:51:47 +020015902 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15903 crtc->pipe == PIPE_A && !crtc->active) {
15904 /* BIOS forgot to enable pipe A, this mostly happens after
15905 * resume. Force-enable the pipe to fix this, the update_dpms
15906 * call below we restore the pipe to the right state, but leave
15907 * the required bits on. */
15908 intel_enable_pipe_a(dev);
15909 }
15910
Daniel Vetter24929352012-07-02 20:28:59 +020015911 /* Adjust the state of the output pipe according to whether we
15912 * have active connectors/encoders. */
Maarten Lankhorst842e0302016-03-02 15:48:01 +010015913 if (crtc->active && !intel_crtc_has_encoders(crtc))
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015914 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015915
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030015916 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010015917 /*
15918 * We start out with underrun reporting disabled to avoid races.
15919 * For correct bookkeeping mark this on active crtcs.
15920 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020015921 * Also on gmch platforms we dont have any hardware bits to
15922 * disable the underrun reporting. Which means we need to start
15923 * out with underrun reporting disabled also on inactive pipes,
15924 * since otherwise we'll complain about the garbage we read when
15925 * e.g. coming up after runtime pm.
15926 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010015927 * No protection against concurrent access is required - at
15928 * worst a fifo underrun happens which also sets this to false.
15929 */
15930 crtc->cpu_fifo_underrun_disabled = true;
15931 crtc->pch_fifo_underrun_disabled = true;
15932 }
Daniel Vetter24929352012-07-02 20:28:59 +020015933}
15934
15935static void intel_sanitize_encoder(struct intel_encoder *encoder)
15936{
15937 struct intel_connector *connector;
15938 struct drm_device *dev = encoder->base.dev;
15939
15940 /* We need to check both for a crtc link (meaning that the
15941 * encoder is active and trying to read from a pipe) and the
15942 * pipe itself being active. */
15943 bool has_active_crtc = encoder->base.crtc &&
15944 to_intel_crtc(encoder->base.crtc)->active;
15945
Ville Syrjälädd756192016-02-17 21:28:45 +020015946 if (intel_encoder_has_connectors(encoder) && !has_active_crtc) {
Daniel Vetter24929352012-07-02 20:28:59 +020015947 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15948 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015949 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015950
15951 /* Connector is active, but has no active pipe. This is
15952 * fallout from our resume register restoring. Disable
15953 * the encoder manually again. */
15954 if (encoder->base.crtc) {
15955 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15956 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015957 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015958 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030015959 if (encoder->post_disable)
15960 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020015961 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020015962 encoder->base.crtc = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015963
15964 /* Inconsistent output/port/pipe state happens presumably due to
15965 * a bug in one of the get_hw_state functions. Or someplace else
15966 * in our code, like the register restore mess on resume. Clamp
15967 * things to off as a safer default. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015968 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015969 if (connector->encoder != encoder)
15970 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020015971 connector->base.dpms = DRM_MODE_DPMS_OFF;
15972 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015973 }
15974 }
15975 /* Enabled encoders without active connectors will be fixed in
15976 * the crtc fixup. */
15977}
15978
Imre Deak04098752014-02-18 00:02:16 +020015979void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015980{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015981 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020015982 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015983
Imre Deak04098752014-02-18 00:02:16 +020015984 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15985 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15986 i915_disable_vga(dev);
15987 }
15988}
15989
15990void i915_redisable_vga(struct drm_device *dev)
15991{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015992 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak04098752014-02-18 00:02:16 +020015993
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015994 /* This function can be called both from intel_modeset_setup_hw_state or
15995 * at a very early point in our resume sequence, where the power well
15996 * structures are not yet restored. Since this function is at a very
15997 * paranoid "someone might have enabled VGA while we were not looking"
15998 * level, just check if the power well is enabled instead of trying to
15999 * follow the "don't touch the power well if we don't need it" policy
16000 * the rest of the driver uses. */
Imre Deak6392f842016-02-12 18:55:13 +020016001 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030016002 return;
16003
Imre Deak04098752014-02-18 00:02:16 +020016004 i915_redisable_vga_power_on(dev);
Imre Deak6392f842016-02-12 18:55:13 +020016005
16006 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010016007}
16008
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030016009static bool primary_get_hw_state(struct intel_plane *plane)
Ville Syrjälä98ec7732014-04-30 17:43:01 +030016010{
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030016011 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030016012
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030016013 return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020016014}
Ville Syrjälä98ec7732014-04-30 17:43:01 +030016015
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030016016/* FIXME read out full plane state for all planes */
16017static void readout_plane_state(struct intel_crtc *crtc)
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020016018{
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020016019 struct drm_plane *primary = crtc->base.primary;
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030016020 struct intel_plane_state *plane_state =
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020016021 to_intel_plane_state(primary->state);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020016022
Matt Roper19b8d382015-09-24 15:53:17 -070016023 plane_state->visible = crtc->active &&
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020016024 primary_get_hw_state(to_intel_plane(primary));
16025
16026 if (plane_state->visible)
16027 crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030016028}
16029
Daniel Vetter30e984d2013-06-05 13:34:17 +020016030static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020016031{
Chris Wilsonfac5e232016-07-04 11:34:36 +010016032 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020016033 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020016034 struct intel_crtc *crtc;
16035 struct intel_encoder *encoder;
16036 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020016037 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020016038
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016039 dev_priv->active_crtcs = 0;
16040
Damien Lespiaud3fcc802014-05-13 23:32:22 +010016041 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016042 struct intel_crtc_state *crtc_state = crtc->config;
16043 int pixclk = 0;
Daniel Vetter3b117c82013-04-17 20:15:07 +020016044
Daniel Vetterec2dc6a2016-05-09 16:34:09 +020016045 __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016046 memset(crtc_state, 0, sizeof(*crtc_state));
16047 crtc_state->base.crtc = &crtc->base;
Daniel Vetter24929352012-07-02 20:28:59 +020016048
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016049 crtc_state->base.active = crtc_state->base.enable =
16050 dev_priv->display.get_pipe_config(crtc, crtc_state);
16051
16052 crtc->base.enabled = crtc_state->base.enable;
16053 crtc->active = crtc_state->base.active;
16054
16055 if (crtc_state->base.active) {
16056 dev_priv->active_crtcs |= 1 << crtc->pipe;
16057
Clint Taylorc89e39f2016-05-13 23:41:21 +030016058 if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016059 pixclk = ilk_pipe_pixel_rate(crtc_state);
Ville Syrjälä9558d152016-05-13 23:41:20 +030016060 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016061 pixclk = crtc_state->base.adjusted_mode.crtc_clock;
16062 else
16063 WARN_ON(dev_priv->display.modeset_calc_cdclk);
Ville Syrjälä9558d152016-05-13 23:41:20 +030016064
16065 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
16066 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
16067 pixclk = DIV_ROUND_UP(pixclk * 100, 95);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016068 }
16069
16070 dev_priv->min_pixclk[crtc->pipe] = pixclk;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030016071
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030016072 readout_plane_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020016073
Ville Syrjälä78108b72016-05-27 20:59:19 +030016074 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
16075 crtc->base.base.id, crtc->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020016076 crtc->active ? "enabled" : "disabled");
16077 }
16078
Daniel Vetter53589012013-06-05 13:34:16 +020016079 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
16080 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
16081
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020016082 pll->on = pll->funcs.get_hw_state(dev_priv, pll,
16083 &pll->config.hw_state);
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020016084 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010016085 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010016086 if (crtc->active && crtc->config->shared_dpll == pll)
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020016087 pll->config.crtc_mask |= 1 << crtc->pipe;
Daniel Vetter53589012013-06-05 13:34:16 +020016088 }
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010016089 pll->active_mask = pll->config.crtc_mask;
Daniel Vetter53589012013-06-05 13:34:16 +020016090
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020016091 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020016092 pll->name, pll->config.crtc_mask, pll->on);
Daniel Vetter53589012013-06-05 13:34:16 +020016093 }
16094
Damien Lespiaub2784e12014-08-05 11:29:37 +010016095 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020016096 pipe = 0;
16097
16098 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070016099 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
16100 encoder->base.crtc = &crtc->base;
Ville Syrjälä253c84c2016-06-22 21:57:01 +030016101 crtc->config->output_types |= 1 << encoder->type;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020016102 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020016103 } else {
16104 encoder->base.crtc = NULL;
16105 }
16106
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010016107 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020016108 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030016109 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020016110 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010016111 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020016112 }
16113
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020016114 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020016115 if (connector->get_hw_state(connector)) {
16116 connector->base.dpms = DRM_MODE_DPMS_ON;
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010016117
16118 encoder = connector->encoder;
16119 connector->base.encoder = &encoder->base;
16120
16121 if (encoder->base.crtc &&
16122 encoder->base.crtc->state->active) {
16123 /*
16124 * This has to be done during hardware readout
16125 * because anything calling .crtc_disable may
16126 * rely on the connector_mask being accurate.
16127 */
16128 encoder->base.crtc->state->connector_mask |=
16129 1 << drm_connector_index(&connector->base);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +010016130 encoder->base.crtc->state->encoder_mask |=
16131 1 << drm_encoder_index(&encoder->base);
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010016132 }
16133
Daniel Vetter24929352012-07-02 20:28:59 +020016134 } else {
16135 connector->base.dpms = DRM_MODE_DPMS_OFF;
16136 connector->base.encoder = NULL;
16137 }
16138 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
16139 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030016140 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020016141 connector->base.encoder ? "enabled" : "disabled");
16142 }
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030016143
16144 for_each_intel_crtc(dev, crtc) {
16145 crtc->base.hwmode = crtc->config->base.adjusted_mode;
16146
16147 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
16148 if (crtc->base.state->active) {
16149 intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
16150 intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
16151 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
16152
16153 /*
16154 * The initial mode needs to be set in order to keep
16155 * the atomic core happy. It wants a valid mode if the
16156 * crtc's enabled, so we do the above call.
16157 *
16158 * At this point some state updated by the connectors
16159 * in their ->detect() callback has not run yet, so
16160 * no recalculation can be done yet.
16161 *
16162 * Even if we could do a recalculation and modeset
16163 * right now it would cause a double modeset if
16164 * fbdev or userspace chooses a different initial mode.
16165 *
16166 * If that happens, someone indicated they wanted a
16167 * mode change, which means it's safe to do a full
16168 * recalculation.
16169 */
16170 crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
Ville Syrjälä9eca68322015-09-10 18:59:10 +030016171
16172 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
16173 update_scanline_offset(crtc);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030016174 }
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020016175
16176 intel_pipe_config_sanity_check(dev_priv, crtc->config);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030016177 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020016178}
16179
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016180/* Scan out the current hw modeset state,
16181 * and sanitizes it to the current state
16182 */
16183static void
16184intel_modeset_setup_hw_state(struct drm_device *dev)
Daniel Vetter30e984d2013-06-05 13:34:17 +020016185{
Chris Wilsonfac5e232016-07-04 11:34:36 +010016186 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter30e984d2013-06-05 13:34:17 +020016187 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020016188 struct intel_crtc *crtc;
16189 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020016190 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020016191
16192 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020016193
16194 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010016195 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020016196 intel_sanitize_encoder(encoder);
16197 }
16198
Damien Lespiau055e3932014-08-18 13:49:10 +010016199 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020016200 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
16201 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020016202 intel_dump_pipe_config(crtc, crtc->config,
16203 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020016204 }
Daniel Vetter9a935852012-07-05 22:34:27 +020016205
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020016206 intel_modeset_update_connector_atomic_state(dev);
16207
Daniel Vetter35c95372013-07-17 06:55:04 +020016208 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
16209 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
16210
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010016211 if (!pll->on || pll->active_mask)
Daniel Vetter35c95372013-07-17 06:55:04 +020016212 continue;
16213
16214 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
16215
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020016216 pll->funcs.disable(dev_priv, pll);
Daniel Vetter35c95372013-07-17 06:55:04 +020016217 pll->on = false;
16218 }
16219
Wayne Boyer666a4532015-12-09 12:29:35 -080016220 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Ville Syrjälä6eb1a682015-06-24 22:00:03 +030016221 vlv_wm_get_hw_state(dev);
16222 else if (IS_GEN9(dev))
Pradeep Bhat30789992014-11-04 17:06:45 +000016223 skl_wm_get_hw_state(dev);
16224 else if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030016225 ilk_wm_get_hw_state(dev);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020016226
16227 for_each_intel_crtc(dev, crtc) {
16228 unsigned long put_domains;
16229
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +010016230 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020016231 if (WARN_ON(put_domains))
16232 modeset_put_power_domains(dev_priv, put_domains);
16233 }
16234 intel_display_set_init_power(dev_priv, false);
Paulo Zanoni010cf732016-01-19 11:35:48 -020016235
16236 intel_fbc_init_pipe_state(dev_priv);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016237}
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030016238
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016239void intel_display_resume(struct drm_device *dev)
16240{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016241 struct drm_i915_private *dev_priv = to_i915(dev);
16242 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
16243 struct drm_modeset_acquire_ctx ctx;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016244 int ret;
Daniel Vetterf30da182013-04-11 20:22:50 +020016245
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016246 dev_priv->modeset_restore_state = NULL;
Maarten Lankhorstdfa29972016-08-05 23:28:27 +030016247 if (state)
16248 state->acquire_ctx = &ctx;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016249
Maarten Lankhorstea49c9a2016-02-16 15:27:42 +010016250 /*
16251 * This is a cludge because with real atomic modeset mode_config.mutex
16252 * won't be taken. Unfortunately some probed state like
16253 * audio_codec_enable is still protected by mode_config.mutex, so lock
16254 * it here for now.
16255 */
16256 mutex_lock(&dev->mode_config.mutex);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016257 drm_modeset_acquire_init(&ctx, 0);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016258
Maarten Lankhorstdfa29972016-08-05 23:28:27 +030016259 while (1) {
16260 ret = drm_modeset_lock_all_ctx(dev, &ctx);
16261 if (ret != -EDEADLK)
16262 break;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016263
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016264 drm_modeset_backoff(&ctx);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016265 }
16266
Maarten Lankhorstdfa29972016-08-05 23:28:27 +030016267 if (!ret)
16268 ret = __intel_display_resume(dev, state);
16269
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016270 drm_modeset_drop_locks(&ctx);
16271 drm_modeset_acquire_fini(&ctx);
Maarten Lankhorstea49c9a2016-02-16 15:27:42 +010016272 mutex_unlock(&dev->mode_config.mutex);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016273
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016274 if (ret) {
16275 DRM_ERROR("Restoring old state failed with %i\n", ret);
16276 drm_atomic_state_free(state);
16277 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010016278}
16279
16280void intel_modeset_gem_init(struct drm_device *dev)
16281{
Chris Wilsondc979972016-05-10 14:10:04 +010016282 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080016283 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070016284 struct drm_i915_gem_object *obj;
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010016285 int ret;
Jesse Barnes484b41d2014-03-07 08:57:55 -080016286
Chris Wilsondc979972016-05-10 14:10:04 +010016287 intel_init_gt_powersave(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +030016288
Chris Wilson1833b132012-05-09 11:56:28 +010016289 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020016290
Chris Wilson1ee8da62016-05-12 12:43:23 +010016291 intel_setup_overlay(dev_priv);
Jesse Barnes484b41d2014-03-07 08:57:55 -080016292
16293 /*
16294 * Make sure any fbs we allocated at startup are properly
16295 * pinned & fenced. When we do the allocation it's too early
16296 * for this.
16297 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010016298 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070016299 obj = intel_fb_obj(c->primary->fb);
16300 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080016301 continue;
16302
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010016303 mutex_lock(&dev->struct_mutex);
Ville Syrjälä3465c582016-02-15 22:54:43 +020016304 ret = intel_pin_and_fence_fb_obj(c->primary->fb,
16305 c->primary->state->rotation);
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010016306 mutex_unlock(&dev->struct_mutex);
16307 if (ret) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080016308 DRM_ERROR("failed to pin boot fb on pipe %d\n",
16309 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100016310 drm_framebuffer_unreference(c->primary->fb);
Daniel Vetter5a21b662016-05-24 17:13:53 +020016311 c->primary->fb = NULL;
Maarten Lankhorst36750f22015-06-01 12:49:54 +020016312 c->primary->crtc = c->primary->state->crtc = NULL;
Daniel Vetter5a21b662016-05-24 17:13:53 +020016313 update_state_fb(c->primary);
Maarten Lankhorst36750f22015-06-01 12:49:54 +020016314 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
Jesse Barnes484b41d2014-03-07 08:57:55 -080016315 }
16316 }
Chris Wilson1ebaa0b2016-06-24 14:00:15 +010016317}
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020016318
Chris Wilson1ebaa0b2016-06-24 14:00:15 +010016319int intel_connector_register(struct drm_connector *connector)
16320{
16321 struct intel_connector *intel_connector = to_intel_connector(connector);
16322 int ret;
16323
16324 ret = intel_backlight_device_register(intel_connector);
16325 if (ret)
16326 goto err;
16327
16328 return 0;
16329
16330err:
16331 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080016332}
16333
Chris Wilsonc191eca2016-06-17 11:40:33 +010016334void intel_connector_unregister(struct drm_connector *connector)
Imre Deak4932e2c2014-02-11 17:12:48 +020016335{
Chris Wilsone63d87c2016-06-17 11:40:34 +010016336 struct intel_connector *intel_connector = to_intel_connector(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020016337
Chris Wilsone63d87c2016-06-17 11:40:34 +010016338 intel_backlight_device_unregister(intel_connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020016339 intel_panel_destroy_backlight(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020016340}
16341
Jesse Barnes79e53942008-11-07 14:24:08 -080016342void intel_modeset_cleanup(struct drm_device *dev)
16343{
Chris Wilsonfac5e232016-07-04 11:34:36 +010016344 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes652c3932009-08-17 13:31:43 -070016345
Chris Wilsondc979972016-05-10 14:10:04 +010016346 intel_disable_gt_powersave(dev_priv);
Imre Deak2eb52522014-11-19 15:30:05 +020016347
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016348 /*
16349 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020016350 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016351 * experience fancy races otherwise.
16352 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020016353 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070016354
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016355 /*
16356 * Due to the hpd irq storm handling the hotplug work can re-arm the
16357 * poll handlers. Hence disable polling after hpd handling is shut down.
16358 */
Keith Packardf87ea762010-10-03 19:36:26 -070016359 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016360
Jesse Barnes723bfd72010-10-07 16:01:13 -070016361 intel_unregister_dsm_handler();
16362
Paulo Zanonic937ab3e52016-01-19 11:35:46 -020016363 intel_fbc_global_disable(dev_priv);
Kristian Høgsberg69341a52009-11-11 12:19:17 -050016364
Chris Wilson1630fe72011-07-08 12:22:42 +010016365 /* flush any delayed tasks or pending work */
16366 flush_scheduled_work();
16367
Jesse Barnes79e53942008-11-07 14:24:08 -080016368 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010016369
Chris Wilson1ee8da62016-05-12 12:43:23 +010016370 intel_cleanup_overlay(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +030016371
Chris Wilsondc979972016-05-10 14:10:04 +010016372 intel_cleanup_gt_powersave(dev_priv);
Daniel Vetterf5949142016-01-13 11:55:28 +010016373
16374 intel_teardown_gmbus(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080016375}
16376
Chris Wilsondf0e9242010-09-09 16:20:55 +010016377void intel_connector_attach_encoder(struct intel_connector *connector,
16378 struct intel_encoder *encoder)
16379{
16380 connector->encoder = encoder;
16381 drm_mode_connector_attach_encoder(&connector->base,
16382 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080016383}
Dave Airlie28d52042009-09-21 14:33:58 +100016384
16385/*
16386 * set vga decode state - true == enable VGA decode
16387 */
16388int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
16389{
Chris Wilsonfac5e232016-07-04 11:34:36 +010016390 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsona885b3c2013-12-17 14:34:50 +000016391 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100016392 u16 gmch_ctrl;
16393
Chris Wilson75fa0412014-02-07 18:37:02 -020016394 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
16395 DRM_ERROR("failed to read control word\n");
16396 return -EIO;
16397 }
16398
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020016399 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
16400 return 0;
16401
Dave Airlie28d52042009-09-21 14:33:58 +100016402 if (state)
16403 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
16404 else
16405 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020016406
16407 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
16408 DRM_ERROR("failed to write control word\n");
16409 return -EIO;
16410 }
16411
Dave Airlie28d52042009-09-21 14:33:58 +100016412 return 0;
16413}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016414
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016415struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016416
16417 u32 power_well_driver;
16418
Chris Wilson63b66e52013-08-08 15:12:06 +020016419 int num_transcoders;
16420
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016421 struct intel_cursor_error_state {
16422 u32 control;
16423 u32 position;
16424 u32 base;
16425 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010016426 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016427
16428 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020016429 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016430 u32 source;
Imre Deakf301b1e2014-04-18 15:55:04 +030016431 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010016432 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016433
16434 struct intel_plane_error_state {
16435 u32 control;
16436 u32 stride;
16437 u32 size;
16438 u32 pos;
16439 u32 addr;
16440 u32 surface;
16441 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010016442 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020016443
16444 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020016445 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020016446 enum transcoder cpu_transcoder;
16447
16448 u32 conf;
16449
16450 u32 htotal;
16451 u32 hblank;
16452 u32 hsync;
16453 u32 vtotal;
16454 u32 vblank;
16455 u32 vsync;
16456 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016457};
16458
16459struct intel_display_error_state *
Chris Wilsonc0336662016-05-06 15:40:21 +010016460intel_display_capture_error_state(struct drm_i915_private *dev_priv)
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016461{
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016462 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020016463 int transcoders[] = {
16464 TRANSCODER_A,
16465 TRANSCODER_B,
16466 TRANSCODER_C,
16467 TRANSCODER_EDP,
16468 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016469 int i;
16470
Chris Wilsonc0336662016-05-06 15:40:21 +010016471 if (INTEL_INFO(dev_priv)->num_pipes == 0)
Chris Wilson63b66e52013-08-08 15:12:06 +020016472 return NULL;
16473
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016474 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016475 if (error == NULL)
16476 return NULL;
16477
Chris Wilsonc0336662016-05-06 15:40:21 +010016478 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016479 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
16480
Damien Lespiau055e3932014-08-18 13:49:10 +010016481 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020016482 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020016483 __intel_display_power_is_enabled(dev_priv,
16484 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020016485 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016486 continue;
16487
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030016488 error->cursor[i].control = I915_READ(CURCNTR(i));
16489 error->cursor[i].position = I915_READ(CURPOS(i));
16490 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016491
16492 error->plane[i].control = I915_READ(DSPCNTR(i));
16493 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Chris Wilsonc0336662016-05-06 15:40:21 +010016494 if (INTEL_GEN(dev_priv) <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030016495 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016496 error->plane[i].pos = I915_READ(DSPPOS(i));
16497 }
Chris Wilsonc0336662016-05-06 15:40:21 +010016498 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
Paulo Zanonica291362013-03-06 20:03:14 -030016499 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc0336662016-05-06 15:40:21 +010016500 if (INTEL_GEN(dev_priv) >= 4) {
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016501 error->plane[i].surface = I915_READ(DSPSURF(i));
16502 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
16503 }
16504
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016505 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e2014-04-18 15:55:04 +030016506
Chris Wilsonc0336662016-05-06 15:40:21 +010016507 if (HAS_GMCH_DISPLAY(dev_priv))
Imre Deakf301b1e2014-04-18 15:55:04 +030016508 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020016509 }
16510
Jani Nikula4d1de972016-03-18 17:05:42 +020016511 /* Note: this does not include DSI transcoders. */
Chris Wilsonc0336662016-05-06 15:40:21 +010016512 error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +030016513 if (HAS_DDI(dev_priv))
Chris Wilson63b66e52013-08-08 15:12:06 +020016514 error->num_transcoders++; /* Account for eDP. */
16515
16516 for (i = 0; i < error->num_transcoders; i++) {
16517 enum transcoder cpu_transcoder = transcoders[i];
16518
Imre Deakddf9c532013-11-27 22:02:02 +020016519 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020016520 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020016521 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020016522 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016523 continue;
16524
Chris Wilson63b66e52013-08-08 15:12:06 +020016525 error->transcoder[i].cpu_transcoder = cpu_transcoder;
16526
16527 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
16528 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
16529 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
16530 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
16531 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
16532 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
16533 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016534 }
16535
16536 return error;
16537}
16538
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016539#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
16540
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016541void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016542intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016543 struct drm_device *dev,
16544 struct intel_display_error_state *error)
16545{
Chris Wilsonfac5e232016-07-04 11:34:36 +010016546 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016547 int i;
16548
Chris Wilson63b66e52013-08-08 15:12:06 +020016549 if (!error)
16550 return;
16551
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016552 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020016553 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016554 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016555 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010016556 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016557 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020016558 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020016559 onoff(error->pipe[i].power_domain_on));
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016560 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e2014-04-18 15:55:04 +030016561 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016562
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016563 err_printf(m, "Plane [%d]:\n", i);
16564 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
16565 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016566 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016567 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
16568 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016569 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030016570 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016571 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016572 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016573 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
16574 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016575 }
16576
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016577 err_printf(m, "Cursor [%d]:\n", i);
16578 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
16579 err_printf(m, " POS: %08x\n", error->cursor[i].position);
16580 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016581 }
Chris Wilson63b66e52013-08-08 15:12:06 +020016582
16583 for (i = 0; i < error->num_transcoders; i++) {
Jani Nikulada205632016-03-15 21:51:10 +020016584 err_printf(m, "CPU transcoder: %s\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020016585 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020016586 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020016587 onoff(error->transcoder[i].power_domain_on));
Chris Wilson63b66e52013-08-08 15:12:06 +020016588 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
16589 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
16590 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
16591 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
16592 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
16593 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
16594 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
16595 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016596}