blob: 5fa37aaf7e513d815d41ae29d9ce982da4b162e3 [file] [log] [blame]
Jesse Barnes79e53942008-11-07 14:24:08 -08001/*
2 * Copyright © 2006-2007 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 */
26
Daniel Vetter618563e2012-04-01 13:38:50 +020027#include <linux/dmi.h>
Jesse Barnesc1c7af62009-09-10 15:28:03 -070028#include <linux/module.h>
29#include <linux/input.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080030#include <linux/i2c.h>
Shaohua Li7662c8b2009-06-26 11:23:55 +080031#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Jesse Barnes9cce37f2010-08-13 15:11:26 -070033#include <linux/vgaarb.h>
Wu Fengguange0dac652011-09-05 14:25:34 +080034#include <drm/drm_edid.h>
David Howells760285e2012-10-02 18:01:07 +010035#include <drm/drmP.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080036#include "intel_drv.h"
David Howells760285e2012-10-02 18:01:07 +010037#include <drm/i915_drm.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080038#include "i915_drv.h"
Chris Wilsonc37efb92016-06-17 08:28:47 +010039#include "i915_gem_dmabuf.h"
Imre Deakdb18b6a2016-03-24 12:41:40 +020040#include "intel_dsi.h"
Jesse Barnese5510fa2010-07-01 16:48:37 -070041#include "i915_trace.h"
Xi Ruoyao319c1d42015-03-12 20:16:32 +080042#include <drm/drm_atomic.h>
Matt Roperc196e1d2015-01-21 16:35:48 -080043#include <drm/drm_atomic_helper.h>
David Howells760285e2012-10-02 18:01:07 +010044#include <drm/drm_dp_helper.h>
45#include <drm/drm_crtc_helper.h>
Matt Roper465c1202014-05-29 08:06:54 -070046#include <drm/drm_plane_helper.h>
47#include <drm/drm_rect.h>
Keith Packardc0f372b32011-11-16 22:24:52 -080048#include <linux/dma_remapping.h>
Alex Goinsfd8e0582015-11-25 18:43:38 -080049#include <linux/reservation.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080050
Daniel Vetter5a21b662016-05-24 17:13:53 +020051static bool is_mmio_work(struct intel_flip_work *work)
52{
53 return work->mmio_work.func;
54}
55
Matt Roper465c1202014-05-29 08:06:54 -070056/* Primary plane formats for gen <= 3 */
Damien Lespiau568db4f2015-05-12 16:13:18 +010057static const uint32_t i8xx_primary_formats[] = {
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010058 DRM_FORMAT_C8,
59 DRM_FORMAT_RGB565,
Matt Roper465c1202014-05-29 08:06:54 -070060 DRM_FORMAT_XRGB1555,
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010061 DRM_FORMAT_XRGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070062};
63
64/* Primary plane formats for gen >= 4 */
Damien Lespiau568db4f2015-05-12 16:13:18 +010065static const uint32_t i965_primary_formats[] = {
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010066 DRM_FORMAT_C8,
67 DRM_FORMAT_RGB565,
68 DRM_FORMAT_XRGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070069 DRM_FORMAT_XBGR8888,
Damien Lespiau6c0fd452015-05-19 12:29:16 +010070 DRM_FORMAT_XRGB2101010,
71 DRM_FORMAT_XBGR2101010,
72};
73
74static const uint32_t skl_primary_formats[] = {
75 DRM_FORMAT_C8,
76 DRM_FORMAT_RGB565,
77 DRM_FORMAT_XRGB8888,
78 DRM_FORMAT_XBGR8888,
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010079 DRM_FORMAT_ARGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070080 DRM_FORMAT_ABGR8888,
81 DRM_FORMAT_XRGB2101010,
Matt Roper465c1202014-05-29 08:06:54 -070082 DRM_FORMAT_XBGR2101010,
Kumar, Maheshea916ea2015-09-03 16:17:09 +053083 DRM_FORMAT_YUYV,
84 DRM_FORMAT_YVYU,
85 DRM_FORMAT_UYVY,
86 DRM_FORMAT_VYUY,
Matt Roper465c1202014-05-29 08:06:54 -070087};
88
Matt Roper3d7d6512014-06-10 08:28:13 -070089/* Cursor formats */
90static const uint32_t intel_cursor_formats[] = {
91 DRM_FORMAT_ARGB8888,
92};
93
Jesse Barnesf1f644d2013-06-27 00:39:25 +030094static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020095 struct intel_crtc_state *pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +030096static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020097 struct intel_crtc_state *pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030098
Jesse Barneseb1bfe82014-02-12 12:26:25 -080099static int intel_framebuffer_init(struct drm_device *dev,
100 struct intel_framebuffer *ifb,
101 struct drm_mode_fb_cmd2 *mode_cmd,
102 struct drm_i915_gem_object *obj);
Daniel Vetter5b18e572014-04-24 23:55:06 +0200103static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
104static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +0200105static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +0200106static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -0700107 struct intel_link_m_n *m_n,
108 struct intel_link_m_n *m2_n2);
Daniel Vetter29407aa2014-04-24 23:55:08 +0200109static void ironlake_set_pipeconf(struct drm_crtc *crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +0200110static void haswell_set_pipeconf(struct drm_crtc *crtc);
Jani Nikula391bf042016-03-18 17:05:40 +0200111static void haswell_set_pipemisc(struct drm_crtc *crtc);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200112static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200113 const struct intel_crtc_state *pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200114static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200115 const struct intel_crtc_state *pipe_config);
Daniel Vetter5a21b662016-05-24 17:13:53 +0200116static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
117static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
Chandra Konduru549e2bf2015-04-07 15:28:38 -0700118static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
119 struct intel_crtc_state *crtc_state);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +0200120static void skylake_pfit_enable(struct intel_crtc *crtc);
121static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
122static void ironlake_pfit_enable(struct intel_crtc *crtc);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +0200123static void intel_modeset_setup_hw_state(struct drm_device *dev);
Ville Syrjälä2622a082016-03-09 19:07:26 +0200124static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
Ville Syrjälä4e5ca602016-05-11 22:44:44 +0300125static int ilk_max_pixel_rate(struct drm_atomic_state *state);
Imre Deak324513c2016-06-13 16:44:36 +0300126static int bxt_calc_cdclk(int max_pixclk);
Damien Lespiaue7457a92013-08-08 22:28:59 +0100127
Ma Lingd4906092009-03-18 20:13:27 +0800128struct intel_limit {
Ander Conselvan de Oliveira4c5def92016-05-04 12:11:58 +0300129 struct {
130 int min, max;
131 } dot, vco, n, m, m1, m2, p, p1;
132
133 struct {
134 int dot_limit;
135 int p2_slow, p2_fast;
136 } p2;
Ma Lingd4906092009-03-18 20:13:27 +0800137};
Jesse Barnes79e53942008-11-07 14:24:08 -0800138
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300139/* returns HPLL frequency in kHz */
140static int valleyview_get_vco(struct drm_i915_private *dev_priv)
141{
142 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
143
144 /* Obtain SKU information */
145 mutex_lock(&dev_priv->sb_lock);
146 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
147 CCK_FUSE_HPLL_FREQ_MASK;
148 mutex_unlock(&dev_priv->sb_lock);
149
150 return vco_freq[hpll_freq] * 1000;
151}
152
Ville Syrjäläc30fec62016-03-04 21:43:02 +0200153int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
154 const char *name, u32 reg, int ref_freq)
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300155{
156 u32 val;
157 int divider;
158
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300159 mutex_lock(&dev_priv->sb_lock);
160 val = vlv_cck_read(dev_priv, reg);
161 mutex_unlock(&dev_priv->sb_lock);
162
163 divider = val & CCK_FREQUENCY_VALUES;
164
165 WARN((val & CCK_FREQUENCY_STATUS) !=
166 (divider << CCK_FREQUENCY_STATUS_SHIFT),
167 "%s change in progress\n", name);
168
Ville Syrjäläc30fec62016-03-04 21:43:02 +0200169 return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
170}
171
172static int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
173 const char *name, u32 reg)
174{
175 if (dev_priv->hpll_freq == 0)
176 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
177
178 return vlv_get_cck_clock(dev_priv, name, reg,
179 dev_priv->hpll_freq);
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300180}
181
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200182static int
183intel_pch_rawclk(struct drm_i915_private *dev_priv)
Daniel Vetterd2acd212012-10-20 20:57:43 +0200184{
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200185 return (I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK) * 1000;
Daniel Vetterd2acd212012-10-20 20:57:43 +0200186}
187
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200188static int
189intel_vlv_hrawclk(struct drm_i915_private *dev_priv)
Jani Nikula79e50a42015-08-26 10:58:20 +0300190{
Ville Syrjälä19ab4ed2016-04-27 17:43:22 +0300191 /* RAWCLK_FREQ_VLV register updated from power well code */
Ville Syrjälä35d38d12016-03-02 17:22:16 +0200192 return vlv_get_cck_clock_hpll(dev_priv, "hrawclk",
193 CCK_DISPLAY_REF_CLOCK_CONTROL);
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200194}
195
196static int
197intel_g4x_hrawclk(struct drm_i915_private *dev_priv)
198{
Jani Nikula79e50a42015-08-26 10:58:20 +0300199 uint32_t clkcfg;
200
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200201 /* hrawclock is 1/4 the FSB frequency */
Jani Nikula79e50a42015-08-26 10:58:20 +0300202 clkcfg = I915_READ(CLKCFG);
203 switch (clkcfg & CLKCFG_FSB_MASK) {
204 case CLKCFG_FSB_400:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200205 return 100000;
Jani Nikula79e50a42015-08-26 10:58:20 +0300206 case CLKCFG_FSB_533:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200207 return 133333;
Jani Nikula79e50a42015-08-26 10:58:20 +0300208 case CLKCFG_FSB_667:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200209 return 166667;
Jani Nikula79e50a42015-08-26 10:58:20 +0300210 case CLKCFG_FSB_800:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200211 return 200000;
Jani Nikula79e50a42015-08-26 10:58:20 +0300212 case CLKCFG_FSB_1067:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200213 return 266667;
Jani Nikula79e50a42015-08-26 10:58:20 +0300214 case CLKCFG_FSB_1333:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200215 return 333333;
Jani Nikula79e50a42015-08-26 10:58:20 +0300216 /* these two are just a guess; one of them might be right */
217 case CLKCFG_FSB_1600:
218 case CLKCFG_FSB_1600_ALT:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200219 return 400000;
Jani Nikula79e50a42015-08-26 10:58:20 +0300220 default:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200221 return 133333;
Jani Nikula79e50a42015-08-26 10:58:20 +0300222 }
223}
224
Ville Syrjälä19ab4ed2016-04-27 17:43:22 +0300225void intel_update_rawclk(struct drm_i915_private *dev_priv)
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200226{
227 if (HAS_PCH_SPLIT(dev_priv))
228 dev_priv->rawclk_freq = intel_pch_rawclk(dev_priv);
229 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
230 dev_priv->rawclk_freq = intel_vlv_hrawclk(dev_priv);
231 else if (IS_G4X(dev_priv) || IS_PINEVIEW(dev_priv))
232 dev_priv->rawclk_freq = intel_g4x_hrawclk(dev_priv);
233 else
234 return; /* no rawclk on other platforms, or no need to know it */
235
236 DRM_DEBUG_DRIVER("rawclk rate: %d kHz\n", dev_priv->rawclk_freq);
237}
238
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300239static void intel_update_czclk(struct drm_i915_private *dev_priv)
240{
Wayne Boyer666a4532015-12-09 12:29:35 -0800241 if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300242 return;
243
244 dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
245 CCK_CZ_CLOCK_CONTROL);
246
247 DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
248}
249
Chris Wilson021357a2010-09-07 20:54:59 +0100250static inline u32 /* units of 100MHz */
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200251intel_fdi_link_freq(struct drm_i915_private *dev_priv,
252 const struct intel_crtc_state *pipe_config)
Chris Wilson021357a2010-09-07 20:54:59 +0100253{
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200254 if (HAS_DDI(dev_priv))
255 return pipe_config->port_clock; /* SPLL */
256 else if (IS_GEN5(dev_priv))
257 return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
Ville Syrjäläe3b247d2016-02-17 21:41:09 +0200258 else
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200259 return 270000;
Chris Wilson021357a2010-09-07 20:54:59 +0100260}
261
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300262static const struct intel_limit intel_limits_i8xx_dac = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400263 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200264 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200265 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400266 .m = { .min = 96, .max = 140 },
267 .m1 = { .min = 18, .max = 26 },
268 .m2 = { .min = 6, .max = 16 },
269 .p = { .min = 4, .max = 128 },
270 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700271 .p2 = { .dot_limit = 165000,
272 .p2_slow = 4, .p2_fast = 2 },
Keith Packarde4b36692009-06-05 19:22:17 -0700273};
274
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300275static const struct intel_limit intel_limits_i8xx_dvo = {
Daniel Vetter5d536e22013-07-06 12:52:06 +0200276 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200277 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200278 .n = { .min = 2, .max = 16 },
Daniel Vetter5d536e22013-07-06 12:52:06 +0200279 .m = { .min = 96, .max = 140 },
280 .m1 = { .min = 18, .max = 26 },
281 .m2 = { .min = 6, .max = 16 },
282 .p = { .min = 4, .max = 128 },
283 .p1 = { .min = 2, .max = 33 },
284 .p2 = { .dot_limit = 165000,
285 .p2_slow = 4, .p2_fast = 4 },
286};
287
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300288static const struct intel_limit intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400289 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200290 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200291 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400292 .m = { .min = 96, .max = 140 },
293 .m1 = { .min = 18, .max = 26 },
294 .m2 = { .min = 6, .max = 16 },
295 .p = { .min = 4, .max = 128 },
296 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700297 .p2 = { .dot_limit = 165000,
298 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700299};
Eric Anholt273e27c2011-03-30 13:01:10 -0700300
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300301static const struct intel_limit intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400302 .dot = { .min = 20000, .max = 400000 },
303 .vco = { .min = 1400000, .max = 2800000 },
304 .n = { .min = 1, .max = 6 },
305 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100306 .m1 = { .min = 8, .max = 18 },
307 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400308 .p = { .min = 5, .max = 80 },
309 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700310 .p2 = { .dot_limit = 200000,
311 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700312};
313
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300314static const struct intel_limit intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400315 .dot = { .min = 20000, .max = 400000 },
316 .vco = { .min = 1400000, .max = 2800000 },
317 .n = { .min = 1, .max = 6 },
318 .m = { .min = 70, .max = 120 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100319 .m1 = { .min = 8, .max = 18 },
320 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400321 .p = { .min = 7, .max = 98 },
322 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700323 .p2 = { .dot_limit = 112000,
324 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700325};
326
Eric Anholt273e27c2011-03-30 13:01:10 -0700327
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300328static const struct intel_limit intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700329 .dot = { .min = 25000, .max = 270000 },
330 .vco = { .min = 1750000, .max = 3500000},
331 .n = { .min = 1, .max = 4 },
332 .m = { .min = 104, .max = 138 },
333 .m1 = { .min = 17, .max = 23 },
334 .m2 = { .min = 5, .max = 11 },
335 .p = { .min = 10, .max = 30 },
336 .p1 = { .min = 1, .max = 3},
337 .p2 = { .dot_limit = 270000,
338 .p2_slow = 10,
339 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800340 },
Keith Packarde4b36692009-06-05 19:22:17 -0700341};
342
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300343static const struct intel_limit intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700344 .dot = { .min = 22000, .max = 400000 },
345 .vco = { .min = 1750000, .max = 3500000},
346 .n = { .min = 1, .max = 4 },
347 .m = { .min = 104, .max = 138 },
348 .m1 = { .min = 16, .max = 23 },
349 .m2 = { .min = 5, .max = 11 },
350 .p = { .min = 5, .max = 80 },
351 .p1 = { .min = 1, .max = 8},
352 .p2 = { .dot_limit = 165000,
353 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700354};
355
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300356static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700357 .dot = { .min = 20000, .max = 115000 },
358 .vco = { .min = 1750000, .max = 3500000 },
359 .n = { .min = 1, .max = 3 },
360 .m = { .min = 104, .max = 138 },
361 .m1 = { .min = 17, .max = 23 },
362 .m2 = { .min = 5, .max = 11 },
363 .p = { .min = 28, .max = 112 },
364 .p1 = { .min = 2, .max = 8 },
365 .p2 = { .dot_limit = 0,
366 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800367 },
Keith Packarde4b36692009-06-05 19:22:17 -0700368};
369
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300370static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700371 .dot = { .min = 80000, .max = 224000 },
372 .vco = { .min = 1750000, .max = 3500000 },
373 .n = { .min = 1, .max = 3 },
374 .m = { .min = 104, .max = 138 },
375 .m1 = { .min = 17, .max = 23 },
376 .m2 = { .min = 5, .max = 11 },
377 .p = { .min = 14, .max = 42 },
378 .p1 = { .min = 2, .max = 6 },
379 .p2 = { .dot_limit = 0,
380 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800381 },
Keith Packarde4b36692009-06-05 19:22:17 -0700382};
383
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300384static const struct intel_limit intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400385 .dot = { .min = 20000, .max = 400000},
386 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700387 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400388 .n = { .min = 3, .max = 6 },
389 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700390 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400391 .m1 = { .min = 0, .max = 0 },
392 .m2 = { .min = 0, .max = 254 },
393 .p = { .min = 5, .max = 80 },
394 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700395 .p2 = { .dot_limit = 200000,
396 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700397};
398
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300399static const struct intel_limit intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400400 .dot = { .min = 20000, .max = 400000 },
401 .vco = { .min = 1700000, .max = 3500000 },
402 .n = { .min = 3, .max = 6 },
403 .m = { .min = 2, .max = 256 },
404 .m1 = { .min = 0, .max = 0 },
405 .m2 = { .min = 0, .max = 254 },
406 .p = { .min = 7, .max = 112 },
407 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700408 .p2 = { .dot_limit = 112000,
409 .p2_slow = 14, .p2_fast = 14 },
Keith Packarde4b36692009-06-05 19:22:17 -0700410};
411
Eric Anholt273e27c2011-03-30 13:01:10 -0700412/* Ironlake / Sandybridge
413 *
414 * We calculate clock using (register_value + 2) for N/M1/M2, so here
415 * the range value for them is (actual_value - 2).
416 */
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300417static const struct intel_limit intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700418 .dot = { .min = 25000, .max = 350000 },
419 .vco = { .min = 1760000, .max = 3510000 },
420 .n = { .min = 1, .max = 5 },
421 .m = { .min = 79, .max = 127 },
422 .m1 = { .min = 12, .max = 22 },
423 .m2 = { .min = 5, .max = 9 },
424 .p = { .min = 5, .max = 80 },
425 .p1 = { .min = 1, .max = 8 },
426 .p2 = { .dot_limit = 225000,
427 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700428};
429
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300430static const struct intel_limit intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700431 .dot = { .min = 25000, .max = 350000 },
432 .vco = { .min = 1760000, .max = 3510000 },
433 .n = { .min = 1, .max = 3 },
434 .m = { .min = 79, .max = 118 },
435 .m1 = { .min = 12, .max = 22 },
436 .m2 = { .min = 5, .max = 9 },
437 .p = { .min = 28, .max = 112 },
438 .p1 = { .min = 2, .max = 8 },
439 .p2 = { .dot_limit = 225000,
440 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800441};
442
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300443static const struct intel_limit intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700444 .dot = { .min = 25000, .max = 350000 },
445 .vco = { .min = 1760000, .max = 3510000 },
446 .n = { .min = 1, .max = 3 },
447 .m = { .min = 79, .max = 127 },
448 .m1 = { .min = 12, .max = 22 },
449 .m2 = { .min = 5, .max = 9 },
450 .p = { .min = 14, .max = 56 },
451 .p1 = { .min = 2, .max = 8 },
452 .p2 = { .dot_limit = 225000,
453 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800454};
455
Eric Anholt273e27c2011-03-30 13:01:10 -0700456/* LVDS 100mhz refclk limits. */
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300457static const struct intel_limit intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700458 .dot = { .min = 25000, .max = 350000 },
459 .vco = { .min = 1760000, .max = 3510000 },
460 .n = { .min = 1, .max = 2 },
461 .m = { .min = 79, .max = 126 },
462 .m1 = { .min = 12, .max = 22 },
463 .m2 = { .min = 5, .max = 9 },
464 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400465 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700466 .p2 = { .dot_limit = 225000,
467 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800468};
469
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300470static const struct intel_limit intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700471 .dot = { .min = 25000, .max = 350000 },
472 .vco = { .min = 1760000, .max = 3510000 },
473 .n = { .min = 1, .max = 3 },
474 .m = { .min = 79, .max = 126 },
475 .m1 = { .min = 12, .max = 22 },
476 .m2 = { .min = 5, .max = 9 },
477 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400478 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700479 .p2 = { .dot_limit = 225000,
480 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800481};
482
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300483static const struct intel_limit intel_limits_vlv = {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300484 /*
485 * These are the data rate limits (measured in fast clocks)
486 * since those are the strictest limits we have. The fast
487 * clock and actual rate limits are more relaxed, so checking
488 * them would make no difference.
489 */
490 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200491 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700492 .n = { .min = 1, .max = 7 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700493 .m1 = { .min = 2, .max = 3 },
494 .m2 = { .min = 11, .max = 156 },
Ville Syrjäläb99ab662013-09-24 21:26:26 +0300495 .p1 = { .min = 2, .max = 3 },
Ville Syrjälä5fdc9c492013-09-24 21:26:29 +0300496 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700497};
498
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300499static const struct intel_limit intel_limits_chv = {
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300500 /*
501 * These are the data rate limits (measured in fast clocks)
502 * since those are the strictest limits we have. The fast
503 * clock and actual rate limits are more relaxed, so checking
504 * them would make no difference.
505 */
506 .dot = { .min = 25000 * 5, .max = 540000 * 5},
Ville Syrjälä17fe1022015-02-26 21:01:52 +0200507 .vco = { .min = 4800000, .max = 6480000 },
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300508 .n = { .min = 1, .max = 1 },
509 .m1 = { .min = 2, .max = 2 },
510 .m2 = { .min = 24 << 22, .max = 175 << 22 },
511 .p1 = { .min = 2, .max = 4 },
512 .p2 = { .p2_slow = 1, .p2_fast = 14 },
513};
514
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300515static const struct intel_limit intel_limits_bxt = {
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200516 /* FIXME: find real dot limits */
517 .dot = { .min = 0, .max = INT_MAX },
Vandana Kannane6292552015-07-01 17:02:57 +0530518 .vco = { .min = 4800000, .max = 6700000 },
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200519 .n = { .min = 1, .max = 1 },
520 .m1 = { .min = 2, .max = 2 },
521 /* FIXME: find real m2 limits */
522 .m2 = { .min = 2 << 22, .max = 255 << 22 },
523 .p1 = { .min = 2, .max = 4 },
524 .p2 = { .p2_slow = 1, .p2_fast = 20 },
525};
526
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200527static bool
528needs_modeset(struct drm_crtc_state *state)
529{
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200530 return drm_atomic_crtc_needs_modeset(state);
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200531}
532
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300533/**
534 * Returns whether any output on the specified pipe is of the specified type
535 */
Damien Lespiau40935612014-10-29 11:16:59 +0000536bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300537{
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300538 struct drm_device *dev = crtc->base.dev;
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300539 struct intel_encoder *encoder;
540
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300541 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300542 if (encoder->type == type)
543 return true;
544
545 return false;
546}
547
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200548/**
549 * Returns whether any output on the specified pipe will have the specified
550 * type after a staged modeset is complete, i.e., the same as
551 * intel_pipe_has_type() but looking at encoder->new_crtc instead of
552 * encoder->crtc.
553 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200554static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
555 int type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200556{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200557 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +0300558 struct drm_connector *connector;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200559 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200560 struct intel_encoder *encoder;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200561 int i, num_connectors = 0;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200562
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +0300563 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200564 if (connector_state->crtc != crtc_state->base.crtc)
565 continue;
566
567 num_connectors++;
568
569 encoder = to_intel_encoder(connector_state->best_encoder);
570 if (encoder->type == type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200571 return true;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200572 }
573
574 WARN_ON(num_connectors == 0);
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200575
576 return false;
577}
578
Imre Deakdccbea32015-06-22 23:35:51 +0300579/*
580 * Platform specific helpers to calculate the port PLL loopback- (clock.m),
581 * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
582 * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
583 * The helpers' return value is the rate of the clock that is fed to the
584 * display engine's pipe which can be the above fast dot clock rate or a
585 * divided-down version of it.
586 */
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500587/* m1 is reserved as 0 in Pineview, n is a ring counter */
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300588static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800589{
Shaohua Li21778322009-02-23 15:19:16 +0800590 clock->m = clock->m2 + 2;
591 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200592 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300593 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300594 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
595 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300596
597 return clock->dot;
Shaohua Li21778322009-02-23 15:19:16 +0800598}
599
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200600static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
601{
602 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
603}
604
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300605static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800606{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200607 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800608 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200609 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300610 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300611 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
612 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300613
614 return clock->dot;
Jesse Barnes79e53942008-11-07 14:24:08 -0800615}
616
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300617static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
Imre Deak589eca62015-06-22 23:35:50 +0300618{
619 clock->m = clock->m1 * clock->m2;
620 clock->p = clock->p1 * clock->p2;
621 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300622 return 0;
Imre Deak589eca62015-06-22 23:35:50 +0300623 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
624 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300625
626 return clock->dot / 5;
Imre Deak589eca62015-06-22 23:35:50 +0300627}
628
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300629int chv_calc_dpll_params(int refclk, struct dpll *clock)
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300630{
631 clock->m = clock->m1 * clock->m2;
632 clock->p = clock->p1 * clock->p2;
633 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300634 return 0;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300635 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
636 clock->n << 22);
637 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300638
639 return clock->dot / 5;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300640}
641
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800642#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800643/**
644 * Returns whether the given set of divisors are valid for a given refclk with
645 * the given connectors.
646 */
647
Chris Wilson1b894b52010-12-14 20:04:54 +0000648static bool intel_PLL_is_valid(struct drm_device *dev,
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300649 const struct intel_limit *limit,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300650 const struct dpll *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800651{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300652 if (clock->n < limit->n.min || limit->n.max < clock->n)
653 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800654 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400655 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800656 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400657 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800658 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400659 INTELPllInvalid("m1 out of range\n");
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300660
Wayne Boyer666a4532015-12-09 12:29:35 -0800661 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) &&
662 !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev))
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300663 if (clock->m1 <= clock->m2)
664 INTELPllInvalid("m1 <= m2\n");
665
Wayne Boyer666a4532015-12-09 12:29:35 -0800666 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev)) {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300667 if (clock->p < limit->p.min || limit->p.max < clock->p)
668 INTELPllInvalid("p out of range\n");
669 if (clock->m < limit->m.min || limit->m.max < clock->m)
670 INTELPllInvalid("m out of range\n");
671 }
672
Jesse Barnes79e53942008-11-07 14:24:08 -0800673 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400674 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800675 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
676 * connector, etc., rather than just a single range.
677 */
678 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400679 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800680
681 return true;
682}
683
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300684static int
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300685i9xx_select_p2_div(const struct intel_limit *limit,
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300686 const struct intel_crtc_state *crtc_state,
687 int target)
Jesse Barnes79e53942008-11-07 14:24:08 -0800688{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300689 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800690
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200691 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800692 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100693 * For LVDS just rely on its current settings for dual-channel.
694 * We haven't figured out how to reliably set up different
695 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800696 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100697 if (intel_is_dual_link_lvds(dev))
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300698 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800699 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300700 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800701 } else {
702 if (target < limit->p2.dot_limit)
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300703 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800704 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300705 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800706 }
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300707}
708
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200709/*
710 * Returns a set of divisors for the desired target clock with the given
711 * refclk, or FALSE. The returned values represent the clock equation:
712 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
713 *
714 * Target and reference clocks are specified in kHz.
715 *
716 * If match_clock is provided, then best_clock P divider must match the P
717 * divider from @match_clock used for LVDS downclocking.
718 */
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300719static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300720i9xx_find_best_dpll(const struct intel_limit *limit,
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300721 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300722 int target, int refclk, struct dpll *match_clock,
723 struct dpll *best_clock)
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300724{
725 struct drm_device *dev = crtc_state->base.crtc->dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300726 struct dpll clock;
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300727 int err = target;
Jesse Barnes79e53942008-11-07 14:24:08 -0800728
Akshay Joshi0206e352011-08-16 15:34:10 -0400729 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800730
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300731 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
732
Zhao Yakui42158662009-11-20 11:24:18 +0800733 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
734 clock.m1++) {
735 for (clock.m2 = limit->m2.min;
736 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200737 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800738 break;
739 for (clock.n = limit->n.min;
740 clock.n <= limit->n.max; clock.n++) {
741 for (clock.p1 = limit->p1.min;
742 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800743 int this_err;
744
Imre Deakdccbea32015-06-22 23:35:51 +0300745 i9xx_calc_dpll_params(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000746 if (!intel_PLL_is_valid(dev, limit,
747 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800748 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800749 if (match_clock &&
750 clock.p != match_clock->p)
751 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800752
753 this_err = abs(clock.dot - target);
754 if (this_err < err) {
755 *best_clock = clock;
756 err = this_err;
757 }
758 }
759 }
760 }
761 }
762
763 return (err != target);
764}
765
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200766/*
767 * Returns a set of divisors for the desired target clock with the given
768 * refclk, or FALSE. The returned values represent the clock equation:
769 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
770 *
771 * Target and reference clocks are specified in kHz.
772 *
773 * If match_clock is provided, then best_clock P divider must match the P
774 * divider from @match_clock used for LVDS downclocking.
775 */
Ma Lingd4906092009-03-18 20:13:27 +0800776static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300777pnv_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200778 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300779 int target, int refclk, struct dpll *match_clock,
780 struct dpll *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200781{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300782 struct drm_device *dev = crtc_state->base.crtc->dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300783 struct dpll clock;
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200784 int err = target;
785
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200786 memset(best_clock, 0, sizeof(*best_clock));
787
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300788 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
789
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200790 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
791 clock.m1++) {
792 for (clock.m2 = limit->m2.min;
793 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200794 for (clock.n = limit->n.min;
795 clock.n <= limit->n.max; clock.n++) {
796 for (clock.p1 = limit->p1.min;
797 clock.p1 <= limit->p1.max; clock.p1++) {
798 int this_err;
799
Imre Deakdccbea32015-06-22 23:35:51 +0300800 pnv_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800801 if (!intel_PLL_is_valid(dev, limit,
802 &clock))
803 continue;
804 if (match_clock &&
805 clock.p != match_clock->p)
806 continue;
807
808 this_err = abs(clock.dot - target);
809 if (this_err < err) {
810 *best_clock = clock;
811 err = this_err;
812 }
813 }
814 }
815 }
816 }
817
818 return (err != target);
819}
820
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +0200821/*
822 * Returns a set of divisors for the desired target clock with the given
823 * refclk, or FALSE. The returned values represent the clock equation:
824 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200825 *
826 * Target and reference clocks are specified in kHz.
827 *
828 * If match_clock is provided, then best_clock P divider must match the P
829 * divider from @match_clock used for LVDS downclocking.
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +0200830 */
Ma Lingd4906092009-03-18 20:13:27 +0800831static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300832g4x_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200833 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300834 int target, int refclk, struct dpll *match_clock,
835 struct dpll *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800836{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300837 struct drm_device *dev = crtc_state->base.crtc->dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300838 struct dpll clock;
Ma Lingd4906092009-03-18 20:13:27 +0800839 int max_n;
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300840 bool found = false;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400841 /* approximately equals target * 0.00585 */
842 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800843
844 memset(best_clock, 0, sizeof(*best_clock));
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300845
846 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
847
Ma Lingd4906092009-03-18 20:13:27 +0800848 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200849 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800850 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200851 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800852 for (clock.m1 = limit->m1.max;
853 clock.m1 >= limit->m1.min; clock.m1--) {
854 for (clock.m2 = limit->m2.max;
855 clock.m2 >= limit->m2.min; clock.m2--) {
856 for (clock.p1 = limit->p1.max;
857 clock.p1 >= limit->p1.min; clock.p1--) {
858 int this_err;
859
Imre Deakdccbea32015-06-22 23:35:51 +0300860 i9xx_calc_dpll_params(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000861 if (!intel_PLL_is_valid(dev, limit,
862 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800863 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000864
865 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800866 if (this_err < err_most) {
867 *best_clock = clock;
868 err_most = this_err;
869 max_n = clock.n;
870 found = true;
871 }
872 }
873 }
874 }
875 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800876 return found;
877}
Ma Lingd4906092009-03-18 20:13:27 +0800878
Imre Deakd5dd62b2015-03-17 11:40:03 +0200879/*
880 * Check if the calculated PLL configuration is more optimal compared to the
881 * best configuration and error found so far. Return the calculated error.
882 */
883static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300884 const struct dpll *calculated_clock,
885 const struct dpll *best_clock,
Imre Deakd5dd62b2015-03-17 11:40:03 +0200886 unsigned int best_error_ppm,
887 unsigned int *error_ppm)
888{
Imre Deak9ca3ba02015-03-17 11:40:05 +0200889 /*
890 * For CHV ignore the error and consider only the P value.
891 * Prefer a bigger P value based on HW requirements.
892 */
893 if (IS_CHERRYVIEW(dev)) {
894 *error_ppm = 0;
895
896 return calculated_clock->p > best_clock->p;
897 }
898
Imre Deak24be4e42015-03-17 11:40:04 +0200899 if (WARN_ON_ONCE(!target_freq))
900 return false;
901
Imre Deakd5dd62b2015-03-17 11:40:03 +0200902 *error_ppm = div_u64(1000000ULL *
903 abs(target_freq - calculated_clock->dot),
904 target_freq);
905 /*
906 * Prefer a better P value over a better (smaller) error if the error
907 * is small. Ensure this preference for future configurations too by
908 * setting the error to 0.
909 */
910 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
911 *error_ppm = 0;
912
913 return true;
914 }
915
916 return *error_ppm + 10 < best_error_ppm;
917}
918
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200919/*
920 * Returns a set of divisors for the desired target clock with the given
921 * refclk, or FALSE. The returned values represent the clock equation:
922 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
923 */
Zhenyu Wang2c072452009-06-05 15:38:42 +0800924static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300925vlv_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200926 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300927 int target, int refclk, struct dpll *match_clock,
928 struct dpll *best_clock)
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700929{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200930 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300931 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300932 struct dpll clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300933 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300934 /* min update 19.2 MHz */
935 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300936 bool found = false;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700937
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300938 target *= 5; /* fast clock */
939
940 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700941
942 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300943 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300944 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300945 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300946 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300947 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700948 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300949 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Imre Deakd5dd62b2015-03-17 11:40:03 +0200950 unsigned int ppm;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300951
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300952 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
953 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300954
Imre Deakdccbea32015-06-22 23:35:51 +0300955 vlv_calc_dpll_params(refclk, &clock);
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300956
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300957 if (!intel_PLL_is_valid(dev, limit,
958 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300959 continue;
960
Imre Deakd5dd62b2015-03-17 11:40:03 +0200961 if (!vlv_PLL_is_optimal(dev, target,
962 &clock,
963 best_clock,
964 bestppm, &ppm))
965 continue;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300966
Imre Deakd5dd62b2015-03-17 11:40:03 +0200967 *best_clock = clock;
968 bestppm = ppm;
969 found = true;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700970 }
971 }
972 }
973 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700974
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300975 return found;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700976}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700977
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200978/*
979 * Returns a set of divisors for the desired target clock with the given
980 * refclk, or FALSE. The returned values represent the clock equation:
981 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
982 */
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300983static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300984chv_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200985 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300986 int target, int refclk, struct dpll *match_clock,
987 struct dpll *best_clock)
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300988{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200989 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300990 struct drm_device *dev = crtc->base.dev;
Imre Deak9ca3ba02015-03-17 11:40:05 +0200991 unsigned int best_error_ppm;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300992 struct dpll clock;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300993 uint64_t m2;
994 int found = false;
995
996 memset(best_clock, 0, sizeof(*best_clock));
Imre Deak9ca3ba02015-03-17 11:40:05 +0200997 best_error_ppm = 1000000;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300998
999 /*
1000 * Based on hardware doc, the n always set to 1, and m1 always
1001 * set to 2. If requires to support 200Mhz refclk, we need to
1002 * revisit this because n may not 1 anymore.
1003 */
1004 clock.n = 1, clock.m1 = 2;
1005 target *= 5; /* fast clock */
1006
1007 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
1008 for (clock.p2 = limit->p2.p2_fast;
1009 clock.p2 >= limit->p2.p2_slow;
1010 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Imre Deak9ca3ba02015-03-17 11:40:05 +02001011 unsigned int error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001012
1013 clock.p = clock.p1 * clock.p2;
1014
1015 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
1016 clock.n) << 22, refclk * clock.m1);
1017
1018 if (m2 > INT_MAX/clock.m1)
1019 continue;
1020
1021 clock.m2 = m2;
1022
Imre Deakdccbea32015-06-22 23:35:51 +03001023 chv_calc_dpll_params(refclk, &clock);
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001024
1025 if (!intel_PLL_is_valid(dev, limit, &clock))
1026 continue;
1027
Imre Deak9ca3ba02015-03-17 11:40:05 +02001028 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
1029 best_error_ppm, &error_ppm))
1030 continue;
1031
1032 *best_clock = clock;
1033 best_error_ppm = error_ppm;
1034 found = true;
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001035 }
1036 }
1037
1038 return found;
1039}
1040
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001041bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03001042 struct dpll *best_clock)
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001043{
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02001044 int refclk = 100000;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03001045 const struct intel_limit *limit = &intel_limits_bxt;
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001046
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02001047 return chv_find_best_dpll(limit, crtc_state,
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001048 target_clock, refclk, NULL, best_clock);
1049}
1050
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001051bool intel_crtc_active(struct drm_crtc *crtc)
1052{
1053 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1054
1055 /* Be paranoid as we can arrive here with only partial
1056 * state retrieved from the hardware during setup.
1057 *
Damien Lespiau241bfc32013-09-25 16:45:37 +01001058 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001059 * as Haswell has gained clock readout/fastboot support.
1060 *
Dave Airlie66e514c2014-04-03 07:51:54 +10001061 * We can ditch the crtc->primary->fb check as soon as we can
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001062 * properly reconstruct framebuffers.
Matt Roperc3d1f432015-03-09 10:19:23 -07001063 *
1064 * FIXME: The intel_crtc->active here should be switched to
1065 * crtc->state->active once we have proper CRTC states wired up
1066 * for atomic.
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001067 */
Matt Roperc3d1f432015-03-09 10:19:23 -07001068 return intel_crtc->active && crtc->primary->state->fb &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001069 intel_crtc->config->base.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001070}
1071
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001072enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1073 enum pipe pipe)
1074{
1075 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1076 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1077
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001078 return intel_crtc->config->cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001079}
1080
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001081static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1082{
1083 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001084 i915_reg_t reg = PIPEDSL(pipe);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001085 u32 line1, line2;
1086 u32 line_mask;
1087
1088 if (IS_GEN2(dev))
1089 line_mask = DSL_LINEMASK_GEN2;
1090 else
1091 line_mask = DSL_LINEMASK_GEN3;
1092
1093 line1 = I915_READ(reg) & line_mask;
Daniel Vetter6adfb1e2015-07-07 09:10:40 +02001094 msleep(5);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001095 line2 = I915_READ(reg) & line_mask;
1096
1097 return line1 == line2;
1098}
1099
Keith Packardab7ad7f2010-10-03 00:33:06 -07001100/*
1101 * intel_wait_for_pipe_off - wait for pipe to turn off
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001102 * @crtc: crtc whose pipe to wait for
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001103 *
1104 * After disabling a pipe, we can't wait for vblank in the usual way,
1105 * spinning on the vblank interrupt status bit, since we won't actually
1106 * see an interrupt when the pipe is disabled.
1107 *
Keith Packardab7ad7f2010-10-03 00:33:06 -07001108 * On Gen4 and above:
1109 * wait for the pipe register state bit to turn off
1110 *
1111 * Otherwise:
1112 * wait for the display line value to settle (it usually
1113 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +01001114 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001115 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001116static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001117{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001118 struct drm_device *dev = crtc->base.dev;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001119 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001120 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001121 enum pipe pipe = crtc->pipe;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001122
Keith Packardab7ad7f2010-10-03 00:33:06 -07001123 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001124 i915_reg_t reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001125
Keith Packardab7ad7f2010-10-03 00:33:06 -07001126 /* Wait for the Pipe State to go off */
Chris Wilsonb8511f52016-06-30 15:32:53 +01001127 if (intel_wait_for_register(dev_priv,
1128 reg, I965_PIPECONF_ACTIVE, 0,
1129 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001130 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001131 } else {
Keith Packardab7ad7f2010-10-03 00:33:06 -07001132 /* Wait for the display line to settle */
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001133 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001134 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001135 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001136}
1137
Jesse Barnesb24e7172011-01-04 15:09:30 -08001138/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +02001139void assert_pll(struct drm_i915_private *dev_priv,
1140 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001141{
Jesse Barnesb24e7172011-01-04 15:09:30 -08001142 u32 val;
1143 bool cur_state;
1144
Ville Syrjälä649636e2015-09-22 19:50:01 +03001145 val = I915_READ(DPLL(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001146 cur_state = !!(val & DPLL_VCO_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001147 I915_STATE_WARN(cur_state != state,
Jesse Barnesb24e7172011-01-04 15:09:30 -08001148 "PLL state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001149 onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001150}
Jesse Barnesb24e7172011-01-04 15:09:30 -08001151
Jani Nikula23538ef2013-08-27 15:12:22 +03001152/* XXX: the dsi pll is shared between MIPI DSI ports */
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +00001153void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
Jani Nikula23538ef2013-08-27 15:12:22 +03001154{
1155 u32 val;
1156 bool cur_state;
1157
Ville Syrjäläa5805162015-05-26 20:42:30 +03001158 mutex_lock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001159 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
Ville Syrjäläa5805162015-05-26 20:42:30 +03001160 mutex_unlock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001161
1162 cur_state = val & DSI_PLL_VCO_EN;
Rob Clarke2c719b2014-12-15 13:56:32 -05001163 I915_STATE_WARN(cur_state != state,
Jani Nikula23538ef2013-08-27 15:12:22 +03001164 "DSI PLL state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001165 onoff(state), onoff(cur_state));
Jani Nikula23538ef2013-08-27 15:12:22 +03001166}
Jani Nikula23538ef2013-08-27 15:12:22 +03001167
Jesse Barnes040484a2011-01-03 12:14:26 -08001168static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1169 enum pipe pipe, bool state)
1170{
Jesse Barnes040484a2011-01-03 12:14:26 -08001171 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001172 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1173 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001174
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001175 if (HAS_DDI(dev_priv)) {
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001176 /* DDI does not have a specific FDI_TX register */
Ville Syrjälä649636e2015-09-22 19:50:01 +03001177 u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
Paulo Zanoniad80a812012-10-24 16:06:19 -02001178 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001179 } else {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001180 u32 val = I915_READ(FDI_TX_CTL(pipe));
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001181 cur_state = !!(val & FDI_TX_ENABLE);
1182 }
Rob Clarke2c719b2014-12-15 13:56:32 -05001183 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001184 "FDI TX state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001185 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001186}
1187#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1188#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1189
1190static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1191 enum pipe pipe, bool state)
1192{
Jesse Barnes040484a2011-01-03 12:14:26 -08001193 u32 val;
1194 bool cur_state;
1195
Ville Syrjälä649636e2015-09-22 19:50:01 +03001196 val = I915_READ(FDI_RX_CTL(pipe));
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001197 cur_state = !!(val & FDI_RX_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001198 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001199 "FDI RX state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001200 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001201}
1202#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1203#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1204
1205static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1206 enum pipe pipe)
1207{
Jesse Barnes040484a2011-01-03 12:14:26 -08001208 u32 val;
1209
1210 /* ILK FDI PLL is always enabled */
Tvrtko Ursulin7e22dbb2016-05-10 10:57:06 +01001211 if (IS_GEN5(dev_priv))
Jesse Barnes040484a2011-01-03 12:14:26 -08001212 return;
1213
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001214 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001215 if (HAS_DDI(dev_priv))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001216 return;
1217
Ville Syrjälä649636e2015-09-22 19:50:01 +03001218 val = I915_READ(FDI_TX_CTL(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001219 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 -08001220}
1221
Daniel Vetter55607e82013-06-16 21:42:39 +02001222void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1223 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001224{
Jesse Barnes040484a2011-01-03 12:14:26 -08001225 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001226 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001227
Ville Syrjälä649636e2015-09-22 19:50:01 +03001228 val = I915_READ(FDI_RX_CTL(pipe));
Daniel Vetter55607e82013-06-16 21:42:39 +02001229 cur_state = !!(val & FDI_RX_PLL_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001230 I915_STATE_WARN(cur_state != state,
Daniel Vetter55607e82013-06-16 21:42:39 +02001231 "FDI RX PLL assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001232 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001233}
1234
Daniel Vetterb680c372014-09-19 18:27:27 +02001235void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1236 enum pipe pipe)
Jesse Barnesea0760c2011-01-04 15:09:32 -08001237{
Jani Nikulabedd4db2014-08-22 15:04:13 +03001238 struct drm_device *dev = dev_priv->dev;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001239 i915_reg_t pp_reg;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001240 u32 val;
1241 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001242 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001243
Jani Nikulabedd4db2014-08-22 15:04:13 +03001244 if (WARN_ON(HAS_DDI(dev)))
1245 return;
1246
1247 if (HAS_PCH_SPLIT(dev)) {
1248 u32 port_sel;
1249
Jesse Barnesea0760c2011-01-04 15:09:32 -08001250 pp_reg = PCH_PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001251 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1252
1253 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1254 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1255 panel_pipe = PIPE_B;
1256 /* XXX: else fix for eDP */
Wayne Boyer666a4532015-12-09 12:29:35 -08001257 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Jani Nikulabedd4db2014-08-22 15:04:13 +03001258 /* presumably write lock depends on pipe, not port select */
1259 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1260 panel_pipe = pipe;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001261 } else {
1262 pp_reg = PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001263 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1264 panel_pipe = PIPE_B;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001265 }
1266
1267 val = I915_READ(pp_reg);
1268 if (!(val & PANEL_POWER_ON) ||
Jani Nikulaec49ba22014-08-21 15:06:25 +03001269 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
Jesse Barnesea0760c2011-01-04 15:09:32 -08001270 locked = false;
1271
Rob Clarke2c719b2014-12-15 13:56:32 -05001272 I915_STATE_WARN(panel_pipe == pipe && locked,
Jesse Barnesea0760c2011-01-04 15:09:32 -08001273 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001274 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001275}
1276
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001277static void assert_cursor(struct drm_i915_private *dev_priv,
1278 enum pipe pipe, bool state)
1279{
1280 struct drm_device *dev = dev_priv->dev;
1281 bool cur_state;
1282
Paulo Zanonid9d82082014-02-27 16:30:56 -03001283 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä0b87c242015-09-22 19:47:51 +03001284 cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
Paulo Zanonid9d82082014-02-27 16:30:56 -03001285 else
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03001286 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001287
Rob Clarke2c719b2014-12-15 13:56:32 -05001288 I915_STATE_WARN(cur_state != state,
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001289 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001290 pipe_name(pipe), onoff(state), onoff(cur_state));
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001291}
1292#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1293#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1294
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001295void assert_pipe(struct drm_i915_private *dev_priv,
1296 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001297{
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001298 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001299 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1300 pipe);
Imre Deak4feed0e2016-02-12 18:55:14 +02001301 enum intel_display_power_domain power_domain;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001302
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001303 /* if we need the pipe quirk it must be always on */
1304 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1305 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetter8e636782012-01-22 01:36:48 +01001306 state = true;
1307
Imre Deak4feed0e2016-02-12 18:55:14 +02001308 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
1309 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001310 u32 val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni69310162013-01-29 16:35:19 -02001311 cur_state = !!(val & PIPECONF_ENABLE);
Imre Deak4feed0e2016-02-12 18:55:14 +02001312
1313 intel_display_power_put(dev_priv, power_domain);
1314 } else {
1315 cur_state = false;
Paulo Zanoni69310162013-01-29 16:35:19 -02001316 }
1317
Rob Clarke2c719b2014-12-15 13:56:32 -05001318 I915_STATE_WARN(cur_state != state,
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001319 "pipe %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001320 pipe_name(pipe), onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001321}
1322
Chris Wilson931872f2012-01-16 23:01:13 +00001323static void assert_plane(struct drm_i915_private *dev_priv,
1324 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001325{
Jesse Barnesb24e7172011-01-04 15:09:30 -08001326 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001327 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001328
Ville Syrjälä649636e2015-09-22 19:50:01 +03001329 val = I915_READ(DSPCNTR(plane));
Chris Wilson931872f2012-01-16 23:01:13 +00001330 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001331 I915_STATE_WARN(cur_state != state,
Chris Wilson931872f2012-01-16 23:01:13 +00001332 "plane %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001333 plane_name(plane), onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001334}
1335
Chris Wilson931872f2012-01-16 23:01:13 +00001336#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1337#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1338
Jesse Barnesb24e7172011-01-04 15:09:30 -08001339static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1340 enum pipe pipe)
1341{
Ville Syrjälä653e1022013-06-04 13:49:05 +03001342 struct drm_device *dev = dev_priv->dev;
Ville Syrjälä649636e2015-09-22 19:50:01 +03001343 int i;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001344
Ville Syrjälä653e1022013-06-04 13:49:05 +03001345 /* Primary planes are fixed to pipes on gen4+ */
1346 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001347 u32 val = I915_READ(DSPCNTR(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001348 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
Adam Jackson28c057942011-10-07 14:38:42 -04001349 "plane %c assertion failure, should be disabled but not\n",
1350 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001351 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001352 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001353
Jesse Barnesb24e7172011-01-04 15:09:30 -08001354 /* Need to check both planes against the pipe */
Damien Lespiau055e3932014-08-18 13:49:10 +01001355 for_each_pipe(dev_priv, i) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001356 u32 val = I915_READ(DSPCNTR(i));
1357 enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
Jesse Barnesb24e7172011-01-04 15:09:30 -08001358 DISPPLANE_SEL_PIPE_SHIFT;
Rob Clarke2c719b2014-12-15 13:56:32 -05001359 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001360 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1361 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001362 }
1363}
1364
Jesse Barnes19332d72013-03-28 09:55:38 -07001365static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1366 enum pipe pipe)
1367{
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001368 struct drm_device *dev = dev_priv->dev;
Ville Syrjälä649636e2015-09-22 19:50:01 +03001369 int sprite;
Jesse Barnes19332d72013-03-28 09:55:38 -07001370
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001371 if (INTEL_INFO(dev)->gen >= 9) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001372 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001373 u32 val = I915_READ(PLANE_CTL(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001374 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001375 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1376 sprite, pipe_name(pipe));
1377 }
Wayne Boyer666a4532015-12-09 12:29:35 -08001378 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001379 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001380 u32 val = I915_READ(SPCNTR(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001381 I915_STATE_WARN(val & SP_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001382 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +00001383 sprite_name(pipe, sprite), pipe_name(pipe));
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001384 }
1385 } else if (INTEL_INFO(dev)->gen >= 7) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001386 u32 val = I915_READ(SPRCTL(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001387 I915_STATE_WARN(val & SPRITE_ENABLE,
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001388 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001389 plane_name(pipe), pipe_name(pipe));
1390 } else if (INTEL_INFO(dev)->gen >= 5) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001391 u32 val = I915_READ(DVSCNTR(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001392 I915_STATE_WARN(val & DVS_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001393 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1394 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001395 }
1396}
1397
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001398static void assert_vblank_disabled(struct drm_crtc *crtc)
1399{
Rob Clarke2c719b2014-12-15 13:56:32 -05001400 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001401 drm_crtc_vblank_put(crtc);
1402}
1403
Ander Conselvan de Oliveira7abd4b32016-03-08 17:46:15 +02001404void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1405 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001406{
Jesse Barnes92f25842011-01-04 15:09:34 -08001407 u32 val;
1408 bool enabled;
1409
Ville Syrjälä649636e2015-09-22 19:50:01 +03001410 val = I915_READ(PCH_TRANSCONF(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001411 enabled = !!(val & TRANS_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001412 I915_STATE_WARN(enabled,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001413 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1414 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001415}
1416
Keith Packard4e634382011-08-06 10:39:45 -07001417static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1418 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001419{
1420 if ((val & DP_PORT_EN) == 0)
1421 return false;
1422
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001423 if (HAS_PCH_CPT(dev_priv)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001424 u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
Keith Packardf0575e92011-07-25 22:12:43 -07001425 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1426 return false;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001427 } else if (IS_CHERRYVIEW(dev_priv)) {
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001428 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1429 return false;
Keith Packardf0575e92011-07-25 22:12:43 -07001430 } else {
1431 if ((val & DP_PIPE_MASK) != (pipe << 30))
1432 return false;
1433 }
1434 return true;
1435}
1436
Keith Packard1519b992011-08-06 10:35:34 -07001437static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1438 enum pipe pipe, u32 val)
1439{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001440 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001441 return false;
1442
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001443 if (HAS_PCH_CPT(dev_priv)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001444 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001445 return false;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001446 } else if (IS_CHERRYVIEW(dev_priv)) {
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001447 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1448 return false;
Keith Packard1519b992011-08-06 10:35:34 -07001449 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001450 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001451 return false;
1452 }
1453 return true;
1454}
1455
1456static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1457 enum pipe pipe, u32 val)
1458{
1459 if ((val & LVDS_PORT_EN) == 0)
1460 return false;
1461
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001462 if (HAS_PCH_CPT(dev_priv)) {
Keith Packard1519b992011-08-06 10:35:34 -07001463 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1464 return false;
1465 } else {
1466 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1467 return false;
1468 }
1469 return true;
1470}
1471
1472static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1473 enum pipe pipe, u32 val)
1474{
1475 if ((val & ADPA_DAC_ENABLE) == 0)
1476 return false;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001477 if (HAS_PCH_CPT(dev_priv)) {
Keith Packard1519b992011-08-06 10:35:34 -07001478 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1479 return false;
1480 } else {
1481 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1482 return false;
1483 }
1484 return true;
1485}
1486
Jesse Barnes291906f2011-02-02 12:28:03 -08001487static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001488 enum pipe pipe, i915_reg_t reg,
1489 u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001490{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001491 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001492 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001493 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001494 i915_mmio_reg_offset(reg), pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001495
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001496 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001497 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001498 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001499}
1500
1501static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001502 enum pipe pipe, i915_reg_t reg)
Jesse Barnes291906f2011-02-02 12:28:03 -08001503{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001504 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001505 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001506 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001507 i915_mmio_reg_offset(reg), pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001508
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001509 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001510 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001511 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001512}
1513
1514static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1515 enum pipe pipe)
1516{
Jesse Barnes291906f2011-02-02 12:28:03 -08001517 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001518
Keith Packardf0575e92011-07-25 22:12:43 -07001519 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1520 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1521 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001522
Ville Syrjälä649636e2015-09-22 19:50:01 +03001523 val = I915_READ(PCH_ADPA);
Rob Clarke2c719b2014-12-15 13:56:32 -05001524 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001525 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001526 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001527
Ville Syrjälä649636e2015-09-22 19:50:01 +03001528 val = I915_READ(PCH_LVDS);
Rob Clarke2c719b2014-12-15 13:56:32 -05001529 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001530 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001531 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001532
Paulo Zanonie2debe92013-02-18 19:00:27 -03001533 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1534 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1535 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001536}
1537
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001538static void _vlv_enable_pll(struct intel_crtc *crtc,
1539 const struct intel_crtc_state *pipe_config)
1540{
1541 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1542 enum pipe pipe = crtc->pipe;
1543
1544 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
1545 POSTING_READ(DPLL(pipe));
1546 udelay(150);
1547
Chris Wilson2c30b432016-06-30 15:32:54 +01001548 if (intel_wait_for_register(dev_priv,
1549 DPLL(pipe),
1550 DPLL_LOCK_VLV,
1551 DPLL_LOCK_VLV,
1552 1))
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001553 DRM_ERROR("DPLL %d failed to lock\n", pipe);
1554}
1555
Ville Syrjäläd288f652014-10-28 13:20:22 +02001556static void vlv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001557 const struct intel_crtc_state *pipe_config)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001558{
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001559 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001560 enum pipe pipe = crtc->pipe;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001561
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001562 assert_pipe_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001563
Daniel Vetter87442f72013-06-06 00:52:17 +02001564 /* PLL is protected by panel, make sure we can write it */
Ville Syrjälä7d1a83c2016-03-15 16:39:58 +02001565 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001566
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001567 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1568 _vlv_enable_pll(crtc, pipe_config);
Daniel Vetter426115c2013-07-11 22:13:42 +02001569
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001570 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1571 POSTING_READ(DPLL_MD(pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001572}
1573
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001574
1575static void _chv_enable_pll(struct intel_crtc *crtc,
1576 const struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001577{
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001578 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001579 enum pipe pipe = crtc->pipe;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001580 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001581 u32 tmp;
1582
Ville Syrjäläa5805162015-05-26 20:42:30 +03001583 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001584
1585 /* Enable back the 10bit clock to display controller */
1586 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1587 tmp |= DPIO_DCLKP_EN;
1588 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1589
Ville Syrjälä54433e92015-05-26 20:42:31 +03001590 mutex_unlock(&dev_priv->sb_lock);
1591
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001592 /*
1593 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1594 */
1595 udelay(1);
1596
1597 /* Enable PLL */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001598 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001599
1600 /* Check PLL is locked */
Chris Wilson6b188262016-06-30 15:32:55 +01001601 if (intel_wait_for_register(dev_priv,
1602 DPLL(pipe), DPLL_LOCK_VLV, DPLL_LOCK_VLV,
1603 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001604 DRM_ERROR("PLL %d failed to lock\n", pipe);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001605}
1606
1607static void chv_enable_pll(struct intel_crtc *crtc,
1608 const struct intel_crtc_state *pipe_config)
1609{
1610 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1611 enum pipe pipe = crtc->pipe;
1612
1613 assert_pipe_disabled(dev_priv, pipe);
1614
1615 /* PLL is protected by panel, make sure we can write it */
1616 assert_panel_unlocked(dev_priv, pipe);
1617
1618 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1619 _chv_enable_pll(crtc, pipe_config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001620
Ville Syrjäläc2317752016-03-15 16:39:56 +02001621 if (pipe != PIPE_A) {
1622 /*
1623 * WaPixelRepeatModeFixForC0:chv
1624 *
1625 * DPLLCMD is AWOL. Use chicken bits to propagate
1626 * the value from DPLLBMD to either pipe B or C.
1627 */
1628 I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
1629 I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
1630 I915_WRITE(CBR4_VLV, 0);
1631 dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
1632
1633 /*
1634 * DPLLB VGA mode also seems to cause problems.
1635 * We should always have it disabled.
1636 */
1637 WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
1638 } else {
1639 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1640 POSTING_READ(DPLL_MD(pipe));
1641 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001642}
1643
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001644static int intel_num_dvo_pipes(struct drm_device *dev)
1645{
1646 struct intel_crtc *crtc;
1647 int count = 0;
1648
1649 for_each_intel_crtc(dev, crtc)
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001650 count += crtc->base.state->active &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001651 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001652
1653 return count;
1654}
1655
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001656static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001657{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001658 struct drm_device *dev = crtc->base.dev;
1659 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001660 i915_reg_t reg = DPLL(crtc->pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001661 u32 dpll = crtc->config->dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001662
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001663 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001664
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001665 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001666 if (IS_MOBILE(dev) && !IS_I830(dev))
1667 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001668
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001669 /* Enable DVO 2x clock on both PLLs if necessary */
1670 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1671 /*
1672 * It appears to be important that we don't enable this
1673 * for the current pipe before otherwise configuring the
1674 * PLL. No idea how this should be handled if multiple
1675 * DVO outputs are enabled simultaneosly.
1676 */
1677 dpll |= DPLL_DVO_2X_MODE;
1678 I915_WRITE(DPLL(!crtc->pipe),
1679 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1680 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001681
Ville Syrjäläc2b63372015-10-07 22:08:25 +03001682 /*
1683 * Apparently we need to have VGA mode enabled prior to changing
1684 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1685 * dividers, even though the register value does change.
1686 */
1687 I915_WRITE(reg, 0);
1688
Ville Syrjälä8e7a65a2015-10-07 22:08:24 +03001689 I915_WRITE(reg, dpll);
1690
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001691 /* Wait for the clocks to stabilize. */
1692 POSTING_READ(reg);
1693 udelay(150);
1694
1695 if (INTEL_INFO(dev)->gen >= 4) {
1696 I915_WRITE(DPLL_MD(crtc->pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001697 crtc->config->dpll_hw_state.dpll_md);
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001698 } else {
1699 /* The pixel multiplier can only be updated once the
1700 * DPLL is enabled and the clocks are stable.
1701 *
1702 * So write it again.
1703 */
1704 I915_WRITE(reg, dpll);
1705 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001706
1707 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001708 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001709 POSTING_READ(reg);
1710 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001711 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001712 POSTING_READ(reg);
1713 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001714 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001715 POSTING_READ(reg);
1716 udelay(150); /* wait for warmup */
1717}
1718
1719/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001720 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001721 * @dev_priv: i915 private structure
1722 * @pipe: pipe PLL to disable
1723 *
1724 * Disable the PLL for @pipe, making sure the pipe is off first.
1725 *
1726 * Note! This is for pre-ILK only.
1727 */
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001728static void i9xx_disable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001729{
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001730 struct drm_device *dev = crtc->base.dev;
1731 struct drm_i915_private *dev_priv = dev->dev_private;
1732 enum pipe pipe = crtc->pipe;
1733
1734 /* Disable DVO 2x clock on both PLLs if necessary */
1735 if (IS_I830(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001736 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001737 !intel_num_dvo_pipes(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001738 I915_WRITE(DPLL(PIPE_B),
1739 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1740 I915_WRITE(DPLL(PIPE_A),
1741 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1742 }
1743
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001744 /* Don't disable pipe or pipe PLLs if needed */
1745 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1746 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001747 return;
1748
1749 /* Make sure the pipe isn't still relying on us */
1750 assert_pipe_disabled(dev_priv, pipe);
1751
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001752 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
Daniel Vetter50b44a42013-06-05 13:34:33 +02001753 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001754}
1755
Jesse Barnesf6071162013-10-01 10:41:38 -07001756static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1757{
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001758 u32 val;
Jesse Barnesf6071162013-10-01 10:41:38 -07001759
1760 /* Make sure the pipe isn't still relying on us */
1761 assert_pipe_disabled(dev_priv, pipe);
1762
Ville Syrjälä03ed5cb2016-03-15 16:39:55 +02001763 val = DPLL_INTEGRATED_REF_CLK_VLV |
1764 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1765 if (pipe != PIPE_A)
1766 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1767
Jesse Barnesf6071162013-10-01 10:41:38 -07001768 I915_WRITE(DPLL(pipe), val);
1769 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001770}
1771
1772static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1773{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001774 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001775 u32 val;
1776
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001777 /* Make sure the pipe isn't still relying on us */
1778 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001779
Ville Syrjälä60bfe442015-06-29 15:25:49 +03001780 val = DPLL_SSC_REF_CLK_CHV |
1781 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001782 if (pipe != PIPE_A)
1783 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä03ed5cb2016-03-15 16:39:55 +02001784
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001785 I915_WRITE(DPLL(pipe), val);
1786 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001787
Ville Syrjäläa5805162015-05-26 20:42:30 +03001788 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd7520482014-04-09 13:28:59 +03001789
1790 /* Disable 10bit clock to display controller */
1791 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1792 val &= ~DPIO_DCLKP_EN;
1793 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1794
Ville Syrjäläa5805162015-05-26 20:42:30 +03001795 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001796}
1797
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001798void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001799 struct intel_digital_port *dport,
1800 unsigned int expected_mask)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001801{
1802 u32 port_mask;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001803 i915_reg_t dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001804
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001805 switch (dport->port) {
1806 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001807 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001808 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001809 break;
1810 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001811 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001812 dpll_reg = DPLL(0);
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001813 expected_mask <<= 4;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001814 break;
1815 case PORT_D:
1816 port_mask = DPLL_PORTD_READY_MASK;
1817 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001818 break;
1819 default:
1820 BUG();
1821 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001822
Chris Wilson370004d2016-06-30 15:32:56 +01001823 if (intel_wait_for_register(dev_priv,
1824 dpll_reg, port_mask, expected_mask,
1825 1000))
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001826 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1827 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
Jesse Barnes89b667f2013-04-18 14:51:36 -07001828}
1829
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001830static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1831 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001832{
Daniel Vetter23670b322012-11-01 09:15:30 +01001833 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001834 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001835 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001836 i915_reg_t reg;
1837 uint32_t val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001838
Jesse Barnes040484a2011-01-03 12:14:26 -08001839 /* Make sure PCH DPLL is enabled */
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02001840 assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
Jesse Barnes040484a2011-01-03 12:14:26 -08001841
1842 /* FDI must be feeding us bits for PCH ports */
1843 assert_fdi_tx_enabled(dev_priv, pipe);
1844 assert_fdi_rx_enabled(dev_priv, pipe);
1845
Daniel Vetter23670b322012-11-01 09:15:30 +01001846 if (HAS_PCH_CPT(dev)) {
1847 /* Workaround: Set the timing override bit before enabling the
1848 * pch transcoder. */
1849 reg = TRANS_CHICKEN2(pipe);
1850 val = I915_READ(reg);
1851 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1852 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001853 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001854
Daniel Vetterab9412b2013-05-03 11:49:46 +02001855 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001856 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001857 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001858
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001859 if (HAS_PCH_IBX(dev_priv)) {
Jesse Barnese9bcff52011-06-24 12:19:20 -07001860 /*
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001861 * Make the BPC in transcoder be consistent with
1862 * that in pipeconf reg. For HDMI we must use 8bpc
1863 * here for both 8bpc and 12bpc.
Jesse Barnese9bcff52011-06-24 12:19:20 -07001864 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001865 val &= ~PIPECONF_BPC_MASK;
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001866 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
1867 val |= PIPECONF_8BPC;
1868 else
1869 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001870 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001871
1872 val &= ~TRANS_INTERLACE_MASK;
1873 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001874 if (HAS_PCH_IBX(dev_priv) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001875 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001876 val |= TRANS_LEGACY_INTERLACED_ILK;
1877 else
1878 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001879 else
1880 val |= TRANS_PROGRESSIVE;
1881
Jesse Barnes040484a2011-01-03 12:14:26 -08001882 I915_WRITE(reg, val | TRANS_ENABLE);
Chris Wilson650fbd82016-06-30 15:32:57 +01001883 if (intel_wait_for_register(dev_priv,
1884 reg, TRANS_STATE_ENABLE, TRANS_STATE_ENABLE,
1885 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001886 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08001887}
1888
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001889static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001890 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001891{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001892 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001893
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001894 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001895 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001896 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001897
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001898 /* Workaround: set timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001899 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01001900 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001901 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001902
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001903 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001904 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001905
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001906 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1907 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001908 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001909 else
1910 val |= TRANS_PROGRESSIVE;
1911
Daniel Vetterab9412b2013-05-03 11:49:46 +02001912 I915_WRITE(LPT_TRANSCONF, val);
Chris Wilsond9f96242016-06-30 15:32:58 +01001913 if (intel_wait_for_register(dev_priv,
1914 LPT_TRANSCONF,
1915 TRANS_STATE_ENABLE,
1916 TRANS_STATE_ENABLE,
1917 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02001918 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001919}
1920
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001921static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1922 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001923{
Daniel Vetter23670b322012-11-01 09:15:30 +01001924 struct drm_device *dev = dev_priv->dev;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001925 i915_reg_t reg;
1926 uint32_t val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001927
1928 /* FDI relies on the transcoder */
1929 assert_fdi_tx_disabled(dev_priv, pipe);
1930 assert_fdi_rx_disabled(dev_priv, pipe);
1931
Jesse Barnes291906f2011-02-02 12:28:03 -08001932 /* Ports must be off as well */
1933 assert_pch_ports_disabled(dev_priv, pipe);
1934
Daniel Vetterab9412b2013-05-03 11:49:46 +02001935 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001936 val = I915_READ(reg);
1937 val &= ~TRANS_ENABLE;
1938 I915_WRITE(reg, val);
1939 /* wait for PCH transcoder off, transcoder state */
Chris Wilsona7d04662016-06-30 15:32:59 +01001940 if (intel_wait_for_register(dev_priv,
1941 reg, TRANS_STATE_ENABLE, 0,
1942 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001943 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01001944
Ville Syrjäläc4656132015-10-29 21:25:56 +02001945 if (HAS_PCH_CPT(dev)) {
Daniel Vetter23670b322012-11-01 09:15:30 +01001946 /* Workaround: Clear the timing override chicken bit again. */
1947 reg = TRANS_CHICKEN2(pipe);
1948 val = I915_READ(reg);
1949 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1950 I915_WRITE(reg, val);
1951 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001952}
1953
Paulo Zanoniab4d9662012-10-31 18:12:55 -02001954static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001955{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001956 u32 val;
1957
Daniel Vetterab9412b2013-05-03 11:49:46 +02001958 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001959 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02001960 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001961 /* wait for PCH transcoder off, transcoder state */
Chris Wilsondfdb4742016-06-30 15:33:00 +01001962 if (intel_wait_for_register(dev_priv,
1963 LPT_TRANSCONF, TRANS_STATE_ENABLE, 0,
1964 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001965 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001966
1967 /* Workaround: clear timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001968 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01001969 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001970 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Jesse Barnes92f25842011-01-04 15:09:34 -08001971}
1972
1973/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001974 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02001975 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08001976 *
Paulo Zanoni03722642014-01-17 13:51:09 -02001977 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001978 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08001979 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02001980static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001981{
Paulo Zanoni03722642014-01-17 13:51:09 -02001982 struct drm_device *dev = crtc->base.dev;
1983 struct drm_i915_private *dev_priv = dev->dev_private;
1984 enum pipe pipe = crtc->pipe;
Ville Syrjälä1a70a7282015-10-29 21:25:50 +02001985 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter1a240d42012-11-29 22:18:51 +01001986 enum pipe pch_transcoder;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001987 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001988 u32 val;
1989
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03001990 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
1991
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001992 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001993 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001994 assert_sprites_disabled(dev_priv, pipe);
1995
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001996 if (HAS_PCH_LPT(dev_priv))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001997 pch_transcoder = TRANSCODER_A;
1998 else
1999 pch_transcoder = pipe;
2000
Jesse Barnesb24e7172011-01-04 15:09:30 -08002001 /*
2002 * A pipe without a PLL won't actually be able to drive bits from
2003 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2004 * need the check.
2005 */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03002006 if (HAS_GMCH_DISPLAY(dev_priv))
Jani Nikulaa65347b2015-11-27 12:21:46 +02002007 if (crtc->config->has_dsi_encoder)
Jani Nikula23538ef2013-08-27 15:12:22 +03002008 assert_dsi_pll_enabled(dev_priv);
2009 else
2010 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002011 else {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002012 if (crtc->config->has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08002013 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002014 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002015 assert_fdi_tx_pll_enabled(dev_priv,
2016 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08002017 }
2018 /* FIXME: assert CPU port conditions for SNB+ */
2019 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08002020
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002021 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002022 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002023 if (val & PIPECONF_ENABLE) {
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002024 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2025 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
Chris Wilson00d70b12011-03-17 07:18:29 +00002026 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002027 }
Chris Wilson00d70b12011-03-17 07:18:29 +00002028
2029 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02002030 POSTING_READ(reg);
Ville Syrjäläb7792d82015-12-14 18:23:43 +02002031
2032 /*
2033 * Until the pipe starts DSL will read as 0, which would cause
2034 * an apparent vblank timestamp jump, which messes up also the
2035 * frame count when it's derived from the timestamps. So let's
2036 * wait for the pipe to start properly before we call
2037 * drm_crtc_vblank_on()
2038 */
2039 if (dev->max_vblank_count == 0 &&
2040 wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
2041 DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08002042}
2043
2044/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002045 * intel_disable_pipe - disable a pipe, asserting requirements
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002046 * @crtc: crtc whose pipes is to be disabled
Jesse Barnesb24e7172011-01-04 15:09:30 -08002047 *
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002048 * Disable the pipe of @crtc, making sure that various hardware
2049 * specific requirements are met, if applicable, e.g. plane
2050 * disabled, panel fitter off, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002051 *
2052 * Will wait until the pipe has shut down before returning.
2053 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002054static void intel_disable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002055{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002056 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002057 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002058 enum pipe pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002059 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002060 u32 val;
2061
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03002062 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
2063
Jesse Barnesb24e7172011-01-04 15:09:30 -08002064 /*
2065 * Make sure planes won't keep trying to pump pixels to us,
2066 * or we might hang the display.
2067 */
2068 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002069 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07002070 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002071
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002072 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002073 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00002074 if ((val & PIPECONF_ENABLE) == 0)
2075 return;
2076
Ville Syrjälä67adc642014-08-15 01:21:57 +03002077 /*
2078 * Double wide has implications for planes
2079 * so best keep it disabled when not needed.
2080 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002081 if (crtc->config->double_wide)
Ville Syrjälä67adc642014-08-15 01:21:57 +03002082 val &= ~PIPECONF_DOUBLE_WIDE;
2083
2084 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002085 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2086 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Ville Syrjälä67adc642014-08-15 01:21:57 +03002087 val &= ~PIPECONF_ENABLE;
2088
2089 I915_WRITE(reg, val);
2090 if ((val & PIPECONF_ENABLE) == 0)
2091 intel_wait_for_pipe_off(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002092}
2093
Chris Wilson693db182013-03-05 14:52:39 +00002094static bool need_vtd_wa(struct drm_device *dev)
2095{
2096#ifdef CONFIG_INTEL_IOMMU
2097 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2098 return true;
2099#endif
2100 return false;
2101}
2102
Ville Syrjälä832be822016-01-12 21:08:33 +02002103static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
2104{
2105 return IS_GEN2(dev_priv) ? 2048 : 4096;
2106}
2107
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002108static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
2109 uint64_t fb_modifier, unsigned int cpp)
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002110{
2111 switch (fb_modifier) {
2112 case DRM_FORMAT_MOD_NONE:
2113 return cpp;
2114 case I915_FORMAT_MOD_X_TILED:
2115 if (IS_GEN2(dev_priv))
2116 return 128;
2117 else
2118 return 512;
2119 case I915_FORMAT_MOD_Y_TILED:
2120 if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
2121 return 128;
2122 else
2123 return 512;
2124 case I915_FORMAT_MOD_Yf_TILED:
2125 switch (cpp) {
2126 case 1:
2127 return 64;
2128 case 2:
2129 case 4:
2130 return 128;
2131 case 8:
2132 case 16:
2133 return 256;
2134 default:
2135 MISSING_CASE(cpp);
2136 return cpp;
2137 }
2138 break;
2139 default:
2140 MISSING_CASE(fb_modifier);
2141 return cpp;
2142 }
2143}
2144
Ville Syrjälä832be822016-01-12 21:08:33 +02002145unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
2146 uint64_t fb_modifier, unsigned int cpp)
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002147{
Ville Syrjälä832be822016-01-12 21:08:33 +02002148 if (fb_modifier == DRM_FORMAT_MOD_NONE)
2149 return 1;
2150 else
2151 return intel_tile_size(dev_priv) /
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002152 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002153}
2154
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002155/* Return the tile dimensions in pixel units */
2156static void intel_tile_dims(const struct drm_i915_private *dev_priv,
2157 unsigned int *tile_width,
2158 unsigned int *tile_height,
2159 uint64_t fb_modifier,
2160 unsigned int cpp)
2161{
2162 unsigned int tile_width_bytes =
2163 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
2164
2165 *tile_width = tile_width_bytes / cpp;
2166 *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
2167}
2168
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002169unsigned int
2170intel_fb_align_height(struct drm_device *dev, unsigned int height,
Ville Syrjälä832be822016-01-12 21:08:33 +02002171 uint32_t pixel_format, uint64_t fb_modifier)
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002172{
Ville Syrjälä832be822016-01-12 21:08:33 +02002173 unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
2174 unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
2175
2176 return ALIGN(height, tile_height);
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002177}
2178
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002179unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
2180{
2181 unsigned int size = 0;
2182 int i;
2183
2184 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
2185 size += rot_info->plane[i].width * rot_info->plane[i].height;
2186
2187 return size;
2188}
2189
Daniel Vetter75c82a52015-10-14 16:51:04 +02002190static void
Ville Syrjälä3465c582016-02-15 22:54:43 +02002191intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
2192 const struct drm_framebuffer *fb,
2193 unsigned int rotation)
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002194{
Ville Syrjälä2d7a2152016-02-15 22:54:47 +02002195 if (intel_rotation_90_or_270(rotation)) {
2196 *view = i915_ggtt_view_rotated;
2197 view->params.rotated = to_intel_framebuffer(fb)->rot_info;
2198 } else {
2199 *view = i915_ggtt_view_normal;
2200 }
2201}
2202
2203static void
2204intel_fill_fb_info(struct drm_i915_private *dev_priv,
2205 struct drm_framebuffer *fb)
2206{
2207 struct intel_rotation_info *info = &to_intel_framebuffer(fb)->rot_info;
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002208 unsigned int tile_size, tile_width, tile_height, cpp;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002209
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002210 tile_size = intel_tile_size(dev_priv);
2211
2212 cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002213 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2214 fb->modifier[0], cpp);
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002215
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002216 info->plane[0].width = DIV_ROUND_UP(fb->pitches[0], tile_width * cpp);
2217 info->plane[0].height = DIV_ROUND_UP(fb->height, tile_height);
Tvrtko Ursulin84fe03f2015-06-23 14:26:46 +01002218
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01002219 if (info->pixel_format == DRM_FORMAT_NV12) {
Ville Syrjälä832be822016-01-12 21:08:33 +02002220 cpp = drm_format_plane_cpp(fb->pixel_format, 1);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002221 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2222 fb->modifier[1], cpp);
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002223
Ville Syrjälä2d7a2152016-02-15 22:54:47 +02002224 info->uv_offset = fb->offsets[1];
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002225 info->plane[1].width = DIV_ROUND_UP(fb->pitches[1], tile_width * cpp);
2226 info->plane[1].height = DIV_ROUND_UP(fb->height / 2, tile_height);
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01002227 }
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002228}
2229
Ville Syrjälä603525d2016-01-12 21:08:37 +02002230static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002231{
2232 if (INTEL_INFO(dev_priv)->gen >= 9)
2233 return 256 * 1024;
Ville Syrjälä985b8bb2015-06-11 16:31:15 +03002234 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
Wayne Boyer666a4532015-12-09 12:29:35 -08002235 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002236 return 128 * 1024;
2237 else if (INTEL_INFO(dev_priv)->gen >= 4)
2238 return 4 * 1024;
2239 else
Ville Syrjälä44c59052015-06-11 16:31:16 +03002240 return 0;
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002241}
2242
Ville Syrjälä603525d2016-01-12 21:08:37 +02002243static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
2244 uint64_t fb_modifier)
2245{
2246 switch (fb_modifier) {
2247 case DRM_FORMAT_MOD_NONE:
2248 return intel_linear_alignment(dev_priv);
2249 case I915_FORMAT_MOD_X_TILED:
2250 if (INTEL_INFO(dev_priv)->gen >= 9)
2251 return 256 * 1024;
2252 return 0;
2253 case I915_FORMAT_MOD_Y_TILED:
2254 case I915_FORMAT_MOD_Yf_TILED:
2255 return 1 * 1024 * 1024;
2256 default:
2257 MISSING_CASE(fb_modifier);
2258 return 0;
2259 }
2260}
2261
Chris Wilson127bd2a2010-07-23 23:32:05 +01002262int
Ville Syrjälä3465c582016-02-15 22:54:43 +02002263intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
2264 unsigned int rotation)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002265{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002266 struct drm_device *dev = fb->dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00002267 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002268 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002269 struct i915_ggtt_view view;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002270 u32 alignment;
2271 int ret;
2272
Matt Roperebcdd392014-07-09 16:22:11 -07002273 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2274
Ville Syrjälä603525d2016-01-12 21:08:37 +02002275 alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002276
Ville Syrjälä3465c582016-02-15 22:54:43 +02002277 intel_fill_fb_ggtt_view(&view, fb, rotation);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002278
Chris Wilson693db182013-03-05 14:52:39 +00002279 /* Note that the w/a also requires 64 PTE of padding following the
2280 * bo. We currently fill all unused PTE with the shadow page and so
2281 * we should always have valid PTE following the scanout preventing
2282 * the VT-d warning.
2283 */
2284 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2285 alignment = 256 * 1024;
2286
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002287 /*
2288 * Global gtt pte registers are special registers which actually forward
2289 * writes to a chunk of system memory. Which means that there is no risk
2290 * that the register values disappear as soon as we call
2291 * intel_runtime_pm_put(), so it is correct to wrap only the
2292 * pin/unpin/fence and not more.
2293 */
2294 intel_runtime_pm_get(dev_priv);
2295
Maarten Lankhorst7580d772015-08-18 13:40:06 +02002296 ret = i915_gem_object_pin_to_display_plane(obj, alignment,
2297 &view);
Chris Wilson48b956c2010-09-14 12:50:34 +01002298 if (ret)
Maarten Lankhorstb26a6b32015-09-23 13:27:09 +02002299 goto err_pm;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002300
2301 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2302 * fence, whereas 965+ only requires a fence if using
2303 * framebuffer compression. For simplicity, we always install
2304 * a fence as the cost is not that onerous.
2305 */
Vivek Kasireddy98072162015-10-29 18:54:38 -07002306 if (view.type == I915_GGTT_VIEW_NORMAL) {
2307 ret = i915_gem_object_get_fence(obj);
2308 if (ret == -EDEADLK) {
2309 /*
2310 * -EDEADLK means there are no free fences
2311 * no pending flips.
2312 *
2313 * This is propagated to atomic, but it uses
2314 * -EDEADLK to force a locking recovery, so
2315 * change the returned error to -EBUSY.
2316 */
2317 ret = -EBUSY;
2318 goto err_unpin;
2319 } else if (ret)
2320 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002321
Vivek Kasireddy98072162015-10-29 18:54:38 -07002322 i915_gem_object_pin_fence(obj);
2323 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002324
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002325 intel_runtime_pm_put(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002326 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002327
2328err_unpin:
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002329 i915_gem_object_unpin_from_display_plane(obj, &view);
Maarten Lankhorstb26a6b32015-09-23 13:27:09 +02002330err_pm:
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002331 intel_runtime_pm_put(dev_priv);
Chris Wilson48b956c2010-09-14 12:50:34 +01002332 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002333}
2334
Chris Wilsonfb4b8ce2016-04-28 09:56:35 +01002335void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
Chris Wilson1690e1e2011-12-14 13:57:08 +01002336{
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002337 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002338 struct i915_ggtt_view view;
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002339
Matt Roperebcdd392014-07-09 16:22:11 -07002340 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2341
Ville Syrjälä3465c582016-02-15 22:54:43 +02002342 intel_fill_fb_ggtt_view(&view, fb, rotation);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002343
Vivek Kasireddy98072162015-10-29 18:54:38 -07002344 if (view.type == I915_GGTT_VIEW_NORMAL)
2345 i915_gem_object_unpin_fence(obj);
2346
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002347 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002348}
2349
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002350/*
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002351 * Adjust the tile offset by moving the difference into
2352 * the x/y offsets.
2353 *
2354 * Input tile dimensions and pitch must already be
2355 * rotated to match x and y, and in pixel units.
2356 */
2357static u32 intel_adjust_tile_offset(int *x, int *y,
2358 unsigned int tile_width,
2359 unsigned int tile_height,
2360 unsigned int tile_size,
2361 unsigned int pitch_tiles,
2362 u32 old_offset,
2363 u32 new_offset)
2364{
2365 unsigned int tiles;
2366
2367 WARN_ON(old_offset & (tile_size - 1));
2368 WARN_ON(new_offset & (tile_size - 1));
2369 WARN_ON(new_offset > old_offset);
2370
2371 tiles = (old_offset - new_offset) / tile_size;
2372
2373 *y += tiles / pitch_tiles * tile_height;
2374 *x += tiles % pitch_tiles * tile_width;
2375
2376 return new_offset;
2377}
2378
2379/*
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002380 * Computes the linear offset to the base tile and adjusts
2381 * x, y. bytes per pixel is assumed to be a power-of-two.
2382 *
2383 * In the 90/270 rotated case, x and y are assumed
2384 * to be already rotated to match the rotated GTT view, and
2385 * pitch is the tile_height aligned framebuffer height.
2386 */
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002387u32 intel_compute_tile_offset(int *x, int *y,
2388 const struct drm_framebuffer *fb, int plane,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002389 unsigned int pitch,
2390 unsigned int rotation)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002391{
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002392 const struct drm_i915_private *dev_priv = to_i915(fb->dev);
2393 uint64_t fb_modifier = fb->modifier[plane];
2394 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002395 u32 offset, offset_aligned, alignment;
2396
2397 alignment = intel_surf_alignment(dev_priv, fb_modifier);
2398 if (alignment)
2399 alignment--;
2400
Ville Syrjäläb5c65332016-01-12 21:08:31 +02002401 if (fb_modifier != DRM_FORMAT_MOD_NONE) {
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002402 unsigned int tile_size, tile_width, tile_height;
2403 unsigned int tile_rows, tiles, pitch_tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002404
Ville Syrjäläd8433102016-01-12 21:08:35 +02002405 tile_size = intel_tile_size(dev_priv);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002406 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2407 fb_modifier, cpp);
2408
2409 if (intel_rotation_90_or_270(rotation)) {
2410 pitch_tiles = pitch / tile_height;
2411 swap(tile_width, tile_height);
2412 } else {
2413 pitch_tiles = pitch / (tile_width * cpp);
2414 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002415
Ville Syrjäläd8433102016-01-12 21:08:35 +02002416 tile_rows = *y / tile_height;
2417 *y %= tile_height;
Chris Wilsonbc752862013-02-21 20:04:31 +00002418
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002419 tiles = *x / tile_width;
2420 *x %= tile_width;
Ville Syrjäläd8433102016-01-12 21:08:35 +02002421
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002422 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2423 offset_aligned = offset & ~alignment;
Chris Wilsonbc752862013-02-21 20:04:31 +00002424
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002425 intel_adjust_tile_offset(x, y, tile_width, tile_height,
2426 tile_size, pitch_tiles,
2427 offset, offset_aligned);
2428 } else {
Chris Wilsonbc752862013-02-21 20:04:31 +00002429 offset = *y * pitch + *x * cpp;
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002430 offset_aligned = offset & ~alignment;
2431
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002432 *y = (offset & alignment) / pitch;
2433 *x = ((offset & alignment) - *y * pitch) / cpp;
Chris Wilsonbc752862013-02-21 20:04:31 +00002434 }
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002435
2436 return offset_aligned;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002437}
2438
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002439static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002440{
2441 switch (format) {
2442 case DISPPLANE_8BPP:
2443 return DRM_FORMAT_C8;
2444 case DISPPLANE_BGRX555:
2445 return DRM_FORMAT_XRGB1555;
2446 case DISPPLANE_BGRX565:
2447 return DRM_FORMAT_RGB565;
2448 default:
2449 case DISPPLANE_BGRX888:
2450 return DRM_FORMAT_XRGB8888;
2451 case DISPPLANE_RGBX888:
2452 return DRM_FORMAT_XBGR8888;
2453 case DISPPLANE_BGRX101010:
2454 return DRM_FORMAT_XRGB2101010;
2455 case DISPPLANE_RGBX101010:
2456 return DRM_FORMAT_XBGR2101010;
2457 }
2458}
2459
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002460static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2461{
2462 switch (format) {
2463 case PLANE_CTL_FORMAT_RGB_565:
2464 return DRM_FORMAT_RGB565;
2465 default:
2466 case PLANE_CTL_FORMAT_XRGB_8888:
2467 if (rgb_order) {
2468 if (alpha)
2469 return DRM_FORMAT_ABGR8888;
2470 else
2471 return DRM_FORMAT_XBGR8888;
2472 } else {
2473 if (alpha)
2474 return DRM_FORMAT_ARGB8888;
2475 else
2476 return DRM_FORMAT_XRGB8888;
2477 }
2478 case PLANE_CTL_FORMAT_XRGB_2101010:
2479 if (rgb_order)
2480 return DRM_FORMAT_XBGR2101010;
2481 else
2482 return DRM_FORMAT_XRGB2101010;
2483 }
2484}
2485
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002486static bool
Daniel Vetterf6936e22015-03-26 12:17:05 +01002487intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2488 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002489{
2490 struct drm_device *dev = crtc->base.dev;
Paulo Zanoni3badb492015-09-23 12:52:23 -03002491 struct drm_i915_private *dev_priv = to_i915(dev);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002492 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002493 struct drm_i915_gem_object *obj = NULL;
2494 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Damien Lespiau2d140302015-02-05 17:22:18 +00002495 struct drm_framebuffer *fb = &plane_config->fb->base;
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002496 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2497 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2498 PAGE_SIZE);
2499
2500 size_aligned -= base_aligned;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002501
Chris Wilsonff2652e2014-03-10 08:07:02 +00002502 if (plane_config->size == 0)
2503 return false;
2504
Paulo Zanoni3badb492015-09-23 12:52:23 -03002505 /* If the FB is too big, just don't use it since fbdev is not very
2506 * important and we should probably use that space with FBC or other
2507 * features. */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002508 if (size_aligned * 2 > ggtt->stolen_usable_size)
Paulo Zanoni3badb492015-09-23 12:52:23 -03002509 return false;
2510
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002511 mutex_lock(&dev->struct_mutex);
2512
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002513 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2514 base_aligned,
2515 base_aligned,
2516 size_aligned);
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002517 if (!obj) {
2518 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002519 return false;
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002520 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002521
Damien Lespiau49af4492015-01-20 12:51:44 +00002522 obj->tiling_mode = plane_config->tiling;
2523 if (obj->tiling_mode == I915_TILING_X)
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002524 obj->stride = fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002525
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002526 mode_cmd.pixel_format = fb->pixel_format;
2527 mode_cmd.width = fb->width;
2528 mode_cmd.height = fb->height;
2529 mode_cmd.pitches[0] = fb->pitches[0];
Daniel Vetter18c52472015-02-10 17:16:09 +00002530 mode_cmd.modifier[0] = fb->modifier[0];
2531 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002532
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002533 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002534 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002535 DRM_DEBUG_KMS("intel fb init failed\n");
2536 goto out_unref_obj;
2537 }
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002538
Jesse Barnes46f297f2014-03-07 08:57:48 -08002539 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002540
Daniel Vetterf6936e22015-03-26 12:17:05 +01002541 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002542 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002543
2544out_unref_obj:
2545 drm_gem_object_unreference(&obj->base);
2546 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002547 return false;
2548}
2549
Daniel Vetter5a21b662016-05-24 17:13:53 +02002550/* Update plane->state->fb to match plane->fb after driver-internal updates */
2551static void
2552update_state_fb(struct drm_plane *plane)
2553{
2554 if (plane->fb == plane->state->fb)
2555 return;
2556
2557 if (plane->state->fb)
2558 drm_framebuffer_unreference(plane->state->fb);
2559 plane->state->fb = plane->fb;
2560 if (plane->state->fb)
2561 drm_framebuffer_reference(plane->state->fb);
2562}
2563
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002564static void
Daniel Vetterf6936e22015-03-26 12:17:05 +01002565intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2566 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002567{
2568 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnesd9ceb812014-10-09 12:57:43 -07002569 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002570 struct drm_crtc *c;
2571 struct intel_crtc *i;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002572 struct drm_i915_gem_object *obj;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002573 struct drm_plane *primary = intel_crtc->base.primary;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002574 struct drm_plane_state *plane_state = primary->state;
Matt Roper200757f2015-12-03 11:37:36 -08002575 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
2576 struct intel_plane *intel_plane = to_intel_plane(primary);
Matt Roper0a8d8a82015-12-03 11:37:38 -08002577 struct intel_plane_state *intel_state =
2578 to_intel_plane_state(plane_state);
Daniel Vetter88595ac2015-03-26 12:42:24 +01002579 struct drm_framebuffer *fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002580
Damien Lespiau2d140302015-02-05 17:22:18 +00002581 if (!plane_config->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002582 return;
2583
Daniel Vetterf6936e22015-03-26 12:17:05 +01002584 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002585 fb = &plane_config->fb->base;
2586 goto valid_fb;
Damien Lespiauf55548b2015-02-05 18:30:20 +00002587 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002588
Damien Lespiau2d140302015-02-05 17:22:18 +00002589 kfree(plane_config->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002590
2591 /*
2592 * Failed to alloc the obj, check to see if we should share
2593 * an fb with another CRTC instead
2594 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002595 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002596 i = to_intel_crtc(c);
2597
2598 if (c == &intel_crtc->base)
2599 continue;
2600
Matt Roper2ff8fde2014-07-08 07:50:07 -07002601 if (!i->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002602 continue;
2603
Daniel Vetter88595ac2015-03-26 12:42:24 +01002604 fb = c->primary->fb;
2605 if (!fb)
Matt Roper2ff8fde2014-07-08 07:50:07 -07002606 continue;
2607
Daniel Vetter88595ac2015-03-26 12:42:24 +01002608 obj = intel_fb_obj(fb);
Matt Roper2ff8fde2014-07-08 07:50:07 -07002609 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002610 drm_framebuffer_reference(fb);
2611 goto valid_fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002612 }
2613 }
Daniel Vetter88595ac2015-03-26 12:42:24 +01002614
Matt Roper200757f2015-12-03 11:37:36 -08002615 /*
2616 * We've failed to reconstruct the BIOS FB. Current display state
2617 * indicates that the primary plane is visible, but has a NULL FB,
2618 * which will lead to problems later if we don't fix it up. The
2619 * simplest solution is to just disable the primary plane now and
2620 * pretend the BIOS never had it enabled.
2621 */
2622 to_intel_plane_state(plane_state)->visible = false;
2623 crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
Ville Syrjälä2622a082016-03-09 19:07:26 +02002624 intel_pre_disable_primary_noatomic(&intel_crtc->base);
Matt Roper200757f2015-12-03 11:37:36 -08002625 intel_plane->disable_plane(primary, &intel_crtc->base);
2626
Daniel Vetter88595ac2015-03-26 12:42:24 +01002627 return;
2628
2629valid_fb:
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002630 plane_state->src_x = 0;
2631 plane_state->src_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002632 plane_state->src_w = fb->width << 16;
2633 plane_state->src_h = fb->height << 16;
2634
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002635 plane_state->crtc_x = 0;
2636 plane_state->crtc_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002637 plane_state->crtc_w = fb->width;
2638 plane_state->crtc_h = fb->height;
2639
Matt Roper0a8d8a82015-12-03 11:37:38 -08002640 intel_state->src.x1 = plane_state->src_x;
2641 intel_state->src.y1 = plane_state->src_y;
2642 intel_state->src.x2 = plane_state->src_x + plane_state->src_w;
2643 intel_state->src.y2 = plane_state->src_y + plane_state->src_h;
2644 intel_state->dst.x1 = plane_state->crtc_x;
2645 intel_state->dst.y1 = plane_state->crtc_y;
2646 intel_state->dst.x2 = plane_state->crtc_x + plane_state->crtc_w;
2647 intel_state->dst.y2 = plane_state->crtc_y + plane_state->crtc_h;
2648
Daniel Vetter88595ac2015-03-26 12:42:24 +01002649 obj = intel_fb_obj(fb);
2650 if (obj->tiling_mode != I915_TILING_NONE)
2651 dev_priv->preserve_bios_swizzle = true;
2652
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002653 drm_framebuffer_reference(fb);
2654 primary->fb = primary->state->fb = fb;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002655 primary->crtc = primary->state->crtc = &intel_crtc->base;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002656 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
Ville Syrjäläa9ff8712015-06-24 21:59:34 +03002657 obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002658}
2659
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002660static void i9xx_update_primary_plane(struct drm_plane *primary,
2661 const struct intel_crtc_state *crtc_state,
2662 const struct intel_plane_state *plane_state)
Jesse Barnes81255562010-08-02 12:07:50 -07002663{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002664 struct drm_device *dev = primary->dev;
Jesse Barnes81255562010-08-02 12:07:50 -07002665 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002666 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2667 struct drm_framebuffer *fb = plane_state->base.fb;
2668 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Jesse Barnes81255562010-08-02 12:07:50 -07002669 int plane = intel_crtc->plane;
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002670 u32 linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002671 u32 dspcntr;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002672 i915_reg_t reg = DSPCNTR(plane);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002673 unsigned int rotation = plane_state->base.rotation;
Ville Syrjäläac484962016-01-20 21:05:26 +02002674 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002675 int x = plane_state->src.x1 >> 16;
2676 int y = plane_state->src.y1 >> 16;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002677
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002678 dspcntr = DISPPLANE_GAMMA_ENABLE;
2679
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002680 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002681
2682 if (INTEL_INFO(dev)->gen < 4) {
2683 if (intel_crtc->pipe == PIPE_B)
2684 dspcntr |= DISPPLANE_SEL_PIPE_B;
2685
2686 /* pipesrc and dspsize control the size that is scaled from,
2687 * which should always be the user's requested size.
2688 */
2689 I915_WRITE(DSPSIZE(plane),
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002690 ((crtc_state->pipe_src_h - 1) << 16) |
2691 (crtc_state->pipe_src_w - 1));
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002692 I915_WRITE(DSPPOS(plane), 0);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002693 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2694 I915_WRITE(PRIMSIZE(plane),
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002695 ((crtc_state->pipe_src_h - 1) << 16) |
2696 (crtc_state->pipe_src_w - 1));
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002697 I915_WRITE(PRIMPOS(plane), 0);
2698 I915_WRITE(PRIMCNSTALPHA(plane), 0);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002699 }
2700
Ville Syrjälä57779d02012-10-31 17:50:14 +02002701 switch (fb->pixel_format) {
2702 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002703 dspcntr |= DISPPLANE_8BPP;
2704 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002705 case DRM_FORMAT_XRGB1555:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002706 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002707 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002708 case DRM_FORMAT_RGB565:
2709 dspcntr |= DISPPLANE_BGRX565;
2710 break;
2711 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002712 dspcntr |= DISPPLANE_BGRX888;
2713 break;
2714 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002715 dspcntr |= DISPPLANE_RGBX888;
2716 break;
2717 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002718 dspcntr |= DISPPLANE_BGRX101010;
2719 break;
2720 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002721 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002722 break;
2723 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002724 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002725 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002726
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002727 if (INTEL_INFO(dev)->gen >= 4 &&
2728 obj->tiling_mode != I915_TILING_NONE)
2729 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07002730
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002731 if (IS_G4X(dev))
2732 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2733
Ville Syrjäläac484962016-01-20 21:05:26 +02002734 linear_offset = y * fb->pitches[0] + x * cpp;
Jesse Barnes81255562010-08-02 12:07:50 -07002735
Daniel Vetterc2c75132012-07-05 12:17:30 +02002736 if (INTEL_INFO(dev)->gen >= 4) {
2737 intel_crtc->dspaddr_offset =
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002738 intel_compute_tile_offset(&x, &y, fb, 0,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002739 fb->pitches[0], rotation);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002740 linear_offset -= intel_crtc->dspaddr_offset;
2741 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002742 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002743 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002744
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002745 if (rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302746 dspcntr |= DISPPLANE_ROTATE_180;
2747
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002748 x += (crtc_state->pipe_src_w - 1);
2749 y += (crtc_state->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302750
2751 /* Finding the last pixel of the last line of the display
2752 data and adding to linear_offset*/
2753 linear_offset +=
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002754 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
Ville Syrjäläac484962016-01-20 21:05:26 +02002755 (crtc_state->pipe_src_w - 1) * cpp;
Sonika Jindal48404c12014-08-22 14:06:04 +05302756 }
2757
Paulo Zanoni2db33662015-09-14 15:20:03 -03002758 intel_crtc->adjusted_x = x;
2759 intel_crtc->adjusted_y = y;
2760
Sonika Jindal48404c12014-08-22 14:06:04 +05302761 I915_WRITE(reg, dspcntr);
2762
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002763 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002764 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002765 I915_WRITE(DSPSURF(plane),
2766 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002767 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002768 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002769 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002770 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002771 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002772}
2773
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002774static void i9xx_disable_primary_plane(struct drm_plane *primary,
2775 struct drm_crtc *crtc)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002776{
2777 struct drm_device *dev = crtc->dev;
2778 struct drm_i915_private *dev_priv = dev->dev_private;
2779 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002780 int plane = intel_crtc->plane;
2781
2782 I915_WRITE(DSPCNTR(plane), 0);
2783 if (INTEL_INFO(dev_priv)->gen >= 4)
2784 I915_WRITE(DSPSURF(plane), 0);
2785 else
2786 I915_WRITE(DSPADDR(plane), 0);
2787 POSTING_READ(DSPCNTR(plane));
2788}
2789
2790static void ironlake_update_primary_plane(struct drm_plane *primary,
2791 const struct intel_crtc_state *crtc_state,
2792 const struct intel_plane_state *plane_state)
2793{
2794 struct drm_device *dev = primary->dev;
2795 struct drm_i915_private *dev_priv = dev->dev_private;
2796 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2797 struct drm_framebuffer *fb = plane_state->base.fb;
2798 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002799 int plane = intel_crtc->plane;
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002800 u32 linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002801 u32 dspcntr;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002802 i915_reg_t reg = DSPCNTR(plane);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002803 unsigned int rotation = plane_state->base.rotation;
Ville Syrjäläac484962016-01-20 21:05:26 +02002804 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002805 int x = plane_state->src.x1 >> 16;
2806 int y = plane_state->src.y1 >> 16;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002807
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002808 dspcntr = DISPPLANE_GAMMA_ENABLE;
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002809 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002810
2811 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2812 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
2813
Ville Syrjälä57779d02012-10-31 17:50:14 +02002814 switch (fb->pixel_format) {
2815 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002816 dspcntr |= DISPPLANE_8BPP;
2817 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002818 case DRM_FORMAT_RGB565:
2819 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002820 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002821 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002822 dspcntr |= DISPPLANE_BGRX888;
2823 break;
2824 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002825 dspcntr |= DISPPLANE_RGBX888;
2826 break;
2827 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002828 dspcntr |= DISPPLANE_BGRX101010;
2829 break;
2830 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002831 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002832 break;
2833 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002834 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002835 }
2836
2837 if (obj->tiling_mode != I915_TILING_NONE)
2838 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002839
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002840 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002841 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002842
Ville Syrjäläac484962016-01-20 21:05:26 +02002843 linear_offset = y * fb->pitches[0] + x * cpp;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002844 intel_crtc->dspaddr_offset =
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002845 intel_compute_tile_offset(&x, &y, fb, 0,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002846 fb->pitches[0], rotation);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002847 linear_offset -= intel_crtc->dspaddr_offset;
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002848 if (rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302849 dspcntr |= DISPPLANE_ROTATE_180;
2850
2851 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002852 x += (crtc_state->pipe_src_w - 1);
2853 y += (crtc_state->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302854
2855 /* Finding the last pixel of the last line of the display
2856 data and adding to linear_offset*/
2857 linear_offset +=
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002858 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
Ville Syrjäläac484962016-01-20 21:05:26 +02002859 (crtc_state->pipe_src_w - 1) * cpp;
Sonika Jindal48404c12014-08-22 14:06:04 +05302860 }
2861 }
2862
Paulo Zanoni2db33662015-09-14 15:20:03 -03002863 intel_crtc->adjusted_x = x;
2864 intel_crtc->adjusted_y = y;
2865
Sonika Jindal48404c12014-08-22 14:06:04 +05302866 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002867
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002868 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002869 I915_WRITE(DSPSURF(plane),
2870 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002871 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002872 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2873 } else {
2874 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2875 I915_WRITE(DSPLINOFF(plane), linear_offset);
2876 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002877 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002878}
2879
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002880u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
2881 uint64_t fb_modifier, uint32_t pixel_format)
Damien Lespiaub3218032015-02-27 11:15:18 +00002882{
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002883 if (fb_modifier == DRM_FORMAT_MOD_NONE) {
2884 return 64;
2885 } else {
2886 int cpp = drm_format_plane_cpp(pixel_format, 0);
Damien Lespiaub3218032015-02-27 11:15:18 +00002887
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002888 return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
Damien Lespiaub3218032015-02-27 11:15:18 +00002889 }
2890}
2891
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002892u32 intel_plane_obj_offset(struct intel_plane *intel_plane,
2893 struct drm_i915_gem_object *obj,
2894 unsigned int plane)
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002895{
Daniel Vetterce7f1722015-10-14 16:51:06 +02002896 struct i915_ggtt_view view;
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002897 struct i915_vma *vma;
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002898 u64 offset;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002899
Ville Syrjäläe7941292016-01-19 18:23:17 +02002900 intel_fill_fb_ggtt_view(&view, intel_plane->base.state->fb,
Ville Syrjälä3465c582016-02-15 22:54:43 +02002901 intel_plane->base.state->rotation);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002902
Daniel Vetterce7f1722015-10-14 16:51:06 +02002903 vma = i915_gem_obj_to_ggtt_view(obj, &view);
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002904 if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
Daniel Vetterce7f1722015-10-14 16:51:06 +02002905 view.type))
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002906 return -1;
2907
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002908 offset = vma->node.start;
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002909
2910 if (plane == 1) {
Ville Syrjälä7723f47d2016-01-20 21:05:22 +02002911 offset += vma->ggtt_view.params.rotated.uv_start_page *
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002912 PAGE_SIZE;
2913 }
2914
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002915 WARN_ON(upper_32_bits(offset));
2916
2917 return lower_32_bits(offset);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002918}
2919
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002920static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
2921{
2922 struct drm_device *dev = intel_crtc->base.dev;
2923 struct drm_i915_private *dev_priv = dev->dev_private;
2924
2925 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
2926 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
2927 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002928}
2929
Chandra Kondurua1b22782015-04-07 15:28:45 -07002930/*
2931 * This function detaches (aka. unbinds) unused scalers in hardware
2932 */
Maarten Lankhorst05832362015-06-15 12:33:48 +02002933static void skl_detach_scalers(struct intel_crtc *intel_crtc)
Chandra Kondurua1b22782015-04-07 15:28:45 -07002934{
Chandra Kondurua1b22782015-04-07 15:28:45 -07002935 struct intel_crtc_scaler_state *scaler_state;
2936 int i;
2937
Chandra Kondurua1b22782015-04-07 15:28:45 -07002938 scaler_state = &intel_crtc->config->scaler_state;
2939
2940 /* loop through and disable scalers that aren't in use */
2941 for (i = 0; i < intel_crtc->num_scalers; i++) {
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002942 if (!scaler_state->scalers[i].in_use)
2943 skl_detach_scaler(intel_crtc, i);
Chandra Kondurua1b22782015-04-07 15:28:45 -07002944 }
2945}
2946
Chandra Konduru6156a452015-04-27 13:48:39 -07002947u32 skl_plane_ctl_format(uint32_t pixel_format)
2948{
Chandra Konduru6156a452015-04-27 13:48:39 -07002949 switch (pixel_format) {
Damien Lespiaud161cf72015-05-12 16:13:17 +01002950 case DRM_FORMAT_C8:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002951 return PLANE_CTL_FORMAT_INDEXED;
Chandra Konduru6156a452015-04-27 13:48:39 -07002952 case DRM_FORMAT_RGB565:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002953 return PLANE_CTL_FORMAT_RGB_565;
Chandra Konduru6156a452015-04-27 13:48:39 -07002954 case DRM_FORMAT_XBGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002955 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
Chandra Konduru6156a452015-04-27 13:48:39 -07002956 case DRM_FORMAT_XRGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002957 return PLANE_CTL_FORMAT_XRGB_8888;
Chandra Konduru6156a452015-04-27 13:48:39 -07002958 /*
2959 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2960 * to be already pre-multiplied. We need to add a knob (or a different
2961 * DRM_FORMAT) for user-space to configure that.
2962 */
2963 case DRM_FORMAT_ABGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002964 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
Chandra Konduru6156a452015-04-27 13:48:39 -07002965 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002966 case DRM_FORMAT_ARGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002967 return PLANE_CTL_FORMAT_XRGB_8888 |
Chandra Konduru6156a452015-04-27 13:48:39 -07002968 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002969 case DRM_FORMAT_XRGB2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002970 return PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002971 case DRM_FORMAT_XBGR2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002972 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002973 case DRM_FORMAT_YUYV:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002974 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
Chandra Konduru6156a452015-04-27 13:48:39 -07002975 case DRM_FORMAT_YVYU:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002976 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
Chandra Konduru6156a452015-04-27 13:48:39 -07002977 case DRM_FORMAT_UYVY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002978 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002979 case DRM_FORMAT_VYUY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002980 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002981 default:
Damien Lespiau4249eee2015-05-12 16:13:16 +01002982 MISSING_CASE(pixel_format);
Chandra Konduru6156a452015-04-27 13:48:39 -07002983 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002984
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002985 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002986}
2987
2988u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
2989{
Chandra Konduru6156a452015-04-27 13:48:39 -07002990 switch (fb_modifier) {
2991 case DRM_FORMAT_MOD_NONE:
2992 break;
2993 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002994 return PLANE_CTL_TILED_X;
Chandra Konduru6156a452015-04-27 13:48:39 -07002995 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002996 return PLANE_CTL_TILED_Y;
Chandra Konduru6156a452015-04-27 13:48:39 -07002997 case I915_FORMAT_MOD_Yf_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002998 return PLANE_CTL_TILED_YF;
Chandra Konduru6156a452015-04-27 13:48:39 -07002999 default:
3000 MISSING_CASE(fb_modifier);
3001 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01003002
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003003 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003004}
3005
3006u32 skl_plane_ctl_rotation(unsigned int rotation)
3007{
Chandra Konduru6156a452015-04-27 13:48:39 -07003008 switch (rotation) {
3009 case BIT(DRM_ROTATE_0):
3010 break;
Sonika Jindal1e8df162015-05-20 13:40:48 +05303011 /*
3012 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
3013 * while i915 HW rotation is clockwise, thats why this swapping.
3014 */
Chandra Konduru6156a452015-04-27 13:48:39 -07003015 case BIT(DRM_ROTATE_90):
Sonika Jindal1e8df162015-05-20 13:40:48 +05303016 return PLANE_CTL_ROTATE_270;
Chandra Konduru6156a452015-04-27 13:48:39 -07003017 case BIT(DRM_ROTATE_180):
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003018 return PLANE_CTL_ROTATE_180;
Chandra Konduru6156a452015-04-27 13:48:39 -07003019 case BIT(DRM_ROTATE_270):
Sonika Jindal1e8df162015-05-20 13:40:48 +05303020 return PLANE_CTL_ROTATE_90;
Chandra Konduru6156a452015-04-27 13:48:39 -07003021 default:
3022 MISSING_CASE(rotation);
3023 }
3024
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003025 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003026}
3027
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003028static void skylake_update_primary_plane(struct drm_plane *plane,
3029 const struct intel_crtc_state *crtc_state,
3030 const struct intel_plane_state *plane_state)
Damien Lespiau70d21f02013-07-03 21:06:04 +01003031{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003032 struct drm_device *dev = plane->dev;
Damien Lespiau70d21f02013-07-03 21:06:04 +01003033 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003034 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3035 struct drm_framebuffer *fb = plane_state->base.fb;
3036 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003037 int pipe = intel_crtc->pipe;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303038 u32 plane_ctl, stride_div, stride;
3039 u32 tile_height, plane_offset, plane_size;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003040 unsigned int rotation = plane_state->base.rotation;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303041 int x_offset, y_offset;
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02003042 u32 surf_addr;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003043 int scaler_id = plane_state->scaler_id;
3044 int src_x = plane_state->src.x1 >> 16;
3045 int src_y = plane_state->src.y1 >> 16;
3046 int src_w = drm_rect_width(&plane_state->src) >> 16;
3047 int src_h = drm_rect_height(&plane_state->src) >> 16;
3048 int dst_x = plane_state->dst.x1;
3049 int dst_y = plane_state->dst.y1;
3050 int dst_w = drm_rect_width(&plane_state->dst);
3051 int dst_h = drm_rect_height(&plane_state->dst);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003052
3053 plane_ctl = PLANE_CTL_ENABLE |
3054 PLANE_CTL_PIPE_GAMMA_ENABLE |
3055 PLANE_CTL_PIPE_CSC_ENABLE;
3056
Chandra Konduru6156a452015-04-27 13:48:39 -07003057 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3058 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003059 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Chandra Konduru6156a452015-04-27 13:48:39 -07003060 plane_ctl |= skl_plane_ctl_rotation(rotation);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003061
Ville Syrjälä7b49f942016-01-12 21:08:32 +02003062 stride_div = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
Damien Lespiaub3218032015-02-27 11:15:18 +00003063 fb->pixel_format);
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01003064 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj, 0);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303065
Paulo Zanonia42e5a22015-09-30 17:05:43 -03003066 WARN_ON(drm_rect_width(&plane_state->src) == 0);
Chandra Konduru6156a452015-04-27 13:48:39 -07003067
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303068 if (intel_rotation_90_or_270(rotation)) {
Ville Syrjälä832be822016-01-12 21:08:33 +02003069 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
3070
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303071 /* stride = Surface height in tiles */
Ville Syrjälä832be822016-01-12 21:08:33 +02003072 tile_height = intel_tile_height(dev_priv, fb->modifier[0], cpp);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303073 stride = DIV_ROUND_UP(fb->height, tile_height);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003074 x_offset = stride * tile_height - src_y - src_h;
3075 y_offset = src_x;
Chandra Konduru6156a452015-04-27 13:48:39 -07003076 plane_size = (src_w - 1) << 16 | (src_h - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303077 } else {
3078 stride = fb->pitches[0] / stride_div;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003079 x_offset = src_x;
3080 y_offset = src_y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003081 plane_size = (src_h - 1) << 16 | (src_w - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303082 }
3083 plane_offset = y_offset << 16 | x_offset;
Damien Lespiaub3218032015-02-27 11:15:18 +00003084
Paulo Zanoni2db33662015-09-14 15:20:03 -03003085 intel_crtc->adjusted_x = x_offset;
3086 intel_crtc->adjusted_y = y_offset;
3087
Damien Lespiau70d21f02013-07-03 21:06:04 +01003088 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303089 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3090 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3091 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
Chandra Konduru6156a452015-04-27 13:48:39 -07003092
3093 if (scaler_id >= 0) {
3094 uint32_t ps_ctrl = 0;
3095
3096 WARN_ON(!dst_w || !dst_h);
3097 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3098 crtc_state->scaler_state.scalers[scaler_id].mode;
3099 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3100 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3101 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3102 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3103 I915_WRITE(PLANE_POS(pipe, 0), 0);
3104 } else {
3105 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3106 }
3107
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003108 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003109
3110 POSTING_READ(PLANE_SURF(pipe, 0));
3111}
3112
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003113static void skylake_disable_primary_plane(struct drm_plane *primary,
3114 struct drm_crtc *crtc)
3115{
3116 struct drm_device *dev = crtc->dev;
3117 struct drm_i915_private *dev_priv = dev->dev_private;
3118 int pipe = to_intel_crtc(crtc)->pipe;
3119
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003120 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3121 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3122 POSTING_READ(PLANE_SURF(pipe, 0));
3123}
3124
Jesse Barnes17638cd2011-06-24 12:19:23 -07003125/* Assume fb object is pinned & idle & fenced and just update base pointers */
3126static int
3127intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3128 int x, int y, enum mode_set_atomic state)
3129{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003130 /* Support for kgdboc is disabled, this needs a major rework. */
3131 DRM_ERROR("legacy panic handler not supported any more.\n");
Jesse Barnes17638cd2011-06-24 12:19:23 -07003132
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003133 return -ENODEV;
Jesse Barnes81255562010-08-02 12:07:50 -07003134}
3135
Daniel Vetter5a21b662016-05-24 17:13:53 +02003136static void intel_complete_page_flips(struct drm_i915_private *dev_priv)
3137{
3138 struct intel_crtc *crtc;
3139
3140 for_each_intel_crtc(dev_priv->dev, crtc)
3141 intel_finish_page_flip_cs(dev_priv, crtc->pipe);
3142}
3143
Ville Syrjälä75147472014-11-24 18:28:11 +02003144static void intel_update_primary_planes(struct drm_device *dev)
3145{
Ville Syrjälä75147472014-11-24 18:28:11 +02003146 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003147
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003148 for_each_crtc(dev, crtc) {
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003149 struct intel_plane *plane = to_intel_plane(crtc->primary);
3150 struct intel_plane_state *plane_state;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003151
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003152 drm_modeset_lock_crtc(crtc, &plane->base);
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003153 plane_state = to_intel_plane_state(plane->base.state);
3154
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003155 if (plane_state->visible)
3156 plane->update_plane(&plane->base,
3157 to_intel_crtc_state(crtc->state),
3158 plane_state);
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003159
3160 drm_modeset_unlock_crtc(crtc);
Ville Syrjälä96a02912013-02-18 19:08:49 +02003161 }
3162}
3163
Chris Wilsonc0336662016-05-06 15:40:21 +01003164void intel_prepare_reset(struct drm_i915_private *dev_priv)
Ville Syrjälä75147472014-11-24 18:28:11 +02003165{
3166 /* no reset support for gen2 */
Chris Wilsonc0336662016-05-06 15:40:21 +01003167 if (IS_GEN2(dev_priv))
Ville Syrjälä75147472014-11-24 18:28:11 +02003168 return;
3169
3170 /* reset doesn't touch the display */
Chris Wilsonc0336662016-05-06 15:40:21 +01003171 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
Ville Syrjälä75147472014-11-24 18:28:11 +02003172 return;
3173
Chris Wilsonc0336662016-05-06 15:40:21 +01003174 drm_modeset_lock_all(dev_priv->dev);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003175 /*
3176 * Disabling the crtcs gracefully seems nicer. Also the
3177 * g33 docs say we should at least disable all the planes.
3178 */
Chris Wilsonc0336662016-05-06 15:40:21 +01003179 intel_display_suspend(dev_priv->dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003180}
3181
Chris Wilsonc0336662016-05-06 15:40:21 +01003182void intel_finish_reset(struct drm_i915_private *dev_priv)
Ville Syrjälä75147472014-11-24 18:28:11 +02003183{
Daniel Vetter5a21b662016-05-24 17:13:53 +02003184 /*
3185 * Flips in the rings will be nuked by the reset,
3186 * so complete all pending flips so that user space
3187 * will get its events and not get stuck.
3188 */
3189 intel_complete_page_flips(dev_priv);
3190
Ville Syrjälä75147472014-11-24 18:28:11 +02003191 /* no reset support for gen2 */
Chris Wilsonc0336662016-05-06 15:40:21 +01003192 if (IS_GEN2(dev_priv))
Ville Syrjälä75147472014-11-24 18:28:11 +02003193 return;
3194
3195 /* reset doesn't touch the display */
Chris Wilsonc0336662016-05-06 15:40:21 +01003196 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv)) {
Ville Syrjälä75147472014-11-24 18:28:11 +02003197 /*
3198 * Flips in the rings have been nuked by the reset,
3199 * so update the base address of all primary
3200 * planes to the the last fb to make sure we're
3201 * showing the correct fb after a reset.
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003202 *
3203 * FIXME: Atomic will make this obsolete since we won't schedule
3204 * CS-based flips (which might get lost in gpu resets) any more.
Ville Syrjälä75147472014-11-24 18:28:11 +02003205 */
Chris Wilsonc0336662016-05-06 15:40:21 +01003206 intel_update_primary_planes(dev_priv->dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003207 return;
3208 }
3209
3210 /*
3211 * The display has been reset as well,
3212 * so need a full re-initialization.
3213 */
3214 intel_runtime_pm_disable_interrupts(dev_priv);
3215 intel_runtime_pm_enable_interrupts(dev_priv);
3216
Chris Wilsonc0336662016-05-06 15:40:21 +01003217 intel_modeset_init_hw(dev_priv->dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003218
3219 spin_lock_irq(&dev_priv->irq_lock);
3220 if (dev_priv->display.hpd_irq_setup)
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01003221 dev_priv->display.hpd_irq_setup(dev_priv);
Ville Syrjälä75147472014-11-24 18:28:11 +02003222 spin_unlock_irq(&dev_priv->irq_lock);
3223
Chris Wilsonc0336662016-05-06 15:40:21 +01003224 intel_display_resume(dev_priv->dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003225
3226 intel_hpd_init(dev_priv);
3227
Chris Wilsonc0336662016-05-06 15:40:21 +01003228 drm_modeset_unlock_all(dev_priv->dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003229}
3230
Chris Wilson7d5e3792014-03-04 13:15:08 +00003231static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3232{
Daniel Vetter5a21b662016-05-24 17:13:53 +02003233 struct drm_device *dev = crtc->dev;
3234 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3235 unsigned reset_counter;
3236 bool pending;
3237
3238 reset_counter = i915_reset_counter(&to_i915(dev)->gpu_error);
3239 if (intel_crtc->reset_counter != reset_counter)
3240 return false;
3241
3242 spin_lock_irq(&dev->event_lock);
3243 pending = to_intel_crtc(crtc)->flip_work != NULL;
3244 spin_unlock_irq(&dev->event_lock);
3245
3246 return pending;
Chris Wilson7d5e3792014-03-04 13:15:08 +00003247}
3248
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003249static void intel_update_pipe_config(struct intel_crtc *crtc,
3250 struct intel_crtc_state *old_crtc_state)
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003251{
3252 struct drm_device *dev = crtc->base.dev;
3253 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003254 struct intel_crtc_state *pipe_config =
3255 to_intel_crtc_state(crtc->base.state);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003256
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003257 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
3258 crtc->base.mode = crtc->base.state->mode;
3259
3260 DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
3261 old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
3262 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003263
3264 /*
3265 * Update pipe size and adjust fitter if needed: the reason for this is
3266 * that in compute_mode_changes we check the native mode (not the pfit
3267 * mode) to see if we can flip rather than do a full mode set. In the
3268 * fastboot case, we'll flip, but if we don't update the pipesrc and
3269 * pfit state, we'll end up with a big fb scanned out into the wrong
3270 * sized surface.
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003271 */
3272
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003273 I915_WRITE(PIPESRC(crtc->pipe),
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003274 ((pipe_config->pipe_src_w - 1) << 16) |
3275 (pipe_config->pipe_src_h - 1));
3276
3277 /* on skylake this is done by detaching scalers */
3278 if (INTEL_INFO(dev)->gen >= 9) {
3279 skl_detach_scalers(crtc);
3280
3281 if (pipe_config->pch_pfit.enabled)
3282 skylake_pfit_enable(crtc);
3283 } else if (HAS_PCH_SPLIT(dev)) {
3284 if (pipe_config->pch_pfit.enabled)
3285 ironlake_pfit_enable(crtc);
3286 else if (old_crtc_state->pch_pfit.enabled)
3287 ironlake_pfit_disable(crtc, true);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003288 }
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003289}
3290
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003291static void intel_fdi_normal_train(struct drm_crtc *crtc)
3292{
3293 struct drm_device *dev = crtc->dev;
3294 struct drm_i915_private *dev_priv = dev->dev_private;
3295 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3296 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003297 i915_reg_t reg;
3298 u32 temp;
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003299
3300 /* enable normal train */
3301 reg = FDI_TX_CTL(pipe);
3302 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07003303 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003304 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3305 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003306 } else {
3307 temp &= ~FDI_LINK_TRAIN_NONE;
3308 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003309 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003310 I915_WRITE(reg, temp);
3311
3312 reg = FDI_RX_CTL(pipe);
3313 temp = I915_READ(reg);
3314 if (HAS_PCH_CPT(dev)) {
3315 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3316 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3317 } else {
3318 temp &= ~FDI_LINK_TRAIN_NONE;
3319 temp |= FDI_LINK_TRAIN_NONE;
3320 }
3321 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3322
3323 /* wait one idle pattern time */
3324 POSTING_READ(reg);
3325 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003326
3327 /* IVB wants error correction enabled */
3328 if (IS_IVYBRIDGE(dev))
3329 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3330 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003331}
3332
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003333/* The FDI link training functions for ILK/Ibexpeak. */
3334static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3335{
3336 struct drm_device *dev = crtc->dev;
3337 struct drm_i915_private *dev_priv = dev->dev_private;
3338 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3339 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003340 i915_reg_t reg;
3341 u32 temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003342
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003343 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003344 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003345
Adam Jacksone1a44742010-06-25 15:32:14 -04003346 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3347 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003348 reg = FDI_RX_IMR(pipe);
3349 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003350 temp &= ~FDI_RX_SYMBOL_LOCK;
3351 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003352 I915_WRITE(reg, temp);
3353 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003354 udelay(150);
3355
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003356 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003357 reg = FDI_TX_CTL(pipe);
3358 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003359 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003360 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003361 temp &= ~FDI_LINK_TRAIN_NONE;
3362 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003363 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003364
Chris Wilson5eddb702010-09-11 13:48:45 +01003365 reg = FDI_RX_CTL(pipe);
3366 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003367 temp &= ~FDI_LINK_TRAIN_NONE;
3368 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003369 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3370
3371 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003372 udelay(150);
3373
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003374 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003375 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3376 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3377 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003378
Chris Wilson5eddb702010-09-11 13:48:45 +01003379 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003380 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003381 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003382 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3383
3384 if ((temp & FDI_RX_BIT_LOCK)) {
3385 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003386 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003387 break;
3388 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003389 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003390 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003391 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003392
3393 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003394 reg = FDI_TX_CTL(pipe);
3395 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003396 temp &= ~FDI_LINK_TRAIN_NONE;
3397 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003398 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003399
Chris Wilson5eddb702010-09-11 13:48:45 +01003400 reg = FDI_RX_CTL(pipe);
3401 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003402 temp &= ~FDI_LINK_TRAIN_NONE;
3403 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003404 I915_WRITE(reg, temp);
3405
3406 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003407 udelay(150);
3408
Chris Wilson5eddb702010-09-11 13:48:45 +01003409 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003410 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003411 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003412 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3413
3414 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003415 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003416 DRM_DEBUG_KMS("FDI train 2 done.\n");
3417 break;
3418 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003419 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003420 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003421 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003422
3423 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003424
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003425}
3426
Akshay Joshi0206e352011-08-16 15:34:10 -04003427static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003428 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3429 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3430 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3431 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3432};
3433
3434/* The FDI link training functions for SNB/Cougarpoint. */
3435static void gen6_fdi_link_train(struct drm_crtc *crtc)
3436{
3437 struct drm_device *dev = crtc->dev;
3438 struct drm_i915_private *dev_priv = dev->dev_private;
3439 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3440 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003441 i915_reg_t reg;
3442 u32 temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003443
Adam Jacksone1a44742010-06-25 15:32:14 -04003444 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3445 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003446 reg = FDI_RX_IMR(pipe);
3447 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003448 temp &= ~FDI_RX_SYMBOL_LOCK;
3449 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003450 I915_WRITE(reg, temp);
3451
3452 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003453 udelay(150);
3454
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003455 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003456 reg = FDI_TX_CTL(pipe);
3457 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003458 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003459 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003460 temp &= ~FDI_LINK_TRAIN_NONE;
3461 temp |= FDI_LINK_TRAIN_PATTERN_1;
3462 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3463 /* SNB-B */
3464 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003465 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003466
Daniel Vetterd74cf322012-10-26 10:58:13 +02003467 I915_WRITE(FDI_RX_MISC(pipe),
3468 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3469
Chris Wilson5eddb702010-09-11 13:48:45 +01003470 reg = FDI_RX_CTL(pipe);
3471 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003472 if (HAS_PCH_CPT(dev)) {
3473 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3474 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3475 } else {
3476 temp &= ~FDI_LINK_TRAIN_NONE;
3477 temp |= FDI_LINK_TRAIN_PATTERN_1;
3478 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003479 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3480
3481 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003482 udelay(150);
3483
Akshay Joshi0206e352011-08-16 15:34:10 -04003484 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003485 reg = FDI_TX_CTL(pipe);
3486 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003487 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3488 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003489 I915_WRITE(reg, temp);
3490
3491 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003492 udelay(500);
3493
Sean Paulfa37d392012-03-02 12:53:39 -05003494 for (retry = 0; retry < 5; retry++) {
3495 reg = FDI_RX_IIR(pipe);
3496 temp = I915_READ(reg);
3497 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3498 if (temp & FDI_RX_BIT_LOCK) {
3499 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3500 DRM_DEBUG_KMS("FDI train 1 done.\n");
3501 break;
3502 }
3503 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003504 }
Sean Paulfa37d392012-03-02 12:53:39 -05003505 if (retry < 5)
3506 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003507 }
3508 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003509 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003510
3511 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003512 reg = FDI_TX_CTL(pipe);
3513 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003514 temp &= ~FDI_LINK_TRAIN_NONE;
3515 temp |= FDI_LINK_TRAIN_PATTERN_2;
3516 if (IS_GEN6(dev)) {
3517 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3518 /* SNB-B */
3519 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3520 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003521 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003522
Chris Wilson5eddb702010-09-11 13:48:45 +01003523 reg = FDI_RX_CTL(pipe);
3524 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003525 if (HAS_PCH_CPT(dev)) {
3526 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3527 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3528 } else {
3529 temp &= ~FDI_LINK_TRAIN_NONE;
3530 temp |= FDI_LINK_TRAIN_PATTERN_2;
3531 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003532 I915_WRITE(reg, temp);
3533
3534 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003535 udelay(150);
3536
Akshay Joshi0206e352011-08-16 15:34:10 -04003537 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003538 reg = FDI_TX_CTL(pipe);
3539 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003540 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3541 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003542 I915_WRITE(reg, temp);
3543
3544 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003545 udelay(500);
3546
Sean Paulfa37d392012-03-02 12:53:39 -05003547 for (retry = 0; retry < 5; retry++) {
3548 reg = FDI_RX_IIR(pipe);
3549 temp = I915_READ(reg);
3550 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3551 if (temp & FDI_RX_SYMBOL_LOCK) {
3552 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3553 DRM_DEBUG_KMS("FDI train 2 done.\n");
3554 break;
3555 }
3556 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003557 }
Sean Paulfa37d392012-03-02 12:53:39 -05003558 if (retry < 5)
3559 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003560 }
3561 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003562 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003563
3564 DRM_DEBUG_KMS("FDI train done.\n");
3565}
3566
Jesse Barnes357555c2011-04-28 15:09:55 -07003567/* Manual link training for Ivy Bridge A0 parts */
3568static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3569{
3570 struct drm_device *dev = crtc->dev;
3571 struct drm_i915_private *dev_priv = dev->dev_private;
3572 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3573 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003574 i915_reg_t reg;
3575 u32 temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003576
3577 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3578 for train result */
3579 reg = FDI_RX_IMR(pipe);
3580 temp = I915_READ(reg);
3581 temp &= ~FDI_RX_SYMBOL_LOCK;
3582 temp &= ~FDI_RX_BIT_LOCK;
3583 I915_WRITE(reg, temp);
3584
3585 POSTING_READ(reg);
3586 udelay(150);
3587
Daniel Vetter01a415f2012-10-27 15:58:40 +02003588 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3589 I915_READ(FDI_RX_IIR(pipe)));
3590
Jesse Barnes139ccd32013-08-19 11:04:55 -07003591 /* Try each vswing and preemphasis setting twice before moving on */
3592 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3593 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003594 reg = FDI_TX_CTL(pipe);
3595 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003596 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3597 temp &= ~FDI_TX_ENABLE;
3598 I915_WRITE(reg, temp);
3599
3600 reg = FDI_RX_CTL(pipe);
3601 temp = I915_READ(reg);
3602 temp &= ~FDI_LINK_TRAIN_AUTO;
3603 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3604 temp &= ~FDI_RX_ENABLE;
3605 I915_WRITE(reg, temp);
3606
3607 /* enable CPU FDI TX and PCH FDI RX */
3608 reg = FDI_TX_CTL(pipe);
3609 temp = I915_READ(reg);
3610 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003611 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003612 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003613 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003614 temp |= snb_b_fdi_train_param[j/2];
3615 temp |= FDI_COMPOSITE_SYNC;
3616 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3617
3618 I915_WRITE(FDI_RX_MISC(pipe),
3619 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3620
3621 reg = FDI_RX_CTL(pipe);
3622 temp = I915_READ(reg);
3623 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3624 temp |= FDI_COMPOSITE_SYNC;
3625 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3626
3627 POSTING_READ(reg);
3628 udelay(1); /* should be 0.5us */
3629
3630 for (i = 0; i < 4; i++) {
3631 reg = FDI_RX_IIR(pipe);
3632 temp = I915_READ(reg);
3633 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3634
3635 if (temp & FDI_RX_BIT_LOCK ||
3636 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3637 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3638 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3639 i);
3640 break;
3641 }
3642 udelay(1); /* should be 0.5us */
3643 }
3644 if (i == 4) {
3645 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3646 continue;
3647 }
3648
3649 /* Train 2 */
3650 reg = FDI_TX_CTL(pipe);
3651 temp = I915_READ(reg);
3652 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3653 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3654 I915_WRITE(reg, temp);
3655
3656 reg = FDI_RX_CTL(pipe);
3657 temp = I915_READ(reg);
3658 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3659 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003660 I915_WRITE(reg, temp);
3661
3662 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003663 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003664
Jesse Barnes139ccd32013-08-19 11:04:55 -07003665 for (i = 0; i < 4; i++) {
3666 reg = FDI_RX_IIR(pipe);
3667 temp = I915_READ(reg);
3668 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003669
Jesse Barnes139ccd32013-08-19 11:04:55 -07003670 if (temp & FDI_RX_SYMBOL_LOCK ||
3671 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3672 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3673 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3674 i);
3675 goto train_done;
3676 }
3677 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003678 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003679 if (i == 4)
3680 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003681 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003682
Jesse Barnes139ccd32013-08-19 11:04:55 -07003683train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003684 DRM_DEBUG_KMS("FDI train done.\n");
3685}
3686
Daniel Vetter88cefb62012-08-12 19:27:14 +02003687static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003688{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003689 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003690 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003691 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003692 i915_reg_t reg;
3693 u32 temp;
Jesse Barnesc64e3112010-09-10 11:27:03 -07003694
Jesse Barnes0e23b992010-09-10 11:10:00 -07003695 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003696 reg = FDI_RX_CTL(pipe);
3697 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003698 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003699 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003700 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003701 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3702
3703 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003704 udelay(200);
3705
3706 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003707 temp = I915_READ(reg);
3708 I915_WRITE(reg, temp | FDI_PCDCLK);
3709
3710 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003711 udelay(200);
3712
Paulo Zanoni20749732012-11-23 15:30:38 -02003713 /* Enable CPU FDI TX PLL, always on for Ironlake */
3714 reg = FDI_TX_CTL(pipe);
3715 temp = I915_READ(reg);
3716 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3717 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003718
Paulo Zanoni20749732012-11-23 15:30:38 -02003719 POSTING_READ(reg);
3720 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003721 }
3722}
3723
Daniel Vetter88cefb62012-08-12 19:27:14 +02003724static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3725{
3726 struct drm_device *dev = intel_crtc->base.dev;
3727 struct drm_i915_private *dev_priv = dev->dev_private;
3728 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003729 i915_reg_t reg;
3730 u32 temp;
Daniel Vetter88cefb62012-08-12 19:27:14 +02003731
3732 /* Switch from PCDclk to Rawclk */
3733 reg = FDI_RX_CTL(pipe);
3734 temp = I915_READ(reg);
3735 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3736
3737 /* Disable CPU FDI TX PLL */
3738 reg = FDI_TX_CTL(pipe);
3739 temp = I915_READ(reg);
3740 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3741
3742 POSTING_READ(reg);
3743 udelay(100);
3744
3745 reg = FDI_RX_CTL(pipe);
3746 temp = I915_READ(reg);
3747 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3748
3749 /* Wait for the clocks to turn off. */
3750 POSTING_READ(reg);
3751 udelay(100);
3752}
3753
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003754static void ironlake_fdi_disable(struct drm_crtc *crtc)
3755{
3756 struct drm_device *dev = crtc->dev;
3757 struct drm_i915_private *dev_priv = dev->dev_private;
3758 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3759 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003760 i915_reg_t reg;
3761 u32 temp;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003762
3763 /* disable CPU FDI tx and PCH FDI rx */
3764 reg = FDI_TX_CTL(pipe);
3765 temp = I915_READ(reg);
3766 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3767 POSTING_READ(reg);
3768
3769 reg = FDI_RX_CTL(pipe);
3770 temp = I915_READ(reg);
3771 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003772 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003773 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3774
3775 POSTING_READ(reg);
3776 udelay(100);
3777
3778 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003779 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003780 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003781
3782 /* still set train pattern 1 */
3783 reg = FDI_TX_CTL(pipe);
3784 temp = I915_READ(reg);
3785 temp &= ~FDI_LINK_TRAIN_NONE;
3786 temp |= FDI_LINK_TRAIN_PATTERN_1;
3787 I915_WRITE(reg, temp);
3788
3789 reg = FDI_RX_CTL(pipe);
3790 temp = I915_READ(reg);
3791 if (HAS_PCH_CPT(dev)) {
3792 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3793 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3794 } else {
3795 temp &= ~FDI_LINK_TRAIN_NONE;
3796 temp |= FDI_LINK_TRAIN_PATTERN_1;
3797 }
3798 /* BPC in FDI rx is consistent with that in PIPECONF */
3799 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003800 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003801 I915_WRITE(reg, temp);
3802
3803 POSTING_READ(reg);
3804 udelay(100);
3805}
3806
Chris Wilson5dce5b932014-01-20 10:17:36 +00003807bool intel_has_pending_fb_unpin(struct drm_device *dev)
3808{
3809 struct intel_crtc *crtc;
3810
3811 /* Note that we don't need to be called with mode_config.lock here
3812 * as our list of CRTC objects is static for the lifetime of the
3813 * device and so cannot disappear as we iterate. Similarly, we can
3814 * happily treat the predicates as racy, atomic checks as userspace
3815 * cannot claim and pin a new fb without at least acquring the
3816 * struct_mutex and so serialising with us.
3817 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003818 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003819 if (atomic_read(&crtc->unpin_work_count) == 0)
3820 continue;
3821
Daniel Vetter5a21b662016-05-24 17:13:53 +02003822 if (crtc->flip_work)
Chris Wilson5dce5b932014-01-20 10:17:36 +00003823 intel_wait_for_vblank(dev, crtc->pipe);
3824
3825 return true;
3826 }
3827
3828 return false;
3829}
3830
Daniel Vetter5a21b662016-05-24 17:13:53 +02003831static void page_flip_completed(struct intel_crtc *intel_crtc)
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003832{
3833 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +02003834 struct intel_flip_work *work = intel_crtc->flip_work;
3835
3836 intel_crtc->flip_work = NULL;
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003837
3838 if (work->event)
Gustavo Padovan560ce1d2016-04-14 10:48:15 -07003839 drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003840
3841 drm_crtc_vblank_put(&intel_crtc->base);
3842
Daniel Vetter5a21b662016-05-24 17:13:53 +02003843 wake_up_all(&dev_priv->pending_flip_queue);
Maarten Lankhorst143f73b2016-05-17 15:07:54 +02003844 queue_work(dev_priv->wq, &work->unpin_work);
Daniel Vetter5a21b662016-05-24 17:13:53 +02003845
3846 trace_i915_flip_complete(intel_crtc->plane,
3847 work->pending_flip_obj);
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003848}
3849
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003850static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003851{
Chris Wilson0f911282012-04-17 10:05:38 +01003852 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003853 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003854 long ret;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003855
Daniel Vetter2c10d572012-12-20 21:24:07 +01003856 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003857
3858 ret = wait_event_interruptible_timeout(
3859 dev_priv->pending_flip_queue,
3860 !intel_crtc_has_pending_flip(crtc),
3861 60*HZ);
3862
3863 if (ret < 0)
3864 return ret;
3865
Daniel Vetter5a21b662016-05-24 17:13:53 +02003866 if (ret == 0) {
3867 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3868 struct intel_flip_work *work;
3869
3870 spin_lock_irq(&dev->event_lock);
3871 work = intel_crtc->flip_work;
3872 if (work && !is_mmio_work(work)) {
3873 WARN_ONCE(1, "Removing stuck page flip\n");
3874 page_flip_completed(intel_crtc);
3875 }
3876 spin_unlock_irq(&dev->event_lock);
3877 }
Chris Wilson5bb61642012-09-27 21:25:58 +01003878
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003879 return 0;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003880}
3881
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003882static void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
3883{
3884 u32 temp;
3885
3886 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3887
3888 mutex_lock(&dev_priv->sb_lock);
3889
3890 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3891 temp |= SBI_SSCCTL_DISABLE;
3892 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
3893
3894 mutex_unlock(&dev_priv->sb_lock);
3895}
3896
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003897/* Program iCLKIP clock to the desired frequency */
3898static void lpt_program_iclkip(struct drm_crtc *crtc)
3899{
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003900 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003901 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003902 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3903 u32 temp;
3904
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003905 lpt_disable_iclkip(dev_priv);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003906
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003907 /* The iCLK virtual clock root frequency is in MHz,
3908 * but the adjusted_mode->crtc_clock in in KHz. To get the
3909 * divisors, it is necessary to divide one by another, so we
3910 * convert the virtual clock precision to KHz here for higher
3911 * precision.
3912 */
3913 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003914 u32 iclk_virtual_root_freq = 172800 * 1000;
3915 u32 iclk_pi_range = 64;
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003916 u32 desired_divisor;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003917
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003918 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
3919 clock << auxdiv);
3920 divsel = (desired_divisor / iclk_pi_range) - 2;
3921 phaseinc = desired_divisor % iclk_pi_range;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003922
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003923 /*
3924 * Near 20MHz is a corner case which is
3925 * out of range for the 7-bit divisor
3926 */
3927 if (divsel <= 0x7f)
3928 break;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003929 }
3930
3931 /* This should not happen with any sane values */
3932 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3933 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3934 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3935 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3936
3937 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 +03003938 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003939 auxdiv,
3940 divsel,
3941 phasedir,
3942 phaseinc);
3943
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003944 mutex_lock(&dev_priv->sb_lock);
3945
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003946 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003947 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003948 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3949 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3950 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3951 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3952 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3953 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003954 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003955
3956 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003957 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003958 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3959 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003960 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003961
3962 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003963 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003964 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003965 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003966
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003967 mutex_unlock(&dev_priv->sb_lock);
3968
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003969 /* Wait for initialization time */
3970 udelay(24);
3971
3972 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
3973}
3974
Ville Syrjälä8802e5b2016-02-17 21:41:12 +02003975int lpt_get_iclkip(struct drm_i915_private *dev_priv)
3976{
3977 u32 divsel, phaseinc, auxdiv;
3978 u32 iclk_virtual_root_freq = 172800 * 1000;
3979 u32 iclk_pi_range = 64;
3980 u32 desired_divisor;
3981 u32 temp;
3982
3983 if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
3984 return 0;
3985
3986 mutex_lock(&dev_priv->sb_lock);
3987
3988 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3989 if (temp & SBI_SSCCTL_DISABLE) {
3990 mutex_unlock(&dev_priv->sb_lock);
3991 return 0;
3992 }
3993
3994 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
3995 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
3996 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
3997 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
3998 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
3999
4000 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
4001 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
4002 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
4003
4004 mutex_unlock(&dev_priv->sb_lock);
4005
4006 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
4007
4008 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4009 desired_divisor << auxdiv);
4010}
4011
Daniel Vetter275f01b22013-05-03 11:49:47 +02004012static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4013 enum pipe pch_transcoder)
4014{
4015 struct drm_device *dev = crtc->base.dev;
4016 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004017 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02004018
4019 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4020 I915_READ(HTOTAL(cpu_transcoder)));
4021 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4022 I915_READ(HBLANK(cpu_transcoder)));
4023 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4024 I915_READ(HSYNC(cpu_transcoder)));
4025
4026 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4027 I915_READ(VTOTAL(cpu_transcoder)));
4028 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4029 I915_READ(VBLANK(cpu_transcoder)));
4030 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4031 I915_READ(VSYNC(cpu_transcoder)));
4032 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4033 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4034}
4035
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004036static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004037{
4038 struct drm_i915_private *dev_priv = dev->dev_private;
4039 uint32_t temp;
4040
4041 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004042 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004043 return;
4044
4045 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4046 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4047
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004048 temp &= ~FDI_BC_BIFURCATION_SELECT;
4049 if (enable)
4050 temp |= FDI_BC_BIFURCATION_SELECT;
4051
4052 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004053 I915_WRITE(SOUTH_CHICKEN1, temp);
4054 POSTING_READ(SOUTH_CHICKEN1);
4055}
4056
4057static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4058{
4059 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004060
4061 switch (intel_crtc->pipe) {
4062 case PIPE_A:
4063 break;
4064 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004065 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004066 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004067 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004068 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004069
4070 break;
4071 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004072 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004073
4074 break;
4075 default:
4076 BUG();
4077 }
4078}
4079
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004080/* Return which DP Port should be selected for Transcoder DP control */
4081static enum port
4082intel_trans_dp_port_sel(struct drm_crtc *crtc)
4083{
4084 struct drm_device *dev = crtc->dev;
4085 struct intel_encoder *encoder;
4086
4087 for_each_encoder_on_crtc(dev, crtc, encoder) {
4088 if (encoder->type == INTEL_OUTPUT_DISPLAYPORT ||
4089 encoder->type == INTEL_OUTPUT_EDP)
4090 return enc_to_dig_port(&encoder->base)->port;
4091 }
4092
4093 return -1;
4094}
4095
Jesse Barnesf67a5592011-01-05 10:31:48 -08004096/*
4097 * Enable PCH resources required for PCH ports:
4098 * - PCH PLLs
4099 * - FDI training & RX/TX
4100 * - update transcoder timings
4101 * - DP transcoding bits
4102 * - transcoder
4103 */
4104static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08004105{
4106 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004107 struct drm_i915_private *dev_priv = dev->dev_private;
4108 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4109 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004110 u32 temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004111
Daniel Vetterab9412b2013-05-03 11:49:46 +02004112 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004113
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004114 if (IS_IVYBRIDGE(dev))
4115 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4116
Daniel Vettercd986ab2012-10-26 10:58:12 +02004117 /* Write the TU size bits before fdi link training, so that error
4118 * detection works. */
4119 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4120 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4121
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004122 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07004123 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004124
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004125 /* We need to program the right clock selection before writing the pixel
4126 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004127 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004128 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004129
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004130 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004131 temp |= TRANS_DPLL_ENABLE(pipe);
4132 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02004133 if (intel_crtc->config->shared_dpll ==
4134 intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004135 temp |= sel;
4136 else
4137 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004138 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004139 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004140
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004141 /* XXX: pch pll's can be enabled any time before we enable the PCH
4142 * transcoder, and we actually should do this to not upset any PCH
4143 * transcoder that already use the clock when we share it.
4144 *
4145 * Note that enable_shared_dpll tries to do the right thing, but
4146 * get_shared_dpll unconditionally resets the pll - we need that to have
4147 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02004148 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004149
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004150 /* set transcoder timing, panel must allow it */
4151 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02004152 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004153
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004154 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004155
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004156 /* For PCH DP, enable TRANS_DP_CTL */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004157 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004158 const struct drm_display_mode *adjusted_mode =
4159 &intel_crtc->config->base.adjusted_mode;
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004160 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004161 i915_reg_t reg = TRANS_DP_CTL(pipe);
Chris Wilson5eddb702010-09-11 13:48:45 +01004162 temp = I915_READ(reg);
4163 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004164 TRANS_DP_SYNC_MASK |
4165 TRANS_DP_BPC_MASK);
Ville Syrjäläe3ef4472015-05-05 17:17:31 +03004166 temp |= TRANS_DP_OUTPUT_ENABLE;
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004167 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004168
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004169 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004170 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004171 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004172 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004173
4174 switch (intel_trans_dp_port_sel(crtc)) {
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004175 case PORT_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004176 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004177 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004178 case PORT_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004179 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004180 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004181 case PORT_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004182 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004183 break;
4184 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004185 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004186 }
4187
Chris Wilson5eddb702010-09-11 13:48:45 +01004188 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004189 }
4190
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004191 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004192}
4193
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004194static void lpt_pch_enable(struct drm_crtc *crtc)
4195{
4196 struct drm_device *dev = crtc->dev;
4197 struct drm_i915_private *dev_priv = dev->dev_private;
4198 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004199 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004200
Daniel Vetterab9412b2013-05-03 11:49:46 +02004201 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004202
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004203 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004204
Paulo Zanoni0540e482012-10-31 18:12:40 -02004205 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02004206 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004207
Paulo Zanoni937bb612012-10-31 18:12:47 -02004208 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004209}
4210
Daniel Vettera1520312013-05-03 11:49:50 +02004211static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004212{
4213 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004214 i915_reg_t dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004215 u32 temp;
4216
4217 temp = I915_READ(dslreg);
4218 udelay(500);
4219 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004220 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004221 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004222 }
4223}
4224
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004225static int
4226skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4227 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4228 int src_w, int src_h, int dst_w, int dst_h)
Chandra Kondurua1b22782015-04-07 15:28:45 -07004229{
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004230 struct intel_crtc_scaler_state *scaler_state =
4231 &crtc_state->scaler_state;
4232 struct intel_crtc *intel_crtc =
4233 to_intel_crtc(crtc_state->base.crtc);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004234 int need_scaling;
Chandra Konduru6156a452015-04-27 13:48:39 -07004235
4236 need_scaling = intel_rotation_90_or_270(rotation) ?
4237 (src_h != dst_w || src_w != dst_h):
4238 (src_w != dst_w || src_h != dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004239
4240 /*
4241 * if plane is being disabled or scaler is no more required or force detach
4242 * - free scaler binded to this plane/crtc
4243 * - in order to do this, update crtc->scaler_usage
4244 *
4245 * Here scaler state in crtc_state is set free so that
4246 * scaler can be assigned to other user. Actual register
4247 * update to free the scaler is done in plane/panel-fit programming.
4248 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4249 */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004250 if (force_detach || !need_scaling) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004251 if (*scaler_id >= 0) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004252 scaler_state->scaler_users &= ~(1 << scaler_user);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004253 scaler_state->scalers[*scaler_id].in_use = 0;
4254
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004255 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4256 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4257 intel_crtc->pipe, scaler_user, *scaler_id,
Chandra Kondurua1b22782015-04-07 15:28:45 -07004258 scaler_state->scaler_users);
4259 *scaler_id = -1;
4260 }
4261 return 0;
4262 }
4263
4264 /* range checks */
4265 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4266 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4267
4268 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4269 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004270 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
Chandra Kondurua1b22782015-04-07 15:28:45 -07004271 "size is out of scaler range\n",
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004272 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004273 return -EINVAL;
4274 }
4275
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004276 /* mark this plane as a scaler user in crtc_state */
4277 scaler_state->scaler_users |= (1 << scaler_user);
4278 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4279 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4280 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4281 scaler_state->scaler_users);
4282
4283 return 0;
4284}
4285
4286/**
4287 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4288 *
4289 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004290 *
4291 * Return
4292 * 0 - scaler_usage updated successfully
4293 * error - requested scaling cannot be supported or other error condition
4294 */
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004295int skl_update_scaler_crtc(struct intel_crtc_state *state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004296{
4297 struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03004298 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004299
Ville Syrjälä78108b72016-05-27 20:59:19 +03004300 DRM_DEBUG_KMS("Updating scaler for [CRTC:%d:%s] scaler_user index %u.%u\n",
4301 intel_crtc->base.base.id, intel_crtc->base.name,
4302 intel_crtc->pipe, SKL_CRTC_INDEX);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004303
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004304 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
Ville Syrjäläfa5a7972015-10-15 17:01:58 +03004305 &state->scaler_state.scaler_id, BIT(DRM_ROTATE_0),
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004306 state->pipe_src_w, state->pipe_src_h,
Ville Syrjäläaad941d2015-09-25 16:38:56 +03004307 adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004308}
4309
4310/**
4311 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4312 *
4313 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004314 * @plane_state: atomic plane state to update
4315 *
4316 * Return
4317 * 0 - scaler_usage updated successfully
4318 * error - requested scaling cannot be supported or other error condition
4319 */
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004320static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4321 struct intel_plane_state *plane_state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004322{
4323
4324 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004325 struct intel_plane *intel_plane =
4326 to_intel_plane(plane_state->base.plane);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004327 struct drm_framebuffer *fb = plane_state->base.fb;
4328 int ret;
4329
4330 bool force_detach = !fb || !plane_state->visible;
4331
Ville Syrjälä72660ce2016-05-27 20:59:20 +03004332 DRM_DEBUG_KMS("Updating scaler for [PLANE:%d:%s] scaler_user index %u.%u\n",
4333 intel_plane->base.base.id, intel_plane->base.name,
4334 intel_crtc->pipe, drm_plane_index(&intel_plane->base));
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004335
4336 ret = skl_update_scaler(crtc_state, force_detach,
4337 drm_plane_index(&intel_plane->base),
4338 &plane_state->scaler_id,
4339 plane_state->base.rotation,
4340 drm_rect_width(&plane_state->src) >> 16,
4341 drm_rect_height(&plane_state->src) >> 16,
4342 drm_rect_width(&plane_state->dst),
4343 drm_rect_height(&plane_state->dst));
4344
4345 if (ret || plane_state->scaler_id < 0)
4346 return ret;
4347
Chandra Kondurua1b22782015-04-07 15:28:45 -07004348 /* check colorkey */
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004349 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
Ville Syrjälä72660ce2016-05-27 20:59:20 +03004350 DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
4351 intel_plane->base.base.id,
4352 intel_plane->base.name);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004353 return -EINVAL;
4354 }
4355
4356 /* Check src format */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004357 switch (fb->pixel_format) {
4358 case DRM_FORMAT_RGB565:
4359 case DRM_FORMAT_XBGR8888:
4360 case DRM_FORMAT_XRGB8888:
4361 case DRM_FORMAT_ABGR8888:
4362 case DRM_FORMAT_ARGB8888:
4363 case DRM_FORMAT_XRGB2101010:
4364 case DRM_FORMAT_XBGR2101010:
4365 case DRM_FORMAT_YUYV:
4366 case DRM_FORMAT_YVYU:
4367 case DRM_FORMAT_UYVY:
4368 case DRM_FORMAT_VYUY:
4369 break;
4370 default:
Ville Syrjälä72660ce2016-05-27 20:59:20 +03004371 DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
4372 intel_plane->base.base.id, intel_plane->base.name,
4373 fb->base.id, fb->pixel_format);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004374 return -EINVAL;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004375 }
4376
Chandra Kondurua1b22782015-04-07 15:28:45 -07004377 return 0;
4378}
4379
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004380static void skylake_scaler_disable(struct intel_crtc *crtc)
4381{
4382 int i;
4383
4384 for (i = 0; i < crtc->num_scalers; i++)
4385 skl_detach_scaler(crtc, i);
4386}
4387
4388static void skylake_pfit_enable(struct intel_crtc *crtc)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004389{
4390 struct drm_device *dev = crtc->base.dev;
4391 struct drm_i915_private *dev_priv = dev->dev_private;
4392 int pipe = crtc->pipe;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004393 struct intel_crtc_scaler_state *scaler_state =
4394 &crtc->config->scaler_state;
4395
4396 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4397
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004398 if (crtc->config->pch_pfit.enabled) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004399 int id;
4400
4401 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4402 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4403 return;
4404 }
4405
4406 id = scaler_state->scaler_id;
4407 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4408 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4409 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4410 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4411
4412 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004413 }
4414}
4415
Jesse Barnesb074cec2013-04-25 12:55:02 -07004416static void ironlake_pfit_enable(struct intel_crtc *crtc)
4417{
4418 struct drm_device *dev = crtc->base.dev;
4419 struct drm_i915_private *dev_priv = dev->dev_private;
4420 int pipe = crtc->pipe;
4421
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004422 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004423 /* Force use of hard-coded filter coefficients
4424 * as some pre-programmed values are broken,
4425 * e.g. x201.
4426 */
4427 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4428 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4429 PF_PIPE_SEL_IVB(pipe));
4430 else
4431 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004432 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4433 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004434 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004435}
4436
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004437void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004438{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004439 struct drm_device *dev = crtc->base.dev;
4440 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid77e4532013-09-24 13:52:55 -03004441
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004442 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004443 return;
4444
Maarten Lankhorst307e4492016-03-23 14:33:28 +01004445 /*
4446 * We can only enable IPS after we enable a plane and wait for a vblank
4447 * This function is called from post_plane_update, which is run after
4448 * a vblank wait.
4449 */
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004450
Paulo Zanonid77e4532013-09-24 13:52:55 -03004451 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004452 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004453 mutex_lock(&dev_priv->rps.hw_lock);
4454 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4455 mutex_unlock(&dev_priv->rps.hw_lock);
4456 /* Quoting Art Runyan: "its not safe to expect any particular
4457 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004458 * mailbox." Moreover, the mailbox may return a bogus state,
4459 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004460 */
4461 } else {
4462 I915_WRITE(IPS_CTL, IPS_ENABLE);
4463 /* The bit only becomes 1 in the next vblank, so this wait here
4464 * is essentially intel_wait_for_vblank. If we don't have this
4465 * and don't wait for vblanks until the end of crtc_enable, then
4466 * the HW state readout code will complain that the expected
4467 * IPS_CTL value is not the one we read. */
Chris Wilson2ec9ba32016-06-30 15:33:01 +01004468 if (intel_wait_for_register(dev_priv,
4469 IPS_CTL, IPS_ENABLE, IPS_ENABLE,
4470 50))
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004471 DRM_ERROR("Timed out waiting for IPS enable\n");
4472 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004473}
4474
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004475void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004476{
4477 struct drm_device *dev = crtc->base.dev;
4478 struct drm_i915_private *dev_priv = dev->dev_private;
4479
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004480 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004481 return;
4482
4483 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004484 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004485 mutex_lock(&dev_priv->rps.hw_lock);
4486 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4487 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004488 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
Chris Wilsonb85c1ec2016-06-30 15:33:02 +01004489 if (intel_wait_for_register(dev_priv,
4490 IPS_CTL, IPS_ENABLE, 0,
4491 42))
Ben Widawsky23d0b132014-04-10 14:32:41 -07004492 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004493 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004494 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004495 POSTING_READ(IPS_CTL);
4496 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004497
4498 /* We need to wait for a vblank before we can disable the plane. */
4499 intel_wait_for_vblank(dev, crtc->pipe);
4500}
4501
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004502static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004503{
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004504 if (intel_crtc->overlay) {
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004505 struct drm_device *dev = intel_crtc->base.dev;
4506 struct drm_i915_private *dev_priv = dev->dev_private;
4507
4508 mutex_lock(&dev->struct_mutex);
4509 dev_priv->mm.interruptible = false;
4510 (void) intel_overlay_switch_off(intel_crtc->overlay);
4511 dev_priv->mm.interruptible = true;
4512 mutex_unlock(&dev->struct_mutex);
4513 }
4514
4515 /* Let userspace switch the overlay on again. In most cases userspace
4516 * has to recompute where to put it anyway.
4517 */
4518}
4519
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004520/**
4521 * intel_post_enable_primary - Perform operations after enabling primary plane
4522 * @crtc: the CRTC whose primary plane was just enabled
4523 *
4524 * Performs potentially sleeping operations that must be done after the primary
4525 * plane is enabled, such as updating FBC and IPS. Note that this may be
4526 * called due to an explicit primary plane update, or due to an implicit
4527 * re-enable that is caused when a sprite plane is updated to no longer
4528 * completely hide the primary plane.
4529 */
4530static void
4531intel_post_enable_primary(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004532{
4533 struct drm_device *dev = crtc->dev;
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004534 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004535 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4536 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004537
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004538 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004539 * FIXME IPS should be fine as long as one plane is
4540 * enabled, but in practice it seems to have problems
4541 * when going from primary only to sprite only and vice
4542 * versa.
4543 */
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004544 hsw_enable_ips(intel_crtc);
4545
Daniel Vetterf99d7062014-06-19 16:01:59 +02004546 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004547 * Gen2 reports pipe underruns whenever all planes are disabled.
4548 * So don't enable underrun reporting before at least some planes
4549 * are enabled.
4550 * FIXME: Need to fix the logic to work when we turn off all planes
4551 * but leave the pipe running.
Daniel Vetterf99d7062014-06-19 16:01:59 +02004552 */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004553 if (IS_GEN2(dev))
4554 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4555
Ville Syrjäläaca7b682015-10-30 19:22:21 +02004556 /* Underruns don't always raise interrupts, so check manually. */
4557 intel_check_cpu_fifo_underruns(dev_priv);
4558 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004559}
4560
Ville Syrjälä2622a082016-03-09 19:07:26 +02004561/* FIXME move all this to pre_plane_update() with proper state tracking */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004562static void
4563intel_pre_disable_primary(struct drm_crtc *crtc)
4564{
4565 struct drm_device *dev = crtc->dev;
4566 struct drm_i915_private *dev_priv = dev->dev_private;
4567 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4568 int pipe = intel_crtc->pipe;
4569
4570 /*
4571 * Gen2 reports pipe underruns whenever all planes are disabled.
4572 * So diasble underrun reporting before all the planes get disabled.
4573 * FIXME: Need to fix the logic to work when we turn off all planes
4574 * but leave the pipe running.
4575 */
4576 if (IS_GEN2(dev))
4577 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4578
4579 /*
Ville Syrjälä2622a082016-03-09 19:07:26 +02004580 * FIXME IPS should be fine as long as one plane is
4581 * enabled, but in practice it seems to have problems
4582 * when going from primary only to sprite only and vice
4583 * versa.
4584 */
4585 hsw_disable_ips(intel_crtc);
4586}
4587
4588/* FIXME get rid of this and use pre_plane_update */
4589static void
4590intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
4591{
4592 struct drm_device *dev = crtc->dev;
4593 struct drm_i915_private *dev_priv = dev->dev_private;
4594 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4595 int pipe = intel_crtc->pipe;
4596
4597 intel_pre_disable_primary(crtc);
4598
4599 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004600 * Vblank time updates from the shadow to live plane control register
4601 * are blocked if the memory self-refresh mode is active at that
4602 * moment. So to make sure the plane gets truly disabled, disable
4603 * first the self-refresh mode. The self-refresh enable bit in turn
4604 * will be checked/applied by the HW only at the next frame start
4605 * event which is after the vblank start event, so we need to have a
4606 * wait-for-vblank between disabling the plane and the pipe.
4607 */
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004608 if (HAS_GMCH_DISPLAY(dev)) {
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004609 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004610 dev_priv->wm.vlv.cxsr = false;
4611 intel_wait_for_vblank(dev, pipe);
4612 }
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004613}
4614
Daniel Vetter5a21b662016-05-24 17:13:53 +02004615static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
4616{
4617 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
4618 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4619 struct intel_crtc_state *pipe_config =
4620 to_intel_crtc_state(crtc->base.state);
4621 struct drm_device *dev = crtc->base.dev;
4622 struct drm_plane *primary = crtc->base.primary;
4623 struct drm_plane_state *old_pri_state =
4624 drm_atomic_get_existing_plane_state(old_state, primary);
4625
4626 intel_frontbuffer_flip(dev, pipe_config->fb_bits);
4627
4628 crtc->wm.cxsr_allowed = true;
4629
4630 if (pipe_config->update_wm_post && pipe_config->base.active)
4631 intel_update_watermarks(&crtc->base);
4632
4633 if (old_pri_state) {
4634 struct intel_plane_state *primary_state =
4635 to_intel_plane_state(primary->state);
4636 struct intel_plane_state *old_primary_state =
4637 to_intel_plane_state(old_pri_state);
4638
4639 intel_fbc_post_update(crtc);
4640
4641 if (primary_state->visible &&
4642 (needs_modeset(&pipe_config->base) ||
4643 !old_primary_state->visible))
4644 intel_post_enable_primary(&crtc->base);
4645 }
4646}
4647
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004648static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004649{
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004650 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004651 struct drm_device *dev = crtc->base.dev;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +02004652 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01004653 struct intel_crtc_state *pipe_config =
4654 to_intel_crtc_state(crtc->base.state);
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004655 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4656 struct drm_plane *primary = crtc->base.primary;
4657 struct drm_plane_state *old_pri_state =
4658 drm_atomic_get_existing_plane_state(old_state, primary);
4659 bool modeset = needs_modeset(&pipe_config->base);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004660
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004661 if (old_pri_state) {
4662 struct intel_plane_state *primary_state =
4663 to_intel_plane_state(primary->state);
4664 struct intel_plane_state *old_primary_state =
4665 to_intel_plane_state(old_pri_state);
4666
Maarten Lankhorstfaf68d92016-06-14 14:24:20 +02004667 intel_fbc_pre_update(crtc, pipe_config, primary_state);
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +01004668
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004669 if (old_primary_state->visible &&
4670 (modeset || !primary_state->visible))
4671 intel_pre_disable_primary(&crtc->base);
4672 }
Ville Syrjälä852eb002015-06-24 22:00:07 +03004673
David Weinehalla4015f92016-05-19 15:50:36 +03004674 if (pipe_config->disable_cxsr && HAS_GMCH_DISPLAY(dev)) {
Ville Syrjälä852eb002015-06-24 22:00:07 +03004675 crtc->wm.cxsr_allowed = false;
Maarten Lankhorst2dfd1782016-02-03 16:53:25 +01004676
Ville Syrjälä2622a082016-03-09 19:07:26 +02004677 /*
4678 * Vblank time updates from the shadow to live plane control register
4679 * are blocked if the memory self-refresh mode is active at that
4680 * moment. So to make sure the plane gets truly disabled, disable
4681 * first the self-refresh mode. The self-refresh enable bit in turn
4682 * will be checked/applied by the HW only at the next frame start
4683 * event which is after the vblank start event, so we need to have a
4684 * wait-for-vblank between disabling the plane and the pipe.
4685 */
4686 if (old_crtc_state->base.active) {
Maarten Lankhorst2dfd1782016-02-03 16:53:25 +01004687 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä2622a082016-03-09 19:07:26 +02004688 dev_priv->wm.vlv.cxsr = false;
4689 intel_wait_for_vblank(dev, crtc->pipe);
4690 }
Ville Syrjälä852eb002015-06-24 22:00:07 +03004691 }
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004692
Matt Ropered4a6a72016-02-23 17:20:13 -08004693 /*
4694 * IVB workaround: must disable low power watermarks for at least
4695 * one frame before enabling scaling. LP watermarks can be re-enabled
4696 * when scaling is disabled.
4697 *
4698 * WaCxSRDisabledForSpriteScaling:ivb
4699 */
4700 if (pipe_config->disable_lp_wm) {
4701 ilk_disable_lp_wm(dev);
4702 intel_wait_for_vblank(dev, crtc->pipe);
4703 }
4704
4705 /*
4706 * If we're doing a modeset, we're done. No need to do any pre-vblank
4707 * watermark programming here.
4708 */
4709 if (needs_modeset(&pipe_config->base))
4710 return;
4711
4712 /*
4713 * For platforms that support atomic watermarks, program the
4714 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
4715 * will be the intermediate values that are safe for both pre- and
4716 * post- vblank; when vblank happens, the 'active' values will be set
4717 * to the final 'target' values and we'll do this again to get the
4718 * optimal watermarks. For gen9+ platforms, the values we program here
4719 * will be the final target values which will get automatically latched
4720 * at vblank time; no further programming will be necessary.
4721 *
4722 * If a platform hasn't been transitioned to atomic watermarks yet,
4723 * we'll continue to update watermarks the old way, if flags tell
4724 * us to.
4725 */
4726 if (dev_priv->display.initial_watermarks != NULL)
4727 dev_priv->display.initial_watermarks(pipe_config);
Ville Syrjäläcaed3612016-03-09 19:07:25 +02004728 else if (pipe_config->update_wm_pre)
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004729 intel_update_watermarks(&crtc->base);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004730}
4731
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004732static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004733{
4734 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004735 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004736 struct drm_plane *p;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004737 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004738
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004739 intel_crtc_dpms_overlay_disable(intel_crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004740
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004741 drm_for_each_plane_mask(p, dev, plane_mask)
4742 to_intel_plane(p)->disable_plane(p, crtc);
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004743
Daniel Vetterf99d7062014-06-19 16:01:59 +02004744 /*
4745 * FIXME: Once we grow proper nuclear flip support out of this we need
4746 * to compute the mask of flip planes precisely. For the time being
4747 * consider this a flip to a NULL plane.
4748 */
4749 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004750}
4751
Jesse Barnesf67a5592011-01-05 10:31:48 -08004752static void ironlake_crtc_enable(struct drm_crtc *crtc)
4753{
4754 struct drm_device *dev = crtc->dev;
4755 struct drm_i915_private *dev_priv = dev->dev_private;
4756 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004757 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004758 int pipe = intel_crtc->pipe;
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004759 struct intel_crtc_state *pipe_config =
4760 to_intel_crtc_state(crtc->state);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004761
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004762 if (WARN_ON(intel_crtc->active))
Jesse Barnesf67a5592011-01-05 10:31:48 -08004763 return;
4764
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004765 /*
4766 * Sometimes spurious CPU pipe underruns happen during FDI
4767 * training, at least with VGA+HDMI cloning. Suppress them.
4768 *
4769 * On ILK we get an occasional spurious CPU pipe underruns
4770 * between eDP port A enable and vdd enable. Also PCH port
4771 * enable seems to result in the occasional CPU pipe underrun.
4772 *
4773 * Spurious PCH underruns also occur during PCH enabling.
4774 */
4775 if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
4776 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004777 if (intel_crtc->config->has_pch_encoder)
Ville Syrjälä81b088c2015-10-30 19:21:31 +02004778 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
4779
4780 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02004781 intel_prepare_shared_dpll(intel_crtc);
4782
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004783 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05304784 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004785
4786 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02004787 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004788
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004789 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02004790 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004791 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004792 }
4793
4794 ironlake_set_pipeconf(crtc);
4795
Jesse Barnesf67a5592011-01-05 10:31:48 -08004796 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004797
Daniel Vetterf6736a12013-06-05 13:34:30 +02004798 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004799 if (encoder->pre_enable)
4800 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004801
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004802 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004803 /* Note: FDI PLL enabling _must_ be done before we enable the
4804 * cpu pipes, hence this is separate from all the other fdi/pch
4805 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004806 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004807 } else {
4808 assert_fdi_tx_disabled(dev_priv, pipe);
4809 assert_fdi_rx_disabled(dev_priv, pipe);
4810 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004811
Jesse Barnesb074cec2013-04-25 12:55:02 -07004812 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004813
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004814 /*
4815 * On ILK+ LUT must be loaded before the pipe is running but with
4816 * clocks enabled
4817 */
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004818 intel_color_load_luts(&pipe_config->base);
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004819
Imre Deak1d5bf5d2016-02-29 22:10:33 +02004820 if (dev_priv->display.initial_watermarks != NULL)
4821 dev_priv->display.initial_watermarks(intel_crtc->config);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004822 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004823
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004824 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004825 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004826
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004827 assert_vblank_disabled(crtc);
4828 drm_crtc_vblank_on(crtc);
4829
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004830 for_each_encoder_on_crtc(dev, crtc, encoder)
4831 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004832
4833 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004834 cpt_verify_modeset(dev, intel_crtc->pipe);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004835
4836 /* Must wait for vblank to avoid spurious PCH FIFO underruns */
4837 if (intel_crtc->config->has_pch_encoder)
4838 intel_wait_for_vblank(dev, pipe);
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004839 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004840 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004841}
4842
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004843/* IPS only exists on ULT machines and is tied to pipe A. */
4844static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4845{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004846 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004847}
4848
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004849static void haswell_crtc_enable(struct drm_crtc *crtc)
4850{
4851 struct drm_device *dev = crtc->dev;
4852 struct drm_i915_private *dev_priv = dev->dev_private;
4853 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4854 struct intel_encoder *encoder;
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004855 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
Jani Nikula4d1de972016-03-18 17:05:42 +02004856 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004857 struct intel_crtc_state *pipe_config =
4858 to_intel_crtc_state(crtc->state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004859
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004860 if (WARN_ON(intel_crtc->active))
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004861 return;
4862
Ville Syrjälä81b088c2015-10-30 19:21:31 +02004863 if (intel_crtc->config->has_pch_encoder)
4864 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4865 false);
4866
Imre Deak95a7a2a2016-06-13 16:44:35 +03004867 for_each_encoder_on_crtc(dev, crtc, encoder)
4868 if (encoder->pre_pll_enable)
4869 encoder->pre_pll_enable(encoder);
4870
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02004871 if (intel_crtc->config->shared_dpll)
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004872 intel_enable_shared_dpll(intel_crtc);
4873
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004874 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05304875 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02004876
Jani Nikula4d1de972016-03-18 17:05:42 +02004877 if (!intel_crtc->config->has_dsi_encoder)
4878 intel_set_pipe_timings(intel_crtc);
4879
Jani Nikulabc58be62016-03-18 17:05:39 +02004880 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02004881
Jani Nikula4d1de972016-03-18 17:05:42 +02004882 if (cpu_transcoder != TRANSCODER_EDP &&
4883 !transcoder_is_dsi(cpu_transcoder)) {
4884 I915_WRITE(PIPE_MULT(cpu_transcoder),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004885 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07004886 }
4887
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004888 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02004889 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004890 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02004891 }
4892
Jani Nikula4d1de972016-03-18 17:05:42 +02004893 if (!intel_crtc->config->has_dsi_encoder)
4894 haswell_set_pipeconf(crtc);
4895
Jani Nikula391bf042016-03-18 17:05:40 +02004896 haswell_set_pipemisc(crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02004897
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004898 intel_color_set_csc(&pipe_config->base);
Daniel Vetter229fca92014-04-24 23:55:09 +02004899
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004900 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004901
Daniel Vetter6b698512015-11-28 11:05:39 +01004902 if (intel_crtc->config->has_pch_encoder)
4903 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4904 else
4905 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4906
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304907 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004908 if (encoder->pre_enable)
4909 encoder->pre_enable(encoder);
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304910 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004911
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004912 if (intel_crtc->config->has_pch_encoder)
Imre Deak4fe94672014-06-25 22:01:49 +03004913 dev_priv->display.fdi_link_train(crtc);
Imre Deak4fe94672014-06-25 22:01:49 +03004914
Jani Nikulaa65347b2015-11-27 12:21:46 +02004915 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304916 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004917
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07004918 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004919 skylake_pfit_enable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08004920 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07004921 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004922
4923 /*
4924 * On ILK+ LUT must be loaded before the pipe is running but with
4925 * clocks enabled
4926 */
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004927 intel_color_load_luts(&pipe_config->base);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004928
Paulo Zanoni1f544382012-10-24 11:32:00 -02004929 intel_ddi_set_pipe_settings(crtc);
Jani Nikulaa65347b2015-11-27 12:21:46 +02004930 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304931 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004932
Imre Deak1d5bf5d2016-02-29 22:10:33 +02004933 if (dev_priv->display.initial_watermarks != NULL)
4934 dev_priv->display.initial_watermarks(pipe_config);
4935 else
4936 intel_update_watermarks(crtc);
Jani Nikula4d1de972016-03-18 17:05:42 +02004937
4938 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
4939 if (!intel_crtc->config->has_dsi_encoder)
4940 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004941
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004942 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004943 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004944
Jani Nikulaa65347b2015-11-27 12:21:46 +02004945 if (intel_crtc->config->dp_encoder_is_mst)
Dave Airlie0e32b392014-05-02 14:02:48 +10004946 intel_ddi_set_vc_payload_alloc(crtc, true);
4947
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004948 assert_vblank_disabled(crtc);
4949 drm_crtc_vblank_on(crtc);
4950
Jani Nikula8807e552013-08-30 19:40:32 +03004951 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004952 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004953 intel_opregion_notify_encoder(encoder, true);
4954 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004955
Daniel Vetter6b698512015-11-28 11:05:39 +01004956 if (intel_crtc->config->has_pch_encoder) {
4957 intel_wait_for_vblank(dev, pipe);
4958 intel_wait_for_vblank(dev, pipe);
4959 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004960 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4961 true);
Daniel Vetter6b698512015-11-28 11:05:39 +01004962 }
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004963
Paulo Zanonie4916942013-09-20 16:21:19 -03004964 /* If we change the relative order between pipe/planes enabling, we need
4965 * to change the workaround. */
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004966 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
4967 if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
4968 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4969 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4970 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004971}
4972
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02004973static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004974{
4975 struct drm_device *dev = crtc->base.dev;
4976 struct drm_i915_private *dev_priv = dev->dev_private;
4977 int pipe = crtc->pipe;
4978
4979 /* To avoid upsetting the power well on haswell only disable the pfit if
4980 * it's in use. The hw state code will make sure we get this right. */
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02004981 if (force || crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004982 I915_WRITE(PF_CTL(pipe), 0);
4983 I915_WRITE(PF_WIN_POS(pipe), 0);
4984 I915_WRITE(PF_WIN_SZ(pipe), 0);
4985 }
4986}
4987
Jesse Barnes6be4a602010-09-10 10:26:01 -07004988static void ironlake_crtc_disable(struct drm_crtc *crtc)
4989{
4990 struct drm_device *dev = crtc->dev;
4991 struct drm_i915_private *dev_priv = dev->dev_private;
4992 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004993 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004994 int pipe = intel_crtc->pipe;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004995
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004996 /*
4997 * Sometimes spurious CPU pipe underruns happen when the
4998 * pipe is already disabled, but FDI RX/TX is still enabled.
4999 * Happens at least with VGA+HDMI cloning. Suppress them.
5000 */
5001 if (intel_crtc->config->has_pch_encoder) {
5002 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02005003 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005004 }
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02005005
Daniel Vetterea9d7582012-07-10 10:42:52 +02005006 for_each_encoder_on_crtc(dev, crtc, encoder)
5007 encoder->disable(encoder);
5008
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005009 drm_crtc_vblank_off(crtc);
5010 assert_vblank_disabled(crtc);
5011
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005012 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005013
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005014 ironlake_pfit_disable(intel_crtc, false);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005015
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005016 if (intel_crtc->config->has_pch_encoder)
Ville Syrjälä5a74f702015-05-05 17:17:38 +03005017 ironlake_fdi_disable(crtc);
5018
Daniel Vetterbf49ec82012-09-06 22:15:40 +02005019 for_each_encoder_on_crtc(dev, crtc, encoder)
5020 if (encoder->post_disable)
5021 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005022
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005023 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02005024 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005025
Daniel Vetterd925c592013-06-05 13:34:04 +02005026 if (HAS_PCH_CPT(dev)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02005027 i915_reg_t reg;
5028 u32 temp;
5029
Daniel Vetterd925c592013-06-05 13:34:04 +02005030 /* disable TRANS_DP_CTL */
5031 reg = TRANS_DP_CTL(pipe);
5032 temp = I915_READ(reg);
5033 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5034 TRANS_DP_PORT_SEL_MASK);
5035 temp |= TRANS_DP_PORT_SEL_NONE;
5036 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005037
Daniel Vetterd925c592013-06-05 13:34:04 +02005038 /* disable DPLL_SEL */
5039 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02005040 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02005041 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005042 }
Daniel Vetterd925c592013-06-05 13:34:04 +02005043
Daniel Vetterd925c592013-06-05 13:34:04 +02005044 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005045 }
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005046
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005047 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005048 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005049}
5050
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005051static void haswell_crtc_disable(struct drm_crtc *crtc)
5052{
5053 struct drm_device *dev = crtc->dev;
5054 struct drm_i915_private *dev_priv = dev->dev_private;
5055 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5056 struct intel_encoder *encoder;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005057 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005058
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005059 if (intel_crtc->config->has_pch_encoder)
5060 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5061 false);
5062
Jani Nikula8807e552013-08-30 19:40:32 +03005063 for_each_encoder_on_crtc(dev, crtc, encoder) {
5064 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005065 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005066 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005067
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005068 drm_crtc_vblank_off(crtc);
5069 assert_vblank_disabled(crtc);
5070
Jani Nikula4d1de972016-03-18 17:05:42 +02005071 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
5072 if (!intel_crtc->config->has_dsi_encoder)
5073 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005074
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005075 if (intel_crtc->config->dp_encoder_is_mst)
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03005076 intel_ddi_set_vc_payload_alloc(crtc, false);
5077
Jani Nikulaa65347b2015-11-27 12:21:46 +02005078 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305079 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005080
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07005081 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02005082 skylake_scaler_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005083 else
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005084 ironlake_pfit_disable(intel_crtc, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005085
Jani Nikulaa65347b2015-11-27 12:21:46 +02005086 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305087 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005088
Imre Deak97b040a2014-06-25 22:01:50 +03005089 for_each_encoder_on_crtc(dev, crtc, encoder)
5090 if (encoder->post_disable)
5091 encoder->post_disable(encoder);
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005092
Ville Syrjälä92966a32015-12-08 16:05:48 +02005093 if (intel_crtc->config->has_pch_encoder) {
5094 lpt_disable_pch_transcoder(dev_priv);
Ville Syrjälä503a74e2015-12-04 22:22:14 +02005095 lpt_disable_iclkip(dev_priv);
Ville Syrjälä92966a32015-12-08 16:05:48 +02005096 intel_ddi_fdi_disable(crtc);
5097
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005098 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5099 true);
Ville Syrjälä92966a32015-12-08 16:05:48 +02005100 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005101}
5102
Jesse Barnes2dd24552013-04-25 12:55:01 -07005103static void i9xx_pfit_enable(struct intel_crtc *crtc)
5104{
5105 struct drm_device *dev = crtc->base.dev;
5106 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005107 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07005108
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02005109 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005110 return;
5111
Daniel Vetterc0b03412013-05-28 12:05:54 +02005112 /*
5113 * The panel fitter should only be adjusted whilst the pipe is disabled,
5114 * according to register description and PRM.
5115 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005116 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5117 assert_pipe_disabled(dev_priv, crtc->pipe);
5118
Jesse Barnesb074cec2013-04-25 12:55:02 -07005119 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5120 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005121
5122 /* Border color in case we don't scale up to the full screen. Black by
5123 * default, change to something else for debugging. */
5124 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005125}
5126
Dave Airlied05410f2014-06-05 13:22:59 +10005127static enum intel_display_power_domain port_to_power_domain(enum port port)
5128{
5129 switch (port) {
5130 case PORT_A:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005131 return POWER_DOMAIN_PORT_DDI_A_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005132 case PORT_B:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005133 return POWER_DOMAIN_PORT_DDI_B_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005134 case PORT_C:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005135 return POWER_DOMAIN_PORT_DDI_C_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005136 case PORT_D:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005137 return POWER_DOMAIN_PORT_DDI_D_LANES;
Xiong Zhangd8e19f92015-08-13 18:00:12 +08005138 case PORT_E:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005139 return POWER_DOMAIN_PORT_DDI_E_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005140 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005141 MISSING_CASE(port);
Dave Airlied05410f2014-06-05 13:22:59 +10005142 return POWER_DOMAIN_PORT_OTHER;
5143 }
5144}
5145
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005146static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
5147{
5148 switch (port) {
5149 case PORT_A:
5150 return POWER_DOMAIN_AUX_A;
5151 case PORT_B:
5152 return POWER_DOMAIN_AUX_B;
5153 case PORT_C:
5154 return POWER_DOMAIN_AUX_C;
5155 case PORT_D:
5156 return POWER_DOMAIN_AUX_D;
5157 case PORT_E:
5158 /* FIXME: Check VBT for actual wiring of PORT E */
5159 return POWER_DOMAIN_AUX_D;
5160 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005161 MISSING_CASE(port);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005162 return POWER_DOMAIN_AUX_A;
5163 }
5164}
5165
Imre Deak319be8a2014-03-04 19:22:57 +02005166enum intel_display_power_domain
5167intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02005168{
Imre Deak319be8a2014-03-04 19:22:57 +02005169 struct drm_device *dev = intel_encoder->base.dev;
5170 struct intel_digital_port *intel_dig_port;
5171
5172 switch (intel_encoder->type) {
5173 case INTEL_OUTPUT_UNKNOWN:
5174 /* Only DDI platforms should ever use this output type */
5175 WARN_ON_ONCE(!HAS_DDI(dev));
5176 case INTEL_OUTPUT_DISPLAYPORT:
5177 case INTEL_OUTPUT_HDMI:
5178 case INTEL_OUTPUT_EDP:
5179 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10005180 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10005181 case INTEL_OUTPUT_DP_MST:
5182 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5183 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02005184 case INTEL_OUTPUT_ANALOG:
5185 return POWER_DOMAIN_PORT_CRT;
5186 case INTEL_OUTPUT_DSI:
5187 return POWER_DOMAIN_PORT_DSI;
5188 default:
5189 return POWER_DOMAIN_PORT_OTHER;
5190 }
5191}
5192
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005193enum intel_display_power_domain
5194intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
5195{
5196 struct drm_device *dev = intel_encoder->base.dev;
5197 struct intel_digital_port *intel_dig_port;
5198
5199 switch (intel_encoder->type) {
5200 case INTEL_OUTPUT_UNKNOWN:
Imre Deak651174a2015-11-18 15:57:24 +02005201 case INTEL_OUTPUT_HDMI:
5202 /*
5203 * Only DDI platforms should ever use these output types.
5204 * We can get here after the HDMI detect code has already set
5205 * the type of the shared encoder. Since we can't be sure
5206 * what's the status of the given connectors, play safe and
5207 * run the DP detection too.
5208 */
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005209 WARN_ON_ONCE(!HAS_DDI(dev));
5210 case INTEL_OUTPUT_DISPLAYPORT:
5211 case INTEL_OUTPUT_EDP:
5212 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
5213 return port_to_aux_power_domain(intel_dig_port->port);
5214 case INTEL_OUTPUT_DP_MST:
5215 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5216 return port_to_aux_power_domain(intel_dig_port->port);
5217 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005218 MISSING_CASE(intel_encoder->type);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005219 return POWER_DOMAIN_AUX_A;
5220 }
5221}
5222
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005223static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
5224 struct intel_crtc_state *crtc_state)
Imre Deak319be8a2014-03-04 19:22:57 +02005225{
5226 struct drm_device *dev = crtc->dev;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005227 struct drm_encoder *encoder;
Imre Deak319be8a2014-03-04 19:22:57 +02005228 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5229 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02005230 unsigned long mask;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005231 enum transcoder transcoder = crtc_state->cpu_transcoder;
Imre Deak77d22dc2014-03-05 16:20:52 +02005232
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005233 if (!crtc_state->base.active)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005234 return 0;
5235
Imre Deak77d22dc2014-03-05 16:20:52 +02005236 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5237 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005238 if (crtc_state->pch_pfit.enabled ||
5239 crtc_state->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02005240 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5241
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005242 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
5243 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5244
Imre Deak319be8a2014-03-04 19:22:57 +02005245 mask |= BIT(intel_display_port_power_domain(intel_encoder));
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005246 }
Imre Deak319be8a2014-03-04 19:22:57 +02005247
Maarten Lankhorst15e7ec22016-03-14 09:27:54 +01005248 if (crtc_state->shared_dpll)
5249 mask |= BIT(POWER_DOMAIN_PLLS);
5250
Imre Deak77d22dc2014-03-05 16:20:52 +02005251 return mask;
5252}
5253
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005254static unsigned long
5255modeset_get_crtc_power_domains(struct drm_crtc *crtc,
5256 struct intel_crtc_state *crtc_state)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005257{
5258 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5259 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5260 enum intel_display_power_domain domain;
Daniel Vetter5a21b662016-05-24 17:13:53 +02005261 unsigned long domains, new_domains, old_domains;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005262
5263 old_domains = intel_crtc->enabled_power_domains;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005264 intel_crtc->enabled_power_domains = new_domains =
5265 get_crtc_power_domains(crtc, crtc_state);
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005266
Daniel Vetter5a21b662016-05-24 17:13:53 +02005267 domains = new_domains & ~old_domains;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005268
5269 for_each_power_domain(domain, domains)
5270 intel_display_power_get(dev_priv, domain);
5271
Daniel Vetter5a21b662016-05-24 17:13:53 +02005272 return old_domains & ~new_domains;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005273}
5274
5275static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
5276 unsigned long domains)
5277{
5278 enum intel_display_power_domain domain;
5279
5280 for_each_power_domain(domain, domains)
5281 intel_display_power_put(dev_priv, domain);
5282}
5283
Mika Kaholaadafdc62015-08-18 14:36:59 +03005284static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
5285{
5286 int max_cdclk_freq = dev_priv->max_cdclk_freq;
5287
5288 if (INTEL_INFO(dev_priv)->gen >= 9 ||
5289 IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
5290 return max_cdclk_freq;
5291 else if (IS_CHERRYVIEW(dev_priv))
5292 return max_cdclk_freq*95/100;
5293 else if (INTEL_INFO(dev_priv)->gen < 4)
5294 return 2*max_cdclk_freq*90/100;
5295 else
5296 return max_cdclk_freq*90/100;
5297}
5298
Ville Syrjäläb2045352016-05-13 23:41:27 +03005299static int skl_calc_cdclk(int max_pixclk, int vco);
5300
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005301static void intel_update_max_cdclk(struct drm_device *dev)
5302{
5303 struct drm_i915_private *dev_priv = dev->dev_private;
5304
Rodrigo Vivief11bdb2015-10-28 04:16:45 -07005305 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005306 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
Ville Syrjäläb2045352016-05-13 23:41:27 +03005307 int max_cdclk, vco;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005308
Ville Syrjäläb2045352016-05-13 23:41:27 +03005309 vco = dev_priv->skl_preferred_vco_freq;
Ville Syrjälä63911d72016-05-13 23:41:32 +03005310 WARN_ON(vco != 8100000 && vco != 8640000);
Ville Syrjäläb2045352016-05-13 23:41:27 +03005311
5312 /*
5313 * Use the lower (vco 8640) cdclk values as a
5314 * first guess. skl_calc_cdclk() will correct it
5315 * if the preferred vco is 8100 instead.
5316 */
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005317 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005318 max_cdclk = 617143;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005319 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
Ville Syrjäläb2045352016-05-13 23:41:27 +03005320 max_cdclk = 540000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005321 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
Ville Syrjäläb2045352016-05-13 23:41:27 +03005322 max_cdclk = 432000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005323 else
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005324 max_cdclk = 308571;
Ville Syrjäläb2045352016-05-13 23:41:27 +03005325
5326 dev_priv->max_cdclk_freq = skl_calc_cdclk(max_cdclk, vco);
Matt Roper281c1142016-04-05 14:37:19 -07005327 } else if (IS_BROXTON(dev)) {
5328 dev_priv->max_cdclk_freq = 624000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005329 } else if (IS_BROADWELL(dev)) {
5330 /*
5331 * FIXME with extra cooling we can allow
5332 * 540 MHz for ULX and 675 Mhz for ULT.
5333 * How can we know if extra cooling is
5334 * available? PCI ID, VTB, something else?
5335 */
5336 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5337 dev_priv->max_cdclk_freq = 450000;
5338 else if (IS_BDW_ULX(dev))
5339 dev_priv->max_cdclk_freq = 450000;
5340 else if (IS_BDW_ULT(dev))
5341 dev_priv->max_cdclk_freq = 540000;
5342 else
5343 dev_priv->max_cdclk_freq = 675000;
Mika Kahola0904dea2015-06-12 10:11:32 +03005344 } else if (IS_CHERRYVIEW(dev)) {
5345 dev_priv->max_cdclk_freq = 320000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005346 } else if (IS_VALLEYVIEW(dev)) {
5347 dev_priv->max_cdclk_freq = 400000;
5348 } else {
5349 /* otherwise assume cdclk is fixed */
5350 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5351 }
5352
Mika Kaholaadafdc62015-08-18 14:36:59 +03005353 dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
5354
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005355 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5356 dev_priv->max_cdclk_freq);
Mika Kaholaadafdc62015-08-18 14:36:59 +03005357
5358 DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
5359 dev_priv->max_dotclk_freq);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005360}
5361
5362static void intel_update_cdclk(struct drm_device *dev)
5363{
5364 struct drm_i915_private *dev_priv = dev->dev_private;
5365
5366 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
Ville Syrjälä2f2a1212016-05-13 23:41:25 +03005367
Ville Syrjälä83d7c812016-05-13 23:41:35 +03005368 if (INTEL_GEN(dev_priv) >= 9)
Ville Syrjälä709e05c2016-05-13 23:41:33 +03005369 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz, VCO: %d kHz, ref: %d kHz\n",
5370 dev_priv->cdclk_freq, dev_priv->cdclk_pll.vco,
5371 dev_priv->cdclk_pll.ref);
Ville Syrjälä2f2a1212016-05-13 23:41:25 +03005372 else
5373 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5374 dev_priv->cdclk_freq);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005375
5376 /*
Ville Syrjäläb5d99ff2016-04-26 19:46:34 +03005377 * 9:0 CMBUS [sic] CDCLK frequency (cdfreq):
5378 * Programmng [sic] note: bit[9:2] should be programmed to the number
5379 * of cdclk that generates 4MHz reference clock freq which is used to
5380 * generate GMBus clock. This will vary with the cdclk freq.
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005381 */
Ville Syrjäläb5d99ff2016-04-26 19:46:34 +03005382 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005383 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005384}
5385
Ville Syrjälä92891e42016-05-11 22:44:45 +03005386/* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5387static int skl_cdclk_decimal(int cdclk)
5388{
5389 return DIV_ROUND_CLOSEST(cdclk - 1000, 500);
5390}
5391
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005392static int bxt_de_pll_vco(struct drm_i915_private *dev_priv, int cdclk)
5393{
5394 int ratio;
5395
5396 if (cdclk == dev_priv->cdclk_pll.ref)
5397 return 0;
5398
5399 switch (cdclk) {
5400 default:
5401 MISSING_CASE(cdclk);
5402 case 144000:
5403 case 288000:
5404 case 384000:
5405 case 576000:
5406 ratio = 60;
5407 break;
5408 case 624000:
5409 ratio = 65;
5410 break;
5411 }
5412
5413 return dev_priv->cdclk_pll.ref * ratio;
5414}
5415
Ville Syrjälä2b730012016-05-13 23:41:34 +03005416static void bxt_de_pll_disable(struct drm_i915_private *dev_priv)
5417{
5418 I915_WRITE(BXT_DE_PLL_ENABLE, 0);
5419
5420 /* Timeout 200us */
Chris Wilson95cac282016-06-30 15:33:03 +01005421 if (intel_wait_for_register(dev_priv,
5422 BXT_DE_PLL_ENABLE, BXT_DE_PLL_LOCK, 0,
5423 1))
Ville Syrjälä2b730012016-05-13 23:41:34 +03005424 DRM_ERROR("timeout waiting for DE PLL unlock\n");
Ville Syrjälä83d7c812016-05-13 23:41:35 +03005425
5426 dev_priv->cdclk_pll.vco = 0;
Ville Syrjälä2b730012016-05-13 23:41:34 +03005427}
5428
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005429static void bxt_de_pll_enable(struct drm_i915_private *dev_priv, int vco)
Ville Syrjälä2b730012016-05-13 23:41:34 +03005430{
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005431 int ratio = DIV_ROUND_CLOSEST(vco, dev_priv->cdclk_pll.ref);
Ville Syrjälä2b730012016-05-13 23:41:34 +03005432 u32 val;
5433
5434 val = I915_READ(BXT_DE_PLL_CTL);
5435 val &= ~BXT_DE_PLL_RATIO_MASK;
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005436 val |= BXT_DE_PLL_RATIO(ratio);
Ville Syrjälä2b730012016-05-13 23:41:34 +03005437 I915_WRITE(BXT_DE_PLL_CTL, val);
5438
5439 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5440
5441 /* Timeout 200us */
Chris Wilsone084e1b2016-06-30 15:33:04 +01005442 if (intel_wait_for_register(dev_priv,
5443 BXT_DE_PLL_ENABLE,
5444 BXT_DE_PLL_LOCK,
5445 BXT_DE_PLL_LOCK,
5446 1))
Ville Syrjälä2b730012016-05-13 23:41:34 +03005447 DRM_ERROR("timeout waiting for DE PLL lock\n");
Ville Syrjälä83d7c812016-05-13 23:41:35 +03005448
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005449 dev_priv->cdclk_pll.vco = vco;
Ville Syrjälä2b730012016-05-13 23:41:34 +03005450}
5451
Imre Deak324513c2016-06-13 16:44:36 +03005452static void bxt_set_cdclk(struct drm_i915_private *dev_priv, int cdclk)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305453{
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005454 u32 val, divider;
5455 int vco, ret;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305456
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005457 vco = bxt_de_pll_vco(dev_priv, cdclk);
5458
5459 DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
5460
5461 /* cdclk = vco / 2 / div{1,1.5,2,4} */
5462 switch (DIV_ROUND_CLOSEST(vco, cdclk)) {
5463 case 8:
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305464 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305465 break;
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005466 case 4:
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305467 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305468 break;
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005469 case 3:
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305470 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305471 break;
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005472 case 2:
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305473 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305474 break;
5475 default:
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005476 WARN_ON(cdclk != dev_priv->cdclk_pll.ref);
5477 WARN_ON(vco != 0);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305478
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005479 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5480 break;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305481 }
5482
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305483 /* Inform power controller of upcoming frequency change */
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005484 mutex_lock(&dev_priv->rps.hw_lock);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305485 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5486 0x80000000);
5487 mutex_unlock(&dev_priv->rps.hw_lock);
5488
5489 if (ret) {
5490 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005491 ret, cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305492 return;
5493 }
5494
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005495 if (dev_priv->cdclk_pll.vco != 0 &&
5496 dev_priv->cdclk_pll.vco != vco)
Ville Syrjälä2b730012016-05-13 23:41:34 +03005497 bxt_de_pll_disable(dev_priv);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305498
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005499 if (dev_priv->cdclk_pll.vco != vco)
5500 bxt_de_pll_enable(dev_priv, vco);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305501
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005502 val = divider | skl_cdclk_decimal(cdclk);
5503 /*
5504 * FIXME if only the cd2x divider needs changing, it could be done
5505 * without shutting off the pipe (if only one pipe is active).
5506 */
5507 val |= BXT_CDCLK_CD2X_PIPE_NONE;
5508 /*
5509 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5510 * enable otherwise.
5511 */
5512 if (cdclk >= 500000)
5513 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5514 I915_WRITE(CDCLK_CTL, val);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305515
5516 mutex_lock(&dev_priv->rps.hw_lock);
5517 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005518 DIV_ROUND_UP(cdclk, 25000));
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305519 mutex_unlock(&dev_priv->rps.hw_lock);
5520
5521 if (ret) {
5522 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005523 ret, cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305524 return;
5525 }
5526
Imre Deakc6c46962016-04-01 16:02:40 +03005527 intel_update_cdclk(dev_priv->dev);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305528}
5529
Imre Deakd66a2192016-05-24 15:38:33 +03005530static void bxt_sanitize_cdclk(struct drm_i915_private *dev_priv)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305531{
Imre Deakd66a2192016-05-24 15:38:33 +03005532 u32 cdctl, expected;
5533
Ville Syrjälä089c6fd2016-05-13 23:41:36 +03005534 intel_update_cdclk(dev_priv->dev);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305535
Imre Deakd66a2192016-05-24 15:38:33 +03005536 if (dev_priv->cdclk_pll.vco == 0 ||
5537 dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
5538 goto sanitize;
5539
5540 /* DPLL okay; verify the cdclock
5541 *
5542 * Some BIOS versions leave an incorrect decimal frequency value and
5543 * set reserved MBZ bits in CDCLK_CTL at least during exiting from S4,
5544 * so sanitize this register.
5545 */
5546 cdctl = I915_READ(CDCLK_CTL);
5547 /*
5548 * Let's ignore the pipe field, since BIOS could have configured the
5549 * dividers both synching to an active pipe, or asynchronously
5550 * (PIPE_NONE).
5551 */
5552 cdctl &= ~BXT_CDCLK_CD2X_PIPE_NONE;
5553
5554 expected = (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) |
5555 skl_cdclk_decimal(dev_priv->cdclk_freq);
5556 /*
5557 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5558 * enable otherwise.
5559 */
5560 if (dev_priv->cdclk_freq >= 500000)
5561 expected |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5562
5563 if (cdctl == expected)
5564 /* All well; nothing to sanitize */
5565 return;
5566
5567sanitize:
5568 DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
5569
5570 /* force cdclk programming */
5571 dev_priv->cdclk_freq = 0;
5572
5573 /* force full PLL disable + enable */
5574 dev_priv->cdclk_pll.vco = -1;
5575}
5576
Imre Deak324513c2016-06-13 16:44:36 +03005577void bxt_init_cdclk(struct drm_i915_private *dev_priv)
Imre Deakd66a2192016-05-24 15:38:33 +03005578{
5579 bxt_sanitize_cdclk(dev_priv);
5580
5581 if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0)
Ville Syrjälä089c6fd2016-05-13 23:41:36 +03005582 return;
Imre Deakc2e001e2016-04-01 16:02:43 +03005583
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305584 /*
5585 * FIXME:
5586 * - The initial CDCLK needs to be read from VBT.
5587 * Need to make this change after VBT has changes for BXT.
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305588 */
Imre Deak324513c2016-06-13 16:44:36 +03005589 bxt_set_cdclk(dev_priv, bxt_calc_cdclk(0));
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305590}
5591
Imre Deak324513c2016-06-13 16:44:36 +03005592void bxt_uninit_cdclk(struct drm_i915_private *dev_priv)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305593{
Imre Deak324513c2016-06-13 16:44:36 +03005594 bxt_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305595}
5596
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005597static int skl_calc_cdclk(int max_pixclk, int vco)
5598{
Ville Syrjälä63911d72016-05-13 23:41:32 +03005599 if (vco == 8640000) {
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005600 if (max_pixclk > 540000)
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005601 return 617143;
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005602 else if (max_pixclk > 432000)
5603 return 540000;
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005604 else if (max_pixclk > 308571)
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005605 return 432000;
5606 else
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005607 return 308571;
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005608 } else {
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005609 if (max_pixclk > 540000)
5610 return 675000;
5611 else if (max_pixclk > 450000)
5612 return 540000;
5613 else if (max_pixclk > 337500)
5614 return 450000;
5615 else
5616 return 337500;
5617 }
5618}
5619
Ville Syrjäläea617912016-05-13 23:41:24 +03005620static void
5621skl_dpll0_update(struct drm_i915_private *dev_priv)
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005622{
Ville Syrjäläea617912016-05-13 23:41:24 +03005623 u32 val;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005624
Ville Syrjälä709e05c2016-05-13 23:41:33 +03005625 dev_priv->cdclk_pll.ref = 24000;
Imre Deak1c3f7702016-05-24 15:38:32 +03005626 dev_priv->cdclk_pll.vco = 0;
Ville Syrjälä709e05c2016-05-13 23:41:33 +03005627
Ville Syrjäläea617912016-05-13 23:41:24 +03005628 val = I915_READ(LCPLL1_CTL);
Imre Deak1c3f7702016-05-24 15:38:32 +03005629 if ((val & LCPLL_PLL_ENABLE) == 0)
Ville Syrjäläea617912016-05-13 23:41:24 +03005630 return;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005631
Imre Deak1c3f7702016-05-24 15:38:32 +03005632 if (WARN_ON((val & LCPLL_PLL_LOCK) == 0))
5633 return;
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005634
Ville Syrjäläea617912016-05-13 23:41:24 +03005635 val = I915_READ(DPLL_CTRL1);
5636
Imre Deak1c3f7702016-05-24 15:38:32 +03005637 if (WARN_ON((val & (DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) |
5638 DPLL_CTRL1_SSC(SKL_DPLL0) |
5639 DPLL_CTRL1_OVERRIDE(SKL_DPLL0))) !=
5640 DPLL_CTRL1_OVERRIDE(SKL_DPLL0)))
5641 return;
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005642
Ville Syrjäläea617912016-05-13 23:41:24 +03005643 switch (val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) {
5644 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810, SKL_DPLL0):
5645 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1350, SKL_DPLL0):
5646 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1620, SKL_DPLL0):
5647 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2700, SKL_DPLL0):
Ville Syrjälä63911d72016-05-13 23:41:32 +03005648 dev_priv->cdclk_pll.vco = 8100000;
Ville Syrjäläea617912016-05-13 23:41:24 +03005649 break;
5650 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080, SKL_DPLL0):
5651 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2160, SKL_DPLL0):
Ville Syrjälä63911d72016-05-13 23:41:32 +03005652 dev_priv->cdclk_pll.vco = 8640000;
Ville Syrjäläea617912016-05-13 23:41:24 +03005653 break;
5654 default:
5655 MISSING_CASE(val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
Ville Syrjäläea617912016-05-13 23:41:24 +03005656 break;
5657 }
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005658}
5659
Ville Syrjäläb2045352016-05-13 23:41:27 +03005660void skl_set_preferred_cdclk_vco(struct drm_i915_private *dev_priv, int vco)
5661{
5662 bool changed = dev_priv->skl_preferred_vco_freq != vco;
5663
5664 dev_priv->skl_preferred_vco_freq = vco;
5665
5666 if (changed)
5667 intel_update_max_cdclk(dev_priv->dev);
5668}
5669
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005670static void
Ville Syrjälä3861fc62016-05-11 22:44:50 +03005671skl_dpll0_enable(struct drm_i915_private *dev_priv, int vco)
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005672{
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005673 int min_cdclk = skl_calc_cdclk(0, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005674 u32 val;
5675
Ville Syrjälä63911d72016-05-13 23:41:32 +03005676 WARN_ON(vco != 8100000 && vco != 8640000);
Ville Syrjäläb2045352016-05-13 23:41:27 +03005677
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005678 /* select the minimum CDCLK before enabling DPLL 0 */
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005679 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_cdclk);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005680 I915_WRITE(CDCLK_CTL, val);
5681 POSTING_READ(CDCLK_CTL);
5682
5683 /*
5684 * We always enable DPLL0 with the lowest link rate possible, but still
5685 * taking into account the VCO required to operate the eDP panel at the
5686 * desired frequency. The usual DP link rates operate with a VCO of
5687 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5688 * The modeset code is responsible for the selection of the exact link
5689 * rate later on, with the constraint of choosing a frequency that
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005690 * works with vco.
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005691 */
5692 val = I915_READ(DPLL_CTRL1);
5693
5694 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5695 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5696 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
Ville Syrjälä63911d72016-05-13 23:41:32 +03005697 if (vco == 8640000)
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005698 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5699 SKL_DPLL0);
5700 else
5701 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5702 SKL_DPLL0);
5703
5704 I915_WRITE(DPLL_CTRL1, val);
5705 POSTING_READ(DPLL_CTRL1);
5706
5707 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5708
Chris Wilsone24ca052016-06-30 15:33:05 +01005709 if (intel_wait_for_register(dev_priv,
5710 LCPLL1_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
5711 5))
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005712 DRM_ERROR("DPLL0 not locked\n");
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005713
Ville Syrjälä63911d72016-05-13 23:41:32 +03005714 dev_priv->cdclk_pll.vco = vco;
Ville Syrjäläb2045352016-05-13 23:41:27 +03005715
5716 /* We'll want to keep using the current vco from now on. */
5717 skl_set_preferred_cdclk_vco(dev_priv, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005718}
5719
Ville Syrjälä430e05d2016-05-11 22:44:47 +03005720static void
5721skl_dpll0_disable(struct drm_i915_private *dev_priv)
5722{
5723 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
Chris Wilson8ad32a02016-06-30 15:33:06 +01005724 if (intel_wait_for_register(dev_priv,
5725 LCPLL1_CTL, LCPLL_PLL_LOCK, 0,
5726 1))
Ville Syrjälä430e05d2016-05-11 22:44:47 +03005727 DRM_ERROR("Couldn't disable DPLL0\n");
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005728
Ville Syrjälä63911d72016-05-13 23:41:32 +03005729 dev_priv->cdclk_pll.vco = 0;
Ville Syrjälä430e05d2016-05-11 22:44:47 +03005730}
5731
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005732static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5733{
5734 int ret;
5735 u32 val;
5736
5737 /* inform PCU we want to change CDCLK */
5738 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5739 mutex_lock(&dev_priv->rps.hw_lock);
5740 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5741 mutex_unlock(&dev_priv->rps.hw_lock);
5742
5743 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5744}
5745
5746static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5747{
5748 unsigned int i;
5749
5750 for (i = 0; i < 15; i++) {
5751 if (skl_cdclk_pcu_ready(dev_priv))
5752 return true;
5753 udelay(10);
5754 }
5755
5756 return false;
5757}
5758
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005759static void skl_set_cdclk(struct drm_i915_private *dev_priv, int cdclk, int vco)
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005760{
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005761 struct drm_device *dev = dev_priv->dev;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005762 u32 freq_select, pcu_ack;
5763
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005764 WARN_ON((cdclk == 24000) != (vco == 0));
5765
Ville Syrjälä63911d72016-05-13 23:41:32 +03005766 DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005767
5768 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5769 DRM_ERROR("failed to inform PCU about cdclk change\n");
5770 return;
5771 }
5772
5773 /* set CDCLK_CTL */
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005774 switch (cdclk) {
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005775 case 450000:
5776 case 432000:
5777 freq_select = CDCLK_FREQ_450_432;
5778 pcu_ack = 1;
5779 break;
5780 case 540000:
5781 freq_select = CDCLK_FREQ_540;
5782 pcu_ack = 2;
5783 break;
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005784 case 308571:
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005785 case 337500:
5786 default:
5787 freq_select = CDCLK_FREQ_337_308;
5788 pcu_ack = 0;
5789 break;
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005790 case 617143:
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005791 case 675000:
5792 freq_select = CDCLK_FREQ_675_617;
5793 pcu_ack = 3;
5794 break;
5795 }
5796
Ville Syrjälä63911d72016-05-13 23:41:32 +03005797 if (dev_priv->cdclk_pll.vco != 0 &&
5798 dev_priv->cdclk_pll.vco != vco)
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005799 skl_dpll0_disable(dev_priv);
5800
Ville Syrjälä63911d72016-05-13 23:41:32 +03005801 if (dev_priv->cdclk_pll.vco != vco)
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005802 skl_dpll0_enable(dev_priv, vco);
5803
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005804 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(cdclk));
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005805 POSTING_READ(CDCLK_CTL);
5806
5807 /* inform PCU of the change */
5808 mutex_lock(&dev_priv->rps.hw_lock);
5809 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5810 mutex_unlock(&dev_priv->rps.hw_lock);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005811
5812 intel_update_cdclk(dev);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005813}
5814
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005815static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv);
5816
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005817void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5818{
Ville Syrjälä709e05c2016-05-13 23:41:33 +03005819 skl_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref, 0);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005820}
5821
5822void skl_init_cdclk(struct drm_i915_private *dev_priv)
5823{
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005824 int cdclk, vco;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005825
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005826 skl_sanitize_cdclk(dev_priv);
5827
Ville Syrjälä63911d72016-05-13 23:41:32 +03005828 if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0) {
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005829 /*
5830 * Use the current vco as our initial
5831 * guess as to what the preferred vco is.
5832 */
5833 if (dev_priv->skl_preferred_vco_freq == 0)
5834 skl_set_preferred_cdclk_vco(dev_priv,
Ville Syrjälä63911d72016-05-13 23:41:32 +03005835 dev_priv->cdclk_pll.vco);
Ville Syrjälä70c2c182016-05-13 23:41:30 +03005836 return;
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005837 }
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005838
Ville Syrjälä70c2c182016-05-13 23:41:30 +03005839 vco = dev_priv->skl_preferred_vco_freq;
5840 if (vco == 0)
Ville Syrjälä63911d72016-05-13 23:41:32 +03005841 vco = 8100000;
Ville Syrjälä70c2c182016-05-13 23:41:30 +03005842 cdclk = skl_calc_cdclk(0, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005843
Ville Syrjälä70c2c182016-05-13 23:41:30 +03005844 skl_set_cdclk(dev_priv, cdclk, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005845}
5846
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005847static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305848{
Ville Syrjälä09492492016-05-13 23:41:28 +03005849 uint32_t cdctl, expected;
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305850
Shobhit Kumarf1b391a2015-11-05 18:05:32 +05305851 /*
5852 * check if the pre-os intialized the display
5853 * There is SWF18 scratchpad register defined which is set by the
5854 * pre-os which can be used by the OS drivers to check the status
5855 */
5856 if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
5857 goto sanitize;
5858
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005859 intel_update_cdclk(dev_priv->dev);
Imre Deak1c3f7702016-05-24 15:38:32 +03005860 /* Is PLL enabled and locked ? */
5861 if (dev_priv->cdclk_pll.vco == 0 ||
5862 dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
5863 goto sanitize;
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005864
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305865 /* DPLL okay; verify the cdclock
5866 *
5867 * Noticed in some instances that the freq selection is correct but
5868 * decimal part is programmed wrong from BIOS where pre-os does not
5869 * enable display. Verify the same as well.
5870 */
Ville Syrjälä09492492016-05-13 23:41:28 +03005871 cdctl = I915_READ(CDCLK_CTL);
5872 expected = (cdctl & CDCLK_FREQ_SEL_MASK) |
5873 skl_cdclk_decimal(dev_priv->cdclk_freq);
5874 if (cdctl == expected)
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305875 /* All well; nothing to sanitize */
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005876 return;
5877
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305878sanitize:
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005879 DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
Clint Taylorc89e39f2016-05-13 23:41:21 +03005880
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005881 /* force cdclk programming */
5882 dev_priv->cdclk_freq = 0;
5883 /* force full PLL disable + enable */
Ville Syrjälä63911d72016-05-13 23:41:32 +03005884 dev_priv->cdclk_pll.vco = -1;
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305885}
5886
Jesse Barnes30a970c2013-11-04 13:48:12 -08005887/* Adjust CDclk dividers to allow high res or save power if possible */
5888static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5889{
5890 struct drm_i915_private *dev_priv = dev->dev_private;
5891 u32 val, cmd;
5892
Vandana Kannan164dfd22014-11-24 13:37:41 +05305893 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5894 != dev_priv->cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02005895
Ville Syrjälädfcab172014-06-13 13:37:47 +03005896 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08005897 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03005898 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005899 cmd = 1;
5900 else
5901 cmd = 0;
5902
5903 mutex_lock(&dev_priv->rps.hw_lock);
5904 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5905 val &= ~DSPFREQGUAR_MASK;
5906 val |= (cmd << DSPFREQGUAR_SHIFT);
5907 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5908 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5909 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5910 50)) {
5911 DRM_ERROR("timed out waiting for CDclk change\n");
5912 }
5913 mutex_unlock(&dev_priv->rps.hw_lock);
5914
Ville Syrjälä54433e92015-05-26 20:42:31 +03005915 mutex_lock(&dev_priv->sb_lock);
5916
Ville Syrjälädfcab172014-06-13 13:37:47 +03005917 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005918 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005919
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005920 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005921
Jesse Barnes30a970c2013-11-04 13:48:12 -08005922 /* adjust cdclk divider */
5923 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Vandana Kannan87d5d252015-09-24 23:29:17 +03005924 val &= ~CCK_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005925 val |= divider;
5926 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03005927
5928 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
Vandana Kannan87d5d252015-09-24 23:29:17 +03005929 CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
Ville Syrjäläa877e802014-06-13 13:37:52 +03005930 50))
5931 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08005932 }
5933
Jesse Barnes30a970c2013-11-04 13:48:12 -08005934 /* adjust self-refresh exit latency value */
5935 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5936 val &= ~0x7f;
5937
5938 /*
5939 * For high bandwidth configs, we set a higher latency in the bunit
5940 * so that the core display fetch happens in time to avoid underruns.
5941 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03005942 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005943 val |= 4500 / 250; /* 4.5 usec */
5944 else
5945 val |= 3000 / 250; /* 3.0 usec */
5946 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
Ville Syrjälä54433e92015-05-26 20:42:31 +03005947
Ville Syrjäläa5805162015-05-26 20:42:30 +03005948 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005949
Ville Syrjäläb6283052015-06-03 15:45:07 +03005950 intel_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005951}
5952
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005953static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5954{
5955 struct drm_i915_private *dev_priv = dev->dev_private;
5956 u32 val, cmd;
5957
Vandana Kannan164dfd22014-11-24 13:37:41 +05305958 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5959 != dev_priv->cdclk_freq);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005960
5961 switch (cdclk) {
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005962 case 333333:
5963 case 320000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005964 case 266667:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005965 case 200000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005966 break;
5967 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01005968 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005969 return;
5970 }
5971
Ville Syrjälä9d0d3fd2015-03-02 20:07:17 +02005972 /*
5973 * Specs are full of misinformation, but testing on actual
5974 * hardware has shown that we just need to write the desired
5975 * CCK divider into the Punit register.
5976 */
5977 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5978
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005979 mutex_lock(&dev_priv->rps.hw_lock);
5980 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5981 val &= ~DSPFREQGUAR_MASK_CHV;
5982 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5983 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5984 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5985 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5986 50)) {
5987 DRM_ERROR("timed out waiting for CDclk change\n");
5988 }
5989 mutex_unlock(&dev_priv->rps.hw_lock);
5990
Ville Syrjäläb6283052015-06-03 15:45:07 +03005991 intel_update_cdclk(dev);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005992}
5993
Jesse Barnes30a970c2013-11-04 13:48:12 -08005994static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5995 int max_pixclk)
5996{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005997 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005998 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005999
Jesse Barnes30a970c2013-11-04 13:48:12 -08006000 /*
6001 * Really only a few cases to deal with, as only 4 CDclks are supported:
6002 * 200MHz
6003 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03006004 * 320/333MHz (depends on HPLL freq)
Ville Syrjälä6cca3192015-03-02 20:07:16 +02006005 * 400MHz (VLV only)
6006 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
6007 * of the lower bin and adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03006008 *
6009 * We seem to get an unstable or solid color picture at 200MHz.
6010 * Not sure what's wrong. For now use 200MHz only when all pipes
6011 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08006012 */
Ville Syrjälä6cca3192015-03-02 20:07:16 +02006013 if (!IS_CHERRYVIEW(dev_priv) &&
6014 max_pixclk > freq_320*limit/100)
Ville Syrjälädfcab172014-06-13 13:37:47 +03006015 return 400000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02006016 else if (max_pixclk > 266667*limit/100)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03006017 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03006018 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03006019 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03006020 else
6021 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006022}
6023
Imre Deak324513c2016-06-13 16:44:36 +03006024static int bxt_calc_cdclk(int max_pixclk)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006025{
Ville Syrjälä760e1472016-05-11 22:44:46 +03006026 if (max_pixclk > 576000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306027 return 624000;
Ville Syrjälä760e1472016-05-11 22:44:46 +03006028 else if (max_pixclk > 384000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306029 return 576000;
Ville Syrjälä760e1472016-05-11 22:44:46 +03006030 else if (max_pixclk > 288000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306031 return 384000;
Ville Syrjälä760e1472016-05-11 22:44:46 +03006032 else if (max_pixclk > 144000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306033 return 288000;
6034 else
6035 return 144000;
6036}
6037
Maarten Lankhorste8788cb2016-02-16 10:25:11 +01006038/* Compute the max pixel clock for new configuration. */
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006039static int intel_mode_max_pixclk(struct drm_device *dev,
6040 struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006041{
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006042 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
6043 struct drm_i915_private *dev_priv = dev->dev_private;
6044 struct drm_crtc *crtc;
6045 struct drm_crtc_state *crtc_state;
6046 unsigned max_pixclk = 0, i;
6047 enum pipe pipe;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006048
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006049 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
6050 sizeof(intel_state->min_pixclk));
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006051
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006052 for_each_crtc_in_state(state, crtc, crtc_state, i) {
6053 int pixclk = 0;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006054
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006055 if (crtc_state->enable)
6056 pixclk = crtc_state->adjusted_mode.crtc_clock;
6057
6058 intel_state->min_pixclk[i] = pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006059 }
6060
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006061 for_each_pipe(dev_priv, pipe)
6062 max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
6063
Jesse Barnes30a970c2013-11-04 13:48:12 -08006064 return max_pixclk;
6065}
6066
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006067static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006068{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006069 struct drm_device *dev = state->dev;
6070 struct drm_i915_private *dev_priv = dev->dev_private;
6071 int max_pixclk = intel_mode_max_pixclk(dev, state);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006072 struct intel_atomic_state *intel_state =
6073 to_intel_atomic_state(state);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006074
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006075 intel_state->cdclk = intel_state->dev_cdclk =
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006076 valleyview_calc_cdclk(dev_priv, max_pixclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306077
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006078 if (!intel_state->active_crtcs)
6079 intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
6080
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006081 return 0;
6082}
Jesse Barnes30a970c2013-11-04 13:48:12 -08006083
Imre Deak324513c2016-06-13 16:44:36 +03006084static int bxt_modeset_calc_cdclk(struct drm_atomic_state *state)
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006085{
Ville Syrjälä4e5ca602016-05-11 22:44:44 +03006086 int max_pixclk = ilk_max_pixel_rate(state);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006087 struct intel_atomic_state *intel_state =
6088 to_intel_atomic_state(state);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02006089
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006090 intel_state->cdclk = intel_state->dev_cdclk =
Imre Deak324513c2016-06-13 16:44:36 +03006091 bxt_calc_cdclk(max_pixclk);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02006092
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006093 if (!intel_state->active_crtcs)
Imre Deak324513c2016-06-13 16:44:36 +03006094 intel_state->dev_cdclk = bxt_calc_cdclk(0);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006095
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006096 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006097}
6098
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006099static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
6100{
6101 unsigned int credits, default_credits;
6102
6103 if (IS_CHERRYVIEW(dev_priv))
6104 default_credits = PFI_CREDIT(12);
6105 else
6106 default_credits = PFI_CREDIT(8);
6107
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03006108 if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006109 /* CHV suggested value is 31 or 63 */
6110 if (IS_CHERRYVIEW(dev_priv))
Ville Syrjäläfcc00082015-05-26 20:22:40 +03006111 credits = PFI_CREDIT_63;
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006112 else
6113 credits = PFI_CREDIT(15);
6114 } else {
6115 credits = default_credits;
6116 }
6117
6118 /*
6119 * WA - write default credits before re-programming
6120 * FIXME: should we also set the resend bit here?
6121 */
6122 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6123 default_credits);
6124
6125 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6126 credits | PFI_CREDIT_RESEND);
6127
6128 /*
6129 * FIXME is this guaranteed to clear
6130 * immediately or should we poll for it?
6131 */
6132 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
6133}
6134
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006135static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006136{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006137 struct drm_device *dev = old_state->dev;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006138 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006139 struct intel_atomic_state *old_intel_state =
6140 to_intel_atomic_state(old_state);
6141 unsigned req_cdclk = old_intel_state->dev_cdclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006142
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006143 /*
6144 * FIXME: We can end up here with all power domains off, yet
6145 * with a CDCLK frequency other than the minimum. To account
6146 * for this take the PIPE-A power domain, which covers the HW
6147 * blocks needed for the following programming. This can be
6148 * removed once it's guaranteed that we get here either with
6149 * the minimum CDCLK set, or the required power domains
6150 * enabled.
6151 */
6152 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006153
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006154 if (IS_CHERRYVIEW(dev))
6155 cherryview_set_cdclk(dev, req_cdclk);
6156 else
6157 valleyview_set_cdclk(dev, req_cdclk);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006158
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006159 vlv_program_pfi_credits(dev_priv);
Imre Deak738c05c2014-11-19 16:25:37 +02006160
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006161 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006162}
6163
Jesse Barnes89b667f2013-04-18 14:51:36 -07006164static void valleyview_crtc_enable(struct drm_crtc *crtc)
6165{
6166 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006167 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006168 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6169 struct intel_encoder *encoder;
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006170 struct intel_crtc_state *pipe_config =
6171 to_intel_crtc_state(crtc->state);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006172 int pipe = intel_crtc->pipe;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006173
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006174 if (WARN_ON(intel_crtc->active))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006175 return;
6176
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006177 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306178 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006179
6180 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02006181 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006182
Ville Syrjäläc14b0482014-10-16 20:52:34 +03006183 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
6184 struct drm_i915_private *dev_priv = dev->dev_private;
6185
6186 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6187 I915_WRITE(CHV_CANVAS(pipe), 0);
6188 }
6189
Daniel Vetter5b18e572014-04-24 23:55:06 +02006190 i9xx_set_pipeconf(intel_crtc);
6191
Jesse Barnes89b667f2013-04-18 14:51:36 -07006192 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006193
Daniel Vettera72e4c92014-09-30 10:56:47 +02006194 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006195
Jesse Barnes89b667f2013-04-18 14:51:36 -07006196 for_each_encoder_on_crtc(dev, crtc, encoder)
6197 if (encoder->pre_pll_enable)
6198 encoder->pre_pll_enable(encoder);
6199
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006200 if (IS_CHERRYVIEW(dev)) {
6201 chv_prepare_pll(intel_crtc, intel_crtc->config);
6202 chv_enable_pll(intel_crtc, intel_crtc->config);
6203 } else {
6204 vlv_prepare_pll(intel_crtc, intel_crtc->config);
6205 vlv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006206 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07006207
6208 for_each_encoder_on_crtc(dev, crtc, encoder)
6209 if (encoder->pre_enable)
6210 encoder->pre_enable(encoder);
6211
Jesse Barnes2dd24552013-04-25 12:55:01 -07006212 i9xx_pfit_enable(intel_crtc);
6213
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006214 intel_color_load_luts(&pipe_config->base);
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006215
Ville Syrjäläcaed3612016-03-09 19:07:25 +02006216 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006217 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006218
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006219 assert_vblank_disabled(crtc);
6220 drm_crtc_vblank_on(crtc);
6221
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006222 for_each_encoder_on_crtc(dev, crtc, encoder)
6223 encoder->enable(encoder);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006224}
6225
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006226static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6227{
6228 struct drm_device *dev = crtc->base.dev;
6229 struct drm_i915_private *dev_priv = dev->dev_private;
6230
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006231 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6232 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006233}
6234
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006235static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006236{
6237 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006238 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006239 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006240 struct intel_encoder *encoder;
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006241 struct intel_crtc_state *pipe_config =
6242 to_intel_crtc_state(crtc->state);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006243 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08006244
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006245 if (WARN_ON(intel_crtc->active))
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006246 return;
6247
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006248 i9xx_set_pll_dividers(intel_crtc);
6249
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006250 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306251 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006252
6253 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02006254 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006255
Daniel Vetter5b18e572014-04-24 23:55:06 +02006256 i9xx_set_pipeconf(intel_crtc);
6257
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006258 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01006259
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006260 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006261 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006262
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02006263 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02006264 if (encoder->pre_enable)
6265 encoder->pre_enable(encoder);
6266
Daniel Vetterf6736a12013-06-05 13:34:30 +02006267 i9xx_enable_pll(intel_crtc);
6268
Jesse Barnes2dd24552013-04-25 12:55:01 -07006269 i9xx_pfit_enable(intel_crtc);
6270
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006271 intel_color_load_luts(&pipe_config->base);
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006272
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006273 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006274 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006275
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006276 assert_vblank_disabled(crtc);
6277 drm_crtc_vblank_on(crtc);
6278
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006279 for_each_encoder_on_crtc(dev, crtc, encoder)
6280 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006281}
6282
Daniel Vetter87476d62013-04-11 16:29:06 +02006283static void i9xx_pfit_disable(struct intel_crtc *crtc)
6284{
6285 struct drm_device *dev = crtc->base.dev;
6286 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02006287
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006288 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02006289 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02006290
6291 assert_pipe_disabled(dev_priv, crtc->pipe);
6292
Daniel Vetter328d8e82013-05-08 10:36:31 +02006293 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6294 I915_READ(PFIT_CONTROL));
6295 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02006296}
6297
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006298static void i9xx_crtc_disable(struct drm_crtc *crtc)
6299{
6300 struct drm_device *dev = crtc->dev;
6301 struct drm_i915_private *dev_priv = dev->dev_private;
6302 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006303 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006304 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006305
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006306 /*
6307 * On gen2 planes are double buffered but the pipe isn't, so we must
6308 * wait for planes to fully turn off before disabling the pipe.
6309 */
Ander Conselvan de Oliveira90e83e52016-03-22 10:11:24 +02006310 if (IS_GEN2(dev))
6311 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006312
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006313 for_each_encoder_on_crtc(dev, crtc, encoder)
6314 encoder->disable(encoder);
6315
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006316 drm_crtc_vblank_off(crtc);
6317 assert_vblank_disabled(crtc);
6318
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03006319 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006320
Daniel Vetter87476d62013-04-11 16:29:06 +02006321 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006322
Jesse Barnes89b667f2013-04-18 14:51:36 -07006323 for_each_encoder_on_crtc(dev, crtc, encoder)
6324 if (encoder->post_disable)
6325 encoder->post_disable(encoder);
6326
Jani Nikulaa65347b2015-11-27 12:21:46 +02006327 if (!intel_crtc->config->has_dsi_encoder) {
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006328 if (IS_CHERRYVIEW(dev))
6329 chv_disable_pll(dev_priv, pipe);
6330 else if (IS_VALLEYVIEW(dev))
6331 vlv_disable_pll(dev_priv, pipe);
6332 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03006333 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006334 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006335
Ville Syrjäläd6db9952015-07-08 23:45:49 +03006336 for_each_encoder_on_crtc(dev, crtc, encoder)
6337 if (encoder->post_pll_disable)
6338 encoder->post_pll_disable(encoder);
6339
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006340 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006341 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006342}
6343
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006344static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006345{
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006346 struct intel_encoder *encoder;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006347 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006348 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006349 enum intel_display_power_domain domain;
6350 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006351
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006352 if (!intel_crtc->active)
6353 return;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006354
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006355 if (to_intel_plane_state(crtc->primary->state)->visible) {
Daniel Vetter5a21b662016-05-24 17:13:53 +02006356 WARN_ON(intel_crtc->flip_work);
Maarten Lankhorstfc32b1f2015-10-19 17:09:23 +02006357
Ville Syrjälä2622a082016-03-09 19:07:26 +02006358 intel_pre_disable_primary_noatomic(crtc);
Maarten Lankhorst54a419612015-11-23 10:25:28 +01006359
6360 intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
6361 to_intel_plane_state(crtc->primary->state)->visible = false;
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006362 }
6363
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006364 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006365
Ville Syrjälä78108b72016-05-27 20:59:19 +03006366 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
6367 crtc->base.id, crtc->name);
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006368
6369 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
6370 crtc->state->active = false;
Matt Roper37d90782015-09-24 15:53:06 -07006371 intel_crtc->active = false;
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006372 crtc->enabled = false;
6373 crtc->state->connector_mask = 0;
6374 crtc->state->encoder_mask = 0;
6375
6376 for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
6377 encoder->base.crtc = NULL;
6378
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -02006379 intel_fbc_disable(intel_crtc);
Matt Roper37d90782015-09-24 15:53:06 -07006380 intel_update_watermarks(crtc);
Maarten Lankhorst1f7457b2015-07-13 11:55:05 +02006381 intel_disable_shared_dpll(intel_crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006382
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006383 domains = intel_crtc->enabled_power_domains;
6384 for_each_power_domain(domain, domains)
6385 intel_display_power_put(dev_priv, domain);
6386 intel_crtc->enabled_power_domains = 0;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006387
6388 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
6389 dev_priv->min_pixclk[intel_crtc->pipe] = 0;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006390}
6391
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006392/*
6393 * turn all crtc's off, but do not adjust state
6394 * This has to be paired with a call to intel_modeset_setup_hw_state.
6395 */
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006396int intel_display_suspend(struct drm_device *dev)
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006397{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006398 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006399 struct drm_atomic_state *state;
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006400 int ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006401
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006402 state = drm_atomic_helper_suspend(dev);
6403 ret = PTR_ERR_OR_ZERO(state);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006404 if (ret)
6405 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006406 else
6407 dev_priv->modeset_restore_state = state;
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006408 return ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006409}
6410
Chris Wilsonea5b2132010-08-04 13:50:23 +01006411void intel_encoder_destroy(struct drm_encoder *encoder)
6412{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006413 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01006414
Chris Wilsonea5b2132010-08-04 13:50:23 +01006415 drm_encoder_cleanup(encoder);
6416 kfree(intel_encoder);
6417}
6418
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006419/* Cross check the actual hw state with our own modeset state tracking (and it's
6420 * internal consistency). */
Daniel Vetter5a21b662016-05-24 17:13:53 +02006421static void intel_connector_verify_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006422{
Daniel Vetter5a21b662016-05-24 17:13:53 +02006423 struct drm_crtc *crtc = connector->base.state->crtc;
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006424
6425 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6426 connector->base.base.id,
6427 connector->base.name);
6428
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006429 if (connector->get_hw_state(connector)) {
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006430 struct intel_encoder *encoder = connector->encoder;
Daniel Vetter5a21b662016-05-24 17:13:53 +02006431 struct drm_connector_state *conn_state = connector->base.state;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006432
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006433 I915_STATE_WARN(!crtc,
6434 "connector enabled without attached crtc\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006435
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006436 if (!crtc)
6437 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006438
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006439 I915_STATE_WARN(!crtc->state->active,
6440 "connector is active, but attached crtc isn't\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006441
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006442 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006443 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006444
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006445 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006446 "atomic encoder doesn't match attached encoder\n");
Dave Airlie36cd7442014-05-02 13:44:18 +10006447
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006448 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006449 "attached encoder crtc differs from connector crtc\n");
6450 } else {
Maarten Lankhorst4d688a22015-08-05 12:37:06 +02006451 I915_STATE_WARN(crtc && crtc->state->active,
6452 "attached crtc is active, but connector isn't\n");
Daniel Vetter5a21b662016-05-24 17:13:53 +02006453 I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006454 "best encoder set without crtc!\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006455 }
6456}
6457
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006458int intel_connector_init(struct intel_connector *connector)
6459{
Maarten Lankhorst5350a032016-01-04 12:53:15 +01006460 drm_atomic_helper_connector_reset(&connector->base);
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006461
Maarten Lankhorst5350a032016-01-04 12:53:15 +01006462 if (!connector->base.state)
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006463 return -ENOMEM;
6464
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006465 return 0;
6466}
6467
6468struct intel_connector *intel_connector_alloc(void)
6469{
6470 struct intel_connector *connector;
6471
6472 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6473 if (!connector)
6474 return NULL;
6475
6476 if (intel_connector_init(connector) < 0) {
6477 kfree(connector);
6478 return NULL;
6479 }
6480
6481 return connector;
6482}
6483
Daniel Vetterf0947c32012-07-02 13:10:34 +02006484/* Simple connector->get_hw_state implementation for encoders that support only
6485 * one connector and no cloning and hence the encoder state determines the state
6486 * of the connector. */
6487bool intel_connector_get_hw_state(struct intel_connector *connector)
6488{
Daniel Vetter24929352012-07-02 20:28:59 +02006489 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02006490 struct intel_encoder *encoder = connector->encoder;
6491
6492 return encoder->get_hw_state(encoder, &pipe);
6493}
6494
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006495static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006496{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006497 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6498 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006499
6500 return 0;
6501}
6502
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006503static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006504 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006505{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006506 struct drm_atomic_state *state = pipe_config->base.state;
6507 struct intel_crtc *other_crtc;
6508 struct intel_crtc_state *other_crtc_state;
6509
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006510 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6511 pipe_name(pipe), pipe_config->fdi_lanes);
6512 if (pipe_config->fdi_lanes > 4) {
6513 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6514 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006515 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006516 }
6517
Paulo Zanonibafb6552013-11-02 21:07:44 -07006518 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006519 if (pipe_config->fdi_lanes > 2) {
6520 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6521 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006522 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006523 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006524 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006525 }
6526 }
6527
6528 if (INTEL_INFO(dev)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006529 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006530
6531 /* Ivybridge 3 pipe is really complicated */
6532 switch (pipe) {
6533 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006534 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006535 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006536 if (pipe_config->fdi_lanes <= 2)
6537 return 0;
6538
6539 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6540 other_crtc_state =
6541 intel_atomic_get_crtc_state(state, other_crtc);
6542 if (IS_ERR(other_crtc_state))
6543 return PTR_ERR(other_crtc_state);
6544
6545 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006546 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6547 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006548 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006549 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006550 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006551 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02006552 if (pipe_config->fdi_lanes > 2) {
6553 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6554 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006555 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02006556 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006557
6558 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6559 other_crtc_state =
6560 intel_atomic_get_crtc_state(state, other_crtc);
6561 if (IS_ERR(other_crtc_state))
6562 return PTR_ERR(other_crtc_state);
6563
6564 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006565 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006566 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006567 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006568 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006569 default:
6570 BUG();
6571 }
6572}
6573
Daniel Vettere29c22c2013-02-21 00:00:16 +01006574#define RETRY 1
6575static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006576 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02006577{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006578 struct drm_device *dev = intel_crtc->base.dev;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006579 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006580 int lane, link_bw, fdi_dotclock, ret;
6581 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006582
Daniel Vettere29c22c2013-02-21 00:00:16 +01006583retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02006584 /* FDI is a binary signal running at ~2.7GHz, encoding
6585 * each output octet as 10 bits. The actual frequency
6586 * is stored as a divider into a 100MHz clock, and the
6587 * mode pixel clock is stored in units of 1KHz.
6588 * Hence the bw of each lane in terms of the mode signal
6589 * is:
6590 */
Ville Syrjälä21a727b2016-02-17 21:41:10 +02006591 link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006592
Damien Lespiau241bfc32013-09-25 16:45:37 +01006593 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006594
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006595 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006596 pipe_config->pipe_bpp);
6597
6598 pipe_config->fdi_lanes = lane;
6599
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006600 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006601 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006602
Ville Syrjäläe3b247d2016-02-17 21:41:09 +02006603 ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006604 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01006605 pipe_config->pipe_bpp -= 2*3;
6606 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6607 pipe_config->pipe_bpp);
6608 needs_recompute = true;
6609 pipe_config->bw_constrained = true;
6610
6611 goto retry;
6612 }
6613
6614 if (needs_recompute)
6615 return RETRY;
6616
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006617 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006618}
6619
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006620static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6621 struct intel_crtc_state *pipe_config)
6622{
6623 if (pipe_config->pipe_bpp > 24)
6624 return false;
6625
6626 /* HSW can handle pixel rate up to cdclk? */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03006627 if (IS_HASWELL(dev_priv))
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006628 return true;
6629
6630 /*
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03006631 * We compare against max which means we must take
6632 * the increased cdclk requirement into account when
6633 * calculating the new cdclk.
6634 *
6635 * Should measure whether using a lower cdclk w/o IPS
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006636 */
6637 return ilk_pipe_pixel_rate(pipe_config) <=
6638 dev_priv->max_cdclk_freq * 95 / 100;
6639}
6640
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006641static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006642 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006643{
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006644 struct drm_device *dev = crtc->base.dev;
6645 struct drm_i915_private *dev_priv = dev->dev_private;
6646
Jani Nikulad330a952014-01-21 11:24:25 +02006647 pipe_config->ips_enabled = i915.enable_ips &&
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006648 hsw_crtc_supports_ips(crtc) &&
6649 pipe_config_supports_ips(dev_priv, pipe_config);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006650}
6651
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006652static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
6653{
6654 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6655
6656 /* GDG double wide on either pipe, otherwise pipe A only */
6657 return INTEL_INFO(dev_priv)->gen < 4 &&
6658 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
6659}
6660
Daniel Vettera43f6e02013-06-07 23:10:32 +02006661static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006662 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006663{
Daniel Vettera43f6e02013-06-07 23:10:32 +02006664 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02006665 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006666 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ville Syrjäläf3261152016-05-24 21:34:18 +03006667 int clock_limit = dev_priv->max_dotclk_freq;
Chris Wilson89749352010-09-12 18:25:19 +01006668
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006669 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjäläf3261152016-05-24 21:34:18 +03006670 clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006671
6672 /*
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006673 * Enable double wide mode when the dot clock
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006674 * is > 90% of the (display) core speed.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006675 */
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006676 if (intel_crtc_supports_double_wide(crtc) &&
6677 adjusted_mode->crtc_clock > clock_limit) {
Ville Syrjäläf3261152016-05-24 21:34:18 +03006678 clock_limit = dev_priv->max_dotclk_freq;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006679 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006680 }
Ville Syrjäläf3261152016-05-24 21:34:18 +03006681 }
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006682
Ville Syrjäläf3261152016-05-24 21:34:18 +03006683 if (adjusted_mode->crtc_clock > clock_limit) {
6684 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
6685 adjusted_mode->crtc_clock, clock_limit,
6686 yesno(pipe_config->double_wide));
6687 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08006688 }
Chris Wilson89749352010-09-12 18:25:19 +01006689
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006690 /*
6691 * Pipe horizontal size must be even in:
6692 * - DVO ganged mode
6693 * - LVDS dual channel mode
6694 * - Double wide pipe
6695 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006696 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006697 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6698 pipe_config->pipe_src_w &= ~1;
6699
Damien Lespiau8693a822013-05-03 18:48:11 +01006700 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6701 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03006702 */
6703 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
Ville Syrjäläaad941d2015-09-25 16:38:56 +03006704 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006705 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03006706
Damien Lespiauf5adf942013-06-24 18:29:34 +01006707 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02006708 hsw_compute_ips_config(crtc, pipe_config);
6709
Daniel Vetter877d48d2013-04-19 11:24:43 +02006710 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02006711 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006712
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +02006713 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006714}
6715
Ville Syrjälä1652d192015-03-31 14:12:01 +03006716static int skylake_get_display_clock_speed(struct drm_device *dev)
6717{
6718 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläea617912016-05-13 23:41:24 +03006719 uint32_t cdctl;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006720
Ville Syrjäläea617912016-05-13 23:41:24 +03006721 skl_dpll0_update(dev_priv);
6722
Ville Syrjälä63911d72016-05-13 23:41:32 +03006723 if (dev_priv->cdclk_pll.vco == 0)
Ville Syrjälä709e05c2016-05-13 23:41:33 +03006724 return dev_priv->cdclk_pll.ref;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006725
Ville Syrjäläea617912016-05-13 23:41:24 +03006726 cdctl = I915_READ(CDCLK_CTL);
Ville Syrjälä1652d192015-03-31 14:12:01 +03006727
Ville Syrjälä63911d72016-05-13 23:41:32 +03006728 if (dev_priv->cdclk_pll.vco == 8640000) {
Ville Syrjälä1652d192015-03-31 14:12:01 +03006729 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6730 case CDCLK_FREQ_450_432:
6731 return 432000;
6732 case CDCLK_FREQ_337_308:
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03006733 return 308571;
Ville Syrjäläea617912016-05-13 23:41:24 +03006734 case CDCLK_FREQ_540:
6735 return 540000;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006736 case CDCLK_FREQ_675_617:
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03006737 return 617143;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006738 default:
Ville Syrjäläea617912016-05-13 23:41:24 +03006739 MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
Ville Syrjälä1652d192015-03-31 14:12:01 +03006740 }
6741 } else {
Ville Syrjälä1652d192015-03-31 14:12:01 +03006742 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6743 case CDCLK_FREQ_450_432:
6744 return 450000;
6745 case CDCLK_FREQ_337_308:
6746 return 337500;
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:
6750 return 675000;
6751 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 }
6755
Ville Syrjälä709e05c2016-05-13 23:41:33 +03006756 return dev_priv->cdclk_pll.ref;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006757}
6758
Ville Syrjälä83d7c812016-05-13 23:41:35 +03006759static void bxt_de_pll_update(struct drm_i915_private *dev_priv)
6760{
6761 u32 val;
6762
6763 dev_priv->cdclk_pll.ref = 19200;
Imre Deak1c3f7702016-05-24 15:38:32 +03006764 dev_priv->cdclk_pll.vco = 0;
Ville Syrjälä83d7c812016-05-13 23:41:35 +03006765
6766 val = I915_READ(BXT_DE_PLL_ENABLE);
Imre Deak1c3f7702016-05-24 15:38:32 +03006767 if ((val & BXT_DE_PLL_PLL_ENABLE) == 0)
Ville Syrjälä83d7c812016-05-13 23:41:35 +03006768 return;
Ville Syrjälä83d7c812016-05-13 23:41:35 +03006769
Imre Deak1c3f7702016-05-24 15:38:32 +03006770 if (WARN_ON((val & BXT_DE_PLL_LOCK) == 0))
6771 return;
Ville Syrjälä83d7c812016-05-13 23:41:35 +03006772
6773 val = I915_READ(BXT_DE_PLL_CTL);
6774 dev_priv->cdclk_pll.vco = (val & BXT_DE_PLL_RATIO_MASK) *
6775 dev_priv->cdclk_pll.ref;
6776}
6777
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006778static int broxton_get_display_clock_speed(struct drm_device *dev)
6779{
6780 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläf5986242016-05-13 23:41:37 +03006781 u32 divider;
6782 int div, vco;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006783
Ville Syrjälä83d7c812016-05-13 23:41:35 +03006784 bxt_de_pll_update(dev_priv);
6785
Ville Syrjäläf5986242016-05-13 23:41:37 +03006786 vco = dev_priv->cdclk_pll.vco;
6787 if (vco == 0)
6788 return dev_priv->cdclk_pll.ref;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006789
Ville Syrjäläf5986242016-05-13 23:41:37 +03006790 divider = I915_READ(CDCLK_CTL) & BXT_CDCLK_CD2X_DIV_SEL_MASK;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006791
Ville Syrjäläf5986242016-05-13 23:41:37 +03006792 switch (divider) {
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006793 case BXT_CDCLK_CD2X_DIV_SEL_1:
Ville Syrjäläf5986242016-05-13 23:41:37 +03006794 div = 2;
6795 break;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006796 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
Ville Syrjäläf5986242016-05-13 23:41:37 +03006797 div = 3;
6798 break;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006799 case BXT_CDCLK_CD2X_DIV_SEL_2:
Ville Syrjäläf5986242016-05-13 23:41:37 +03006800 div = 4;
6801 break;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006802 case BXT_CDCLK_CD2X_DIV_SEL_4:
Ville Syrjäläf5986242016-05-13 23:41:37 +03006803 div = 8;
6804 break;
6805 default:
6806 MISSING_CASE(divider);
6807 return dev_priv->cdclk_pll.ref;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006808 }
6809
Ville Syrjäläf5986242016-05-13 23:41:37 +03006810 return DIV_ROUND_CLOSEST(vco, div);
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006811}
6812
Ville Syrjälä1652d192015-03-31 14:12:01 +03006813static int broadwell_get_display_clock_speed(struct drm_device *dev)
6814{
6815 struct drm_i915_private *dev_priv = dev->dev_private;
6816 uint32_t lcpll = I915_READ(LCPLL_CTL);
6817 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6818
6819 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6820 return 800000;
6821 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6822 return 450000;
6823 else if (freq == LCPLL_CLK_FREQ_450)
6824 return 450000;
6825 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6826 return 540000;
6827 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6828 return 337500;
6829 else
6830 return 675000;
6831}
6832
6833static int haswell_get_display_clock_speed(struct drm_device *dev)
6834{
6835 struct drm_i915_private *dev_priv = dev->dev_private;
6836 uint32_t lcpll = I915_READ(LCPLL_CTL);
6837 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6838
6839 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6840 return 800000;
6841 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6842 return 450000;
6843 else if (freq == LCPLL_CLK_FREQ_450)
6844 return 450000;
6845 else if (IS_HSW_ULT(dev))
6846 return 337500;
6847 else
6848 return 540000;
Jesse Barnes79e53942008-11-07 14:24:08 -08006849}
6850
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006851static int valleyview_get_display_clock_speed(struct drm_device *dev)
6852{
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03006853 return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
6854 CCK_DISPLAY_CLOCK_CONTROL);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006855}
6856
Ville Syrjäläb37a6432015-03-31 14:11:54 +03006857static int ilk_get_display_clock_speed(struct drm_device *dev)
6858{
6859 return 450000;
6860}
6861
Jesse Barnese70236a2009-09-21 10:42:27 -07006862static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006863{
Jesse Barnese70236a2009-09-21 10:42:27 -07006864 return 400000;
6865}
Jesse Barnes79e53942008-11-07 14:24:08 -08006866
Jesse Barnese70236a2009-09-21 10:42:27 -07006867static int i915_get_display_clock_speed(struct drm_device *dev)
6868{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006869 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006870}
Jesse Barnes79e53942008-11-07 14:24:08 -08006871
Jesse Barnese70236a2009-09-21 10:42:27 -07006872static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6873{
6874 return 200000;
6875}
Jesse Barnes79e53942008-11-07 14:24:08 -08006876
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006877static int pnv_get_display_clock_speed(struct drm_device *dev)
6878{
6879 u16 gcfgc = 0;
6880
6881 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6882
6883 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6884 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006885 return 266667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006886 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006887 return 333333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006888 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006889 return 444444;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006890 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6891 return 200000;
6892 default:
6893 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6894 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006895 return 133333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006896 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006897 return 166667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006898 }
6899}
6900
Jesse Barnese70236a2009-09-21 10:42:27 -07006901static int i915gm_get_display_clock_speed(struct drm_device *dev)
6902{
6903 u16 gcfgc = 0;
6904
6905 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6906
6907 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Ville Syrjäläe907f172015-03-31 14:09:47 +03006908 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006909 else {
6910 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6911 case GC_DISPLAY_CLOCK_333_MHZ:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006912 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006913 default:
6914 case GC_DISPLAY_CLOCK_190_200_MHZ:
6915 return 190000;
6916 }
6917 }
6918}
Jesse Barnes79e53942008-11-07 14:24:08 -08006919
Jesse Barnese70236a2009-09-21 10:42:27 -07006920static int i865_get_display_clock_speed(struct drm_device *dev)
6921{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006922 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006923}
6924
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006925static int i85x_get_display_clock_speed(struct drm_device *dev)
Jesse Barnese70236a2009-09-21 10:42:27 -07006926{
6927 u16 hpllcc = 0;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006928
Ville Syrjälä65cd2b32015-05-22 11:22:32 +03006929 /*
6930 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6931 * encoding is different :(
6932 * FIXME is this the right way to detect 852GM/852GMV?
6933 */
6934 if (dev->pdev->revision == 0x1)
6935 return 133333;
6936
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006937 pci_bus_read_config_word(dev->pdev->bus,
6938 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6939
Jesse Barnese70236a2009-09-21 10:42:27 -07006940 /* Assume that the hardware is in the high speed state. This
6941 * should be the default.
6942 */
6943 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6944 case GC_CLOCK_133_200:
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006945 case GC_CLOCK_133_200_2:
Jesse Barnese70236a2009-09-21 10:42:27 -07006946 case GC_CLOCK_100_200:
6947 return 200000;
6948 case GC_CLOCK_166_250:
6949 return 250000;
6950 case GC_CLOCK_100_133:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006951 return 133333;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006952 case GC_CLOCK_133_266:
6953 case GC_CLOCK_133_266_2:
6954 case GC_CLOCK_166_266:
6955 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006956 }
6957
6958 /* Shouldn't happen */
6959 return 0;
6960}
6961
6962static int i830_get_display_clock_speed(struct drm_device *dev)
6963{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006964 return 133333;
Jesse Barnes79e53942008-11-07 14:24:08 -08006965}
6966
Ville Syrjälä34edce22015-05-22 11:22:33 +03006967static unsigned int intel_hpll_vco(struct drm_device *dev)
6968{
6969 struct drm_i915_private *dev_priv = dev->dev_private;
6970 static const unsigned int blb_vco[8] = {
6971 [0] = 3200000,
6972 [1] = 4000000,
6973 [2] = 5333333,
6974 [3] = 4800000,
6975 [4] = 6400000,
6976 };
6977 static const unsigned int pnv_vco[8] = {
6978 [0] = 3200000,
6979 [1] = 4000000,
6980 [2] = 5333333,
6981 [3] = 4800000,
6982 [4] = 2666667,
6983 };
6984 static const unsigned int cl_vco[8] = {
6985 [0] = 3200000,
6986 [1] = 4000000,
6987 [2] = 5333333,
6988 [3] = 6400000,
6989 [4] = 3333333,
6990 [5] = 3566667,
6991 [6] = 4266667,
6992 };
6993 static const unsigned int elk_vco[8] = {
6994 [0] = 3200000,
6995 [1] = 4000000,
6996 [2] = 5333333,
6997 [3] = 4800000,
6998 };
6999 static const unsigned int ctg_vco[8] = {
7000 [0] = 3200000,
7001 [1] = 4000000,
7002 [2] = 5333333,
7003 [3] = 6400000,
7004 [4] = 2666667,
7005 [5] = 4266667,
7006 };
7007 const unsigned int *vco_table;
7008 unsigned int vco;
7009 uint8_t tmp = 0;
7010
7011 /* FIXME other chipsets? */
7012 if (IS_GM45(dev))
7013 vco_table = ctg_vco;
7014 else if (IS_G4X(dev))
7015 vco_table = elk_vco;
7016 else if (IS_CRESTLINE(dev))
7017 vco_table = cl_vco;
7018 else if (IS_PINEVIEW(dev))
7019 vco_table = pnv_vco;
7020 else if (IS_G33(dev))
7021 vco_table = blb_vco;
7022 else
7023 return 0;
7024
7025 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
7026
7027 vco = vco_table[tmp & 0x7];
7028 if (vco == 0)
7029 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
7030 else
7031 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
7032
7033 return vco;
7034}
7035
7036static int gm45_get_display_clock_speed(struct drm_device *dev)
7037{
7038 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7039 uint16_t tmp = 0;
7040
7041 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7042
7043 cdclk_sel = (tmp >> 12) & 0x1;
7044
7045 switch (vco) {
7046 case 2666667:
7047 case 4000000:
7048 case 5333333:
7049 return cdclk_sel ? 333333 : 222222;
7050 case 3200000:
7051 return cdclk_sel ? 320000 : 228571;
7052 default:
7053 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
7054 return 222222;
7055 }
7056}
7057
7058static int i965gm_get_display_clock_speed(struct drm_device *dev)
7059{
7060 static const uint8_t div_3200[] = { 16, 10, 8 };
7061 static const uint8_t div_4000[] = { 20, 12, 10 };
7062 static const uint8_t div_5333[] = { 24, 16, 14 };
7063 const uint8_t *div_table;
7064 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7065 uint16_t tmp = 0;
7066
7067 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7068
7069 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
7070
7071 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7072 goto fail;
7073
7074 switch (vco) {
7075 case 3200000:
7076 div_table = div_3200;
7077 break;
7078 case 4000000:
7079 div_table = div_4000;
7080 break;
7081 case 5333333:
7082 div_table = div_5333;
7083 break;
7084 default:
7085 goto fail;
7086 }
7087
7088 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7089
Damien Lespiaucaf4e252015-06-04 16:56:18 +01007090fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03007091 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
7092 return 200000;
7093}
7094
7095static int g33_get_display_clock_speed(struct drm_device *dev)
7096{
7097 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
7098 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
7099 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
7100 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
7101 const uint8_t *div_table;
7102 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7103 uint16_t tmp = 0;
7104
7105 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7106
7107 cdclk_sel = (tmp >> 4) & 0x7;
7108
7109 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7110 goto fail;
7111
7112 switch (vco) {
7113 case 3200000:
7114 div_table = div_3200;
7115 break;
7116 case 4000000:
7117 div_table = div_4000;
7118 break;
7119 case 4800000:
7120 div_table = div_4800;
7121 break;
7122 case 5333333:
7123 div_table = div_5333;
7124 break;
7125 default:
7126 goto fail;
7127 }
7128
7129 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7130
Damien Lespiaucaf4e252015-06-04 16:56:18 +01007131fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03007132 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
7133 return 190476;
7134}
7135
Zhenyu Wang2c072452009-06-05 15:38:42 +08007136static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007137intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007138{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007139 while (*num > DATA_LINK_M_N_MASK ||
7140 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08007141 *num >>= 1;
7142 *den >>= 1;
7143 }
7144}
7145
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007146static void compute_m_n(unsigned int m, unsigned int n,
7147 uint32_t *ret_m, uint32_t *ret_n)
7148{
7149 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7150 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7151 intel_reduce_m_n_ratio(ret_m, ret_n);
7152}
7153
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007154void
7155intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7156 int pixel_clock, int link_clock,
7157 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007158{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007159 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007160
7161 compute_m_n(bits_per_pixel * pixel_clock,
7162 link_clock * nlanes * 8,
7163 &m_n->gmch_m, &m_n->gmch_n);
7164
7165 compute_m_n(pixel_clock, link_clock,
7166 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08007167}
7168
Chris Wilsona7615032011-01-12 17:04:08 +00007169static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7170{
Jani Nikulad330a952014-01-21 11:24:25 +02007171 if (i915.panel_use_ssc >= 0)
7172 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007173 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07007174 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00007175}
7176
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007177static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007178{
Daniel Vetter7df00d72013-05-21 21:54:55 +02007179 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007180}
Daniel Vetterf47709a2013-03-28 10:42:02 +01007181
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007182static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7183{
7184 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007185}
7186
Daniel Vetterf47709a2013-03-28 10:42:02 +01007187static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007188 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03007189 struct dpll *reduced_clock)
Jesse Barnesa7516a02011-12-15 12:30:37 -08007190{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007191 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007192 u32 fp, fp2 = 0;
7193
7194 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007195 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007196 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007197 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007198 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007199 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007200 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007201 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007202 }
7203
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007204 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007205
Daniel Vetterf47709a2013-03-28 10:42:02 +01007206 crtc->lowfreq_avail = false;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007207 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07007208 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007209 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007210 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007211 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007212 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007213 }
7214}
7215
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007216static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7217 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07007218{
7219 u32 reg_val;
7220
7221 /*
7222 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7223 * and set it to a reasonable value instead.
7224 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007225 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007226 reg_val &= 0xffffff00;
7227 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007228 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007229
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007230 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007231 reg_val &= 0x8cffffff;
7232 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007233 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007234
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007235 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007236 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007237 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007238
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007239 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007240 reg_val &= 0x00ffffff;
7241 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007242 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007243}
7244
Daniel Vetterb5518422013-05-03 11:49:48 +02007245static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7246 struct intel_link_m_n *m_n)
7247{
7248 struct drm_device *dev = crtc->base.dev;
7249 struct drm_i915_private *dev_priv = dev->dev_private;
7250 int pipe = crtc->pipe;
7251
Daniel Vettere3b95f12013-05-03 11:49:49 +02007252 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7253 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7254 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7255 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007256}
7257
7258static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07007259 struct intel_link_m_n *m_n,
7260 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02007261{
7262 struct drm_device *dev = crtc->base.dev;
7263 struct drm_i915_private *dev_priv = dev->dev_private;
7264 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007265 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02007266
7267 if (INTEL_INFO(dev)->gen >= 5) {
7268 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7269 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7270 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7271 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07007272 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7273 * for gen < 8) and if DRRS is supported (to make sure the
7274 * registers are not unnecessarily accessed).
7275 */
Durgadoss R44395bf2015-02-13 15:33:02 +05307276 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007277 crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07007278 I915_WRITE(PIPE_DATA_M2(transcoder),
7279 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7280 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7281 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7282 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7283 }
Daniel Vetterb5518422013-05-03 11:49:48 +02007284 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02007285 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7286 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7287 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7288 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007289 }
7290}
7291
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05307292void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007293{
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05307294 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7295
7296 if (m_n == M1_N1) {
7297 dp_m_n = &crtc->config->dp_m_n;
7298 dp_m2_n2 = &crtc->config->dp_m2_n2;
7299 } else if (m_n == M2_N2) {
7300
7301 /*
7302 * M2_N2 registers are not supported. Hence m2_n2 divider value
7303 * needs to be programmed into M1_N1.
7304 */
7305 dp_m_n = &crtc->config->dp_m2_n2;
7306 } else {
7307 DRM_ERROR("Unsupported divider value\n");
7308 return;
7309 }
7310
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007311 if (crtc->config->has_pch_encoder)
7312 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007313 else
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05307314 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007315}
7316
Daniel Vetter251ac862015-06-18 10:30:24 +02007317static void vlv_compute_dpll(struct intel_crtc *crtc,
7318 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007319{
Ville Syrjälä03ed5cb2016-03-15 16:39:55 +02007320 pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007321 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjälä03ed5cb2016-03-15 16:39:55 +02007322 if (crtc->pipe != PIPE_A)
7323 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007324
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007325 /* DPLL not used with DSI, but still need the rest set up */
Ville Syrjälä187a1c02016-04-18 20:34:04 +03007326 if (!pipe_config->has_dsi_encoder)
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007327 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
7328 DPLL_EXT_BUFFER_ENABLE_VLV;
7329
Ville Syrjälä03ed5cb2016-03-15 16:39:55 +02007330 pipe_config->dpll_hw_state.dpll_md =
7331 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
7332}
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007333
Ville Syrjälä03ed5cb2016-03-15 16:39:55 +02007334static void chv_compute_dpll(struct intel_crtc *crtc,
7335 struct intel_crtc_state *pipe_config)
7336{
7337 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007338 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjälä03ed5cb2016-03-15 16:39:55 +02007339 if (crtc->pipe != PIPE_A)
7340 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7341
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007342 /* DPLL not used with DSI, but still need the rest set up */
Ville Syrjälä187a1c02016-04-18 20:34:04 +03007343 if (!pipe_config->has_dsi_encoder)
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007344 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
7345
Ville Syrjälä03ed5cb2016-03-15 16:39:55 +02007346 pipe_config->dpll_hw_state.dpll_md =
7347 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007348}
7349
Ville Syrjäläd288f652014-10-28 13:20:22 +02007350static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007351 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007352{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007353 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007354 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007355 enum pipe pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007356 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007357 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007358 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007359
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007360 /* Enable Refclk */
7361 I915_WRITE(DPLL(pipe),
7362 pipe_config->dpll_hw_state.dpll &
7363 ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
7364
7365 /* No need to actually set up the DPLL with DSI */
7366 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7367 return;
7368
Ville Syrjäläa5805162015-05-26 20:42:30 +03007369 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01007370
Ville Syrjäläd288f652014-10-28 13:20:22 +02007371 bestn = pipe_config->dpll.n;
7372 bestm1 = pipe_config->dpll.m1;
7373 bestm2 = pipe_config->dpll.m2;
7374 bestp1 = pipe_config->dpll.p1;
7375 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007376
Jesse Barnes89b667f2013-04-18 14:51:36 -07007377 /* See eDP HDMI DPIO driver vbios notes doc */
7378
7379 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007380 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007381 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007382
7383 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007384 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007385
7386 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007387 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007388 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007389 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007390
7391 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007392 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007393
7394 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007395 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7396 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7397 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007398 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07007399
7400 /*
7401 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7402 * but we don't support that).
7403 * Note: don't use the DAC post divider as it seems unstable.
7404 */
7405 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007406 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007407
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007408 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007409 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007410
Jesse Barnes89b667f2013-04-18 14:51:36 -07007411 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02007412 if (pipe_config->port_clock == 162000 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007413 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
7414 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007415 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b0122013-07-05 19:21:38 +03007416 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007417 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007418 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007419 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007420
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02007421 if (pipe_config->has_dp_encoder) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07007422 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007423 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007424 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007425 0x0df40000);
7426 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007427 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007428 0x0df70000);
7429 } else { /* HDMI or VGA */
7430 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007431 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007432 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007433 0x0df70000);
7434 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007435 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007436 0x0df40000);
7437 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007438
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007439 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007440 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007441 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
7442 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
Jesse Barnes89b667f2013-04-18 14:51:36 -07007443 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007444 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007445
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007446 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Ville Syrjäläa5805162015-05-26 20:42:30 +03007447 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007448}
7449
Ville Syrjäläd288f652014-10-28 13:20:22 +02007450static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007451 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007452{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007453 struct drm_device *dev = crtc->base.dev;
7454 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007455 enum pipe pipe = crtc->pipe;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007456 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307457 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007458 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307459 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307460 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007461
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007462 /* Enable Refclk and SSC */
7463 I915_WRITE(DPLL(pipe),
7464 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
7465
7466 /* No need to actually set up the DPLL with DSI */
7467 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7468 return;
7469
Ville Syrjäläd288f652014-10-28 13:20:22 +02007470 bestn = pipe_config->dpll.n;
7471 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7472 bestm1 = pipe_config->dpll.m1;
7473 bestm2 = pipe_config->dpll.m2 >> 22;
7474 bestp1 = pipe_config->dpll.p1;
7475 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307476 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307477 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307478 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007479
Ville Syrjäläa5805162015-05-26 20:42:30 +03007480 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007481
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007482 /* p1 and p2 divider */
7483 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7484 5 << DPIO_CHV_S1_DIV_SHIFT |
7485 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7486 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7487 1 << DPIO_CHV_K_DIV_SHIFT);
7488
7489 /* Feedback post-divider - m2 */
7490 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7491
7492 /* Feedback refclk divider - n and m1 */
7493 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7494 DPIO_CHV_M1_DIV_BY_2 |
7495 1 << DPIO_CHV_N_DIV_SHIFT);
7496
7497 /* M2 fraction division */
Ville Syrjälä25a25df2015-07-08 23:45:47 +03007498 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007499
7500 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307501 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7502 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7503 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7504 if (bestm2_frac)
7505 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7506 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007507
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05307508 /* Program digital lock detect threshold */
7509 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7510 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7511 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7512 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7513 if (!bestm2_frac)
7514 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7515 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7516
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007517 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307518 if (vco == 5400000) {
7519 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7520 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7521 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7522 tribuf_calcntr = 0x9;
7523 } else if (vco <= 6200000) {
7524 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7525 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7526 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7527 tribuf_calcntr = 0x9;
7528 } else if (vco <= 6480000) {
7529 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7530 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7531 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7532 tribuf_calcntr = 0x8;
7533 } else {
7534 /* Not supported. Apply the same limits as in the max case */
7535 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7536 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7537 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7538 tribuf_calcntr = 0;
7539 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007540 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7541
Ville Syrjälä968040b2015-03-11 22:52:08 +02007542 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307543 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7544 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7545 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7546
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007547 /* AFC Recal */
7548 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7549 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7550 DPIO_AFC_RECAL);
7551
Ville Syrjäläa5805162015-05-26 20:42:30 +03007552 mutex_unlock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007553}
7554
Ville Syrjäläd288f652014-10-28 13:20:22 +02007555/**
7556 * vlv_force_pll_on - forcibly enable just the PLL
7557 * @dev_priv: i915 private structure
7558 * @pipe: pipe PLL to enable
7559 * @dpll: PLL configuration
7560 *
7561 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7562 * in cases where we need the PLL enabled even when @pipe is not going to
7563 * be enabled.
7564 */
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007565int vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7566 const struct dpll *dpll)
Ville Syrjäläd288f652014-10-28 13:20:22 +02007567{
7568 struct intel_crtc *crtc =
7569 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007570 struct intel_crtc_state *pipe_config;
7571
7572 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
7573 if (!pipe_config)
7574 return -ENOMEM;
7575
7576 pipe_config->base.crtc = &crtc->base;
7577 pipe_config->pixel_multiplier = 1;
7578 pipe_config->dpll = *dpll;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007579
7580 if (IS_CHERRYVIEW(dev)) {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007581 chv_compute_dpll(crtc, pipe_config);
7582 chv_prepare_pll(crtc, pipe_config);
7583 chv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007584 } else {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007585 vlv_compute_dpll(crtc, pipe_config);
7586 vlv_prepare_pll(crtc, pipe_config);
7587 vlv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007588 }
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007589
7590 kfree(pipe_config);
7591
7592 return 0;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007593}
7594
7595/**
7596 * vlv_force_pll_off - forcibly disable just the PLL
7597 * @dev_priv: i915 private structure
7598 * @pipe: pipe PLL to disable
7599 *
7600 * Disable the PLL for @pipe. To be used in cases where we need
7601 * the PLL enabled even when @pipe is not going to be enabled.
7602 */
7603void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7604{
7605 if (IS_CHERRYVIEW(dev))
7606 chv_disable_pll(to_i915(dev), pipe);
7607 else
7608 vlv_disable_pll(to_i915(dev), pipe);
7609}
7610
Daniel Vetter251ac862015-06-18 10:30:24 +02007611static void i9xx_compute_dpll(struct intel_crtc *crtc,
7612 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03007613 struct dpll *reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007614{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007615 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007616 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007617 u32 dpll;
7618 bool is_sdvo;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007619 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007620
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007621 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307622
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007623 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7624 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007625
7626 dpll = DPLL_VGA_MODE_DIS;
7627
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007628 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007629 dpll |= DPLLB_MODE_LVDS;
7630 else
7631 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007632
Daniel Vetteref1b4602013-06-01 17:17:04 +02007633 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007634 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02007635 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007636 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02007637
7638 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007639 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007640
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007641 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007642 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007643
7644 /* compute bitmask from p1 value */
7645 if (IS_PINEVIEW(dev))
7646 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7647 else {
7648 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7649 if (IS_G4X(dev) && reduced_clock)
7650 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7651 }
7652 switch (clock->p2) {
7653 case 5:
7654 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7655 break;
7656 case 7:
7657 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7658 break;
7659 case 10:
7660 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7661 break;
7662 case 14:
7663 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7664 break;
7665 }
7666 if (INTEL_INFO(dev)->gen >= 4)
7667 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7668
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007669 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007670 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007671 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007672 intel_panel_use_ssc(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007673 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7674 else
7675 dpll |= PLL_REF_INPUT_DREFCLK;
7676
7677 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007678 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007679
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007680 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007681 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007682 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007683 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007684 }
7685}
7686
Daniel Vetter251ac862015-06-18 10:30:24 +02007687static void i8xx_compute_dpll(struct intel_crtc *crtc,
7688 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03007689 struct dpll *reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007690{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007691 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007692 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007693 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007694 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007695
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007696 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307697
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007698 dpll = DPLL_VGA_MODE_DIS;
7699
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007700 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007701 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7702 } else {
7703 if (clock->p1 == 2)
7704 dpll |= PLL_P1_DIVIDE_BY_TWO;
7705 else
7706 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7707 if (clock->p2 == 4)
7708 dpll |= PLL_P2_DIVIDE_BY_4;
7709 }
7710
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007711 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007712 dpll |= DPLL_DVO_2X_MODE;
7713
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007714 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007715 intel_panel_use_ssc(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007716 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7717 else
7718 dpll |= PLL_REF_INPUT_DREFCLK;
7719
7720 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007721 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007722}
7723
Daniel Vetter8a654f32013-06-01 17:16:22 +02007724static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007725{
7726 struct drm_device *dev = intel_crtc->base.dev;
7727 struct drm_i915_private *dev_priv = dev->dev_private;
7728 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007729 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03007730 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007731 uint32_t crtc_vtotal, crtc_vblank_end;
7732 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007733
7734 /* We need to be careful not to changed the adjusted mode, for otherwise
7735 * the hw state checker will get angry at the mismatch. */
7736 crtc_vtotal = adjusted_mode->crtc_vtotal;
7737 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007738
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007739 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007740 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007741 crtc_vtotal -= 1;
7742 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007743
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007744 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007745 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7746 else
7747 vsyncshift = adjusted_mode->crtc_hsync_start -
7748 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007749 if (vsyncshift < 0)
7750 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007751 }
7752
7753 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007754 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007755
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007756 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007757 (adjusted_mode->crtc_hdisplay - 1) |
7758 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007759 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007760 (adjusted_mode->crtc_hblank_start - 1) |
7761 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007762 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007763 (adjusted_mode->crtc_hsync_start - 1) |
7764 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7765
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007766 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007767 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007768 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007769 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007770 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007771 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007772 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007773 (adjusted_mode->crtc_vsync_start - 1) |
7774 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7775
Paulo Zanonib5e508d2012-10-24 11:34:43 -02007776 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7777 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7778 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7779 * bits. */
7780 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7781 (pipe == PIPE_B || pipe == PIPE_C))
7782 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7783
Jani Nikulabc58be62016-03-18 17:05:39 +02007784}
7785
7786static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
7787{
7788 struct drm_device *dev = intel_crtc->base.dev;
7789 struct drm_i915_private *dev_priv = dev->dev_private;
7790 enum pipe pipe = intel_crtc->pipe;
7791
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007792 /* pipesrc controls the size that is scaled from, which should
7793 * always be the user's requested size.
7794 */
7795 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007796 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7797 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007798}
7799
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007800static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007801 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007802{
7803 struct drm_device *dev = crtc->base.dev;
7804 struct drm_i915_private *dev_priv = dev->dev_private;
7805 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7806 uint32_t tmp;
7807
7808 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007809 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7810 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007811 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007812 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7813 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007814 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007815 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7816 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007817
7818 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007819 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7820 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007821 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007822 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7823 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007824 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007825 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7826 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007827
7828 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007829 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7830 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7831 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007832 }
Jani Nikulabc58be62016-03-18 17:05:39 +02007833}
7834
7835static void intel_get_pipe_src_size(struct intel_crtc *crtc,
7836 struct intel_crtc_state *pipe_config)
7837{
7838 struct drm_device *dev = crtc->base.dev;
7839 struct drm_i915_private *dev_priv = dev->dev_private;
7840 u32 tmp;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007841
7842 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03007843 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7844 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7845
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007846 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7847 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007848}
7849
Daniel Vetterf6a83282014-02-11 15:28:57 -08007850void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007851 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03007852{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007853 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7854 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7855 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7856 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007857
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007858 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7859 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7860 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7861 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007862
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007863 mode->flags = pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007864 mode->type = DRM_MODE_TYPE_DRIVER;
Jesse Barnesbabea612013-06-26 18:57:38 +03007865
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007866 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7867 mode->flags |= pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007868
7869 mode->hsync = drm_mode_hsync(mode);
7870 mode->vrefresh = drm_mode_vrefresh(mode);
7871 drm_mode_set_name(mode);
Jesse Barnesbabea612013-06-26 18:57:38 +03007872}
7873
Daniel Vetter84b046f2013-02-19 18:48:54 +01007874static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7875{
7876 struct drm_device *dev = intel_crtc->base.dev;
7877 struct drm_i915_private *dev_priv = dev->dev_private;
7878 uint32_t pipeconf;
7879
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007880 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007881
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03007882 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7883 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7884 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02007885
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007886 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007887 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007888
Daniel Vetterff9ce462013-04-24 14:57:17 +02007889 /* only g4x and later have fancy bpc/dither controls */
Wayne Boyer666a4532015-12-09 12:29:35 -08007890 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007891 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007892 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02007893 pipeconf |= PIPECONF_DITHER_EN |
7894 PIPECONF_DITHER_TYPE_SP;
7895
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007896 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007897 case 18:
7898 pipeconf |= PIPECONF_6BPC;
7899 break;
7900 case 24:
7901 pipeconf |= PIPECONF_8BPC;
7902 break;
7903 case 30:
7904 pipeconf |= PIPECONF_10BPC;
7905 break;
7906 default:
7907 /* Case prevented by intel_choose_pipe_bpp_dither. */
7908 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01007909 }
7910 }
7911
7912 if (HAS_PIPE_CXSR(dev)) {
7913 if (intel_crtc->lowfreq_avail) {
7914 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7915 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7916 } else {
7917 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01007918 }
7919 }
7920
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007921 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007922 if (INTEL_INFO(dev)->gen < 4 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007923 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007924 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7925 else
7926 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7927 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01007928 pipeconf |= PIPECONF_PROGRESSIVE;
7929
Wayne Boyer666a4532015-12-09 12:29:35 -08007930 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
7931 intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007932 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03007933
Daniel Vetter84b046f2013-02-19 18:48:54 +01007934 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7935 POSTING_READ(PIPECONF(intel_crtc->pipe));
7936}
7937
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007938static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
7939 struct intel_crtc_state *crtc_state)
7940{
7941 struct drm_device *dev = crtc->base.dev;
7942 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007943 const struct intel_limit *limit;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007944 int refclk = 48000;
7945
7946 memset(&crtc_state->dpll_hw_state, 0,
7947 sizeof(crtc_state->dpll_hw_state));
7948
7949 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7950 if (intel_panel_use_ssc(dev_priv)) {
7951 refclk = dev_priv->vbt.lvds_ssc_freq;
7952 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7953 }
7954
7955 limit = &intel_limits_i8xx_lvds;
7956 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO)) {
7957 limit = &intel_limits_i8xx_dvo;
7958 } else {
7959 limit = &intel_limits_i8xx_dac;
7960 }
7961
7962 if (!crtc_state->clock_set &&
7963 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7964 refclk, NULL, &crtc_state->dpll)) {
7965 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7966 return -EINVAL;
7967 }
7968
7969 i8xx_compute_dpll(crtc, crtc_state, NULL);
7970
7971 return 0;
7972}
7973
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007974static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
7975 struct intel_crtc_state *crtc_state)
7976{
7977 struct drm_device *dev = crtc->base.dev;
7978 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007979 const struct intel_limit *limit;
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007980 int refclk = 96000;
7981
7982 memset(&crtc_state->dpll_hw_state, 0,
7983 sizeof(crtc_state->dpll_hw_state));
7984
7985 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7986 if (intel_panel_use_ssc(dev_priv)) {
7987 refclk = dev_priv->vbt.lvds_ssc_freq;
7988 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7989 }
7990
7991 if (intel_is_dual_link_lvds(dev))
7992 limit = &intel_limits_g4x_dual_channel_lvds;
7993 else
7994 limit = &intel_limits_g4x_single_channel_lvds;
7995 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
7996 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
7997 limit = &intel_limits_g4x_hdmi;
7998 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
7999 limit = &intel_limits_g4x_sdvo;
8000 } else {
8001 /* The option is for other outputs */
8002 limit = &intel_limits_i9xx_sdvo;
8003 }
8004
8005 if (!crtc_state->clock_set &&
8006 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8007 refclk, NULL, &crtc_state->dpll)) {
8008 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8009 return -EINVAL;
8010 }
8011
8012 i9xx_compute_dpll(crtc, crtc_state, NULL);
8013
8014 return 0;
8015}
8016
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008017static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
8018 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008019{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008020 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08008021 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008022 const struct intel_limit *limit;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02008023 int refclk = 96000;
Jesse Barnes79e53942008-11-07 14:24:08 -08008024
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03008025 memset(&crtc_state->dpll_hw_state, 0,
8026 sizeof(crtc_state->dpll_hw_state));
8027
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008028 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
8029 if (intel_panel_use_ssc(dev_priv)) {
8030 refclk = dev_priv->vbt.lvds_ssc_freq;
8031 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
8032 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008033
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008034 limit = &intel_limits_pineview_lvds;
8035 } else {
8036 limit = &intel_limits_pineview_sdvo;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02008037 }
Jani Nikulaf2335332013-09-13 11:03:09 +03008038
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008039 if (!crtc_state->clock_set &&
8040 !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8041 refclk, NULL, &crtc_state->dpll)) {
8042 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8043 return -EINVAL;
8044 }
8045
8046 i9xx_compute_dpll(crtc, crtc_state, NULL);
8047
8048 return 0;
8049}
8050
8051static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
8052 struct intel_crtc_state *crtc_state)
8053{
8054 struct drm_device *dev = crtc->base.dev;
8055 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008056 const struct intel_limit *limit;
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008057 int refclk = 96000;
8058
8059 memset(&crtc_state->dpll_hw_state, 0,
8060 sizeof(crtc_state->dpll_hw_state));
8061
8062 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
8063 if (intel_panel_use_ssc(dev_priv)) {
8064 refclk = dev_priv->vbt.lvds_ssc_freq;
8065 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03008066 }
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008067
8068 limit = &intel_limits_i9xx_lvds;
8069 } else {
8070 limit = &intel_limits_i9xx_sdvo;
8071 }
8072
8073 if (!crtc_state->clock_set &&
8074 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8075 refclk, NULL, &crtc_state->dpll)) {
8076 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8077 return -EINVAL;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008078 }
Eric Anholtf564048e2011-03-30 13:01:02 -07008079
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02008080 i9xx_compute_dpll(crtc, crtc_state, NULL);
Eric Anholtf564048e2011-03-30 13:01:02 -07008081
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008082 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07008083}
8084
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008085static int chv_crtc_compute_clock(struct intel_crtc *crtc,
8086 struct intel_crtc_state *crtc_state)
8087{
8088 int refclk = 100000;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008089 const struct intel_limit *limit = &intel_limits_chv;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008090
8091 memset(&crtc_state->dpll_hw_state, 0,
8092 sizeof(crtc_state->dpll_hw_state));
8093
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008094 if (!crtc_state->clock_set &&
8095 !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8096 refclk, NULL, &crtc_state->dpll)) {
8097 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8098 return -EINVAL;
8099 }
8100
8101 chv_compute_dpll(crtc, crtc_state);
8102
8103 return 0;
8104}
8105
8106static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
8107 struct intel_crtc_state *crtc_state)
8108{
8109 int refclk = 100000;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008110 const struct intel_limit *limit = &intel_limits_vlv;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008111
8112 memset(&crtc_state->dpll_hw_state, 0,
8113 sizeof(crtc_state->dpll_hw_state));
8114
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008115 if (!crtc_state->clock_set &&
8116 !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8117 refclk, NULL, &crtc_state->dpll)) {
8118 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8119 return -EINVAL;
8120 }
8121
8122 vlv_compute_dpll(crtc, crtc_state);
8123
8124 return 0;
8125}
8126
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008127static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008128 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008129{
8130 struct drm_device *dev = crtc->base.dev;
8131 struct drm_i915_private *dev_priv = dev->dev_private;
8132 uint32_t tmp;
8133
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02008134 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
8135 return;
8136
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008137 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02008138 if (!(tmp & PFIT_ENABLE))
8139 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008140
Daniel Vetter06922822013-07-11 13:35:40 +02008141 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008142 if (INTEL_INFO(dev)->gen < 4) {
8143 if (crtc->pipe != PIPE_B)
8144 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008145 } else {
8146 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
8147 return;
8148 }
8149
Daniel Vetter06922822013-07-11 13:35:40 +02008150 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008151 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008152}
8153
Jesse Barnesacbec812013-09-20 11:29:32 -07008154static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008155 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07008156{
8157 struct drm_device *dev = crtc->base.dev;
8158 struct drm_i915_private *dev_priv = dev->dev_private;
8159 int pipe = pipe_config->cpu_transcoder;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03008160 struct dpll clock;
Jesse Barnesacbec812013-09-20 11:29:32 -07008161 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07008162 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07008163
Ville Syrjäläb5219732016-03-15 16:40:01 +02008164 /* In case of DSI, DPLL will not be used */
8165 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
Shobhit Kumarf573de52014-07-30 20:32:37 +05308166 return;
8167
Ville Syrjäläa5805162015-05-26 20:42:30 +03008168 mutex_lock(&dev_priv->sb_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08008169 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008170 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesacbec812013-09-20 11:29:32 -07008171
8172 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
8173 clock.m2 = mdiv & DPIO_M2DIV_MASK;
8174 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
8175 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
8176 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
8177
Imre Deakdccbea32015-06-22 23:35:51 +03008178 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07008179}
8180
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008181static void
8182i9xx_get_initial_plane_config(struct intel_crtc *crtc,
8183 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008184{
8185 struct drm_device *dev = crtc->base.dev;
8186 struct drm_i915_private *dev_priv = dev->dev_private;
8187 u32 val, base, offset;
8188 int pipe = crtc->pipe, plane = crtc->plane;
8189 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008190 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008191 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008192 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008193
Damien Lespiau42a7b082015-02-05 19:35:13 +00008194 val = I915_READ(DSPCNTR(plane));
8195 if (!(val & DISPLAY_PLANE_ENABLE))
8196 return;
8197
Damien Lespiaud9806c92015-01-21 14:07:19 +00008198 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008199 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008200 DRM_DEBUG_KMS("failed to alloc fb\n");
8201 return;
8202 }
8203
Damien Lespiau1b842c82015-01-21 13:50:54 +00008204 fb = &intel_fb->base;
8205
Daniel Vetter18c52472015-02-10 17:16:09 +00008206 if (INTEL_INFO(dev)->gen >= 4) {
8207 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008208 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00008209 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8210 }
8211 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008212
8213 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00008214 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008215 fb->pixel_format = fourcc;
8216 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008217
8218 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008219 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008220 offset = I915_READ(DSPTILEOFF(plane));
8221 else
8222 offset = I915_READ(DSPLINOFF(plane));
8223 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8224 } else {
8225 base = I915_READ(DSPADDR(plane));
8226 }
8227 plane_config->base = base;
8228
8229 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008230 fb->width = ((val >> 16) & 0xfff) + 1;
8231 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008232
8233 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008234 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008235
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008236 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008237 fb->pixel_format,
8238 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008239
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008240 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008241
Damien Lespiau2844a922015-01-20 12:51:48 +00008242 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8243 pipe_name(pipe), plane, fb->width, fb->height,
8244 fb->bits_per_pixel, base, fb->pitches[0],
8245 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008246
Damien Lespiau2d140302015-02-05 17:22:18 +00008247 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008248}
8249
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008250static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008251 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008252{
8253 struct drm_device *dev = crtc->base.dev;
8254 struct drm_i915_private *dev_priv = dev->dev_private;
8255 int pipe = pipe_config->cpu_transcoder;
8256 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03008257 struct dpll clock;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008258 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008259 int refclk = 100000;
8260
Ville Syrjäläb5219732016-03-15 16:40:01 +02008261 /* In case of DSI, DPLL will not be used */
8262 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
8263 return;
8264
Ville Syrjäläa5805162015-05-26 20:42:30 +03008265 mutex_lock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008266 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8267 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8268 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8269 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
Imre Deak0d7b6b12015-07-02 14:29:58 +03008270 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008271 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008272
8273 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008274 clock.m2 = (pll_dw0 & 0xff) << 22;
8275 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
8276 clock.m2 |= pll_dw2 & 0x3fffff;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008277 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8278 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8279 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8280
Imre Deakdccbea32015-06-22 23:35:51 +03008281 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008282}
8283
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008284static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008285 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008286{
8287 struct drm_device *dev = crtc->base.dev;
8288 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak17290502016-02-12 18:55:11 +02008289 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008290 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02008291 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008292
Imre Deak17290502016-02-12 18:55:11 +02008293 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8294 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +02008295 return false;
8296
Daniel Vettere143a212013-07-04 12:01:15 +02008297 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008298 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02008299
Imre Deak17290502016-02-12 18:55:11 +02008300 ret = false;
8301
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008302 tmp = I915_READ(PIPECONF(crtc->pipe));
8303 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02008304 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008305
Wayne Boyer666a4532015-12-09 12:29:35 -08008306 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008307 switch (tmp & PIPECONF_BPC_MASK) {
8308 case PIPECONF_6BPC:
8309 pipe_config->pipe_bpp = 18;
8310 break;
8311 case PIPECONF_8BPC:
8312 pipe_config->pipe_bpp = 24;
8313 break;
8314 case PIPECONF_10BPC:
8315 pipe_config->pipe_bpp = 30;
8316 break;
8317 default:
8318 break;
8319 }
8320 }
8321
Wayne Boyer666a4532015-12-09 12:29:35 -08008322 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
8323 (tmp & PIPECONF_COLOR_RANGE_SELECT))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008324 pipe_config->limited_color_range = true;
8325
Ville Syrjälä282740f2013-09-04 18:30:03 +03008326 if (INTEL_INFO(dev)->gen < 4)
8327 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8328
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008329 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +02008330 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008331
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008332 i9xx_get_pfit_config(crtc, pipe_config);
8333
Daniel Vetter6c49f242013-06-06 12:45:25 +02008334 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjäläc2317752016-03-15 16:39:56 +02008335 /* No way to read it out on pipes B and C */
8336 if (IS_CHERRYVIEW(dev) && crtc->pipe != PIPE_A)
8337 tmp = dev_priv->chv_dpll_md[crtc->pipe];
8338 else
8339 tmp = I915_READ(DPLL_MD(crtc->pipe));
Daniel Vetter6c49f242013-06-06 12:45:25 +02008340 pipe_config->pixel_multiplier =
8341 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8342 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008343 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02008344 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8345 tmp = I915_READ(DPLL(crtc->pipe));
8346 pipe_config->pixel_multiplier =
8347 ((tmp & SDVO_MULTIPLIER_MASK)
8348 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8349 } else {
8350 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8351 * port and will be fixed up in the encoder->get_config
8352 * function. */
8353 pipe_config->pixel_multiplier = 1;
8354 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008355 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
Wayne Boyer666a4532015-12-09 12:29:35 -08008356 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03008357 /*
8358 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8359 * on 830. Filter it out here so that we don't
8360 * report errors due to that.
8361 */
8362 if (IS_I830(dev))
8363 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8364
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008365 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8366 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03008367 } else {
8368 /* Mask out read-only status bits. */
8369 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8370 DPLL_PORTC_READY_MASK |
8371 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008372 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02008373
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008374 if (IS_CHERRYVIEW(dev))
8375 chv_crtc_clock_get(crtc, pipe_config);
8376 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07008377 vlv_crtc_clock_get(crtc, pipe_config);
8378 else
8379 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03008380
Ville Syrjälä0f646142015-08-26 19:39:18 +03008381 /*
8382 * Normally the dotclock is filled in by the encoder .get_config()
8383 * but in case the pipe is enabled w/o any ports we need a sane
8384 * default.
8385 */
8386 pipe_config->base.adjusted_mode.crtc_clock =
8387 pipe_config->port_clock / pipe_config->pixel_multiplier;
8388
Imre Deak17290502016-02-12 18:55:11 +02008389 ret = true;
8390
8391out:
8392 intel_display_power_put(dev_priv, power_domain);
8393
8394 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008395}
8396
Paulo Zanonidde86e22012-12-01 12:04:25 -02008397static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07008398{
8399 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008400 struct intel_encoder *encoder;
Lyude1c1a24d2016-06-14 11:04:09 -04008401 int i;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008402 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008403 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008404 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008405 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07008406 bool has_ck505 = false;
8407 bool can_ssc = false;
Lyude1c1a24d2016-06-14 11:04:09 -04008408 bool using_ssc_source = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008409
8410 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01008411 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07008412 switch (encoder->type) {
8413 case INTEL_OUTPUT_LVDS:
8414 has_panel = true;
8415 has_lvds = true;
8416 break;
8417 case INTEL_OUTPUT_EDP:
8418 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03008419 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07008420 has_cpu_edp = true;
8421 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008422 default:
8423 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008424 }
8425 }
8426
Keith Packard99eb6a02011-09-26 14:29:12 -07008427 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008428 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07008429 can_ssc = has_ck505;
8430 } else {
8431 has_ck505 = false;
8432 can_ssc = true;
8433 }
8434
Lyude1c1a24d2016-06-14 11:04:09 -04008435 /* Check if any DPLLs are using the SSC source */
8436 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
8437 u32 temp = I915_READ(PCH_DPLL(i));
8438
8439 if (!(temp & DPLL_VCO_ENABLE))
8440 continue;
8441
8442 if ((temp & PLL_REF_INPUT_MASK) ==
8443 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
8444 using_ssc_source = true;
8445 break;
8446 }
8447 }
8448
8449 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
8450 has_panel, has_lvds, has_ck505, using_ssc_source);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008451
8452 /* Ironlake: try to setup display ref clock before DPLL
8453 * enabling. This is only under driver's control after
8454 * PCH B stepping, previous chipset stepping should be
8455 * ignoring this setting.
8456 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008457 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008458
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008459 /* As we must carefully and slowly disable/enable each source in turn,
8460 * compute the final state we want first and check if we need to
8461 * make any changes at all.
8462 */
8463 final = val;
8464 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07008465 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008466 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07008467 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008468 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8469
Daniel Vetter8c07eb62016-06-09 18:39:07 +02008470 final &= ~DREF_SSC_SOURCE_MASK;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008471 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Daniel Vetter8c07eb62016-06-09 18:39:07 +02008472 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008473
Keith Packard199e5d72011-09-22 12:01:57 -07008474 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008475 final |= DREF_SSC_SOURCE_ENABLE;
8476
8477 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8478 final |= DREF_SSC1_ENABLE;
8479
8480 if (has_cpu_edp) {
8481 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8482 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8483 else
8484 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8485 } else
8486 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Lyude1c1a24d2016-06-14 11:04:09 -04008487 } else if (using_ssc_source) {
8488 final |= DREF_SSC_SOURCE_ENABLE;
8489 final |= DREF_SSC1_ENABLE;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008490 }
8491
8492 if (final == val)
8493 return;
8494
8495 /* Always enable nonspread source */
8496 val &= ~DREF_NONSPREAD_SOURCE_MASK;
8497
8498 if (has_ck505)
8499 val |= DREF_NONSPREAD_CK505_ENABLE;
8500 else
8501 val |= DREF_NONSPREAD_SOURCE_ENABLE;
8502
8503 if (has_panel) {
8504 val &= ~DREF_SSC_SOURCE_MASK;
8505 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008506
Keith Packard199e5d72011-09-22 12:01:57 -07008507 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07008508 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008509 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008510 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02008511 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008512 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008513
8514 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008515 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008516 POSTING_READ(PCH_DREF_CONTROL);
8517 udelay(200);
8518
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008519 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008520
8521 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07008522 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07008523 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008524 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008525 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02008526 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008527 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07008528 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008529 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008530
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008531 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008532 POSTING_READ(PCH_DREF_CONTROL);
8533 udelay(200);
8534 } else {
Lyude1c1a24d2016-06-14 11:04:09 -04008535 DRM_DEBUG_KMS("Disabling CPU source output\n");
Keith Packard199e5d72011-09-22 12:01:57 -07008536
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008537 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07008538
8539 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008540 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008541
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008542 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008543 POSTING_READ(PCH_DREF_CONTROL);
8544 udelay(200);
8545
Lyude1c1a24d2016-06-14 11:04:09 -04008546 if (!using_ssc_source) {
8547 DRM_DEBUG_KMS("Disabling SSC source\n");
Keith Packard199e5d72011-09-22 12:01:57 -07008548
Lyude1c1a24d2016-06-14 11:04:09 -04008549 /* Turn off the SSC source */
8550 val &= ~DREF_SSC_SOURCE_MASK;
8551 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008552
Lyude1c1a24d2016-06-14 11:04:09 -04008553 /* Turn off SSC1 */
8554 val &= ~DREF_SSC1_ENABLE;
8555
8556 I915_WRITE(PCH_DREF_CONTROL, val);
8557 POSTING_READ(PCH_DREF_CONTROL);
8558 udelay(200);
8559 }
Jesse Barnes13d83a62011-08-03 12:59:20 -07008560 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008561
8562 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008563}
8564
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008565static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02008566{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008567 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008568
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008569 tmp = I915_READ(SOUTH_CHICKEN2);
8570 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8571 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008572
Imre Deakcf3598c2016-06-28 13:37:31 +03008573 if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
8574 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008575 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02008576
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008577 tmp = I915_READ(SOUTH_CHICKEN2);
8578 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8579 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008580
Imre Deakcf3598c2016-06-28 13:37:31 +03008581 if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
8582 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008583 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008584}
8585
8586/* WaMPhyProgramming:hsw */
8587static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8588{
8589 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008590
8591 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8592 tmp &= ~(0xFF << 24);
8593 tmp |= (0x12 << 24);
8594 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8595
Paulo Zanonidde86e22012-12-01 12:04:25 -02008596 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8597 tmp |= (1 << 11);
8598 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8599
8600 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8601 tmp |= (1 << 11);
8602 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8603
Paulo Zanonidde86e22012-12-01 12:04:25 -02008604 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8605 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8606 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8607
8608 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8609 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8610 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8611
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008612 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8613 tmp &= ~(7 << 13);
8614 tmp |= (5 << 13);
8615 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008616
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008617 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8618 tmp &= ~(7 << 13);
8619 tmp |= (5 << 13);
8620 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008621
8622 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8623 tmp &= ~0xFF;
8624 tmp |= 0x1C;
8625 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8626
8627 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8628 tmp &= ~0xFF;
8629 tmp |= 0x1C;
8630 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8631
8632 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8633 tmp &= ~(0xFF << 16);
8634 tmp |= (0x1C << 16);
8635 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8636
8637 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8638 tmp &= ~(0xFF << 16);
8639 tmp |= (0x1C << 16);
8640 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8641
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008642 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8643 tmp |= (1 << 27);
8644 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008645
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008646 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8647 tmp |= (1 << 27);
8648 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008649
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008650 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8651 tmp &= ~(0xF << 28);
8652 tmp |= (4 << 28);
8653 intel_sbi_write(dev_priv, 0x20EC, 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, 0x21EC, SBI_MPHY);
8656 tmp &= ~(0xF << 28);
8657 tmp |= (4 << 28);
8658 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008659}
8660
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008661/* Implements 3 different sequences from BSpec chapter "Display iCLK
8662 * Programming" based on the parameters passed:
8663 * - Sequence to enable CLKOUT_DP
8664 * - Sequence to enable CLKOUT_DP without spread
8665 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8666 */
8667static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8668 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008669{
8670 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008671 uint32_t reg, tmp;
8672
8673 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8674 with_spread = true;
Ville Syrjäläc2699522015-08-27 23:55:59 +03008675 if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008676 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008677
Ville Syrjäläa5805162015-05-26 20:42:30 +03008678 mutex_lock(&dev_priv->sb_lock);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008679
8680 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8681 tmp &= ~SBI_SSCCTL_DISABLE;
8682 tmp |= SBI_SSCCTL_PATHALT;
8683 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8684
8685 udelay(24);
8686
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008687 if (with_spread) {
8688 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8689 tmp &= ~SBI_SSCCTL_PATHALT;
8690 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008691
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008692 if (with_fdi) {
8693 lpt_reset_fdi_mphy(dev_priv);
8694 lpt_program_fdi_mphy(dev_priv);
8695 }
8696 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02008697
Ville Syrjäläc2699522015-08-27 23:55:59 +03008698 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008699 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8700 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8701 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01008702
Ville Syrjäläa5805162015-05-26 20:42:30 +03008703 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008704}
8705
Paulo Zanoni47701c32013-07-23 11:19:25 -03008706/* Sequence to disable CLKOUT_DP */
8707static void lpt_disable_clkout_dp(struct drm_device *dev)
8708{
8709 struct drm_i915_private *dev_priv = dev->dev_private;
8710 uint32_t reg, tmp;
8711
Ville Syrjäläa5805162015-05-26 20:42:30 +03008712 mutex_lock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008713
Ville Syrjäläc2699522015-08-27 23:55:59 +03008714 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni47701c32013-07-23 11:19:25 -03008715 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8716 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8717 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8718
8719 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8720 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8721 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8722 tmp |= SBI_SSCCTL_PATHALT;
8723 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8724 udelay(32);
8725 }
8726 tmp |= SBI_SSCCTL_DISABLE;
8727 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8728 }
8729
Ville Syrjäläa5805162015-05-26 20:42:30 +03008730 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008731}
8732
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008733#define BEND_IDX(steps) ((50 + (steps)) / 5)
8734
8735static const uint16_t sscdivintphase[] = {
8736 [BEND_IDX( 50)] = 0x3B23,
8737 [BEND_IDX( 45)] = 0x3B23,
8738 [BEND_IDX( 40)] = 0x3C23,
8739 [BEND_IDX( 35)] = 0x3C23,
8740 [BEND_IDX( 30)] = 0x3D23,
8741 [BEND_IDX( 25)] = 0x3D23,
8742 [BEND_IDX( 20)] = 0x3E23,
8743 [BEND_IDX( 15)] = 0x3E23,
8744 [BEND_IDX( 10)] = 0x3F23,
8745 [BEND_IDX( 5)] = 0x3F23,
8746 [BEND_IDX( 0)] = 0x0025,
8747 [BEND_IDX( -5)] = 0x0025,
8748 [BEND_IDX(-10)] = 0x0125,
8749 [BEND_IDX(-15)] = 0x0125,
8750 [BEND_IDX(-20)] = 0x0225,
8751 [BEND_IDX(-25)] = 0x0225,
8752 [BEND_IDX(-30)] = 0x0325,
8753 [BEND_IDX(-35)] = 0x0325,
8754 [BEND_IDX(-40)] = 0x0425,
8755 [BEND_IDX(-45)] = 0x0425,
8756 [BEND_IDX(-50)] = 0x0525,
8757};
8758
8759/*
8760 * Bend CLKOUT_DP
8761 * steps -50 to 50 inclusive, in steps of 5
8762 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
8763 * change in clock period = -(steps / 10) * 5.787 ps
8764 */
8765static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
8766{
8767 uint32_t tmp;
8768 int idx = BEND_IDX(steps);
8769
8770 if (WARN_ON(steps % 5 != 0))
8771 return;
8772
8773 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
8774 return;
8775
8776 mutex_lock(&dev_priv->sb_lock);
8777
8778 if (steps % 10 != 0)
8779 tmp = 0xAAAAAAAB;
8780 else
8781 tmp = 0x00000000;
8782 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
8783
8784 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
8785 tmp &= 0xffff0000;
8786 tmp |= sscdivintphase[idx];
8787 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
8788
8789 mutex_unlock(&dev_priv->sb_lock);
8790}
8791
8792#undef BEND_IDX
8793
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008794static void lpt_init_pch_refclk(struct drm_device *dev)
8795{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008796 struct intel_encoder *encoder;
8797 bool has_vga = false;
8798
Damien Lespiaub2784e12014-08-05 11:29:37 +01008799 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008800 switch (encoder->type) {
8801 case INTEL_OUTPUT_ANALOG:
8802 has_vga = true;
8803 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008804 default:
8805 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008806 }
8807 }
8808
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008809 if (has_vga) {
8810 lpt_bend_clkout_dp(to_i915(dev), 0);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008811 lpt_enable_clkout_dp(dev, true, true);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008812 } else {
Paulo Zanoni47701c32013-07-23 11:19:25 -03008813 lpt_disable_clkout_dp(dev);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008814 }
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008815}
8816
Paulo Zanonidde86e22012-12-01 12:04:25 -02008817/*
8818 * Initialize reference clocks when the driver loads
8819 */
8820void intel_init_pch_refclk(struct drm_device *dev)
8821{
8822 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8823 ironlake_init_pch_refclk(dev);
8824 else if (HAS_PCH_LPT(dev))
8825 lpt_init_pch_refclk(dev);
8826}
8827
Daniel Vetter6ff93602013-04-19 11:24:36 +02008828static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03008829{
8830 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8831 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8832 int pipe = intel_crtc->pipe;
8833 uint32_t val;
8834
Daniel Vetter78114072013-06-13 00:54:57 +02008835 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03008836
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008837 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03008838 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008839 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008840 break;
8841 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008842 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008843 break;
8844 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008845 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008846 break;
8847 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008848 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008849 break;
8850 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03008851 /* Case prevented by intel_choose_pipe_bpp_dither. */
8852 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03008853 }
8854
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008855 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03008856 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8857
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008858 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03008859 val |= PIPECONF_INTERLACED_ILK;
8860 else
8861 val |= PIPECONF_PROGRESSIVE;
8862
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008863 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008864 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008865
Paulo Zanonic8203562012-09-12 10:06:29 -03008866 I915_WRITE(PIPECONF(pipe), val);
8867 POSTING_READ(PIPECONF(pipe));
8868}
8869
Daniel Vetter6ff93602013-04-19 11:24:36 +02008870static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008871{
Jani Nikula391bf042016-03-18 17:05:40 +02008872 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008873 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008874 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jani Nikula391bf042016-03-18 17:05:40 +02008875 u32 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008876
Jani Nikula391bf042016-03-18 17:05:40 +02008877 if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008878 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8879
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008880 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008881 val |= PIPECONF_INTERLACED_ILK;
8882 else
8883 val |= PIPECONF_PROGRESSIVE;
8884
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008885 I915_WRITE(PIPECONF(cpu_transcoder), val);
8886 POSTING_READ(PIPECONF(cpu_transcoder));
Jani Nikula391bf042016-03-18 17:05:40 +02008887}
8888
Jani Nikula391bf042016-03-18 17:05:40 +02008889static void haswell_set_pipemisc(struct drm_crtc *crtc)
8890{
8891 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8892 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8893
8894 if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
8895 u32 val = 0;
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008896
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008897 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008898 case 18:
8899 val |= PIPEMISC_DITHER_6_BPC;
8900 break;
8901 case 24:
8902 val |= PIPEMISC_DITHER_8_BPC;
8903 break;
8904 case 30:
8905 val |= PIPEMISC_DITHER_10_BPC;
8906 break;
8907 case 36:
8908 val |= PIPEMISC_DITHER_12_BPC;
8909 break;
8910 default:
8911 /* Case prevented by pipe_config_set_bpp. */
8912 BUG();
8913 }
8914
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008915 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008916 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8917
Jani Nikula391bf042016-03-18 17:05:40 +02008918 I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008919 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008920}
8921
Paulo Zanonid4b19312012-11-29 11:29:32 -02008922int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8923{
8924 /*
8925 * Account for spread spectrum to avoid
8926 * oversubscribing the link. Max center spread
8927 * is 2.5%; use 5% for safety's sake.
8928 */
8929 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02008930 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02008931}
8932
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008933static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02008934{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008935 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03008936}
8937
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008938static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
8939 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03008940 struct dpll *reduced_clock)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008941{
8942 struct drm_crtc *crtc = &intel_crtc->base;
8943 struct drm_device *dev = crtc->dev;
8944 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008945 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008946 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008947 struct drm_connector_state *connector_state;
8948 struct intel_encoder *encoder;
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008949 u32 dpll, fp, fp2;
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02008950 int factor, i;
Daniel Vetter09ede542013-04-30 14:01:45 +02008951 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008952
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008953 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008954 if (connector_state->crtc != crtc_state->base.crtc)
8955 continue;
8956
8957 encoder = to_intel_encoder(connector_state->best_encoder);
8958
8959 switch (encoder->type) {
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008960 case INTEL_OUTPUT_LVDS:
8961 is_lvds = true;
8962 break;
8963 case INTEL_OUTPUT_SDVO:
8964 case INTEL_OUTPUT_HDMI:
8965 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008966 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008967 default:
8968 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008969 }
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008970 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008971
Chris Wilsonc1858122010-12-03 21:35:48 +00008972 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07008973 factor = 21;
8974 if (is_lvds) {
8975 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008976 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02008977 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07008978 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008979 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07008980 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00008981
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008982 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Chris Wilsonc1858122010-12-03 21:35:48 +00008983
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008984 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
8985 fp |= FP_CB_TUNE;
8986
8987 if (reduced_clock) {
8988 fp2 = i9xx_dpll_compute_fp(reduced_clock);
8989
8990 if (reduced_clock->m < factor * reduced_clock->n)
8991 fp2 |= FP_CB_TUNE;
8992 } else {
8993 fp2 = fp;
8994 }
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008995
Chris Wilson5eddb702010-09-11 13:48:45 +01008996 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08008997
Eric Anholta07d6782011-03-30 13:01:08 -07008998 if (is_lvds)
8999 dpll |= DPLLB_MODE_LVDS;
9000 else
9001 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02009002
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009003 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02009004 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02009005
9006 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02009007 dpll |= DPLL_SDVO_HIGH_SPEED;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009008 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02009009 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08009010
Eric Anholta07d6782011-03-30 13:01:08 -07009011 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009012 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07009013 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009014 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07009015
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009016 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07009017 case 5:
9018 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
9019 break;
9020 case 7:
9021 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
9022 break;
9023 case 10:
9024 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
9025 break;
9026 case 14:
9027 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
9028 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08009029 }
9030
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02009031 if (is_lvds && intel_panel_use_ssc(dev_priv))
Kristian Høgsberg43565a02009-02-13 20:56:52 -05009032 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08009033 else
9034 dpll |= PLL_REF_INPUT_DREFCLK;
9035
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02009036 dpll |= DPLL_VCO_ENABLE;
9037
9038 crtc_state->dpll_hw_state.dpll = dpll;
9039 crtc_state->dpll_hw_state.fp0 = fp;
9040 crtc_state->dpll_hw_state.fp1 = fp2;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03009041}
9042
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009043static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
9044 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08009045{
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02009046 struct drm_device *dev = crtc->base.dev;
9047 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03009048 struct dpll reduced_clock;
Ander Conselvan de Oliveira7ed9f892016-03-21 18:00:07 +02009049 bool has_reduced_clock = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02009050 struct intel_shared_dpll *pll;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03009051 const struct intel_limit *limit;
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02009052 int refclk = 120000;
Jesse Barnes79e53942008-11-07 14:24:08 -08009053
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03009054 memset(&crtc_state->dpll_hw_state, 0,
9055 sizeof(crtc_state->dpll_hw_state));
9056
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02009057 crtc->lowfreq_avail = false;
9058
9059 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
9060 if (!crtc_state->has_pch_encoder)
9061 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009062
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02009063 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
9064 if (intel_panel_use_ssc(dev_priv)) {
9065 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
9066 dev_priv->vbt.lvds_ssc_freq);
9067 refclk = dev_priv->vbt.lvds_ssc_freq;
9068 }
9069
9070 if (intel_is_dual_link_lvds(dev)) {
9071 if (refclk == 100000)
9072 limit = &intel_limits_ironlake_dual_lvds_100m;
9073 else
9074 limit = &intel_limits_ironlake_dual_lvds;
9075 } else {
9076 if (refclk == 100000)
9077 limit = &intel_limits_ironlake_single_lvds_100m;
9078 else
9079 limit = &intel_limits_ironlake_single_lvds;
9080 }
9081 } else {
9082 limit = &intel_limits_ironlake_dac;
9083 }
9084
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02009085 if (!crtc_state->clock_set &&
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02009086 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
9087 refclk, NULL, &crtc_state->dpll)) {
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02009088 DRM_ERROR("Couldn't find PLL settings for mode!\n");
9089 return -EINVAL;
Daniel Vetterf47709a2013-03-28 10:42:02 +01009090 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009091
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02009092 ironlake_compute_dpll(crtc, crtc_state,
9093 has_reduced_clock ? &reduced_clock : NULL);
Daniel Vetter66e985c2013-06-05 13:34:20 +02009094
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02009095 pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
9096 if (pll == NULL) {
9097 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
9098 pipe_name(crtc->pipe));
9099 return -EINVAL;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02009100 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009101
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02009102 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
9103 has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009104 crtc->lowfreq_avail = true;
Daniel Vettere2b78262013-06-07 23:10:03 +02009105
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009106 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009107}
9108
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009109static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
9110 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02009111{
9112 struct drm_device *dev = crtc->base.dev;
9113 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009114 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02009115
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009116 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
9117 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
9118 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
9119 & ~TU_SIZE_MASK;
9120 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
9121 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
9122 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9123}
9124
9125static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
9126 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009127 struct intel_link_m_n *m_n,
9128 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009129{
9130 struct drm_device *dev = crtc->base.dev;
9131 struct drm_i915_private *dev_priv = dev->dev_private;
9132 enum pipe pipe = crtc->pipe;
9133
9134 if (INTEL_INFO(dev)->gen >= 5) {
9135 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
9136 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
9137 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
9138 & ~TU_SIZE_MASK;
9139 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
9140 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
9141 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009142 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
9143 * gen < 8) and if DRRS is supported (to make sure the
9144 * registers are not unnecessarily read).
9145 */
9146 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009147 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009148 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
9149 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
9150 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
9151 & ~TU_SIZE_MASK;
9152 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
9153 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
9154 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9155 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009156 } else {
9157 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
9158 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
9159 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
9160 & ~TU_SIZE_MASK;
9161 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
9162 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
9163 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9164 }
9165}
9166
9167void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009168 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009169{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02009170 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009171 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
9172 else
9173 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009174 &pipe_config->dp_m_n,
9175 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009176}
9177
Daniel Vetter72419202013-04-04 13:28:53 +02009178static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009179 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02009180{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009181 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009182 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02009183}
9184
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009185static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009186 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009187{
9188 struct drm_device *dev = crtc->base.dev;
9189 struct drm_i915_private *dev_priv = dev->dev_private;
Chandra Kondurua1b22782015-04-07 15:28:45 -07009190 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
9191 uint32_t ps_ctrl = 0;
9192 int id = -1;
9193 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009194
Chandra Kondurua1b22782015-04-07 15:28:45 -07009195 /* find scaler attached to this pipe */
9196 for (i = 0; i < crtc->num_scalers; i++) {
9197 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
9198 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
9199 id = i;
9200 pipe_config->pch_pfit.enabled = true;
9201 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9202 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9203 break;
9204 }
9205 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009206
Chandra Kondurua1b22782015-04-07 15:28:45 -07009207 scaler_state->scaler_id = id;
9208 if (id >= 0) {
9209 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9210 } else {
9211 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009212 }
9213}
9214
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009215static void
9216skylake_get_initial_plane_config(struct intel_crtc *crtc,
9217 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009218{
9219 struct drm_device *dev = crtc->base.dev;
9220 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau40f46282015-02-27 11:15:21 +00009221 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009222 int pipe = crtc->pipe;
9223 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009224 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009225 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009226 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009227
Damien Lespiaud9806c92015-01-21 14:07:19 +00009228 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009229 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009230 DRM_DEBUG_KMS("failed to alloc fb\n");
9231 return;
9232 }
9233
Damien Lespiau1b842c82015-01-21 13:50:54 +00009234 fb = &intel_fb->base;
9235
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009236 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00009237 if (!(val & PLANE_CTL_ENABLE))
9238 goto error;
9239
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009240 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9241 fourcc = skl_format_to_fourcc(pixel_format,
9242 val & PLANE_CTL_ORDER_RGBX,
9243 val & PLANE_CTL_ALPHA_MASK);
9244 fb->pixel_format = fourcc;
9245 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9246
Damien Lespiau40f46282015-02-27 11:15:21 +00009247 tiling = val & PLANE_CTL_TILED_MASK;
9248 switch (tiling) {
9249 case PLANE_CTL_TILED_LINEAR:
9250 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9251 break;
9252 case PLANE_CTL_TILED_X:
9253 plane_config->tiling = I915_TILING_X;
9254 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9255 break;
9256 case PLANE_CTL_TILED_Y:
9257 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9258 break;
9259 case PLANE_CTL_TILED_YF:
9260 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9261 break;
9262 default:
9263 MISSING_CASE(tiling);
9264 goto error;
9265 }
9266
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009267 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9268 plane_config->base = base;
9269
9270 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9271
9272 val = I915_READ(PLANE_SIZE(pipe, 0));
9273 fb->height = ((val >> 16) & 0xfff) + 1;
9274 fb->width = ((val >> 0) & 0x1fff) + 1;
9275
9276 val = I915_READ(PLANE_STRIDE(pipe, 0));
Ville Syrjälä7b49f942016-01-12 21:08:32 +02009277 stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
Damien Lespiau40f46282015-02-27 11:15:21 +00009278 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009279 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9280
9281 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009282 fb->pixel_format,
9283 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009284
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009285 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009286
9287 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9288 pipe_name(pipe), fb->width, fb->height,
9289 fb->bits_per_pixel, base, fb->pitches[0],
9290 plane_config->size);
9291
Damien Lespiau2d140302015-02-05 17:22:18 +00009292 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009293 return;
9294
9295error:
9296 kfree(fb);
9297}
9298
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009299static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009300 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009301{
9302 struct drm_device *dev = crtc->base.dev;
9303 struct drm_i915_private *dev_priv = dev->dev_private;
9304 uint32_t tmp;
9305
9306 tmp = I915_READ(PF_CTL(crtc->pipe));
9307
9308 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009309 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009310 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9311 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02009312
9313 /* We currently do not free assignements of panel fitters on
9314 * ivb/hsw (since we don't use the higher upscaling modes which
9315 * differentiates them) so just WARN about this case for now. */
9316 if (IS_GEN7(dev)) {
9317 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9318 PF_PIPE_SEL_IVB(crtc->pipe));
9319 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009320 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009321}
9322
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009323static void
9324ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9325 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009326{
9327 struct drm_device *dev = crtc->base.dev;
9328 struct drm_i915_private *dev_priv = dev->dev_private;
9329 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009330 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009331 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009332 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009333 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009334 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009335
Damien Lespiau42a7b082015-02-05 19:35:13 +00009336 val = I915_READ(DSPCNTR(pipe));
9337 if (!(val & DISPLAY_PLANE_ENABLE))
9338 return;
9339
Damien Lespiaud9806c92015-01-21 14:07:19 +00009340 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009341 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009342 DRM_DEBUG_KMS("failed to alloc fb\n");
9343 return;
9344 }
9345
Damien Lespiau1b842c82015-01-21 13:50:54 +00009346 fb = &intel_fb->base;
9347
Daniel Vetter18c52472015-02-10 17:16:09 +00009348 if (INTEL_INFO(dev)->gen >= 4) {
9349 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00009350 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00009351 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9352 }
9353 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009354
9355 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00009356 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009357 fb->pixel_format = fourcc;
9358 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009359
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009360 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009361 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009362 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009363 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00009364 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009365 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009366 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009367 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009368 }
9369 plane_config->base = base;
9370
9371 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009372 fb->width = ((val >> 16) & 0xfff) + 1;
9373 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009374
9375 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009376 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009377
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009378 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009379 fb->pixel_format,
9380 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009381
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009382 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009383
Damien Lespiau2844a922015-01-20 12:51:48 +00009384 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9385 pipe_name(pipe), fb->width, fb->height,
9386 fb->bits_per_pixel, base, fb->pitches[0],
9387 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009388
Damien Lespiau2d140302015-02-05 17:22:18 +00009389 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009390}
9391
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009392static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009393 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009394{
9395 struct drm_device *dev = crtc->base.dev;
9396 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak17290502016-02-12 18:55:11 +02009397 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009398 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02009399 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009400
Imre Deak17290502016-02-12 18:55:11 +02009401 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9402 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03009403 return false;
9404
Daniel Vettere143a212013-07-04 12:01:15 +02009405 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009406 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02009407
Imre Deak17290502016-02-12 18:55:11 +02009408 ret = false;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009409 tmp = I915_READ(PIPECONF(crtc->pipe));
9410 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02009411 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009412
Ville Syrjälä42571ae2013-09-06 23:29:00 +03009413 switch (tmp & PIPECONF_BPC_MASK) {
9414 case PIPECONF_6BPC:
9415 pipe_config->pipe_bpp = 18;
9416 break;
9417 case PIPECONF_8BPC:
9418 pipe_config->pipe_bpp = 24;
9419 break;
9420 case PIPECONF_10BPC:
9421 pipe_config->pipe_bpp = 30;
9422 break;
9423 case PIPECONF_12BPC:
9424 pipe_config->pipe_bpp = 36;
9425 break;
9426 default:
9427 break;
9428 }
9429
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02009430 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9431 pipe_config->limited_color_range = true;
9432
Daniel Vetterab9412b2013-05-03 11:49:46 +02009433 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02009434 struct intel_shared_dpll *pll;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009435 enum intel_dpll_id pll_id;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009436
Daniel Vetter88adfff2013-03-28 10:42:01 +01009437 pipe_config->has_pch_encoder = true;
9438
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009439 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9440 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9441 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02009442
9443 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009444
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009445 if (HAS_PCH_IBX(dev_priv)) {
Imre Deakd9a7bc62016-05-12 16:18:50 +03009446 /*
9447 * The pipe->pch transcoder and pch transcoder->pll
9448 * mapping is fixed.
9449 */
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009450 pll_id = (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009451 } else {
9452 tmp = I915_READ(PCH_DPLL_SEL);
9453 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009454 pll_id = DPLL_ID_PCH_PLL_B;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009455 else
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009456 pll_id= DPLL_ID_PCH_PLL_A;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009457 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02009458
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009459 pipe_config->shared_dpll =
9460 intel_get_shared_dpll_by_id(dev_priv, pll_id);
9461 pll = pipe_config->shared_dpll;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009462
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +02009463 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9464 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02009465
9466 tmp = pipe_config->dpll_hw_state.dpll;
9467 pipe_config->pixel_multiplier =
9468 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9469 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03009470
9471 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009472 } else {
9473 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009474 }
9475
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009476 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +02009477 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009478
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009479 ironlake_get_pfit_config(crtc, pipe_config);
9480
Imre Deak17290502016-02-12 18:55:11 +02009481 ret = true;
9482
9483out:
9484 intel_display_power_put(dev_priv, power_domain);
9485
9486 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009487}
9488
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009489static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9490{
9491 struct drm_device *dev = dev_priv->dev;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009492 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009493
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009494 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05009495 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009496 pipe_name(crtc->pipe));
9497
Rob Clarke2c719b2014-12-15 13:56:32 -05009498 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9499 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
Ville Syrjälä01403de2015-09-18 20:03:33 +03009500 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9501 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009502 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9503 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009504 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03009505 if (IS_HASWELL(dev))
Rob Clarke2c719b2014-12-15 13:56:32 -05009506 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03009507 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009508 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009509 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009510 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009511 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009512 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009513
Paulo Zanoni9926ada2014-04-01 19:39:47 -03009514 /*
9515 * In theory we can still leave IRQs enabled, as long as only the HPD
9516 * interrupts remain enabled. We used to check for that, but since it's
9517 * gen-specific and since we only disable LCPLL after we fully disable
9518 * the interrupts, the check below should be enough.
9519 */
Rob Clarke2c719b2014-12-15 13:56:32 -05009520 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009521}
9522
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009523static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9524{
9525 struct drm_device *dev = dev_priv->dev;
9526
9527 if (IS_HASWELL(dev))
9528 return I915_READ(D_COMP_HSW);
9529 else
9530 return I915_READ(D_COMP_BDW);
9531}
9532
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009533static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9534{
9535 struct drm_device *dev = dev_priv->dev;
9536
9537 if (IS_HASWELL(dev)) {
9538 mutex_lock(&dev_priv->rps.hw_lock);
9539 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9540 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03009541 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009542 mutex_unlock(&dev_priv->rps.hw_lock);
9543 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009544 I915_WRITE(D_COMP_BDW, val);
9545 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009546 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009547}
9548
9549/*
9550 * This function implements pieces of two sequences from BSpec:
9551 * - Sequence for display software to disable LCPLL
9552 * - Sequence for display software to allow package C8+
9553 * The steps implemented here are just the steps that actually touch the LCPLL
9554 * register. Callers should take care of disabling all the display engine
9555 * functions, doing the mode unset, fixing interrupts, etc.
9556 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009557static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9558 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009559{
9560 uint32_t val;
9561
9562 assert_can_disable_lcpll(dev_priv);
9563
9564 val = I915_READ(LCPLL_CTL);
9565
9566 if (switch_to_fclk) {
9567 val |= LCPLL_CD_SOURCE_FCLK;
9568 I915_WRITE(LCPLL_CTL, val);
9569
Imre Deakf53dd632016-06-28 13:37:32 +03009570 if (wait_for_us(I915_READ(LCPLL_CTL) &
9571 LCPLL_CD_SOURCE_FCLK_DONE, 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009572 DRM_ERROR("Switching to FCLK failed\n");
9573
9574 val = I915_READ(LCPLL_CTL);
9575 }
9576
9577 val |= LCPLL_PLL_DISABLE;
9578 I915_WRITE(LCPLL_CTL, val);
9579 POSTING_READ(LCPLL_CTL);
9580
Chris Wilson24d84412016-06-30 15:33:07 +01009581 if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009582 DRM_ERROR("LCPLL still locked\n");
9583
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009584 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009585 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009586 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009587 ndelay(100);
9588
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009589 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9590 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009591 DRM_ERROR("D_COMP RCOMP still in progress\n");
9592
9593 if (allow_power_down) {
9594 val = I915_READ(LCPLL_CTL);
9595 val |= LCPLL_POWER_DOWN_ALLOW;
9596 I915_WRITE(LCPLL_CTL, val);
9597 POSTING_READ(LCPLL_CTL);
9598 }
9599}
9600
9601/*
9602 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9603 * source.
9604 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009605static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009606{
9607 uint32_t val;
9608
9609 val = I915_READ(LCPLL_CTL);
9610
9611 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9612 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9613 return;
9614
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009615 /*
9616 * Make sure we're not on PC8 state before disabling PC8, otherwise
9617 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009618 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02009619 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03009620
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009621 if (val & LCPLL_POWER_DOWN_ALLOW) {
9622 val &= ~LCPLL_POWER_DOWN_ALLOW;
9623 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02009624 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009625 }
9626
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009627 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009628 val |= D_COMP_COMP_FORCE;
9629 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009630 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009631
9632 val = I915_READ(LCPLL_CTL);
9633 val &= ~LCPLL_PLL_DISABLE;
9634 I915_WRITE(LCPLL_CTL, val);
9635
9636 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9637 DRM_ERROR("LCPLL not locked yet\n");
9638
9639 if (val & LCPLL_CD_SOURCE_FCLK) {
9640 val = I915_READ(LCPLL_CTL);
9641 val &= ~LCPLL_CD_SOURCE_FCLK;
9642 I915_WRITE(LCPLL_CTL, val);
9643
Imre Deakf53dd632016-06-28 13:37:32 +03009644 if (wait_for_us((I915_READ(LCPLL_CTL) &
9645 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009646 DRM_ERROR("Switching back to LCPLL failed\n");
9647 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03009648
Mika Kuoppala59bad942015-01-16 11:34:40 +02009649 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ville Syrjäläb6283052015-06-03 15:45:07 +03009650 intel_update_cdclk(dev_priv->dev);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009651}
9652
Paulo Zanoni765dab672014-03-07 20:08:18 -03009653/*
9654 * Package states C8 and deeper are really deep PC states that can only be
9655 * reached when all the devices on the system allow it, so even if the graphics
9656 * device allows PC8+, it doesn't mean the system will actually get to these
9657 * states. Our driver only allows PC8+ when going into runtime PM.
9658 *
9659 * The requirements for PC8+ are that all the outputs are disabled, the power
9660 * well is disabled and most interrupts are disabled, and these are also
9661 * requirements for runtime PM. When these conditions are met, we manually do
9662 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9663 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9664 * hang the machine.
9665 *
9666 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9667 * the state of some registers, so when we come back from PC8+ we need to
9668 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9669 * need to take care of the registers kept by RC6. Notice that this happens even
9670 * if we don't put the device in PCI D3 state (which is what currently happens
9671 * because of the runtime PM support).
9672 *
9673 * For more, read "Display Sequences for Package C8" on the hardware
9674 * documentation.
9675 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009676void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009677{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009678 struct drm_device *dev = dev_priv->dev;
9679 uint32_t val;
9680
Paulo Zanonic67a4702013-08-19 13:18:09 -03009681 DRM_DEBUG_KMS("Enabling package C8+\n");
9682
Ville Syrjäläc2699522015-08-27 23:55:59 +03009683 if (HAS_PCH_LPT_LP(dev)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009684 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9685 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9686 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9687 }
9688
9689 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009690 hsw_disable_lcpll(dev_priv, true, true);
9691}
9692
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009693void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009694{
9695 struct drm_device *dev = dev_priv->dev;
9696 uint32_t val;
9697
Paulo Zanonic67a4702013-08-19 13:18:09 -03009698 DRM_DEBUG_KMS("Disabling package C8+\n");
9699
9700 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009701 lpt_init_pch_refclk(dev);
9702
Ville Syrjäläc2699522015-08-27 23:55:59 +03009703 if (HAS_PCH_LPT_LP(dev)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009704 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9705 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9706 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9707 }
Paulo Zanonic67a4702013-08-19 13:18:09 -03009708}
9709
Imre Deak324513c2016-06-13 16:44:36 +03009710static void bxt_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309711{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009712 struct drm_device *dev = old_state->dev;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009713 struct intel_atomic_state *old_intel_state =
9714 to_intel_atomic_state(old_state);
9715 unsigned int req_cdclk = old_intel_state->dev_cdclk;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309716
Imre Deak324513c2016-06-13 16:44:36 +03009717 bxt_set_cdclk(to_i915(dev), req_cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309718}
9719
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009720/* compute the max rate for new configuration */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009721static int ilk_max_pixel_rate(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009722{
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009723 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
9724 struct drm_i915_private *dev_priv = state->dev->dev_private;
9725 struct drm_crtc *crtc;
9726 struct drm_crtc_state *cstate;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009727 struct intel_crtc_state *crtc_state;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009728 unsigned max_pixel_rate = 0, i;
9729 enum pipe pipe;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009730
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009731 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
9732 sizeof(intel_state->min_pixclk));
9733
9734 for_each_crtc_in_state(state, crtc, cstate, i) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009735 int pixel_rate;
9736
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009737 crtc_state = to_intel_crtc_state(cstate);
9738 if (!crtc_state->base.enable) {
9739 intel_state->min_pixclk[i] = 0;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009740 continue;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009741 }
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009742
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009743 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009744
9745 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009746 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009747 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9748
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009749 intel_state->min_pixclk[i] = pixel_rate;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009750 }
9751
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009752 for_each_pipe(dev_priv, pipe)
9753 max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
9754
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009755 return max_pixel_rate;
9756}
9757
9758static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9759{
9760 struct drm_i915_private *dev_priv = dev->dev_private;
9761 uint32_t val, data;
9762 int ret;
9763
9764 if (WARN((I915_READ(LCPLL_CTL) &
9765 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9766 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9767 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9768 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9769 "trying to change cdclk frequency with cdclk not enabled\n"))
9770 return;
9771
9772 mutex_lock(&dev_priv->rps.hw_lock);
9773 ret = sandybridge_pcode_write(dev_priv,
9774 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9775 mutex_unlock(&dev_priv->rps.hw_lock);
9776 if (ret) {
9777 DRM_ERROR("failed to inform pcode about cdclk change\n");
9778 return;
9779 }
9780
9781 val = I915_READ(LCPLL_CTL);
9782 val |= LCPLL_CD_SOURCE_FCLK;
9783 I915_WRITE(LCPLL_CTL, val);
9784
Tvrtko Ursulin5ba00172016-03-03 14:36:45 +00009785 if (wait_for_us(I915_READ(LCPLL_CTL) &
9786 LCPLL_CD_SOURCE_FCLK_DONE, 1))
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009787 DRM_ERROR("Switching to FCLK failed\n");
9788
9789 val = I915_READ(LCPLL_CTL);
9790 val &= ~LCPLL_CLK_FREQ_MASK;
9791
9792 switch (cdclk) {
9793 case 450000:
9794 val |= LCPLL_CLK_FREQ_450;
9795 data = 0;
9796 break;
9797 case 540000:
9798 val |= LCPLL_CLK_FREQ_54O_BDW;
9799 data = 1;
9800 break;
9801 case 337500:
9802 val |= LCPLL_CLK_FREQ_337_5_BDW;
9803 data = 2;
9804 break;
9805 case 675000:
9806 val |= LCPLL_CLK_FREQ_675_BDW;
9807 data = 3;
9808 break;
9809 default:
9810 WARN(1, "invalid cdclk frequency\n");
9811 return;
9812 }
9813
9814 I915_WRITE(LCPLL_CTL, val);
9815
9816 val = I915_READ(LCPLL_CTL);
9817 val &= ~LCPLL_CD_SOURCE_FCLK;
9818 I915_WRITE(LCPLL_CTL, val);
9819
Tvrtko Ursulin5ba00172016-03-03 14:36:45 +00009820 if (wait_for_us((I915_READ(LCPLL_CTL) &
9821 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009822 DRM_ERROR("Switching back to LCPLL failed\n");
9823
9824 mutex_lock(&dev_priv->rps.hw_lock);
9825 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9826 mutex_unlock(&dev_priv->rps.hw_lock);
9827
Ville Syrjälä7f1052a2016-04-26 19:46:32 +03009828 I915_WRITE(CDCLK_FREQ, DIV_ROUND_CLOSEST(cdclk, 1000) - 1);
9829
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009830 intel_update_cdclk(dev);
9831
9832 WARN(cdclk != dev_priv->cdclk_freq,
9833 "cdclk requested %d kHz but got %d kHz\n",
9834 cdclk, dev_priv->cdclk_freq);
9835}
9836
Ville Syrjälä587c7912016-05-11 22:44:41 +03009837static int broadwell_calc_cdclk(int max_pixclk)
9838{
9839 if (max_pixclk > 540000)
9840 return 675000;
9841 else if (max_pixclk > 450000)
9842 return 540000;
9843 else if (max_pixclk > 337500)
9844 return 450000;
9845 else
9846 return 337500;
9847}
9848
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009849static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009850{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009851 struct drm_i915_private *dev_priv = to_i915(state->dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009852 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009853 int max_pixclk = ilk_max_pixel_rate(state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009854 int cdclk;
9855
9856 /*
9857 * FIXME should also account for plane ratio
9858 * once 64bpp pixel formats are supported.
9859 */
Ville Syrjälä587c7912016-05-11 22:44:41 +03009860 cdclk = broadwell_calc_cdclk(max_pixclk);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009861
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009862 if (cdclk > dev_priv->max_cdclk_freq) {
Maarten Lankhorst63ba5342015-11-24 11:29:03 +01009863 DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9864 cdclk, dev_priv->max_cdclk_freq);
9865 return -EINVAL;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009866 }
9867
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009868 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
9869 if (!intel_state->active_crtcs)
Ville Syrjälä587c7912016-05-11 22:44:41 +03009870 intel_state->dev_cdclk = broadwell_calc_cdclk(0);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009871
9872 return 0;
9873}
9874
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009875static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009876{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009877 struct drm_device *dev = old_state->dev;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009878 struct intel_atomic_state *old_intel_state =
9879 to_intel_atomic_state(old_state);
9880 unsigned req_cdclk = old_intel_state->dev_cdclk;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009881
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009882 broadwell_set_cdclk(dev, req_cdclk);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009883}
9884
Clint Taylorc89e39f2016-05-13 23:41:21 +03009885static int skl_modeset_calc_cdclk(struct drm_atomic_state *state)
9886{
9887 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
9888 struct drm_i915_private *dev_priv = to_i915(state->dev);
9889 const int max_pixclk = ilk_max_pixel_rate(state);
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03009890 int vco = intel_state->cdclk_pll_vco;
Clint Taylorc89e39f2016-05-13 23:41:21 +03009891 int cdclk;
9892
9893 /*
9894 * FIXME should also account for plane ratio
9895 * once 64bpp pixel formats are supported.
9896 */
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03009897 cdclk = skl_calc_cdclk(max_pixclk, vco);
Clint Taylorc89e39f2016-05-13 23:41:21 +03009898
9899 /*
9900 * FIXME move the cdclk caclulation to
9901 * compute_config() so we can fail gracegully.
9902 */
9903 if (cdclk > dev_priv->max_cdclk_freq) {
9904 DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9905 cdclk, dev_priv->max_cdclk_freq);
9906 cdclk = dev_priv->max_cdclk_freq;
9907 }
9908
9909 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
9910 if (!intel_state->active_crtcs)
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03009911 intel_state->dev_cdclk = skl_calc_cdclk(0, vco);
Clint Taylorc89e39f2016-05-13 23:41:21 +03009912
9913 return 0;
9914}
9915
9916static void skl_modeset_commit_cdclk(struct drm_atomic_state *old_state)
9917{
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03009918 struct drm_i915_private *dev_priv = to_i915(old_state->dev);
9919 struct intel_atomic_state *intel_state = to_intel_atomic_state(old_state);
9920 unsigned int req_cdclk = intel_state->dev_cdclk;
9921 unsigned int req_vco = intel_state->cdclk_pll_vco;
Clint Taylorc89e39f2016-05-13 23:41:21 +03009922
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03009923 skl_set_cdclk(dev_priv, req_cdclk, req_vco);
Clint Taylorc89e39f2016-05-13 23:41:21 +03009924}
9925
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009926static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9927 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009928{
Mika Kaholaaf3997b2016-02-05 13:29:28 +02009929 struct intel_encoder *intel_encoder =
9930 intel_ddi_get_crtc_new_encoder(crtc_state);
9931
9932 if (intel_encoder->type != INTEL_OUTPUT_DSI) {
9933 if (!intel_ddi_pll_select(crtc, crtc_state))
9934 return -EINVAL;
9935 }
Daniel Vetter716c2e52014-06-25 22:02:02 +03009936
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009937 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02009938
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009939 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009940}
9941
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309942static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9943 enum port port,
9944 struct intel_crtc_state *pipe_config)
9945{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009946 enum intel_dpll_id id;
9947
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309948 switch (port) {
9949 case PORT_A:
9950 pipe_config->ddi_pll_sel = SKL_DPLL0;
Imre Deak08250c42016-03-14 19:55:34 +02009951 id = DPLL_ID_SKL_DPLL0;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309952 break;
9953 case PORT_B:
9954 pipe_config->ddi_pll_sel = SKL_DPLL1;
Imre Deak08250c42016-03-14 19:55:34 +02009955 id = DPLL_ID_SKL_DPLL1;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309956 break;
9957 case PORT_C:
9958 pipe_config->ddi_pll_sel = SKL_DPLL2;
Imre Deak08250c42016-03-14 19:55:34 +02009959 id = DPLL_ID_SKL_DPLL2;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309960 break;
9961 default:
9962 DRM_ERROR("Incorrect port type\n");
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009963 return;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309964 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009965
9966 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309967}
9968
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009969static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9970 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009971 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009972{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009973 enum intel_dpll_id id;
Ander Conselvan de Oliveiraa3c988e2016-03-08 17:46:27 +02009974 u32 temp;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009975
9976 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9977 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9978
9979 switch (pipe_config->ddi_pll_sel) {
Damien Lespiau3148ade2014-11-21 16:14:56 +00009980 case SKL_DPLL0:
Ander Conselvan de Oliveiraa3c988e2016-03-08 17:46:27 +02009981 id = DPLL_ID_SKL_DPLL0;
9982 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009983 case SKL_DPLL1:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009984 id = DPLL_ID_SKL_DPLL1;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009985 break;
9986 case SKL_DPLL2:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009987 id = DPLL_ID_SKL_DPLL2;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009988 break;
9989 case SKL_DPLL3:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009990 id = DPLL_ID_SKL_DPLL3;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009991 break;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009992 default:
9993 MISSING_CASE(pipe_config->ddi_pll_sel);
9994 return;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009995 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009996
9997 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009998}
9999
Damien Lespiau7d2c8172014-07-29 18:06:18 +010010000static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
10001 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010002 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +010010003{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010004 enum intel_dpll_id id;
10005
Damien Lespiau7d2c8172014-07-29 18:06:18 +010010006 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
10007
10008 switch (pipe_config->ddi_pll_sel) {
10009 case PORT_CLK_SEL_WRPLL1:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010010 id = DPLL_ID_WRPLL1;
Damien Lespiau7d2c8172014-07-29 18:06:18 +010010011 break;
10012 case PORT_CLK_SEL_WRPLL2:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010013 id = DPLL_ID_WRPLL2;
Damien Lespiau7d2c8172014-07-29 18:06:18 +010010014 break;
Maarten Lankhorst00490c22015-11-16 14:42:12 +010010015 case PORT_CLK_SEL_SPLL:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010016 id = DPLL_ID_SPLL;
Ville Syrjälä79bd23d2015-12-01 23:32:07 +020010017 break;
Ander Conselvan de Oliveira9d16da62016-03-08 17:46:26 +020010018 case PORT_CLK_SEL_LCPLL_810:
10019 id = DPLL_ID_LCPLL_810;
10020 break;
10021 case PORT_CLK_SEL_LCPLL_1350:
10022 id = DPLL_ID_LCPLL_1350;
10023 break;
10024 case PORT_CLK_SEL_LCPLL_2700:
10025 id = DPLL_ID_LCPLL_2700;
10026 break;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010027 default:
10028 MISSING_CASE(pipe_config->ddi_pll_sel);
10029 /* fall through */
10030 case PORT_CLK_SEL_NONE:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010031 return;
Damien Lespiau7d2c8172014-07-29 18:06:18 +010010032 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010033
10034 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Damien Lespiau7d2c8172014-07-29 18:06:18 +010010035}
10036
Jani Nikulacf304292016-03-18 17:05:41 +020010037static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
10038 struct intel_crtc_state *pipe_config,
10039 unsigned long *power_domain_mask)
10040{
10041 struct drm_device *dev = crtc->base.dev;
10042 struct drm_i915_private *dev_priv = dev->dev_private;
10043 enum intel_display_power_domain power_domain;
10044 u32 tmp;
10045
Imre Deakd9a7bc62016-05-12 16:18:50 +030010046 /*
10047 * The pipe->transcoder mapping is fixed with the exception of the eDP
10048 * transcoder handled below.
10049 */
Jani Nikulacf304292016-03-18 17:05:41 +020010050 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
10051
10052 /*
10053 * XXX: Do intel_display_power_get_if_enabled before reading this (for
10054 * consistency and less surprising code; it's in always on power).
10055 */
10056 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
10057 if (tmp & TRANS_DDI_FUNC_ENABLE) {
10058 enum pipe trans_edp_pipe;
10059 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
10060 default:
10061 WARN(1, "unknown pipe linked to edp transcoder\n");
10062 case TRANS_DDI_EDP_INPUT_A_ONOFF:
10063 case TRANS_DDI_EDP_INPUT_A_ON:
10064 trans_edp_pipe = PIPE_A;
10065 break;
10066 case TRANS_DDI_EDP_INPUT_B_ONOFF:
10067 trans_edp_pipe = PIPE_B;
10068 break;
10069 case TRANS_DDI_EDP_INPUT_C_ONOFF:
10070 trans_edp_pipe = PIPE_C;
10071 break;
10072 }
10073
10074 if (trans_edp_pipe == crtc->pipe)
10075 pipe_config->cpu_transcoder = TRANSCODER_EDP;
10076 }
10077
10078 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
10079 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
10080 return false;
10081 *power_domain_mask |= BIT(power_domain);
10082
10083 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
10084
10085 return tmp & PIPECONF_ENABLE;
10086}
10087
Jani Nikula4d1de972016-03-18 17:05:42 +020010088static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
10089 struct intel_crtc_state *pipe_config,
10090 unsigned long *power_domain_mask)
10091{
10092 struct drm_device *dev = crtc->base.dev;
10093 struct drm_i915_private *dev_priv = dev->dev_private;
10094 enum intel_display_power_domain power_domain;
10095 enum port port;
10096 enum transcoder cpu_transcoder;
10097 u32 tmp;
10098
10099 pipe_config->has_dsi_encoder = false;
10100
10101 for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
10102 if (port == PORT_A)
10103 cpu_transcoder = TRANSCODER_DSI_A;
10104 else
10105 cpu_transcoder = TRANSCODER_DSI_C;
10106
10107 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
10108 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
10109 continue;
10110 *power_domain_mask |= BIT(power_domain);
10111
Imre Deakdb18b6a2016-03-24 12:41:40 +020010112 /*
10113 * The PLL needs to be enabled with a valid divider
10114 * configuration, otherwise accessing DSI registers will hang
10115 * the machine. See BSpec North Display Engine
10116 * registers/MIPI[BXT]. We can break out here early, since we
10117 * need the same DSI PLL to be enabled for both DSI ports.
10118 */
10119 if (!intel_dsi_pll_is_enabled(dev_priv))
10120 break;
10121
Jani Nikula4d1de972016-03-18 17:05:42 +020010122 /* XXX: this works for video mode only */
10123 tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
10124 if (!(tmp & DPI_ENABLE))
10125 continue;
10126
10127 tmp = I915_READ(MIPI_CTRL(port));
10128 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
10129 continue;
10130
10131 pipe_config->cpu_transcoder = cpu_transcoder;
10132 pipe_config->has_dsi_encoder = true;
10133 break;
10134 }
10135
10136 return pipe_config->has_dsi_encoder;
10137}
10138
Daniel Vetter26804af2014-06-25 22:01:55 +030010139static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010140 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +030010141{
10142 struct drm_device *dev = crtc->base.dev;
10143 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd452c5b2014-07-04 11:27:39 -030010144 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +030010145 enum port port;
10146 uint32_t tmp;
10147
10148 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
10149
10150 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
10151
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070010152 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +000010153 skylake_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M3760b592014-08-22 09:49:11 +053010154 else if (IS_BROXTON(dev))
10155 bxt_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +000010156 else
10157 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +030010158
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010159 pll = pipe_config->shared_dpll;
10160 if (pll) {
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020010161 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
10162 &pipe_config->dpll_hw_state));
Daniel Vetterd452c5b2014-07-04 11:27:39 -030010163 }
10164
Daniel Vetter26804af2014-06-25 22:01:55 +030010165 /*
10166 * Haswell has only FDI/PCH transcoder A. It is which is connected to
10167 * DDI E. So just check whether this pipe is wired to DDI E and whether
10168 * the PCH transcoder is on.
10169 */
Damien Lespiauca370452013-12-03 13:56:24 +000010170 if (INTEL_INFO(dev)->gen < 9 &&
10171 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +030010172 pipe_config->has_pch_encoder = true;
10173
10174 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
10175 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
10176 FDI_DP_PORT_WIDTH_SHIFT) + 1;
10177
10178 ironlake_get_fdi_m_n_config(crtc, pipe_config);
10179 }
10180}
10181
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010182static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010183 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010184{
10185 struct drm_device *dev = crtc->base.dev;
10186 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak17290502016-02-12 18:55:11 +020010187 enum intel_display_power_domain power_domain;
10188 unsigned long power_domain_mask;
Jani Nikulacf304292016-03-18 17:05:41 +020010189 bool active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010190
Imre Deak17290502016-02-12 18:55:11 +020010191 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
10192 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +020010193 return false;
Imre Deak17290502016-02-12 18:55:11 +020010194 power_domain_mask = BIT(power_domain);
10195
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010196 pipe_config->shared_dpll = NULL;
Daniel Vetterc0d43d62013-06-07 23:11:08 +020010197
Jani Nikulacf304292016-03-18 17:05:41 +020010198 active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
Daniel Vettereccb1402013-05-22 00:50:22 +020010199
Jani Nikula4d1de972016-03-18 17:05:42 +020010200 if (IS_BROXTON(dev_priv)) {
10201 bxt_get_dsi_transcoder_state(crtc, pipe_config,
10202 &power_domain_mask);
10203 WARN_ON(active && pipe_config->has_dsi_encoder);
10204 if (pipe_config->has_dsi_encoder)
10205 active = true;
10206 }
10207
Jani Nikulacf304292016-03-18 17:05:41 +020010208 if (!active)
Imre Deak17290502016-02-12 18:55:11 +020010209 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010210
Jani Nikula4d1de972016-03-18 17:05:42 +020010211 if (!pipe_config->has_dsi_encoder) {
10212 haswell_get_ddi_port_state(crtc, pipe_config);
10213 intel_get_pipe_timings(crtc, pipe_config);
10214 }
Daniel Vetter627eb5a2013-04-29 19:33:42 +020010215
Jani Nikulabc58be62016-03-18 17:05:39 +020010216 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010217
Lionel Landwerlin05dc6982016-03-16 10:57:15 +000010218 pipe_config->gamma_mode =
10219 I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
10220
Chandra Kondurua1b22782015-04-07 15:28:45 -070010221 if (INTEL_INFO(dev)->gen >= 9) {
10222 skl_init_scalers(dev, crtc, pipe_config);
10223 }
10224
Chandra Konduruaf99ced2015-05-11 14:35:47 -070010225 if (INTEL_INFO(dev)->gen >= 9) {
10226 pipe_config->scaler_state.scaler_id = -1;
10227 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
10228 }
10229
Imre Deak17290502016-02-12 18:55:11 +020010230 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
10231 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
10232 power_domain_mask |= BIT(power_domain);
Rodrigo Vivi1c132b42015-09-02 15:19:26 -070010233 if (INTEL_INFO(dev)->gen >= 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +000010234 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -080010235 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -070010236 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesbd2e2442014-11-13 17:51:47 +000010237 }
Daniel Vetter88adfff2013-03-28 10:42:01 +010010238
Jesse Barnese59150d2014-01-07 13:30:45 -080010239 if (IS_HASWELL(dev))
10240 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
10241 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030010242
Jani Nikula4d1de972016-03-18 17:05:42 +020010243 if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
10244 !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
Clint Taylorebb69c92014-09-30 10:30:22 -070010245 pipe_config->pixel_multiplier =
10246 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
10247 } else {
10248 pipe_config->pixel_multiplier = 1;
10249 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020010250
Imre Deak17290502016-02-12 18:55:11 +020010251out:
10252 for_each_power_domain(power_domain, power_domain_mask)
10253 intel_display_power_put(dev_priv, power_domain);
10254
Jani Nikulacf304292016-03-18 17:05:41 +020010255 return active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010256}
10257
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010258static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
10259 const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +010010260{
10261 struct drm_device *dev = crtc->dev;
10262 struct drm_i915_private *dev_priv = dev->dev_private;
10263 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +030010264 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +010010265
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010266 if (plane_state && plane_state->visible) {
10267 unsigned int width = plane_state->base.crtc_w;
10268 unsigned int height = plane_state->base.crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +030010269 unsigned int stride = roundup_pow_of_two(width) * 4;
10270
10271 switch (stride) {
10272 default:
10273 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
10274 width, stride);
10275 stride = 256;
10276 /* fallthrough */
10277 case 256:
10278 case 512:
10279 case 1024:
10280 case 2048:
10281 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010282 }
10283
Ville Syrjälädc41c152014-08-13 11:57:05 +030010284 cntl |= CURSOR_ENABLE |
10285 CURSOR_GAMMA_ENABLE |
10286 CURSOR_FORMAT_ARGB |
10287 CURSOR_STRIDE(stride);
10288
10289 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010290 }
Chris Wilson560b85b2010-08-07 11:01:38 +010010291
Ville Syrjälädc41c152014-08-13 11:57:05 +030010292 if (intel_crtc->cursor_cntl != 0 &&
10293 (intel_crtc->cursor_base != base ||
10294 intel_crtc->cursor_size != size ||
10295 intel_crtc->cursor_cntl != cntl)) {
10296 /* On these chipsets we can only modify the base/size/stride
10297 * whilst the cursor is disabled.
10298 */
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010299 I915_WRITE(CURCNTR(PIPE_A), 0);
10300 POSTING_READ(CURCNTR(PIPE_A));
Ville Syrjälädc41c152014-08-13 11:57:05 +030010301 intel_crtc->cursor_cntl = 0;
10302 }
10303
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010304 if (intel_crtc->cursor_base != base) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010305 I915_WRITE(CURBASE(PIPE_A), base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010306 intel_crtc->cursor_base = base;
10307 }
Ville Syrjälädc41c152014-08-13 11:57:05 +030010308
10309 if (intel_crtc->cursor_size != size) {
10310 I915_WRITE(CURSIZE, size);
10311 intel_crtc->cursor_size = size;
10312 }
10313
Chris Wilson4b0e3332014-05-30 16:35:26 +030010314 if (intel_crtc->cursor_cntl != cntl) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010315 I915_WRITE(CURCNTR(PIPE_A), cntl);
10316 POSTING_READ(CURCNTR(PIPE_A));
Chris Wilson4b0e3332014-05-30 16:35:26 +030010317 intel_crtc->cursor_cntl = cntl;
10318 }
Chris Wilson560b85b2010-08-07 11:01:38 +010010319}
10320
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010321static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
10322 const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +010010323{
10324 struct drm_device *dev = crtc->dev;
10325 struct drm_i915_private *dev_priv = dev->dev_private;
10326 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10327 int pipe = intel_crtc->pipe;
Ville Syrjälä663f3122015-12-14 13:16:48 +020010328 uint32_t cntl = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +010010329
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010330 if (plane_state && plane_state->visible) {
Chris Wilson4b0e3332014-05-30 16:35:26 +030010331 cntl = MCURSOR_GAMMA_ENABLE;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010332 switch (plane_state->base.crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010333 case 64:
10334 cntl |= CURSOR_MODE_64_ARGB_AX;
10335 break;
10336 case 128:
10337 cntl |= CURSOR_MODE_128_ARGB_AX;
10338 break;
10339 case 256:
10340 cntl |= CURSOR_MODE_256_ARGB_AX;
10341 break;
10342 default:
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010343 MISSING_CASE(plane_state->base.crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010344 return;
Chris Wilson560b85b2010-08-07 11:01:38 +010010345 }
Chris Wilson4b0e3332014-05-30 16:35:26 +030010346 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010347
Bob Paauwefc6f93b2015-08-31 14:03:30 -070010348 if (HAS_DDI(dev))
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010349 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010350
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010351 if (plane_state->base.rotation == BIT(DRM_ROTATE_180))
10352 cntl |= CURSOR_ROTATE_180;
10353 }
Ville Syrjälä4398ad42014-10-23 07:41:34 -070010354
Chris Wilson4b0e3332014-05-30 16:35:26 +030010355 if (intel_crtc->cursor_cntl != cntl) {
10356 I915_WRITE(CURCNTR(pipe), cntl);
10357 POSTING_READ(CURCNTR(pipe));
10358 intel_crtc->cursor_cntl = cntl;
10359 }
10360
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010361 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010362 I915_WRITE(CURBASE(pipe), base);
10363 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010364
10365 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010366}
10367
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010368/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +010010369static void intel_crtc_update_cursor(struct drm_crtc *crtc,
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010370 const struct intel_plane_state *plane_state)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010371{
10372 struct drm_device *dev = crtc->dev;
10373 struct drm_i915_private *dev_priv = dev->dev_private;
10374 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10375 int pipe = intel_crtc->pipe;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010376 u32 base = intel_crtc->cursor_addr;
10377 u32 pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010378
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010379 if (plane_state) {
10380 int x = plane_state->base.crtc_x;
10381 int y = plane_state->base.crtc_y;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010382
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010383 if (x < 0) {
10384 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10385 x = -x;
10386 }
10387 pos |= x << CURSOR_X_SHIFT;
10388
10389 if (y < 0) {
10390 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10391 y = -y;
10392 }
10393 pos |= y << CURSOR_Y_SHIFT;
10394
10395 /* ILK+ do this automagically */
10396 if (HAS_GMCH_DISPLAY(dev) &&
10397 plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
10398 base += (plane_state->base.crtc_h *
10399 plane_state->base.crtc_w - 1) * 4;
10400 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010401 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010402
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010403 I915_WRITE(CURPOS(pipe), pos);
10404
Ville Syrjälä8ac54662014-08-12 19:39:54 +030010405 if (IS_845G(dev) || IS_I865G(dev))
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010406 i845_update_cursor(crtc, base, plane_state);
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010407 else
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010408 i9xx_update_cursor(crtc, base, plane_state);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010409}
10410
Ville Syrjälädc41c152014-08-13 11:57:05 +030010411static bool cursor_size_ok(struct drm_device *dev,
10412 uint32_t width, uint32_t height)
10413{
10414 if (width == 0 || height == 0)
10415 return false;
10416
10417 /*
10418 * 845g/865g are special in that they are only limited by
10419 * the width of their cursors, the height is arbitrary up to
10420 * the precision of the register. Everything else requires
10421 * square cursors, limited to a few power-of-two sizes.
10422 */
10423 if (IS_845G(dev) || IS_I865G(dev)) {
10424 if ((width & 63) != 0)
10425 return false;
10426
10427 if (width > (IS_845G(dev) ? 64 : 512))
10428 return false;
10429
10430 if (height > 1023)
10431 return false;
10432 } else {
10433 switch (width | height) {
10434 case 256:
10435 case 128:
10436 if (IS_GEN2(dev))
10437 return false;
10438 case 64:
10439 break;
10440 default:
10441 return false;
10442 }
10443 }
10444
10445 return true;
10446}
10447
Jesse Barnes79e53942008-11-07 14:24:08 -080010448/* VESA 640x480x72Hz mode to set on the pipe */
10449static struct drm_display_mode load_detect_mode = {
10450 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10451 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10452};
10453
Daniel Vettera8bb6812014-02-10 18:00:39 +010010454struct drm_framebuffer *
10455__intel_framebuffer_create(struct drm_device *dev,
10456 struct drm_mode_fb_cmd2 *mode_cmd,
10457 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +010010458{
10459 struct intel_framebuffer *intel_fb;
10460 int ret;
10461
10462 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010463 if (!intel_fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010464 return ERR_PTR(-ENOMEM);
Chris Wilsond2dff872011-04-19 08:36:26 +010010465
10466 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010467 if (ret)
10468 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +010010469
10470 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010471
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010472err:
10473 kfree(intel_fb);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010474 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +010010475}
10476
Daniel Vetterb5ea6422014-03-02 21:18:00 +010010477static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +010010478intel_framebuffer_create(struct drm_device *dev,
10479 struct drm_mode_fb_cmd2 *mode_cmd,
10480 struct drm_i915_gem_object *obj)
10481{
10482 struct drm_framebuffer *fb;
10483 int ret;
10484
10485 ret = i915_mutex_lock_interruptible(dev);
10486 if (ret)
10487 return ERR_PTR(ret);
10488 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10489 mutex_unlock(&dev->struct_mutex);
10490
10491 return fb;
10492}
10493
Chris Wilsond2dff872011-04-19 08:36:26 +010010494static u32
10495intel_framebuffer_pitch_for_width(int width, int bpp)
10496{
10497 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10498 return ALIGN(pitch, 64);
10499}
10500
10501static u32
10502intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10503{
10504 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +020010505 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +010010506}
10507
10508static struct drm_framebuffer *
10509intel_framebuffer_create_for_mode(struct drm_device *dev,
10510 struct drm_display_mode *mode,
10511 int depth, int bpp)
10512{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010513 struct drm_framebuffer *fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010514 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +000010515 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +010010516
Dave Gordond37cd8a2016-04-22 19:14:32 +010010517 obj = i915_gem_object_create(dev,
Chris Wilsond2dff872011-04-19 08:36:26 +010010518 intel_framebuffer_size_for_mode(mode, bpp));
Chris Wilsonfe3db792016-04-25 13:32:13 +010010519 if (IS_ERR(obj))
10520 return ERR_CAST(obj);
Chris Wilsond2dff872011-04-19 08:36:26 +010010521
10522 mode_cmd.width = mode->hdisplay;
10523 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010524 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10525 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +000010526 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +010010527
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010528 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
10529 if (IS_ERR(fb))
10530 drm_gem_object_unreference_unlocked(&obj->base);
10531
10532 return fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010533}
10534
10535static struct drm_framebuffer *
10536mode_fits_in_fbdev(struct drm_device *dev,
10537 struct drm_display_mode *mode)
10538{
Daniel Vetter06957262015-08-10 13:34:08 +020010539#ifdef CONFIG_DRM_FBDEV_EMULATION
Chris Wilsond2dff872011-04-19 08:36:26 +010010540 struct drm_i915_private *dev_priv = dev->dev_private;
10541 struct drm_i915_gem_object *obj;
10542 struct drm_framebuffer *fb;
10543
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010544 if (!dev_priv->fbdev)
10545 return NULL;
10546
10547 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010548 return NULL;
10549
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010550 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010551 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +010010552
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010553 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010554 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10555 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +010010556 return NULL;
10557
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010558 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +010010559 return NULL;
10560
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010561 drm_framebuffer_reference(fb);
Chris Wilsond2dff872011-04-19 08:36:26 +010010562 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +020010563#else
10564 return NULL;
10565#endif
Chris Wilsond2dff872011-04-19 08:36:26 +010010566}
10567
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010568static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10569 struct drm_crtc *crtc,
10570 struct drm_display_mode *mode,
10571 struct drm_framebuffer *fb,
10572 int x, int y)
10573{
10574 struct drm_plane_state *plane_state;
10575 int hdisplay, vdisplay;
10576 int ret;
10577
10578 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10579 if (IS_ERR(plane_state))
10580 return PTR_ERR(plane_state);
10581
10582 if (mode)
10583 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10584 else
10585 hdisplay = vdisplay = 0;
10586
10587 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10588 if (ret)
10589 return ret;
10590 drm_atomic_set_fb_for_plane(plane_state, fb);
10591 plane_state->crtc_x = 0;
10592 plane_state->crtc_y = 0;
10593 plane_state->crtc_w = hdisplay;
10594 plane_state->crtc_h = vdisplay;
10595 plane_state->src_x = x << 16;
10596 plane_state->src_y = y << 16;
10597 plane_state->src_w = hdisplay << 16;
10598 plane_state->src_h = vdisplay << 16;
10599
10600 return 0;
10601}
10602
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010603bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +010010604 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -050010605 struct intel_load_detect_pipe *old,
10606 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010607{
10608 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010609 struct intel_encoder *intel_encoder =
10610 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -080010611 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +010010612 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -080010613 struct drm_crtc *crtc = NULL;
10614 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +020010615 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -050010616 struct drm_mode_config *config = &dev->mode_config;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010617 struct drm_atomic_state *state = NULL, *restore_state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010618 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010619 struct intel_crtc_state *crtc_state;
Rob Clark51fd3712013-11-19 12:10:12 -050010620 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010621
Chris Wilsond2dff872011-04-19 08:36:26 +010010622 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010623 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010624 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010625
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010626 old->restore_state = NULL;
10627
Rob Clark51fd3712013-11-19 12:10:12 -050010628retry:
10629 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10630 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010631 goto fail;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020010632
Jesse Barnes79e53942008-11-07 14:24:08 -080010633 /*
10634 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +010010635 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010636 * - if the connector already has an assigned crtc, use it (but make
10637 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +010010638 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010639 * - try to find the first unused crtc that can drive this connector,
10640 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -080010641 */
10642
10643 /* See if we already have a CRTC for this connector */
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010644 if (connector->state->crtc) {
10645 crtc = connector->state->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +010010646
Rob Clark51fd3712013-11-19 12:10:12 -050010647 ret = drm_modeset_lock(&crtc->mutex, ctx);
10648 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010649 goto fail;
Chris Wilson8261b192011-04-19 23:18:09 +010010650
10651 /* Make sure the crtc and connector are running */
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010652 goto found;
Jesse Barnes79e53942008-11-07 14:24:08 -080010653 }
10654
10655 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010656 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010657 i++;
10658 if (!(encoder->possible_crtcs & (1 << i)))
10659 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010660
10661 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
10662 if (ret)
10663 goto fail;
10664
10665 if (possible_crtc->state->enable) {
10666 drm_modeset_unlock(&possible_crtc->mutex);
Ville Syrjäläa4592492014-08-11 13:15:36 +030010667 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010668 }
Ville Syrjäläa4592492014-08-11 13:15:36 +030010669
10670 crtc = possible_crtc;
10671 break;
Jesse Barnes79e53942008-11-07 14:24:08 -080010672 }
10673
10674 /*
10675 * If we didn't find an unused CRTC, don't use any.
10676 */
10677 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +010010678 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010679 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010680 }
10681
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010682found:
10683 intel_crtc = to_intel_crtc(crtc);
10684
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010685 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10686 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010687 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010688
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010689 state = drm_atomic_state_alloc(dev);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010690 restore_state = drm_atomic_state_alloc(dev);
10691 if (!state || !restore_state) {
10692 ret = -ENOMEM;
10693 goto fail;
10694 }
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010695
10696 state->acquire_ctx = ctx;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010697 restore_state->acquire_ctx = ctx;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010698
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010699 connector_state = drm_atomic_get_connector_state(state, connector);
10700 if (IS_ERR(connector_state)) {
10701 ret = PTR_ERR(connector_state);
10702 goto fail;
10703 }
10704
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010705 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
10706 if (ret)
10707 goto fail;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010708
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010709 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10710 if (IS_ERR(crtc_state)) {
10711 ret = PTR_ERR(crtc_state);
10712 goto fail;
10713 }
10714
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010715 crtc_state->base.active = crtc_state->base.enable = true;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010716
Chris Wilson64927112011-04-20 07:25:26 +010010717 if (!mode)
10718 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -080010719
Chris Wilsond2dff872011-04-19 08:36:26 +010010720 /* We need a framebuffer large enough to accommodate all accesses
10721 * that the plane may generate whilst we perform load detection.
10722 * We can not rely on the fbcon either being present (we get called
10723 * during its initialisation to detect all boot displays, or it may
10724 * not even exist) or that it is large enough to satisfy the
10725 * requested mode.
10726 */
Daniel Vetter94352cf2012-07-05 22:51:56 +020010727 fb = mode_fits_in_fbdev(dev, mode);
10728 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010729 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010730 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
Chris Wilsond2dff872011-04-19 08:36:26 +010010731 } else
10732 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010733 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010734 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010735 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010736 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010737
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010738 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10739 if (ret)
10740 goto fail;
10741
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010742 drm_framebuffer_unreference(fb);
10743
10744 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
10745 if (ret)
10746 goto fail;
10747
10748 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
10749 if (!ret)
10750 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
10751 if (!ret)
10752 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
10753 if (ret) {
10754 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
10755 goto fail;
10756 }
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030010757
Maarten Lankhorst3ba86072016-02-29 09:18:57 +010010758 ret = drm_atomic_commit(state);
10759 if (ret) {
Chris Wilson64927112011-04-20 07:25:26 +010010760 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010761 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010762 }
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010763
10764 old->restore_state = restore_state;
Chris Wilson71731882011-04-19 23:10:58 +010010765
Jesse Barnes79e53942008-11-07 14:24:08 -080010766 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010767 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +010010768 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010769
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010770fail:
Ander Conselvan de Oliveirae5d958e2015-04-21 17:12:57 +030010771 drm_atomic_state_free(state);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010772 drm_atomic_state_free(restore_state);
10773 restore_state = state = NULL;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010774
Rob Clark51fd3712013-11-19 12:10:12 -050010775 if (ret == -EDEADLK) {
10776 drm_modeset_backoff(ctx);
10777 goto retry;
10778 }
10779
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010780 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -080010781}
10782
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010783void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020010784 struct intel_load_detect_pipe *old,
10785 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010786{
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010787 struct intel_encoder *intel_encoder =
10788 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +010010789 struct drm_encoder *encoder = &intel_encoder->base;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010790 struct drm_atomic_state *state = old->restore_state;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010791 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080010792
Chris Wilsond2dff872011-04-19 08:36:26 +010010793 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010794 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010795 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010796
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010797 if (!state)
Chris Wilson0622a532011-04-21 09:32:11 +010010798 return;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010799
10800 ret = drm_atomic_commit(state);
10801 if (ret) {
10802 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
10803 drm_atomic_state_free(state);
Jesse Barnes79e53942008-11-07 14:24:08 -080010804 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010805}
10806
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010807static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010808 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010809{
10810 struct drm_i915_private *dev_priv = dev->dev_private;
10811 u32 dpll = pipe_config->dpll_hw_state.dpll;
10812
10813 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +020010814 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010815 else if (HAS_PCH_SPLIT(dev))
10816 return 120000;
10817 else if (!IS_GEN2(dev))
10818 return 96000;
10819 else
10820 return 48000;
10821}
10822
Jesse Barnes79e53942008-11-07 14:24:08 -080010823/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010824static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010825 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -080010826{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010827 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080010828 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010829 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010830 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -080010831 u32 fp;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +030010832 struct dpll clock;
Imre Deakdccbea32015-06-22 23:35:51 +030010833 int port_clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010834 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -080010835
10836 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +030010837 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010838 else
Ville Syrjälä293623f2013-09-13 16:18:46 +030010839 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010840
10841 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010842 if (IS_PINEVIEW(dev)) {
10843 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10844 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +080010845 } else {
10846 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10847 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10848 }
10849
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010850 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010851 if (IS_PINEVIEW(dev))
10852 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10853 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +080010854 else
10855 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -080010856 DPLL_FPA01_P1_POST_DIV_SHIFT);
10857
10858 switch (dpll & DPLL_MODE_MASK) {
10859 case DPLLB_MODE_DAC_SERIAL:
10860 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10861 5 : 10;
10862 break;
10863 case DPLLB_MODE_LVDS:
10864 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10865 7 : 14;
10866 break;
10867 default:
Zhao Yakui28c97732009-10-09 11:39:41 +080010868 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -080010869 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010870 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010871 }
10872
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010873 if (IS_PINEVIEW(dev))
Imre Deakdccbea32015-06-22 23:35:51 +030010874 port_clock = pnv_calc_dpll_params(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010875 else
Imre Deakdccbea32015-06-22 23:35:51 +030010876 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010877 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +020010878 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010879 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -080010880
10881 if (is_lvds) {
10882 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10883 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010884
10885 if (lvds & LVDS_CLKB_POWER_UP)
10886 clock.p2 = 7;
10887 else
10888 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -080010889 } else {
10890 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10891 clock.p1 = 2;
10892 else {
10893 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10894 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10895 }
10896 if (dpll & PLL_P2_DIVIDE_BY_4)
10897 clock.p2 = 4;
10898 else
10899 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -080010900 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010901
Imre Deakdccbea32015-06-22 23:35:51 +030010902 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010903 }
10904
Ville Syrjälä18442d02013-09-13 16:00:08 +030010905 /*
10906 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +010010907 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +030010908 * encoder's get_config() function.
10909 */
Imre Deakdccbea32015-06-22 23:35:51 +030010910 pipe_config->port_clock = port_clock;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010911}
10912
Ville Syrjälä6878da02013-09-13 15:59:11 +030010913int intel_dotclock_calculate(int link_freq,
10914 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010915{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010916 /*
10917 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010918 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010919 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010920 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010921 *
10922 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010923 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -080010924 */
10925
Ville Syrjälä6878da02013-09-13 15:59:11 +030010926 if (!m_n->link_n)
10927 return 0;
10928
10929 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10930}
10931
Ville Syrjälä18442d02013-09-13 16:00:08 +030010932static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010933 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +030010934{
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020010935 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä18442d02013-09-13 16:00:08 +030010936
10937 /* read out port_clock from the DPLL */
10938 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +030010939
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010940 /*
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020010941 * In case there is an active pipe without active ports,
10942 * we may need some idea for the dotclock anyway.
10943 * Calculate one based on the FDI configuration.
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010944 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010945 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä21a727b2016-02-17 21:41:10 +020010946 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjälä18442d02013-09-13 16:00:08 +030010947 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -080010948}
10949
10950/** Returns the currently programmed mode of the given pipe. */
10951struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10952 struct drm_crtc *crtc)
10953{
Jesse Barnes548f2452011-02-17 10:40:53 -080010954 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080010955 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010956 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010957 struct drm_display_mode *mode;
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010958 struct intel_crtc_state *pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -020010959 int htot = I915_READ(HTOTAL(cpu_transcoder));
10960 int hsync = I915_READ(HSYNC(cpu_transcoder));
10961 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10962 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +030010963 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -080010964
10965 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10966 if (!mode)
10967 return NULL;
10968
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010969 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
10970 if (!pipe_config) {
10971 kfree(mode);
10972 return NULL;
10973 }
10974
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010975 /*
10976 * Construct a pipe_config sufficient for getting the clock info
10977 * back out of crtc_clock_get.
10978 *
10979 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10980 * to use a real value here instead.
10981 */
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010982 pipe_config->cpu_transcoder = (enum transcoder) pipe;
10983 pipe_config->pixel_multiplier = 1;
10984 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10985 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10986 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
10987 i9xx_crtc_clock_get(intel_crtc, pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010988
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010989 mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -080010990 mode->hdisplay = (htot & 0xffff) + 1;
10991 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10992 mode->hsync_start = (hsync & 0xffff) + 1;
10993 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10994 mode->vdisplay = (vtot & 0xffff) + 1;
10995 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10996 mode->vsync_start = (vsync & 0xffff) + 1;
10997 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10998
10999 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -080011000
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000011001 kfree(pipe_config);
11002
Jesse Barnes79e53942008-11-07 14:24:08 -080011003 return mode;
11004}
11005
Tvrtko Ursulin7d993732016-04-28 12:57:00 +010011006void intel_mark_busy(struct drm_i915_private *dev_priv)
Jesse Barnes652c3932009-08-17 13:31:43 -070011007{
Chris Wilsonf62a0072014-02-21 17:55:39 +000011008 if (dev_priv->mm.busy)
11009 return;
11010
Paulo Zanoni43694d62014-03-07 20:08:08 -030011011 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -030011012 i915_update_gfx_val(dev_priv);
Tvrtko Ursulin7d993732016-04-28 12:57:00 +010011013 if (INTEL_GEN(dev_priv) >= 6)
Chris Wilson43cf3bf2015-03-18 09:48:22 +000011014 gen6_rps_busy(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +000011015 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +010011016}
11017
Tvrtko Ursulin7d993732016-04-28 12:57:00 +010011018void intel_mark_idle(struct drm_i915_private *dev_priv)
Chris Wilsonf047e392012-07-21 12:31:41 +010011019{
Chris Wilsonf62a0072014-02-21 17:55:39 +000011020 if (!dev_priv->mm.busy)
11021 return;
11022
11023 dev_priv->mm.busy = false;
11024
Tvrtko Ursulin7d993732016-04-28 12:57:00 +010011025 if (INTEL_GEN(dev_priv) >= 6)
11026 gen6_rps_idle(dev_priv);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -030011027
Paulo Zanoni43694d62014-03-07 20:08:08 -030011028 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +010011029}
11030
Jesse Barnes79e53942008-11-07 14:24:08 -080011031static void intel_crtc_destroy(struct drm_crtc *crtc)
11032{
11033 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020011034 struct drm_device *dev = crtc->dev;
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011035 struct intel_flip_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +020011036
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011037 spin_lock_irq(&dev->event_lock);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011038 work = intel_crtc->flip_work;
11039 intel_crtc->flip_work = NULL;
11040 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020011041
Daniel Vetter5a21b662016-05-24 17:13:53 +020011042 if (work) {
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011043 cancel_work_sync(&work->mmio_work);
11044 cancel_work_sync(&work->unpin_work);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011045 kfree(work);
Daniel Vetter67e77c52010-08-20 22:26:30 +020011046 }
Jesse Barnes79e53942008-11-07 14:24:08 -080011047
11048 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020011049
Jesse Barnes79e53942008-11-07 14:24:08 -080011050 kfree(intel_crtc);
11051}
11052
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011053static void intel_unpin_work_fn(struct work_struct *__work)
11054{
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011055 struct intel_flip_work *work =
11056 container_of(__work, struct intel_flip_work, unpin_work);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011057 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
11058 struct drm_device *dev = crtc->base.dev;
11059 struct drm_plane *primary = crtc->base.primary;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011060
Daniel Vetter5a21b662016-05-24 17:13:53 +020011061 if (is_mmio_work(work))
11062 flush_work(&work->mmio_work);
11063
11064 mutex_lock(&dev->struct_mutex);
11065 intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
11066 drm_gem_object_unreference(&work->pending_flip_obj->base);
11067
11068 if (work->flip_queued_req)
11069 i915_gem_request_assign(&work->flip_queued_req, NULL);
11070 mutex_unlock(&dev->struct_mutex);
11071
11072 intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
11073 intel_fbc_post_update(crtc);
11074 drm_framebuffer_unreference(work->old_fb);
11075
11076 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
11077 atomic_dec(&crtc->unpin_work_count);
11078
11079 kfree(work);
11080}
11081
11082/* Is 'a' after or equal to 'b'? */
11083static bool g4x_flip_count_after_eq(u32 a, u32 b)
11084{
11085 return !((a - b) & 0x80000000);
11086}
11087
11088static bool __pageflip_finished_cs(struct intel_crtc *crtc,
11089 struct intel_flip_work *work)
11090{
11091 struct drm_device *dev = crtc->base.dev;
11092 struct drm_i915_private *dev_priv = dev->dev_private;
11093 unsigned reset_counter;
11094
11095 reset_counter = i915_reset_counter(&dev_priv->gpu_error);
11096 if (crtc->reset_counter != reset_counter)
11097 return true;
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011098
Maarten Lankhorst143f73b2016-05-17 15:07:54 +020011099 /*
Daniel Vetter5a21b662016-05-24 17:13:53 +020011100 * The relevant registers doen't exist on pre-ctg.
11101 * As the flip done interrupt doesn't trigger for mmio
11102 * flips on gmch platforms, a flip count check isn't
11103 * really needed there. But since ctg has the registers,
11104 * include it in the check anyway.
Maarten Lankhorst143f73b2016-05-17 15:07:54 +020011105 */
Daniel Vetter5a21b662016-05-24 17:13:53 +020011106 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
11107 return true;
Maarten Lankhorst143f73b2016-05-17 15:07:54 +020011108
Daniel Vetter5a21b662016-05-24 17:13:53 +020011109 /*
11110 * BDW signals flip done immediately if the plane
11111 * is disabled, even if the plane enable is already
11112 * armed to occur at the next vblank :(
11113 */
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020011114
Daniel Vetter5a21b662016-05-24 17:13:53 +020011115 /*
11116 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
11117 * used the same base address. In that case the mmio flip might
11118 * have completed, but the CS hasn't even executed the flip yet.
11119 *
11120 * A flip count check isn't enough as the CS might have updated
11121 * the base address just after start of vblank, but before we
11122 * managed to process the interrupt. This means we'd complete the
11123 * CS flip too soon.
11124 *
11125 * Combining both checks should get us a good enough result. It may
11126 * still happen that the CS flip has been executed, but has not
11127 * yet actually completed. But in case the base address is the same
11128 * anyway, we don't really care.
11129 */
11130 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
11131 crtc->flip_work->gtt_offset &&
11132 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
11133 crtc->flip_work->flip_count);
11134}
Maarten Lankhorst143f73b2016-05-17 15:07:54 +020011135
Daniel Vetter5a21b662016-05-24 17:13:53 +020011136static bool
11137__pageflip_finished_mmio(struct intel_crtc *crtc,
11138 struct intel_flip_work *work)
11139{
11140 /*
11141 * MMIO work completes when vblank is different from
11142 * flip_queued_vblank.
11143 *
11144 * Reset counter value doesn't matter, this is handled by
11145 * i915_wait_request finishing early, so no need to handle
11146 * reset here.
11147 */
11148 return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011149}
11150
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011151
11152static bool pageflip_finished(struct intel_crtc *crtc,
11153 struct intel_flip_work *work)
11154{
11155 if (!atomic_read(&work->pending))
11156 return false;
11157
11158 smp_rmb();
11159
Daniel Vetter5a21b662016-05-24 17:13:53 +020011160 if (is_mmio_work(work))
11161 return __pageflip_finished_mmio(crtc, work);
11162 else
11163 return __pageflip_finished_cs(crtc, work);
11164}
11165
11166void intel_finish_page_flip_cs(struct drm_i915_private *dev_priv, int pipe)
11167{
11168 struct drm_device *dev = dev_priv->dev;
11169 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11170 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11171 struct intel_flip_work *work;
11172 unsigned long flags;
11173
11174 /* Ignore early vblank irqs */
11175 if (!crtc)
11176 return;
11177
Daniel Vetterf3260382014-09-15 14:55:23 +020011178 /*
Daniel Vetter5a21b662016-05-24 17:13:53 +020011179 * This is called both by irq handlers and the reset code (to complete
11180 * lost pageflips) so needs the full irqsave spinlocks.
Chris Wilsone7d841c2012-12-03 11:36:30 +000011181 */
Daniel Vetter5a21b662016-05-24 17:13:53 +020011182 spin_lock_irqsave(&dev->event_lock, flags);
11183 work = intel_crtc->flip_work;
11184
11185 if (work != NULL &&
11186 !is_mmio_work(work) &&
11187 pageflip_finished(intel_crtc, work))
11188 page_flip_completed(intel_crtc);
11189
11190 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011191}
11192
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011193void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
Chris Wilsone7d841c2012-12-03 11:36:30 +000011194{
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011195 struct drm_device *dev = dev_priv->dev;
11196 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11197 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11198 struct intel_flip_work *work;
11199 unsigned long flags;
11200
11201 /* Ignore early vblank irqs */
11202 if (!crtc)
11203 return;
11204
11205 /*
11206 * This is called both by irq handlers and the reset code (to complete
11207 * lost pageflips) so needs the full irqsave spinlocks.
11208 */
11209 spin_lock_irqsave(&dev->event_lock, flags);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011210 work = intel_crtc->flip_work;
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011211
Daniel Vetter5a21b662016-05-24 17:13:53 +020011212 if (work != NULL &&
11213 is_mmio_work(work) &&
11214 pageflip_finished(intel_crtc, work))
11215 page_flip_completed(intel_crtc);
Maarten Lankhorst68858432016-05-17 15:07:52 +020011216
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011217 spin_unlock_irqrestore(&dev->event_lock, flags);
11218}
11219
Daniel Vetter5a21b662016-05-24 17:13:53 +020011220static inline void intel_mark_page_flip_active(struct intel_crtc *crtc,
11221 struct intel_flip_work *work)
11222{
11223 work->flip_queued_vblank = intel_crtc_get_vblank_counter(crtc);
11224
11225 /* Ensure that the work item is consistent when activating it ... */
11226 smp_mb__before_atomic();
11227 atomic_set(&work->pending, 1);
11228}
11229
11230static int intel_gen2_queue_flip(struct drm_device *dev,
11231 struct drm_crtc *crtc,
11232 struct drm_framebuffer *fb,
11233 struct drm_i915_gem_object *obj,
11234 struct drm_i915_gem_request *req,
11235 uint32_t flags)
11236{
11237 struct intel_engine_cs *engine = req->engine;
11238 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11239 u32 flip_mask;
11240 int ret;
11241
11242 ret = intel_ring_begin(req, 6);
11243 if (ret)
11244 return ret;
11245
11246 /* Can't queue multiple flips, so wait for the previous
11247 * one to finish before executing the next.
11248 */
11249 if (intel_crtc->plane)
11250 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11251 else
11252 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
11253 intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
11254 intel_ring_emit(engine, MI_NOOP);
11255 intel_ring_emit(engine, MI_DISPLAY_FLIP |
11256 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11257 intel_ring_emit(engine, fb->pitches[0]);
11258 intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
11259 intel_ring_emit(engine, 0); /* aux display base address, unused */
11260
11261 return 0;
11262}
11263
11264static int intel_gen3_queue_flip(struct drm_device *dev,
11265 struct drm_crtc *crtc,
11266 struct drm_framebuffer *fb,
11267 struct drm_i915_gem_object *obj,
11268 struct drm_i915_gem_request *req,
11269 uint32_t flags)
11270{
11271 struct intel_engine_cs *engine = req->engine;
11272 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11273 u32 flip_mask;
11274 int ret;
11275
11276 ret = intel_ring_begin(req, 6);
11277 if (ret)
11278 return ret;
11279
11280 if (intel_crtc->plane)
11281 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11282 else
11283 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
11284 intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
11285 intel_ring_emit(engine, MI_NOOP);
11286 intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 |
11287 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11288 intel_ring_emit(engine, fb->pitches[0]);
11289 intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
11290 intel_ring_emit(engine, MI_NOOP);
11291
11292 return 0;
11293}
11294
11295static int intel_gen4_queue_flip(struct drm_device *dev,
11296 struct drm_crtc *crtc,
11297 struct drm_framebuffer *fb,
11298 struct drm_i915_gem_object *obj,
11299 struct drm_i915_gem_request *req,
11300 uint32_t flags)
11301{
11302 struct intel_engine_cs *engine = req->engine;
11303 struct drm_i915_private *dev_priv = dev->dev_private;
11304 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11305 uint32_t pf, pipesrc;
11306 int ret;
11307
11308 ret = intel_ring_begin(req, 4);
11309 if (ret)
11310 return ret;
11311
11312 /* i965+ uses the linear or tiled offsets from the
11313 * Display Registers (which do not change across a page-flip)
11314 * so we need only reprogram the base address.
11315 */
11316 intel_ring_emit(engine, MI_DISPLAY_FLIP |
11317 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11318 intel_ring_emit(engine, fb->pitches[0]);
11319 intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset |
11320 obj->tiling_mode);
11321
11322 /* XXX Enabling the panel-fitter across page-flip is so far
11323 * untested on non-native modes, so ignore it for now.
11324 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
11325 */
11326 pf = 0;
11327 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
11328 intel_ring_emit(engine, pf | pipesrc);
11329
11330 return 0;
11331}
11332
11333static int intel_gen6_queue_flip(struct drm_device *dev,
11334 struct drm_crtc *crtc,
11335 struct drm_framebuffer *fb,
11336 struct drm_i915_gem_object *obj,
11337 struct drm_i915_gem_request *req,
11338 uint32_t flags)
11339{
11340 struct intel_engine_cs *engine = req->engine;
11341 struct drm_i915_private *dev_priv = dev->dev_private;
11342 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11343 uint32_t pf, pipesrc;
11344 int ret;
11345
11346 ret = intel_ring_begin(req, 4);
11347 if (ret)
11348 return ret;
11349
11350 intel_ring_emit(engine, MI_DISPLAY_FLIP |
11351 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11352 intel_ring_emit(engine, fb->pitches[0] | obj->tiling_mode);
11353 intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
11354
11355 /* Contrary to the suggestions in the documentation,
11356 * "Enable Panel Fitter" does not seem to be required when page
11357 * flipping with a non-native mode, and worse causes a normal
11358 * modeset to fail.
11359 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11360 */
11361 pf = 0;
11362 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
11363 intel_ring_emit(engine, pf | pipesrc);
11364
11365 return 0;
11366}
11367
11368static int intel_gen7_queue_flip(struct drm_device *dev,
11369 struct drm_crtc *crtc,
11370 struct drm_framebuffer *fb,
11371 struct drm_i915_gem_object *obj,
11372 struct drm_i915_gem_request *req,
11373 uint32_t flags)
11374{
11375 struct intel_engine_cs *engine = req->engine;
11376 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11377 uint32_t plane_bit = 0;
11378 int len, ret;
11379
11380 switch (intel_crtc->plane) {
11381 case PLANE_A:
11382 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11383 break;
11384 case PLANE_B:
11385 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11386 break;
11387 case PLANE_C:
11388 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11389 break;
11390 default:
11391 WARN_ONCE(1, "unknown plane in flip command\n");
11392 return -ENODEV;
11393 }
11394
11395 len = 4;
11396 if (engine->id == RCS) {
11397 len += 6;
11398 /*
11399 * On Gen 8, SRM is now taking an extra dword to accommodate
11400 * 48bits addresses, and we need a NOOP for the batch size to
11401 * stay even.
11402 */
11403 if (IS_GEN8(dev))
11404 len += 2;
11405 }
11406
11407 /*
11408 * BSpec MI_DISPLAY_FLIP for IVB:
11409 * "The full packet must be contained within the same cache line."
11410 *
11411 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11412 * cacheline, if we ever start emitting more commands before
11413 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11414 * then do the cacheline alignment, and finally emit the
11415 * MI_DISPLAY_FLIP.
11416 */
11417 ret = intel_ring_cacheline_align(req);
11418 if (ret)
11419 return ret;
11420
11421 ret = intel_ring_begin(req, len);
11422 if (ret)
11423 return ret;
11424
11425 /* Unmask the flip-done completion message. Note that the bspec says that
11426 * we should do this for both the BCS and RCS, and that we must not unmask
11427 * more than one flip event at any time (or ensure that one flip message
11428 * can be sent by waiting for flip-done prior to queueing new flips).
11429 * Experimentation says that BCS works despite DERRMR masking all
11430 * flip-done completion events and that unmasking all planes at once
11431 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11432 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11433 */
11434 if (engine->id == RCS) {
11435 intel_ring_emit(engine, MI_LOAD_REGISTER_IMM(1));
11436 intel_ring_emit_reg(engine, DERRMR);
11437 intel_ring_emit(engine, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
11438 DERRMR_PIPEB_PRI_FLIP_DONE |
11439 DERRMR_PIPEC_PRI_FLIP_DONE));
11440 if (IS_GEN8(dev))
11441 intel_ring_emit(engine, MI_STORE_REGISTER_MEM_GEN8 |
11442 MI_SRM_LRM_GLOBAL_GTT);
11443 else
11444 intel_ring_emit(engine, MI_STORE_REGISTER_MEM |
11445 MI_SRM_LRM_GLOBAL_GTT);
11446 intel_ring_emit_reg(engine, DERRMR);
11447 intel_ring_emit(engine, engine->scratch.gtt_offset + 256);
11448 if (IS_GEN8(dev)) {
11449 intel_ring_emit(engine, 0);
11450 intel_ring_emit(engine, MI_NOOP);
11451 }
11452 }
11453
11454 intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 | plane_bit);
11455 intel_ring_emit(engine, (fb->pitches[0] | obj->tiling_mode));
11456 intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
11457 intel_ring_emit(engine, (MI_NOOP));
11458
11459 return 0;
11460}
11461
11462static bool use_mmio_flip(struct intel_engine_cs *engine,
11463 struct drm_i915_gem_object *obj)
11464{
Chris Wilsonc37efb92016-06-17 08:28:47 +010011465 struct reservation_object *resv;
11466
Daniel Vetter5a21b662016-05-24 17:13:53 +020011467 /*
11468 * This is not being used for older platforms, because
11469 * non-availability of flip done interrupt forces us to use
11470 * CS flips. Older platforms derive flip done using some clever
11471 * tricks involving the flip_pending status bits and vblank irqs.
11472 * So using MMIO flips there would disrupt this mechanism.
11473 */
11474
11475 if (engine == NULL)
11476 return true;
11477
11478 if (INTEL_GEN(engine->i915) < 5)
11479 return false;
11480
11481 if (i915.use_mmio_flip < 0)
11482 return false;
11483 else if (i915.use_mmio_flip > 0)
11484 return true;
11485 else if (i915.enable_execlists)
11486 return true;
Chris Wilsonc37efb92016-06-17 08:28:47 +010011487
11488 resv = i915_gem_object_get_dmabuf_resv(obj);
11489 if (resv && !reservation_object_test_signaled_rcu(resv, false))
Daniel Vetter5a21b662016-05-24 17:13:53 +020011490 return true;
Chris Wilsonc37efb92016-06-17 08:28:47 +010011491
11492 return engine != i915_gem_request_get_engine(obj->last_write_req);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011493}
11494
11495static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
11496 unsigned int rotation,
11497 struct intel_flip_work *work)
11498{
11499 struct drm_device *dev = intel_crtc->base.dev;
11500 struct drm_i915_private *dev_priv = dev->dev_private;
11501 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
11502 const enum pipe pipe = intel_crtc->pipe;
11503 u32 ctl, stride, tile_height;
11504
11505 ctl = I915_READ(PLANE_CTL(pipe, 0));
11506 ctl &= ~PLANE_CTL_TILED_MASK;
11507 switch (fb->modifier[0]) {
11508 case DRM_FORMAT_MOD_NONE:
11509 break;
11510 case I915_FORMAT_MOD_X_TILED:
11511 ctl |= PLANE_CTL_TILED_X;
11512 break;
11513 case I915_FORMAT_MOD_Y_TILED:
11514 ctl |= PLANE_CTL_TILED_Y;
11515 break;
11516 case I915_FORMAT_MOD_Yf_TILED:
11517 ctl |= PLANE_CTL_TILED_YF;
11518 break;
11519 default:
11520 MISSING_CASE(fb->modifier[0]);
11521 }
11522
11523 /*
11524 * The stride is either expressed as a multiple of 64 bytes chunks for
11525 * linear buffers or in number of tiles for tiled buffers.
11526 */
11527 if (intel_rotation_90_or_270(rotation)) {
11528 /* stride = Surface height in tiles */
11529 tile_height = intel_tile_height(dev_priv, fb->modifier[0], 0);
11530 stride = DIV_ROUND_UP(fb->height, tile_height);
11531 } else {
11532 stride = fb->pitches[0] /
11533 intel_fb_stride_alignment(dev_priv, fb->modifier[0],
11534 fb->pixel_format);
11535 }
11536
11537 /*
11538 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11539 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11540 */
11541 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11542 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11543
11544 I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
11545 POSTING_READ(PLANE_SURF(pipe, 0));
11546}
11547
11548static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
11549 struct intel_flip_work *work)
11550{
11551 struct drm_device *dev = intel_crtc->base.dev;
11552 struct drm_i915_private *dev_priv = dev->dev_private;
11553 struct intel_framebuffer *intel_fb =
11554 to_intel_framebuffer(intel_crtc->base.primary->fb);
11555 struct drm_i915_gem_object *obj = intel_fb->obj;
11556 i915_reg_t reg = DSPCNTR(intel_crtc->plane);
11557 u32 dspcntr;
11558
11559 dspcntr = I915_READ(reg);
11560
11561 if (obj->tiling_mode != I915_TILING_NONE)
11562 dspcntr |= DISPPLANE_TILED;
11563 else
11564 dspcntr &= ~DISPPLANE_TILED;
11565
11566 I915_WRITE(reg, dspcntr);
11567
11568 I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
11569 POSTING_READ(DSPSURF(intel_crtc->plane));
11570}
11571
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011572static void intel_mmio_flip_work_func(struct work_struct *w)
Damien Lespiauff944562014-11-20 14:58:16 +000011573{
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011574 struct intel_flip_work *work =
11575 container_of(w, struct intel_flip_work, mmio_work);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011576 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
11577 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
11578 struct intel_framebuffer *intel_fb =
11579 to_intel_framebuffer(crtc->base.primary->fb);
11580 struct drm_i915_gem_object *obj = intel_fb->obj;
Chris Wilsonc37efb92016-06-17 08:28:47 +010011581 struct reservation_object *resv;
Daniel Vetter5a21b662016-05-24 17:13:53 +020011582
11583 if (work->flip_queued_req)
11584 WARN_ON(__i915_wait_request(work->flip_queued_req,
11585 false, NULL,
11586 &dev_priv->rps.mmioflips));
11587
11588 /* For framebuffer backed by dmabuf, wait for fence */
Chris Wilsonc37efb92016-06-17 08:28:47 +010011589 resv = i915_gem_object_get_dmabuf_resv(obj);
11590 if (resv)
11591 WARN_ON(reservation_object_wait_timeout_rcu(resv, false, false,
Daniel Vetter5a21b662016-05-24 17:13:53 +020011592 MAX_SCHEDULE_TIMEOUT) < 0);
11593
11594 intel_pipe_update_start(crtc);
11595
11596 if (INTEL_GEN(dev_priv) >= 9)
11597 skl_do_mmio_flip(crtc, work->rotation, work);
11598 else
11599 /* use_mmio_flip() retricts MMIO flips to ilk+ */
11600 ilk_do_mmio_flip(crtc, work);
11601
11602 intel_pipe_update_end(crtc, work);
11603}
11604
11605static int intel_default_queue_flip(struct drm_device *dev,
11606 struct drm_crtc *crtc,
11607 struct drm_framebuffer *fb,
11608 struct drm_i915_gem_object *obj,
11609 struct drm_i915_gem_request *req,
11610 uint32_t flags)
11611{
11612 return -ENODEV;
11613}
11614
11615static bool __pageflip_stall_check_cs(struct drm_i915_private *dev_priv,
11616 struct intel_crtc *intel_crtc,
11617 struct intel_flip_work *work)
11618{
11619 u32 addr, vblank;
11620
11621 if (!atomic_read(&work->pending))
11622 return false;
11623
11624 smp_rmb();
11625
11626 vblank = intel_crtc_get_vblank_counter(intel_crtc);
11627 if (work->flip_ready_vblank == 0) {
11628 if (work->flip_queued_req &&
11629 !i915_gem_request_completed(work->flip_queued_req, true))
11630 return false;
11631
11632 work->flip_ready_vblank = vblank;
11633 }
11634
11635 if (vblank - work->flip_ready_vblank < 3)
11636 return false;
11637
11638 /* Potential stall - if we see that the flip has happened,
11639 * assume a missed interrupt. */
11640 if (INTEL_GEN(dev_priv) >= 4)
11641 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11642 else
11643 addr = I915_READ(DSPADDR(intel_crtc->plane));
11644
11645 /* There is a potential issue here with a false positive after a flip
11646 * to the same address. We could address this by checking for a
11647 * non-incrementing frame counter.
11648 */
11649 return addr == work->gtt_offset;
11650}
11651
11652void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
11653{
11654 struct drm_device *dev = dev_priv->dev;
11655 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Maarten Lankhorst143f73b2016-05-17 15:07:54 +020011656 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011657 struct intel_flip_work *work;
11658
11659 WARN_ON(!in_interrupt());
11660
11661 if (crtc == NULL)
11662 return;
11663
11664 spin_lock(&dev->event_lock);
11665 work = intel_crtc->flip_work;
11666
11667 if (work != NULL && !is_mmio_work(work) &&
11668 __pageflip_stall_check_cs(dev_priv, intel_crtc, work)) {
11669 WARN_ONCE(1,
11670 "Kicking stuck page flip: queued at %d, now %d\n",
11671 work->flip_queued_vblank, intel_crtc_get_vblank_counter(intel_crtc));
11672 page_flip_completed(intel_crtc);
11673 work = NULL;
11674 }
11675
11676 if (work != NULL && !is_mmio_work(work) &&
11677 intel_crtc_get_vblank_counter(intel_crtc) - work->flip_queued_vblank > 1)
11678 intel_queue_rps_boost_for_request(work->flip_queued_req);
11679 spin_unlock(&dev->event_lock);
11680}
11681
11682static int intel_crtc_page_flip(struct drm_crtc *crtc,
11683 struct drm_framebuffer *fb,
11684 struct drm_pending_vblank_event *event,
11685 uint32_t page_flip_flags)
11686{
Maarten Lankhorst143f73b2016-05-17 15:07:54 +020011687 struct drm_device *dev = crtc->dev;
Maarten Lankhorstaa420dd2016-05-17 15:07:51 +020011688 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter5a21b662016-05-24 17:13:53 +020011689 struct drm_framebuffer *old_fb = crtc->primary->fb;
11690 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
11691 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11692 struct drm_plane *primary = crtc->primary;
11693 enum pipe pipe = intel_crtc->pipe;
11694 struct intel_flip_work *work;
11695 struct intel_engine_cs *engine;
11696 bool mmio_flip;
11697 struct drm_i915_gem_request *request = NULL;
11698 int ret;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011699
Daniel Vetter5a21b662016-05-24 17:13:53 +020011700 /*
11701 * drm_mode_page_flip_ioctl() should already catch this, but double
11702 * check to be safe. In the future we may enable pageflipping from
11703 * a disabled primary plane.
11704 */
11705 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11706 return -EBUSY;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020011707
Daniel Vetter5a21b662016-05-24 17:13:53 +020011708 /* Can't change pixel format via MI display flips. */
11709 if (fb->pixel_format != crtc->primary->fb->pixel_format)
11710 return -EINVAL;
Maarten Lankhorst143f73b2016-05-17 15:07:54 +020011711
Daniel Vetter5a21b662016-05-24 17:13:53 +020011712 /*
11713 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11714 * Note that pitch changes could also affect these register.
11715 */
11716 if (INTEL_INFO(dev)->gen > 3 &&
11717 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11718 fb->pitches[0] != crtc->primary->fb->pitches[0]))
11719 return -EINVAL;
Maarten Lankhorst143f73b2016-05-17 15:07:54 +020011720
Daniel Vetter5a21b662016-05-24 17:13:53 +020011721 if (i915_terminally_wedged(&dev_priv->gpu_error))
11722 goto out_hang;
Maarten Lankhorst143f73b2016-05-17 15:07:54 +020011723
Daniel Vetter5a21b662016-05-24 17:13:53 +020011724 work = kzalloc(sizeof(*work), GFP_KERNEL);
11725 if (work == NULL)
11726 return -ENOMEM;
11727
11728 work->event = event;
11729 work->crtc = crtc;
11730 work->old_fb = old_fb;
11731 INIT_WORK(&work->unpin_work, intel_unpin_work_fn);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011732
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020011733 ret = drm_crtc_vblank_get(crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011734 if (ret)
11735 goto free_work;
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020011736
Daniel Vetter5a21b662016-05-24 17:13:53 +020011737 /* We borrow the event spin lock for protecting flip_work */
11738 spin_lock_irq(&dev->event_lock);
11739 if (intel_crtc->flip_work) {
11740 /* Before declaring the flip queue wedged, check if
11741 * the hardware completed the operation behind our backs.
11742 */
11743 if (pageflip_finished(intel_crtc, intel_crtc->flip_work)) {
11744 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11745 page_flip_completed(intel_crtc);
11746 } else {
11747 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
11748 spin_unlock_irq(&dev->event_lock);
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020011749
Daniel Vetter5a21b662016-05-24 17:13:53 +020011750 drm_crtc_vblank_put(crtc);
11751 kfree(work);
11752 return -EBUSY;
11753 }
11754 }
11755 intel_crtc->flip_work = work;
11756 spin_unlock_irq(&dev->event_lock);
Alex Goinsfd8e0582015-11-25 18:43:38 -080011757
Daniel Vetter5a21b662016-05-24 17:13:53 +020011758 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11759 flush_workqueue(dev_priv->wq);
11760
11761 /* Reference the objects for the scheduled work. */
11762 drm_framebuffer_reference(work->old_fb);
11763 drm_gem_object_reference(&obj->base);
11764
11765 crtc->primary->fb = fb;
11766 update_state_fb(crtc->primary);
Maarten Lankhorstfaf68d92016-06-14 14:24:20 +020011767
11768 intel_fbc_pre_update(intel_crtc, intel_crtc->config,
11769 to_intel_plane_state(primary->state));
Daniel Vetter5a21b662016-05-24 17:13:53 +020011770
11771 work->pending_flip_obj = obj;
11772
11773 ret = i915_mutex_lock_interruptible(dev);
11774 if (ret)
11775 goto cleanup;
11776
11777 intel_crtc->reset_counter = i915_reset_counter(&dev_priv->gpu_error);
11778 if (__i915_reset_in_progress_or_wedged(intel_crtc->reset_counter)) {
11779 ret = -EIO;
11780 goto cleanup;
11781 }
11782
11783 atomic_inc(&intel_crtc->unpin_work_count);
11784
11785 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
11786 work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
11787
11788 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
11789 engine = &dev_priv->engine[BCS];
11790 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
11791 /* vlv: DISPLAY_FLIP fails to change tiling */
11792 engine = NULL;
11793 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
11794 engine = &dev_priv->engine[BCS];
11795 } else if (INTEL_INFO(dev)->gen >= 7) {
11796 engine = i915_gem_request_get_engine(obj->last_write_req);
11797 if (engine == NULL || engine->id != RCS)
11798 engine = &dev_priv->engine[BCS];
11799 } else {
11800 engine = &dev_priv->engine[RCS];
11801 }
11802
11803 mmio_flip = use_mmio_flip(engine, obj);
11804
11805 /* When using CS flips, we want to emit semaphores between rings.
11806 * However, when using mmio flips we will create a task to do the
11807 * synchronisation, so all we want here is to pin the framebuffer
11808 * into the display plane and skip any waits.
11809 */
11810 if (!mmio_flip) {
11811 ret = i915_gem_object_sync(obj, engine, &request);
11812 if (!ret && !request) {
11813 request = i915_gem_request_alloc(engine, NULL);
11814 ret = PTR_ERR_OR_ZERO(request);
Maarten Lankhorst143f73b2016-05-17 15:07:54 +020011815 }
Sourab Gupta84c33a62014-06-02 16:47:17 +053011816
Daniel Vetter5a21b662016-05-24 17:13:53 +020011817 if (ret)
11818 goto cleanup_pending;
Maarten Lankhorst143f73b2016-05-17 15:07:54 +020011819 }
11820
Daniel Vetter5a21b662016-05-24 17:13:53 +020011821 ret = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
11822 if (ret)
11823 goto cleanup_pending;
Maarten Lankhorst143f73b2016-05-17 15:07:54 +020011824
Daniel Vetter5a21b662016-05-24 17:13:53 +020011825 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary),
11826 obj, 0);
11827 work->gtt_offset += intel_crtc->dspaddr_offset;
11828 work->rotation = crtc->primary->state->rotation;
11829
11830 if (mmio_flip) {
11831 INIT_WORK(&work->mmio_work, intel_mmio_flip_work_func);
11832
11833 i915_gem_request_assign(&work->flip_queued_req,
11834 obj->last_write_req);
11835
11836 schedule_work(&work->mmio_work);
11837 } else {
11838 i915_gem_request_assign(&work->flip_queued_req, request);
11839 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
11840 page_flip_flags);
11841 if (ret)
11842 goto cleanup_unpin;
11843
11844 intel_mark_page_flip_active(intel_crtc, work);
11845
11846 i915_add_request_no_flush(request);
Maarten Lankhorst143f73b2016-05-17 15:07:54 +020011847 }
11848
Daniel Vetter5a21b662016-05-24 17:13:53 +020011849 i915_gem_track_fb(intel_fb_obj(old_fb), obj,
11850 to_intel_plane(primary)->frontbuffer_bit);
11851 mutex_unlock(&dev->struct_mutex);
11852
11853 intel_frontbuffer_flip_prepare(dev,
11854 to_intel_plane(primary)->frontbuffer_bit);
11855
11856 trace_i915_flip_request(intel_crtc->plane, obj);
11857
11858 return 0;
11859
11860cleanup_unpin:
11861 intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
11862cleanup_pending:
11863 if (!IS_ERR_OR_NULL(request))
11864 i915_add_request_no_flush(request);
11865 atomic_dec(&intel_crtc->unpin_work_count);
11866 mutex_unlock(&dev->struct_mutex);
11867cleanup:
11868 crtc->primary->fb = old_fb;
11869 update_state_fb(crtc->primary);
11870
11871 drm_gem_object_unreference_unlocked(&obj->base);
11872 drm_framebuffer_unreference(work->old_fb);
11873
11874 spin_lock_irq(&dev->event_lock);
11875 intel_crtc->flip_work = NULL;
11876 spin_unlock_irq(&dev->event_lock);
11877
11878 drm_crtc_vblank_put(crtc);
11879free_work:
11880 kfree(work);
11881
11882 if (ret == -EIO) {
11883 struct drm_atomic_state *state;
11884 struct drm_plane_state *plane_state;
11885
11886out_hang:
11887 state = drm_atomic_state_alloc(dev);
11888 if (!state)
11889 return -ENOMEM;
11890 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
11891
11892retry:
11893 plane_state = drm_atomic_get_plane_state(state, primary);
11894 ret = PTR_ERR_OR_ZERO(plane_state);
11895 if (!ret) {
11896 drm_atomic_set_fb_for_plane(plane_state, fb);
11897
11898 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
11899 if (!ret)
11900 ret = drm_atomic_commit(state);
11901 }
11902
11903 if (ret == -EDEADLK) {
11904 drm_modeset_backoff(state->acquire_ctx);
11905 drm_atomic_state_clear(state);
11906 goto retry;
11907 }
11908
11909 if (ret)
11910 drm_atomic_state_free(state);
11911
11912 if (ret == 0 && event) {
11913 spin_lock_irq(&dev->event_lock);
11914 drm_crtc_send_vblank_event(crtc, event);
11915 spin_unlock_irq(&dev->event_lock);
11916 }
11917 }
11918 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011919}
11920
Daniel Vetter5a21b662016-05-24 17:13:53 +020011921
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011922/**
11923 * intel_wm_need_update - Check whether watermarks need updating
11924 * @plane: drm plane
11925 * @state: new plane state
11926 *
11927 * Check current plane state versus the new one to determine whether
11928 * watermarks need to be recalculated.
11929 *
11930 * Returns true or false.
11931 */
11932static bool intel_wm_need_update(struct drm_plane *plane,
11933 struct drm_plane_state *state)
11934{
Matt Roperd21fbe82015-09-24 15:53:12 -070011935 struct intel_plane_state *new = to_intel_plane_state(state);
11936 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
11937
11938 /* Update watermarks on tiling or size changes. */
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010011939 if (new->visible != cur->visible)
11940 return true;
11941
11942 if (!cur->base.fb || !new->base.fb)
11943 return false;
11944
11945 if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
11946 cur->base.rotation != new->base.rotation ||
Matt Roperd21fbe82015-09-24 15:53:12 -070011947 drm_rect_width(&new->src) != drm_rect_width(&cur->src) ||
11948 drm_rect_height(&new->src) != drm_rect_height(&cur->src) ||
11949 drm_rect_width(&new->dst) != drm_rect_width(&cur->dst) ||
11950 drm_rect_height(&new->dst) != drm_rect_height(&cur->dst))
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011951 return true;
11952
11953 return false;
11954}
11955
Matt Roperd21fbe82015-09-24 15:53:12 -070011956static bool needs_scaling(struct intel_plane_state *state)
11957{
11958 int src_w = drm_rect_width(&state->src) >> 16;
11959 int src_h = drm_rect_height(&state->src) >> 16;
11960 int dst_w = drm_rect_width(&state->dst);
11961 int dst_h = drm_rect_height(&state->dst);
11962
11963 return (src_w != dst_w || src_h != dst_h);
11964}
11965
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011966int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
11967 struct drm_plane_state *plane_state)
11968{
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010011969 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011970 struct drm_crtc *crtc = crtc_state->crtc;
11971 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11972 struct drm_plane *plane = plane_state->plane;
11973 struct drm_device *dev = crtc->dev;
Matt Ropered4a6a72016-02-23 17:20:13 -080011974 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011975 struct intel_plane_state *old_plane_state =
11976 to_intel_plane_state(plane->state);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011977 bool mode_changed = needs_modeset(crtc_state);
11978 bool was_crtc_enabled = crtc->state->active;
11979 bool is_crtc_enabled = crtc_state->active;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011980 bool turn_off, turn_on, visible, was_visible;
11981 struct drm_framebuffer *fb = plane_state->fb;
Ville Syrjälä78108b72016-05-27 20:59:19 +030011982 int ret;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011983
11984 if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
11985 plane->type != DRM_PLANE_TYPE_CURSOR) {
11986 ret = skl_update_scaler_plane(
11987 to_intel_crtc_state(crtc_state),
11988 to_intel_plane_state(plane_state));
11989 if (ret)
11990 return ret;
11991 }
11992
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011993 was_visible = old_plane_state->visible;
11994 visible = to_intel_plane_state(plane_state)->visible;
11995
11996 if (!was_crtc_enabled && WARN_ON(was_visible))
11997 was_visible = false;
11998
Maarten Lankhorst35c08f42015-12-03 14:31:07 +010011999 /*
12000 * Visibility is calculated as if the crtc was on, but
12001 * after scaler setup everything depends on it being off
12002 * when the crtc isn't active.
Ville Syrjäläf818ffe2016-04-29 17:31:18 +030012003 *
12004 * FIXME this is wrong for watermarks. Watermarks should also
12005 * be computed as if the pipe would be active. Perhaps move
12006 * per-plane wm computation to the .check_plane() hook, and
12007 * only combine the results from all planes in the current place?
Maarten Lankhorst35c08f42015-12-03 14:31:07 +010012008 */
12009 if (!is_crtc_enabled)
12010 to_intel_plane_state(plane_state)->visible = visible = false;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012011
12012 if (!was_visible && !visible)
12013 return 0;
12014
Maarten Lankhorste8861672016-02-24 11:24:26 +010012015 if (fb != old_plane_state->base.fb)
12016 pipe_config->fb_changed = true;
12017
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012018 turn_off = was_visible && (!visible || mode_changed);
12019 turn_on = visible && (!was_visible || mode_changed);
12020
Ville Syrjälä72660ce2016-05-27 20:59:20 +030012021 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
Ville Syrjälä78108b72016-05-27 20:59:19 +030012022 intel_crtc->base.base.id,
12023 intel_crtc->base.name,
Ville Syrjälä72660ce2016-05-27 20:59:20 +030012024 plane->base.id, plane->name,
12025 fb ? fb->base.id : -1);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012026
Ville Syrjälä72660ce2016-05-27 20:59:20 +030012027 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
12028 plane->base.id, plane->name,
12029 was_visible, visible,
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012030 turn_off, turn_on, mode_changed);
12031
Ville Syrjäläcaed3612016-03-09 19:07:25 +020012032 if (turn_on) {
12033 pipe_config->update_wm_pre = true;
12034
12035 /* must disable cxsr around plane enable/disable */
12036 if (plane->type != DRM_PLANE_TYPE_CURSOR)
12037 pipe_config->disable_cxsr = true;
12038 } else if (turn_off) {
12039 pipe_config->update_wm_post = true;
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010012040
Ville Syrjälä852eb002015-06-24 22:00:07 +030012041 /* must disable cxsr around plane enable/disable */
Maarten Lankhorste8861672016-02-24 11:24:26 +010012042 if (plane->type != DRM_PLANE_TYPE_CURSOR)
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010012043 pipe_config->disable_cxsr = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030012044 } else if (intel_wm_need_update(plane, plane_state)) {
Ville Syrjäläcaed3612016-03-09 19:07:25 +020012045 /* FIXME bollocks */
12046 pipe_config->update_wm_pre = true;
12047 pipe_config->update_wm_post = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030012048 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012049
Matt Ropered4a6a72016-02-23 17:20:13 -080012050 /* Pre-gen9 platforms need two-step watermark updates */
Ville Syrjäläcaed3612016-03-09 19:07:25 +020012051 if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
12052 INTEL_INFO(dev)->gen < 9 && dev_priv->display.optimize_watermarks)
Matt Ropered4a6a72016-02-23 17:20:13 -080012053 to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
12054
Rodrigo Vivi8be6ca82015-08-24 16:38:23 -070012055 if (visible || was_visible)
Maarten Lankhorstcd202f62016-03-09 10:35:44 +010012056 pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030012057
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +010012058 /*
12059 * WaCxSRDisabledForSpriteScaling:ivb
12060 *
12061 * cstate->update_wm was already set above, so this flag will
12062 * take effect when we commit and program watermarks.
12063 */
12064 if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev) &&
12065 needs_scaling(to_intel_plane_state(plane_state)) &&
12066 !needs_scaling(old_plane_state))
12067 pipe_config->disable_lp_wm = true;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012068
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012069 return 0;
12070}
12071
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012072static bool encoders_cloneable(const struct intel_encoder *a,
12073 const struct intel_encoder *b)
12074{
12075 /* masks could be asymmetric, so check both ways */
12076 return a == b || (a->cloneable & (1 << b->type) &&
12077 b->cloneable & (1 << a->type));
12078}
12079
12080static bool check_single_encoder_cloning(struct drm_atomic_state *state,
12081 struct intel_crtc *crtc,
12082 struct intel_encoder *encoder)
12083{
12084 struct intel_encoder *source_encoder;
12085 struct drm_connector *connector;
12086 struct drm_connector_state *connector_state;
12087 int i;
12088
12089 for_each_connector_in_state(state, connector, connector_state, i) {
12090 if (connector_state->crtc != &crtc->base)
12091 continue;
12092
12093 source_encoder =
12094 to_intel_encoder(connector_state->best_encoder);
12095 if (!encoders_cloneable(encoder, source_encoder))
12096 return false;
12097 }
12098
12099 return true;
12100}
12101
12102static bool check_encoder_cloning(struct drm_atomic_state *state,
12103 struct intel_crtc *crtc)
12104{
12105 struct intel_encoder *encoder;
12106 struct drm_connector *connector;
12107 struct drm_connector_state *connector_state;
12108 int i;
12109
12110 for_each_connector_in_state(state, connector, connector_state, i) {
12111 if (connector_state->crtc != &crtc->base)
12112 continue;
12113
12114 encoder = to_intel_encoder(connector_state->best_encoder);
12115 if (!check_single_encoder_cloning(state, crtc, encoder))
12116 return false;
12117 }
12118
12119 return true;
12120}
12121
12122static int intel_crtc_atomic_check(struct drm_crtc *crtc,
12123 struct drm_crtc_state *crtc_state)
12124{
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020012125 struct drm_device *dev = crtc->dev;
Maarten Lankhorstad421372015-06-15 12:33:42 +020012126 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012127 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020012128 struct intel_crtc_state *pipe_config =
12129 to_intel_crtc_state(crtc_state);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012130 struct drm_atomic_state *state = crtc_state->state;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012131 int ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012132 bool mode_changed = needs_modeset(crtc_state);
12133
12134 if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
12135 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
12136 return -EINVAL;
12137 }
12138
Ville Syrjälä852eb002015-06-24 22:00:07 +030012139 if (mode_changed && !crtc_state->active)
Ville Syrjäläcaed3612016-03-09 19:07:25 +020012140 pipe_config->update_wm_post = true;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020012141
Maarten Lankhorstad421372015-06-15 12:33:42 +020012142 if (mode_changed && crtc_state->enable &&
12143 dev_priv->display.crtc_compute_clock &&
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012144 !WARN_ON(pipe_config->shared_dpll)) {
Maarten Lankhorstad421372015-06-15 12:33:42 +020012145 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
12146 pipe_config);
12147 if (ret)
12148 return ret;
12149 }
12150
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000012151 if (crtc_state->color_mgmt_changed) {
12152 ret = intel_color_check(crtc, crtc_state);
12153 if (ret)
12154 return ret;
12155 }
12156
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020012157 ret = 0;
Matt Roper86c8bbb2015-09-24 15:53:16 -070012158 if (dev_priv->display.compute_pipe_wm) {
Maarten Lankhorste3bddde2016-03-01 11:07:22 +010012159 ret = dev_priv->display.compute_pipe_wm(pipe_config);
Matt Ropered4a6a72016-02-23 17:20:13 -080012160 if (ret) {
12161 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
Matt Roper86c8bbb2015-09-24 15:53:16 -070012162 return ret;
Matt Ropered4a6a72016-02-23 17:20:13 -080012163 }
12164 }
12165
12166 if (dev_priv->display.compute_intermediate_wm &&
12167 !to_intel_atomic_state(state)->skip_intermediate_wm) {
12168 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
12169 return 0;
12170
12171 /*
12172 * Calculate 'intermediate' watermarks that satisfy both the
12173 * old state and the new state. We can program these
12174 * immediately.
12175 */
12176 ret = dev_priv->display.compute_intermediate_wm(crtc->dev,
12177 intel_crtc,
12178 pipe_config);
12179 if (ret) {
12180 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
12181 return ret;
12182 }
Ville Syrjäläe3d54572016-05-13 10:10:42 -070012183 } else if (dev_priv->display.compute_intermediate_wm) {
12184 if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
12185 pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
Matt Roper86c8bbb2015-09-24 15:53:16 -070012186 }
12187
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020012188 if (INTEL_INFO(dev)->gen >= 9) {
12189 if (mode_changed)
12190 ret = skl_update_scaler_crtc(pipe_config);
12191
12192 if (!ret)
12193 ret = intel_atomic_setup_scalers(dev, intel_crtc,
12194 pipe_config);
12195 }
12196
12197 return ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012198}
12199
Jani Nikula65b38e02015-04-13 11:26:56 +030012200static const struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012201 .mode_set_base_atomic = intel_pipe_set_base_atomic,
Daniel Vetter5a21b662016-05-24 17:13:53 +020012202 .atomic_begin = intel_begin_crtc_commit,
12203 .atomic_flush = intel_finish_crtc_commit,
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012204 .atomic_check = intel_crtc_atomic_check,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012205};
12206
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020012207static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
12208{
12209 struct intel_connector *connector;
12210
12211 for_each_intel_connector(dev, connector) {
Daniel Vetter8863dc72016-05-06 15:39:03 +020012212 if (connector->base.state->crtc)
12213 drm_connector_unreference(&connector->base);
12214
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020012215 if (connector->base.encoder) {
12216 connector->base.state->best_encoder =
12217 connector->base.encoder;
12218 connector->base.state->crtc =
12219 connector->base.encoder->crtc;
Daniel Vetter8863dc72016-05-06 15:39:03 +020012220
12221 drm_connector_reference(&connector->base);
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020012222 } else {
12223 connector->base.state->best_encoder = NULL;
12224 connector->base.state->crtc = NULL;
12225 }
12226 }
12227}
12228
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012229static void
Robin Schroereba905b2014-05-18 02:24:50 +020012230connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012231 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012232{
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012233 int bpp = pipe_config->pipe_bpp;
12234
12235 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
12236 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030012237 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012238
12239 /* Don't use an invalid EDID bpc value */
12240 if (connector->base.display_info.bpc &&
12241 connector->base.display_info.bpc * 3 < bpp) {
12242 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
12243 bpp, connector->base.display_info.bpc*3);
12244 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
12245 }
12246
Jani Nikula013dd9e2016-01-13 16:35:20 +020012247 /* Clamp bpp to default limit on screens without EDID 1.4 */
12248 if (connector->base.display_info.bpc == 0) {
12249 int type = connector->base.connector_type;
12250 int clamp_bpp = 24;
12251
12252 /* Fall back to 18 bpp when DP sink capability is unknown. */
12253 if (type == DRM_MODE_CONNECTOR_DisplayPort ||
12254 type == DRM_MODE_CONNECTOR_eDP)
12255 clamp_bpp = 18;
12256
12257 if (bpp > clamp_bpp) {
12258 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of %d\n",
12259 bpp, clamp_bpp);
12260 pipe_config->pipe_bpp = clamp_bpp;
12261 }
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012262 }
12263}
12264
12265static int
12266compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012267 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012268{
12269 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012270 struct drm_atomic_state *state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012271 struct drm_connector *connector;
12272 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012273 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012274
Wayne Boyer666a4532015-12-09 12:29:35 -080012275 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012276 bpp = 10*3;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012277 else if (INTEL_INFO(dev)->gen >= 5)
12278 bpp = 12*3;
12279 else
12280 bpp = 8*3;
12281
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012282
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012283 pipe_config->pipe_bpp = bpp;
12284
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012285 state = pipe_config->base.state;
12286
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012287 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012288 for_each_connector_in_state(state, connector, connector_state, i) {
12289 if (connector_state->crtc != &crtc->base)
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012290 continue;
12291
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012292 connected_sink_compute_bpp(to_intel_connector(connector),
12293 pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012294 }
12295
12296 return bpp;
12297}
12298
Daniel Vetter644db712013-09-19 14:53:58 +020012299static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
12300{
12301 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
12302 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010012303 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020012304 mode->crtc_hdisplay, mode->crtc_hsync_start,
12305 mode->crtc_hsync_end, mode->crtc_htotal,
12306 mode->crtc_vdisplay, mode->crtc_vsync_start,
12307 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
12308}
12309
Daniel Vetterc0b03412013-05-28 12:05:54 +020012310static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012311 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012312 const char *context)
12313{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012314 struct drm_device *dev = crtc->base.dev;
12315 struct drm_plane *plane;
12316 struct intel_plane *intel_plane;
12317 struct intel_plane_state *state;
12318 struct drm_framebuffer *fb;
12319
Ville Syrjälä78108b72016-05-27 20:59:19 +030012320 DRM_DEBUG_KMS("[CRTC:%d:%s]%s config %p for pipe %c\n",
12321 crtc->base.base.id, crtc->base.name,
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012322 context, pipe_config, pipe_name(crtc->pipe));
Daniel Vetterc0b03412013-05-28 12:05:54 +020012323
Jani Nikulada205632016-03-15 21:51:10 +020012324 DRM_DEBUG_KMS("cpu_transcoder: %s\n", transcoder_name(pipe_config->cpu_transcoder));
Daniel Vetterc0b03412013-05-28 12:05:54 +020012325 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
12326 pipe_config->pipe_bpp, pipe_config->dither);
12327 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
12328 pipe_config->has_pch_encoder,
12329 pipe_config->fdi_lanes,
12330 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
12331 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
12332 pipe_config->fdi_m_n.tu);
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012333 DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012334 pipe_config->has_dp_encoder,
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012335 pipe_config->lane_count,
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012336 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
12337 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
12338 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012339
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012340 DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012341 pipe_config->has_dp_encoder,
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012342 pipe_config->lane_count,
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012343 pipe_config->dp_m2_n2.gmch_m,
12344 pipe_config->dp_m2_n2.gmch_n,
12345 pipe_config->dp_m2_n2.link_m,
12346 pipe_config->dp_m2_n2.link_n,
12347 pipe_config->dp_m2_n2.tu);
12348
Daniel Vetter55072d12014-11-20 16:10:28 +010012349 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
12350 pipe_config->has_audio,
12351 pipe_config->has_infoframe);
12352
Daniel Vetterc0b03412013-05-28 12:05:54 +020012353 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012354 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012355 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012356 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
12357 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030012358 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030012359 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
12360 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Tvrtko Ursulin0ec463d2015-05-13 16:51:08 +010012361 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
12362 crtc->num_scalers,
12363 pipe_config->scaler_state.scaler_users,
12364 pipe_config->scaler_state.scaler_id);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012365 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
12366 pipe_config->gmch_pfit.control,
12367 pipe_config->gmch_pfit.pgm_ratios,
12368 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012369 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020012370 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012371 pipe_config->pch_pfit.size,
12372 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012373 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030012374 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012375
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012376 if (IS_BROXTON(dev)) {
Imre Deak05712c12015-06-18 17:25:54 +030012377 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012378 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
Imre Deakc8453332015-06-18 17:25:55 +030012379 "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012380 pipe_config->ddi_pll_sel,
12381 pipe_config->dpll_hw_state.ebb0,
Imre Deak05712c12015-06-18 17:25:54 +030012382 pipe_config->dpll_hw_state.ebb4,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012383 pipe_config->dpll_hw_state.pll0,
12384 pipe_config->dpll_hw_state.pll1,
12385 pipe_config->dpll_hw_state.pll2,
12386 pipe_config->dpll_hw_state.pll3,
12387 pipe_config->dpll_hw_state.pll6,
12388 pipe_config->dpll_hw_state.pll8,
Imre Deak05712c12015-06-18 17:25:54 +030012389 pipe_config->dpll_hw_state.pll9,
Imre Deakc8453332015-06-18 17:25:55 +030012390 pipe_config->dpll_hw_state.pll10,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012391 pipe_config->dpll_hw_state.pcsdw12);
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070012392 } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012393 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
12394 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
12395 pipe_config->ddi_pll_sel,
12396 pipe_config->dpll_hw_state.ctrl1,
12397 pipe_config->dpll_hw_state.cfgcr1,
12398 pipe_config->dpll_hw_state.cfgcr2);
12399 } else if (HAS_DDI(dev)) {
Ville Syrjälä1260f072016-02-17 21:41:08 +020012400 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 +010012401 pipe_config->ddi_pll_sel,
Maarten Lankhorst00490c22015-11-16 14:42:12 +010012402 pipe_config->dpll_hw_state.wrpll,
12403 pipe_config->dpll_hw_state.spll);
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012404 } else {
12405 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
12406 "fp0: 0x%x, fp1: 0x%x\n",
12407 pipe_config->dpll_hw_state.dpll,
12408 pipe_config->dpll_hw_state.dpll_md,
12409 pipe_config->dpll_hw_state.fp0,
12410 pipe_config->dpll_hw_state.fp1);
12411 }
12412
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012413 DRM_DEBUG_KMS("planes on this crtc\n");
12414 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
12415 intel_plane = to_intel_plane(plane);
12416 if (intel_plane->pipe != crtc->pipe)
12417 continue;
12418
12419 state = to_intel_plane_state(plane->state);
12420 fb = state->base.fb;
12421 if (!fb) {
Ville Syrjälä1d577e02016-05-27 20:59:25 +030012422 DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
12423 plane->base.id, plane->name, state->scaler_id);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012424 continue;
12425 }
12426
Ville Syrjälä1d577e02016-05-27 20:59:25 +030012427 DRM_DEBUG_KMS("[PLANE:%d:%s] enabled",
12428 plane->base.id, plane->name);
12429 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = %s",
12430 fb->base.id, fb->width, fb->height,
12431 drm_get_format_name(fb->pixel_format));
12432 DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
12433 state->scaler_id,
12434 state->src.x1 >> 16, state->src.y1 >> 16,
12435 drm_rect_width(&state->src) >> 16,
12436 drm_rect_height(&state->src) >> 16,
12437 state->dst.x1, state->dst.y1,
12438 drm_rect_width(&state->dst),
12439 drm_rect_height(&state->dst));
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012440 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020012441}
12442
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012443static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012444{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012445 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012446 struct drm_connector *connector;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012447 unsigned int used_ports = 0;
12448
12449 /*
12450 * Walk the connector list instead of the encoder
12451 * list to detect the problem on ddi platforms
12452 * where there's just one encoder per digital port.
12453 */
Ville Syrjälä0bff4852015-12-10 18:22:31 +020012454 drm_for_each_connector(connector, dev) {
12455 struct drm_connector_state *connector_state;
12456 struct intel_encoder *encoder;
12457
12458 connector_state = drm_atomic_get_existing_connector_state(state, connector);
12459 if (!connector_state)
12460 connector_state = connector->state;
12461
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012462 if (!connector_state->best_encoder)
12463 continue;
12464
12465 encoder = to_intel_encoder(connector_state->best_encoder);
12466
12467 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012468
12469 switch (encoder->type) {
12470 unsigned int port_mask;
12471 case INTEL_OUTPUT_UNKNOWN:
12472 if (WARN_ON(!HAS_DDI(dev)))
12473 break;
12474 case INTEL_OUTPUT_DISPLAYPORT:
12475 case INTEL_OUTPUT_HDMI:
12476 case INTEL_OUTPUT_EDP:
12477 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12478
12479 /* the same port mustn't appear more than once */
12480 if (used_ports & port_mask)
12481 return false;
12482
12483 used_ports |= port_mask;
12484 default:
12485 break;
12486 }
12487 }
12488
12489 return true;
12490}
12491
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012492static void
12493clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12494{
12495 struct drm_crtc_state tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012496 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012497 struct intel_dpll_hw_state dpll_hw_state;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012498 struct intel_shared_dpll *shared_dpll;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012499 uint32_t ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012500 bool force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012501
Ander Conselvan de Oliveira7546a382015-05-20 09:03:27 +030012502 /* FIXME: before the switch to atomic started, a new pipe_config was
12503 * kzalloc'd. Code that depends on any field being zero should be
12504 * fixed, so that the crtc_state can be safely duplicated. For now,
12505 * only fields that are know to not cause problems are preserved. */
12506
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012507 tmp_state = crtc_state->base;
Chandra Konduru663a3642015-04-07 15:28:41 -070012508 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012509 shared_dpll = crtc_state->shared_dpll;
12510 dpll_hw_state = crtc_state->dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012511 ddi_pll_sel = crtc_state->ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012512 force_thru = crtc_state->pch_pfit.force_thru;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012513
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012514 memset(crtc_state, 0, sizeof *crtc_state);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012515
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012516 crtc_state->base = tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012517 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012518 crtc_state->shared_dpll = shared_dpll;
12519 crtc_state->dpll_hw_state = dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012520 crtc_state->ddi_pll_sel = ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012521 crtc_state->pch_pfit.force_thru = force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012522}
12523
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012524static int
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012525intel_modeset_pipe_config(struct drm_crtc *crtc,
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012526 struct intel_crtc_state *pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +020012527{
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012528 struct drm_atomic_state *state = pipe_config->base.state;
Daniel Vetter7758a112012-07-08 19:40:39 +020012529 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012530 struct drm_connector *connector;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012531 struct drm_connector_state *connector_state;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012532 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012533 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010012534 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020012535
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012536 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020012537
Daniel Vettere143a212013-07-04 12:01:15 +020012538 pipe_config->cpu_transcoder =
12539 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012540
Imre Deak2960bc92013-07-30 13:36:32 +030012541 /*
12542 * Sanitize sync polarity flags based on requested ones. If neither
12543 * positive or negative polarity is requested, treat this as meaning
12544 * negative polarity.
12545 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012546 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012547 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012548 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012549
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012550 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012551 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012552 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012553
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012554 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12555 pipe_config);
12556 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012557 goto fail;
12558
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012559 /*
12560 * Determine the real pipe dimensions. Note that stereo modes can
12561 * increase the actual pipe size due to the frame doubling and
12562 * insertion of additional space for blanks between the frame. This
12563 * is stored in the crtc timings. We use the requested mode to do this
12564 * computation to clearly distinguish it from the adjusted mode, which
12565 * can be changed by the connectors in the below retry loop.
12566 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012567 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080012568 &pipe_config->pipe_src_w,
12569 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012570
Daniel Vettere29c22c2013-02-21 00:00:16 +010012571encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020012572 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020012573 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020012574 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012575
Daniel Vetter135c81b2013-07-21 21:37:09 +020012576 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012577 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12578 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020012579
Daniel Vetter7758a112012-07-08 19:40:39 +020012580 /* Pass our mode to the connectors and the CRTC to give them a chance to
12581 * adjust it according to limitations or connector properties, and also
12582 * a chance to reject the mode entirely.
12583 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012584 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012585 if (connector_state->crtc != crtc)
12586 continue;
12587
12588 encoder = to_intel_encoder(connector_state->best_encoder);
12589
Daniel Vetterefea6e82013-07-21 21:36:59 +020012590 if (!(encoder->compute_config(encoder, pipe_config))) {
12591 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020012592 goto fail;
12593 }
12594 }
12595
Daniel Vetterff9a6752013-06-01 17:16:21 +020012596 /* Set default port clock if not overwritten by the encoder. Needs to be
12597 * done afterwards in case the encoder adjusts the mode. */
12598 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012599 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010012600 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012601
Daniel Vettera43f6e02013-06-07 23:10:32 +020012602 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010012603 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020012604 DRM_DEBUG_KMS("CRTC fixup failed\n");
12605 goto fail;
12606 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010012607
12608 if (ret == RETRY) {
12609 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12610 ret = -EINVAL;
12611 goto fail;
12612 }
12613
12614 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12615 retry = false;
12616 goto encoder_retry;
12617 }
12618
Daniel Vettere8fa4272015-08-12 11:43:34 +020012619 /* Dithering seems to not pass-through bits correctly when it should, so
12620 * only enable it on 6bpc panels. */
12621 pipe_config->dither = pipe_config->pipe_bpp == 6*3;
Daniel Vetter62f0ace2015-08-26 18:57:26 +020012622 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012623 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012624
Daniel Vetter7758a112012-07-08 19:40:39 +020012625fail:
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012626 return ret;
Daniel Vetter7758a112012-07-08 19:40:39 +020012627}
12628
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012629static void
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020012630intel_modeset_update_crtc_state(struct drm_atomic_state *state)
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012631{
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012632 struct drm_crtc *crtc;
12633 struct drm_crtc_state *crtc_state;
Maarten Lankhorst8a75d152015-07-13 16:30:14 +020012634 int i;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012635
Ville Syrjälä76688512014-01-10 11:28:06 +020012636 /* Double check state. */
Maarten Lankhorst8a75d152015-07-13 16:30:14 +020012637 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst3cb480b2015-06-01 12:49:49 +020012638 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
Maarten Lankhorstfc467a222015-06-01 12:50:07 +020012639
12640 /* Update hwmode for vblank functions */
12641 if (crtc->state->active)
12642 crtc->hwmode = crtc->state->adjusted_mode;
12643 else
12644 crtc->hwmode.crtc_clock = 0;
Maarten Lankhorst61067a52015-09-23 16:29:36 +020012645
12646 /*
12647 * Update legacy state to satisfy fbc code. This can
12648 * be removed when fbc uses the atomic state.
12649 */
12650 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
12651 struct drm_plane_state *plane_state = crtc->primary->state;
12652
12653 crtc->primary->fb = plane_state->fb;
12654 crtc->x = plane_state->src_x >> 16;
12655 crtc->y = plane_state->src_y >> 16;
12656 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012657 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012658}
12659
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012660static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012661{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012662 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012663
12664 if (clock1 == clock2)
12665 return true;
12666
12667 if (!clock1 || !clock2)
12668 return false;
12669
12670 diff = abs(clock1 - clock2);
12671
12672 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12673 return true;
12674
12675 return false;
12676}
12677
Daniel Vetter25c5b262012-07-08 22:08:04 +020012678#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12679 list_for_each_entry((intel_crtc), \
12680 &(dev)->mode_config.crtc_list, \
12681 base.head) \
Jani Nikula95150bd2015-11-24 21:21:56 +020012682 for_each_if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020012683
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012684static bool
12685intel_compare_m_n(unsigned int m, unsigned int n,
12686 unsigned int m2, unsigned int n2,
12687 bool exact)
12688{
12689 if (m == m2 && n == n2)
12690 return true;
12691
12692 if (exact || !m || !n || !m2 || !n2)
12693 return false;
12694
12695 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
12696
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012697 if (n > n2) {
12698 while (n > n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012699 m2 <<= 1;
12700 n2 <<= 1;
12701 }
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012702 } else if (n < n2) {
12703 while (n < n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012704 m <<= 1;
12705 n <<= 1;
12706 }
12707 }
12708
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012709 if (n != n2)
12710 return false;
12711
12712 return intel_fuzzy_clock_check(m, m2);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012713}
12714
12715static bool
12716intel_compare_link_m_n(const struct intel_link_m_n *m_n,
12717 struct intel_link_m_n *m2_n2,
12718 bool adjust)
12719{
12720 if (m_n->tu == m2_n2->tu &&
12721 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
12722 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
12723 intel_compare_m_n(m_n->link_m, m_n->link_n,
12724 m2_n2->link_m, m2_n2->link_n, !adjust)) {
12725 if (adjust)
12726 *m2_n2 = *m_n;
12727
12728 return true;
12729 }
12730
12731 return false;
12732}
12733
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012734static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012735intel_pipe_config_compare(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012736 struct intel_crtc_state *current_config,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012737 struct intel_crtc_state *pipe_config,
12738 bool adjust)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012739{
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012740 bool ret = true;
12741
12742#define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
12743 do { \
12744 if (!adjust) \
12745 DRM_ERROR(fmt, ##__VA_ARGS__); \
12746 else \
12747 DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
12748 } while (0)
12749
Daniel Vetter66e985c2013-06-05 13:34:20 +020012750#define PIPE_CONF_CHECK_X(name) \
12751 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012752 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012753 "(expected 0x%08x, found 0x%08x)\n", \
12754 current_config->name, \
12755 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012756 ret = false; \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012757 }
12758
Daniel Vetter08a24032013-04-19 11:25:34 +020012759#define PIPE_CONF_CHECK_I(name) \
12760 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012761 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter08a24032013-04-19 11:25:34 +020012762 "(expected %i, found %i)\n", \
12763 current_config->name, \
12764 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012765 ret = false; \
12766 }
12767
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012768#define PIPE_CONF_CHECK_P(name) \
12769 if (current_config->name != pipe_config->name) { \
12770 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12771 "(expected %p, found %p)\n", \
12772 current_config->name, \
12773 pipe_config->name); \
12774 ret = false; \
12775 }
12776
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012777#define PIPE_CONF_CHECK_M_N(name) \
12778 if (!intel_compare_link_m_n(&current_config->name, \
12779 &pipe_config->name,\
12780 adjust)) { \
12781 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12782 "(expected tu %i gmch %i/%i link %i/%i, " \
12783 "found tu %i, gmch %i/%i link %i/%i)\n", \
12784 current_config->name.tu, \
12785 current_config->name.gmch_m, \
12786 current_config->name.gmch_n, \
12787 current_config->name.link_m, \
12788 current_config->name.link_n, \
12789 pipe_config->name.tu, \
12790 pipe_config->name.gmch_m, \
12791 pipe_config->name.gmch_n, \
12792 pipe_config->name.link_m, \
12793 pipe_config->name.link_n); \
12794 ret = false; \
12795 }
12796
Daniel Vetter55c561a2016-03-30 11:34:36 +020012797/* This is required for BDW+ where there is only one set of registers for
12798 * switching between high and low RR.
12799 * This macro can be used whenever a comparison has to be made between one
12800 * hw state and multiple sw state variables.
12801 */
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012802#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
12803 if (!intel_compare_link_m_n(&current_config->name, \
12804 &pipe_config->name, adjust) && \
12805 !intel_compare_link_m_n(&current_config->alt_name, \
12806 &pipe_config->name, adjust)) { \
12807 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12808 "(expected tu %i gmch %i/%i link %i/%i, " \
12809 "or tu %i gmch %i/%i link %i/%i, " \
12810 "found tu %i, gmch %i/%i link %i/%i)\n", \
12811 current_config->name.tu, \
12812 current_config->name.gmch_m, \
12813 current_config->name.gmch_n, \
12814 current_config->name.link_m, \
12815 current_config->name.link_n, \
12816 current_config->alt_name.tu, \
12817 current_config->alt_name.gmch_m, \
12818 current_config->alt_name.gmch_n, \
12819 current_config->alt_name.link_m, \
12820 current_config->alt_name.link_n, \
12821 pipe_config->name.tu, \
12822 pipe_config->name.gmch_m, \
12823 pipe_config->name.gmch_n, \
12824 pipe_config->name.link_m, \
12825 pipe_config->name.link_n); \
12826 ret = false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010012827 }
12828
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012829#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12830 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012831 INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012832 "(expected %i, found %i)\n", \
12833 current_config->name & (mask), \
12834 pipe_config->name & (mask)); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012835 ret = false; \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012836 }
12837
Ville Syrjälä5e550652013-09-06 23:29:07 +030012838#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12839 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012840 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012841 "(expected %i, found %i)\n", \
12842 current_config->name, \
12843 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012844 ret = false; \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012845 }
12846
Daniel Vetterbb760062013-06-06 14:55:52 +020012847#define PIPE_CONF_QUIRK(quirk) \
12848 ((current_config->quirks | pipe_config->quirks) & (quirk))
12849
Daniel Vettereccb1402013-05-22 00:50:22 +020012850 PIPE_CONF_CHECK_I(cpu_transcoder);
12851
Daniel Vetter08a24032013-04-19 11:25:34 +020012852 PIPE_CONF_CHECK_I(has_pch_encoder);
12853 PIPE_CONF_CHECK_I(fdi_lanes);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012854 PIPE_CONF_CHECK_M_N(fdi_m_n);
Daniel Vetter08a24032013-04-19 11:25:34 +020012855
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012856 PIPE_CONF_CHECK_I(has_dp_encoder);
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012857 PIPE_CONF_CHECK_I(lane_count);
Imre Deak95a7a2a2016-06-13 16:44:35 +030012858 PIPE_CONF_CHECK_X(lane_lat_optim_mask);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012859
12860 if (INTEL_INFO(dev)->gen < 8) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012861 PIPE_CONF_CHECK_M_N(dp_m_n);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012862
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012863 if (current_config->has_drrs)
12864 PIPE_CONF_CHECK_M_N(dp_m2_n2);
12865 } else
12866 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012867
Jani Nikulaa65347b2015-11-27 12:21:46 +020012868 PIPE_CONF_CHECK_I(has_dsi_encoder);
12869
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012870 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12871 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12872 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12873 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12874 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12875 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012876
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012877 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12878 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12879 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12880 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12881 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12882 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012883
Daniel Vetterc93f54c2013-06-27 19:47:19 +020012884 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b2014-04-24 23:54:47 +020012885 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012886 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
Wayne Boyer666a4532015-12-09 12:29:35 -080012887 IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012888 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080012889 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020012890
Daniel Vetter9ed109a2014-04-24 23:54:52 +020012891 PIPE_CONF_CHECK_I(has_audio);
12892
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012893 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012894 DRM_MODE_FLAG_INTERLACE);
12895
Daniel Vetterbb760062013-06-06 14:55:52 +020012896 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012897 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012898 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012899 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012900 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012901 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012902 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012903 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012904 DRM_MODE_FLAG_NVSYNC);
12905 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012906
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030012907 PIPE_CONF_CHECK_X(gmch_pfit.control);
Daniel Vettere2ff2d42015-07-15 14:15:50 +020012908 /* pfit ratios are autocomputed by the hw on gen4+ */
12909 if (INTEL_INFO(dev)->gen < 4)
Ville Syrjälä7f7d8dd2016-03-15 16:40:07 +020012910 PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030012911 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
Daniel Vetter99535992014-04-13 12:00:33 +020012912
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020012913 if (!adjust) {
12914 PIPE_CONF_CHECK_I(pipe_src_w);
12915 PIPE_CONF_CHECK_I(pipe_src_h);
12916
12917 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12918 if (current_config->pch_pfit.enabled) {
12919 PIPE_CONF_CHECK_X(pch_pfit.pos);
12920 PIPE_CONF_CHECK_X(pch_pfit.size);
12921 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012922
Maarten Lankhorst7aefe2b2015-09-14 11:30:10 +020012923 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12924 }
Chandra Kondurua1b22782015-04-07 15:28:45 -070012925
Jesse Barnese59150d2014-01-07 13:30:45 -080012926 /* BDW+ don't expose a synchronous way to read the state */
12927 if (IS_HASWELL(dev))
12928 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012929
Ville Syrjälä282740f2013-09-04 18:30:03 +030012930 PIPE_CONF_CHECK_I(double_wide);
12931
Daniel Vetter26804af2014-06-25 22:01:55 +030012932 PIPE_CONF_CHECK_X(ddi_pll_sel);
12933
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012934 PIPE_CONF_CHECK_P(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012935 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020012936 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012937 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12938 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030012939 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Maarten Lankhorst00490c22015-11-16 14:42:12 +010012940 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000012941 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12942 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12943 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012944
Ville Syrjälä47eacba2016-04-12 22:14:35 +030012945 PIPE_CONF_CHECK_X(dsi_pll.ctrl);
12946 PIPE_CONF_CHECK_X(dsi_pll.div);
12947
Ville Syrjälä42571ae2013-09-06 23:29:00 +030012948 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12949 PIPE_CONF_CHECK_I(pipe_bpp);
12950
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012951 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080012952 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030012953
Daniel Vetter66e985c2013-06-05 13:34:20 +020012954#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020012955#undef PIPE_CONF_CHECK_I
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012956#undef PIPE_CONF_CHECK_P
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012957#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030012958#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020012959#undef PIPE_CONF_QUIRK
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012960#undef INTEL_ERR_OR_DBG_KMS
Daniel Vetter627eb5a2013-04-29 19:33:42 +020012961
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012962 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012963}
12964
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020012965static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
12966 const struct intel_crtc_state *pipe_config)
12967{
12968 if (pipe_config->has_pch_encoder) {
Ville Syrjälä21a727b2016-02-17 21:41:10 +020012969 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020012970 &pipe_config->fdi_m_n);
12971 int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
12972
12973 /*
12974 * FDI already provided one idea for the dotclock.
12975 * Yell if the encoder disagrees.
12976 */
12977 WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
12978 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
12979 fdi_dotclock, dotclock);
12980 }
12981}
12982
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012983static void verify_wm_state(struct drm_crtc *crtc,
12984 struct drm_crtc_state *new_state)
Damien Lespiau08db6652014-11-04 17:06:52 +000012985{
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012986 struct drm_device *dev = crtc->dev;
Damien Lespiau08db6652014-11-04 17:06:52 +000012987 struct drm_i915_private *dev_priv = dev->dev_private;
12988 struct skl_ddb_allocation hw_ddb, *sw_ddb;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012989 struct skl_ddb_entry *hw_entry, *sw_entry;
12990 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12991 const enum pipe pipe = intel_crtc->pipe;
Damien Lespiau08db6652014-11-04 17:06:52 +000012992 int plane;
12993
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012994 if (INTEL_INFO(dev)->gen < 9 || !new_state->active)
Damien Lespiau08db6652014-11-04 17:06:52 +000012995 return;
12996
12997 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12998 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12999
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013000 /* planes */
13001 for_each_plane(dev_priv, pipe, plane) {
13002 hw_entry = &hw_ddb.plane[pipe][plane];
13003 sw_entry = &sw_ddb->plane[pipe][plane];
Damien Lespiau08db6652014-11-04 17:06:52 +000013004
13005 if (skl_ddb_entry_equal(hw_entry, sw_entry))
13006 continue;
13007
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013008 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
13009 "(expected (%u,%u), found (%u,%u))\n",
13010 pipe_name(pipe), plane + 1,
13011 sw_entry->start, sw_entry->end,
13012 hw_entry->start, hw_entry->end);
13013 }
13014
13015 /* cursor */
13016 hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
13017 sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
13018
13019 if (!skl_ddb_entry_equal(hw_entry, sw_entry)) {
Damien Lespiau08db6652014-11-04 17:06:52 +000013020 DRM_ERROR("mismatch in DDB state pipe %c cursor "
13021 "(expected (%u,%u), found (%u,%u))\n",
13022 pipe_name(pipe),
13023 sw_entry->start, sw_entry->end,
13024 hw_entry->start, hw_entry->end);
13025 }
13026}
13027
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013028static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013029verify_connector_state(struct drm_device *dev, struct drm_crtc *crtc)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013030{
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020013031 struct drm_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013032
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013033 drm_for_each_connector(connector, dev) {
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020013034 struct drm_encoder *encoder = connector->encoder;
13035 struct drm_connector_state *state = connector->state;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020013036
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013037 if (state->crtc != crtc)
13038 continue;
13039
Daniel Vetter5a21b662016-05-24 17:13:53 +020013040 intel_connector_verify_state(to_intel_connector(connector));
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013041
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020013042 I915_STATE_WARN(state->best_encoder != encoder,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020013043 "connector's atomic encoder doesn't match legacy encoder\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013044 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013045}
13046
13047static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013048verify_encoder_state(struct drm_device *dev)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013049{
13050 struct intel_encoder *encoder;
13051 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013052
Damien Lespiaub2784e12014-08-05 11:29:37 +010013053 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013054 bool enabled = false;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013055 enum pipe pipe;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013056
13057 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
13058 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030013059 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013060
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020013061 for_each_intel_connector(dev, connector) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013062 if (connector->base.state->best_encoder != &encoder->base)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013063 continue;
13064 enabled = true;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020013065
13066 I915_STATE_WARN(connector->base.state->crtc !=
13067 encoder->base.crtc,
13068 "connector's crtc doesn't match encoder crtc\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013069 }
Dave Airlie0e32b392014-05-02 14:02:48 +100013070
Rob Clarke2c719b2014-12-15 13:56:32 -050013071 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013072 "encoder's enabled state mismatch "
13073 "(expected %i, found %i)\n",
13074 !!encoder->base.crtc, enabled);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020013075
13076 if (!encoder->base.crtc) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013077 bool active;
13078
13079 active = encoder->get_hw_state(encoder, &pipe);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020013080 I915_STATE_WARN(active,
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013081 "encoder detached but still enabled on pipe %c.\n",
13082 pipe_name(pipe));
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020013083 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013084 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013085}
13086
13087static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013088verify_crtc_state(struct drm_crtc *crtc,
13089 struct drm_crtc_state *old_crtc_state,
13090 struct drm_crtc_state *new_crtc_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013091{
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013092 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030013093 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013094 struct intel_encoder *encoder;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013095 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13096 struct intel_crtc_state *pipe_config, *sw_config;
13097 struct drm_atomic_state *old_state;
13098 bool active;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013099
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013100 old_state = old_crtc_state->state;
Daniel Vetterec2dc6a2016-05-09 16:34:09 +020013101 __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013102 pipe_config = to_intel_crtc_state(old_crtc_state);
13103 memset(pipe_config, 0, sizeof(*pipe_config));
13104 pipe_config->base.crtc = crtc;
13105 pipe_config->base.state = old_state;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013106
Ville Syrjälä78108b72016-05-27 20:59:19 +030013107 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013108
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013109 active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013110
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013111 /* hw state is inconsistent with the pipe quirk */
13112 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
13113 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
13114 active = new_crtc_state->active;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013115
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013116 I915_STATE_WARN(new_crtc_state->active != active,
13117 "crtc active state doesn't match with hw state "
13118 "(expected %i, found %i)\n", new_crtc_state->active, active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013119
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013120 I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
13121 "transitional active state does not match atomic hw state "
13122 "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013123
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013124 for_each_encoder_on_crtc(dev, crtc, encoder) {
13125 enum pipe pipe;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013126
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013127 active = encoder->get_hw_state(encoder, &pipe);
13128 I915_STATE_WARN(active != new_crtc_state->active,
13129 "[ENCODER:%i] active %i with crtc active %i\n",
13130 encoder->base.base.id, active, new_crtc_state->active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013131
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013132 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
13133 "Encoder connected to wrong pipe %c\n",
13134 pipe_name(pipe));
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013135
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013136 if (active)
13137 encoder->get_config(encoder, pipe_config);
13138 }
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013139
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013140 if (!new_crtc_state->active)
13141 return;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013142
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013143 intel_pipe_config_sanity_check(dev_priv, pipe_config);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013144
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013145 sw_config = to_intel_crtc_state(crtc->state);
13146 if (!intel_pipe_config_compare(dev, sw_config,
13147 pipe_config, false)) {
13148 I915_STATE_WARN(1, "pipe state doesn't match!\n");
13149 intel_dump_pipe_config(intel_crtc, pipe_config,
13150 "[hw state]");
13151 intel_dump_pipe_config(intel_crtc, sw_config,
13152 "[sw state]");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013153 }
13154}
13155
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013156static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013157verify_single_dpll_state(struct drm_i915_private *dev_priv,
13158 struct intel_shared_dpll *pll,
13159 struct drm_crtc *crtc,
13160 struct drm_crtc_state *new_state)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013161{
13162 struct intel_dpll_hw_state dpll_hw_state;
13163 unsigned crtc_mask;
13164 bool active;
13165
13166 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
13167
13168 DRM_DEBUG_KMS("%s\n", pll->name);
13169
13170 active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
13171
13172 if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
13173 I915_STATE_WARN(!pll->on && pll->active_mask,
13174 "pll in active use but not on in sw tracking\n");
13175 I915_STATE_WARN(pll->on && !pll->active_mask,
13176 "pll is on but not used by any active crtc\n");
13177 I915_STATE_WARN(pll->on != active,
13178 "pll on state mismatch (expected %i, found %i)\n",
13179 pll->on, active);
13180 }
13181
13182 if (!crtc) {
13183 I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
13184 "more active pll users than references: %x vs %x\n",
13185 pll->active_mask, pll->config.crtc_mask);
13186
13187 return;
13188 }
13189
13190 crtc_mask = 1 << drm_crtc_index(crtc);
13191
13192 if (new_state->active)
13193 I915_STATE_WARN(!(pll->active_mask & crtc_mask),
13194 "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
13195 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
13196 else
13197 I915_STATE_WARN(pll->active_mask & crtc_mask,
13198 "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
13199 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
13200
13201 I915_STATE_WARN(!(pll->config.crtc_mask & crtc_mask),
13202 "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
13203 crtc_mask, pll->config.crtc_mask);
13204
13205 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state,
13206 &dpll_hw_state,
13207 sizeof(dpll_hw_state)),
13208 "pll hw state mismatch\n");
13209}
13210
13211static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013212verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
13213 struct drm_crtc_state *old_crtc_state,
13214 struct drm_crtc_state *new_crtc_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013215{
Jani Nikulafbee40d2014-03-31 14:27:18 +030013216 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013217 struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
13218 struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
13219
13220 if (new_state->shared_dpll)
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013221 verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013222
13223 if (old_state->shared_dpll &&
13224 old_state->shared_dpll != new_state->shared_dpll) {
13225 unsigned crtc_mask = 1 << drm_crtc_index(crtc);
13226 struct intel_shared_dpll *pll = old_state->shared_dpll;
13227
13228 I915_STATE_WARN(pll->active_mask & crtc_mask,
13229 "pll active mismatch (didn't expect pipe %c in active mask)\n",
13230 pipe_name(drm_crtc_index(crtc)));
13231 I915_STATE_WARN(pll->config.crtc_mask & crtc_mask,
13232 "pll enabled crtcs mismatch (found %x in enabled mask)\n",
13233 pipe_name(drm_crtc_index(crtc)));
13234 }
13235}
13236
13237static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013238intel_modeset_verify_crtc(struct drm_crtc *crtc,
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013239 struct drm_crtc_state *old_state,
13240 struct drm_crtc_state *new_state)
13241{
Daniel Vetter5a21b662016-05-24 17:13:53 +020013242 if (!needs_modeset(new_state) &&
13243 !to_intel_crtc_state(new_state)->update_pipe)
13244 return;
13245
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013246 verify_wm_state(crtc, new_state);
Daniel Vetter5a21b662016-05-24 17:13:53 +020013247 verify_connector_state(crtc->dev, crtc);
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013248 verify_crtc_state(crtc, old_state, new_state);
13249 verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013250}
13251
13252static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013253verify_disabled_dpll_state(struct drm_device *dev)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013254{
13255 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013256 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020013257
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013258 for (i = 0; i < dev_priv->num_shared_dpll; i++)
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013259 verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013260}
Daniel Vetter53589012013-06-05 13:34:16 +020013261
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013262static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013263intel_modeset_verify_disabled(struct drm_device *dev)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013264{
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013265 verify_encoder_state(dev);
13266 verify_connector_state(dev, NULL);
13267 verify_disabled_dpll_state(dev);
Daniel Vettere2e1ed42012-07-08 21:14:38 +020013268}
13269
Ville Syrjälä80715b22014-05-15 20:23:23 +030013270static void update_scanline_offset(struct intel_crtc *crtc)
13271{
13272 struct drm_device *dev = crtc->base.dev;
13273
13274 /*
13275 * The scanline counter increments at the leading edge of hsync.
13276 *
13277 * On most platforms it starts counting from vtotal-1 on the
13278 * first active line. That means the scanline counter value is
13279 * always one less than what we would expect. Ie. just after
13280 * start of vblank, which also occurs at start of hsync (on the
13281 * last active line), the scanline counter will read vblank_start-1.
13282 *
13283 * On gen2 the scanline counter starts counting from 1 instead
13284 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
13285 * to keep the value positive), instead of adding one.
13286 *
13287 * On HSW+ the behaviour of the scanline counter depends on the output
13288 * type. For DP ports it behaves like most other platforms, but on HDMI
13289 * there's an extra 1 line difference. So we need to add two instead of
13290 * one to the value.
13291 */
13292 if (IS_GEN2(dev)) {
Ville Syrjälä124abe02015-09-08 13:40:45 +030013293 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030013294 int vtotal;
13295
Ville Syrjälä124abe02015-09-08 13:40:45 +030013296 vtotal = adjusted_mode->crtc_vtotal;
13297 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
Ville Syrjälä80715b22014-05-15 20:23:23 +030013298 vtotal /= 2;
13299
13300 crtc->scanline_offset = vtotal - 1;
13301 } else if (HAS_DDI(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +030013302 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030013303 crtc->scanline_offset = 2;
13304 } else
13305 crtc->scanline_offset = 1;
13306}
13307
Maarten Lankhorstad421372015-06-15 12:33:42 +020013308static void intel_modeset_clear_plls(struct drm_atomic_state *state)
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013309{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030013310 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013311 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstad421372015-06-15 12:33:42 +020013312 struct intel_shared_dpll_config *shared_dpll = NULL;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013313 struct drm_crtc *crtc;
13314 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013315 int i;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013316
13317 if (!dev_priv->display.crtc_compute_clock)
Maarten Lankhorstad421372015-06-15 12:33:42 +020013318 return;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013319
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013320 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013321 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013322 struct intel_shared_dpll *old_dpll =
13323 to_intel_crtc_state(crtc->state)->shared_dpll;
Maarten Lankhorstad421372015-06-15 12:33:42 +020013324
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013325 if (!needs_modeset(crtc_state))
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030013326 continue;
13327
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013328 to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013329
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013330 if (!old_dpll)
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013331 continue;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013332
Maarten Lankhorstad421372015-06-15 12:33:42 +020013333 if (!shared_dpll)
13334 shared_dpll = intel_atomic_get_shared_dpll_state(state);
13335
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013336 intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013337 }
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013338}
13339
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013340/*
13341 * This implements the workaround described in the "notes" section of the mode
13342 * set sequence documentation. When going from no pipes or single pipe to
13343 * multiple pipes, and planes are enabled after the pipe, we need to wait at
13344 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
13345 */
13346static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
13347{
13348 struct drm_crtc_state *crtc_state;
13349 struct intel_crtc *intel_crtc;
13350 struct drm_crtc *crtc;
13351 struct intel_crtc_state *first_crtc_state = NULL;
13352 struct intel_crtc_state *other_crtc_state = NULL;
13353 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
13354 int i;
13355
13356 /* look at all crtc's that are going to be enabled in during modeset */
13357 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13358 intel_crtc = to_intel_crtc(crtc);
13359
13360 if (!crtc_state->active || !needs_modeset(crtc_state))
13361 continue;
13362
13363 if (first_crtc_state) {
13364 other_crtc_state = to_intel_crtc_state(crtc_state);
13365 break;
13366 } else {
13367 first_crtc_state = to_intel_crtc_state(crtc_state);
13368 first_pipe = intel_crtc->pipe;
13369 }
13370 }
13371
13372 /* No workaround needed? */
13373 if (!first_crtc_state)
13374 return 0;
13375
13376 /* w/a possibly needed, check how many crtc's are already enabled. */
13377 for_each_intel_crtc(state->dev, intel_crtc) {
13378 struct intel_crtc_state *pipe_config;
13379
13380 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
13381 if (IS_ERR(pipe_config))
13382 return PTR_ERR(pipe_config);
13383
13384 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
13385
13386 if (!pipe_config->base.active ||
13387 needs_modeset(&pipe_config->base))
13388 continue;
13389
13390 /* 2 or more enabled crtcs means no need for w/a */
13391 if (enabled_pipe != INVALID_PIPE)
13392 return 0;
13393
13394 enabled_pipe = intel_crtc->pipe;
13395 }
13396
13397 if (enabled_pipe != INVALID_PIPE)
13398 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
13399 else if (other_crtc_state)
13400 other_crtc_state->hsw_workaround_pipe = first_pipe;
13401
13402 return 0;
13403}
13404
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013405static int intel_modeset_all_pipes(struct drm_atomic_state *state)
13406{
13407 struct drm_crtc *crtc;
13408 struct drm_crtc_state *crtc_state;
13409 int ret = 0;
13410
13411 /* add all active pipes to the state */
13412 for_each_crtc(state->dev, crtc) {
13413 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13414 if (IS_ERR(crtc_state))
13415 return PTR_ERR(crtc_state);
13416
13417 if (!crtc_state->active || needs_modeset(crtc_state))
13418 continue;
13419
13420 crtc_state->mode_changed = true;
13421
13422 ret = drm_atomic_add_affected_connectors(state, crtc);
13423 if (ret)
13424 break;
13425
13426 ret = drm_atomic_add_affected_planes(state, crtc);
13427 if (ret)
13428 break;
13429 }
13430
13431 return ret;
13432}
13433
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013434static int intel_modeset_checks(struct drm_atomic_state *state)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013435{
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013436 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
13437 struct drm_i915_private *dev_priv = state->dev->dev_private;
13438 struct drm_crtc *crtc;
13439 struct drm_crtc_state *crtc_state;
13440 int ret = 0, i;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013441
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013442 if (!check_digital_port_conflicts(state)) {
13443 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
13444 return -EINVAL;
13445 }
13446
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013447 intel_state->modeset = true;
13448 intel_state->active_crtcs = dev_priv->active_crtcs;
13449
13450 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13451 if (crtc_state->active)
13452 intel_state->active_crtcs |= 1 << i;
13453 else
13454 intel_state->active_crtcs &= ~(1 << i);
Matt Roper8b4a7d02016-05-12 07:06:00 -070013455
13456 if (crtc_state->active != crtc->state->active)
13457 intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013458 }
13459
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013460 /*
13461 * See if the config requires any additional preparation, e.g.
13462 * to adjust global state with pipes off. We need to do this
13463 * here so we can get the modeset_pipe updated config for the new
13464 * mode set on this crtc. For other crtcs we need to use the
13465 * adjusted_mode bits in the crtc directly.
13466 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013467 if (dev_priv->display.modeset_calc_cdclk) {
Clint Taylorc89e39f2016-05-13 23:41:21 +030013468 if (!intel_state->cdclk_pll_vco)
Ville Syrjälä63911d72016-05-13 23:41:32 +030013469 intel_state->cdclk_pll_vco = dev_priv->cdclk_pll.vco;
Ville Syrjäläb2045352016-05-13 23:41:27 +030013470 if (!intel_state->cdclk_pll_vco)
13471 intel_state->cdclk_pll_vco = dev_priv->skl_preferred_vco_freq;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013472
Clint Taylorc89e39f2016-05-13 23:41:21 +030013473 ret = dev_priv->display.modeset_calc_cdclk(state);
13474 if (ret < 0)
13475 return ret;
13476
13477 if (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
Ville Syrjälä63911d72016-05-13 23:41:32 +030013478 intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco)
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013479 ret = intel_modeset_all_pipes(state);
13480
13481 if (ret < 0)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013482 return ret;
Maarten Lankhorste8788cb2016-02-16 10:25:11 +010013483
13484 DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
13485 intel_state->cdclk, intel_state->dev_cdclk);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013486 } else
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013487 to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013488
Maarten Lankhorstad421372015-06-15 12:33:42 +020013489 intel_modeset_clear_plls(state);
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013490
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013491 if (IS_HASWELL(dev_priv))
Maarten Lankhorstad421372015-06-15 12:33:42 +020013492 return haswell_mode_set_planes_workaround(state);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013493
Maarten Lankhorstad421372015-06-15 12:33:42 +020013494 return 0;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013495}
13496
Matt Roperaa363132015-09-24 15:53:18 -070013497/*
13498 * Handle calculation of various watermark data at the end of the atomic check
13499 * phase. The code here should be run after the per-crtc and per-plane 'check'
13500 * handlers to ensure that all derived state has been updated.
13501 */
Matt Roper55994c22016-05-12 07:06:08 -070013502static int calc_watermark_data(struct drm_atomic_state *state)
Matt Roperaa363132015-09-24 15:53:18 -070013503{
13504 struct drm_device *dev = state->dev;
Matt Roper98d39492016-05-12 07:06:03 -070013505 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roper98d39492016-05-12 07:06:03 -070013506
13507 /* Is there platform-specific watermark information to calculate? */
13508 if (dev_priv->display.compute_global_watermarks)
Matt Roper55994c22016-05-12 07:06:08 -070013509 return dev_priv->display.compute_global_watermarks(state);
13510
13511 return 0;
Matt Roperaa363132015-09-24 15:53:18 -070013512}
13513
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013514/**
13515 * intel_atomic_check - validate state object
13516 * @dev: drm device
13517 * @state: state to validate
13518 */
13519static int intel_atomic_check(struct drm_device *dev,
13520 struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020013521{
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013522 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roperaa363132015-09-24 15:53:18 -070013523 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013524 struct drm_crtc *crtc;
13525 struct drm_crtc_state *crtc_state;
13526 int ret, i;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013527 bool any_ms = false;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013528
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013529 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020013530 if (ret)
13531 return ret;
13532
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013533 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013534 struct intel_crtc_state *pipe_config =
13535 to_intel_crtc_state(crtc_state);
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013536
13537 /* Catch I915_MODE_FLAG_INHERITED */
13538 if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
13539 crtc_state->mode_changed = true;
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013540
Daniel Vetter26495482015-07-15 14:15:52 +020013541 if (!needs_modeset(crtc_state))
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013542 continue;
13543
Daniel Vetteraf4a8792016-05-09 09:31:25 +020013544 if (!crtc_state->enable) {
13545 any_ms = true;
13546 continue;
13547 }
13548
Daniel Vetter26495482015-07-15 14:15:52 +020013549 /* FIXME: For only active_changed we shouldn't need to do any
13550 * state recomputation at all. */
13551
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013552 ret = drm_atomic_add_affected_connectors(state, crtc);
13553 if (ret)
13554 return ret;
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013555
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013556 ret = intel_modeset_pipe_config(crtc, pipe_config);
Maarten Lankhorst25aa1c32016-05-03 10:30:38 +020013557 if (ret) {
13558 intel_dump_pipe_config(to_intel_crtc(crtc),
13559 pipe_config, "[failed]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013560 return ret;
Maarten Lankhorst25aa1c32016-05-03 10:30:38 +020013561 }
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013562
Jani Nikula73831232015-11-19 10:26:30 +020013563 if (i915.fastboot &&
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013564 intel_pipe_config_compare(dev,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013565 to_intel_crtc_state(crtc->state),
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013566 pipe_config, true)) {
Daniel Vetter26495482015-07-15 14:15:52 +020013567 crtc_state->mode_changed = false;
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013568 to_intel_crtc_state(crtc_state)->update_pipe = true;
Daniel Vetter26495482015-07-15 14:15:52 +020013569 }
13570
Daniel Vetteraf4a8792016-05-09 09:31:25 +020013571 if (needs_modeset(crtc_state))
Daniel Vetter26495482015-07-15 14:15:52 +020013572 any_ms = true;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013573
Daniel Vetteraf4a8792016-05-09 09:31:25 +020013574 ret = drm_atomic_add_affected_planes(state, crtc);
13575 if (ret)
13576 return ret;
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013577
Daniel Vetter26495482015-07-15 14:15:52 +020013578 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
13579 needs_modeset(crtc_state) ?
13580 "[modeset]" : "[fastset]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013581 }
13582
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013583 if (any_ms) {
13584 ret = intel_modeset_checks(state);
13585
13586 if (ret)
13587 return ret;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013588 } else
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013589 intel_state->cdclk = dev_priv->cdclk_freq;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013590
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013591 ret = drm_atomic_helper_check_planes(dev, state);
Matt Roperaa363132015-09-24 15:53:18 -070013592 if (ret)
13593 return ret;
13594
Paulo Zanonif51be2e2016-01-19 11:35:50 -020013595 intel_fbc_choose_crtc(dev_priv, state);
Matt Roper55994c22016-05-12 07:06:08 -070013596 return calc_watermark_data(state);
Daniel Vettera6778b32012-07-02 09:56:42 +020013597}
13598
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013599static int intel_atomic_prepare_commit(struct drm_device *dev,
13600 struct drm_atomic_state *state,
Maarten Lankhorst81072bf2016-04-26 16:11:45 +020013601 bool nonblock)
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013602{
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013603 struct drm_i915_private *dev_priv = dev->dev_private;
13604 struct drm_plane_state *plane_state;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013605 struct drm_crtc_state *crtc_state;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013606 struct drm_plane *plane;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013607 struct drm_crtc *crtc;
13608 int i, ret;
13609
Daniel Vetter5a21b662016-05-24 17:13:53 +020013610 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13611 if (state->legacy_cursor_update)
13612 continue;
13613
13614 ret = intel_crtc_wait_for_pending_flips(crtc);
13615 if (ret)
13616 return ret;
13617
13618 if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
13619 flush_workqueue(dev_priv->wq);
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020013620 }
13621
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013622 ret = mutex_lock_interruptible(&dev->struct_mutex);
13623 if (ret)
13624 return ret;
13625
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013626 ret = drm_atomic_helper_prepare_planes(dev, state);
Chris Wilsonf7e58382016-04-13 17:35:07 +010013627 mutex_unlock(&dev->struct_mutex);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013628
Dave Airlie21daaee2016-05-05 09:56:30 +100013629 if (!ret && !nonblock) {
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013630 for_each_plane_in_state(state, plane, plane_state, i) {
13631 struct intel_plane_state *intel_plane_state =
13632 to_intel_plane_state(plane_state);
13633
13634 if (!intel_plane_state->wait_req)
13635 continue;
13636
13637 ret = __i915_wait_request(intel_plane_state->wait_req,
Chris Wilson299259a2016-04-13 17:35:06 +010013638 true, NULL, NULL);
Chris Wilsonf7e58382016-04-13 17:35:07 +010013639 if (ret) {
Chris Wilsonf4457ae2016-04-13 17:35:08 +010013640 /* Any hang should be swallowed by the wait */
13641 WARN_ON(ret == -EIO);
Chris Wilsonf7e58382016-04-13 17:35:07 +010013642 mutex_lock(&dev->struct_mutex);
13643 drm_atomic_helper_cleanup_planes(dev, state);
13644 mutex_unlock(&dev->struct_mutex);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013645 break;
Chris Wilsonf7e58382016-04-13 17:35:07 +010013646 }
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013647 }
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013648 }
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013649
13650 return ret;
13651}
13652
Maarten Lankhorsta2991412016-05-17 15:07:48 +020013653u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
13654{
13655 struct drm_device *dev = crtc->base.dev;
13656
13657 if (!dev->max_vblank_count)
13658 return drm_accurate_vblank_count(&crtc->base);
13659
13660 return dev->driver->get_vblank_counter(dev, crtc->pipe);
13661}
13662
Daniel Vetter5a21b662016-05-24 17:13:53 +020013663static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
13664 struct drm_i915_private *dev_priv,
13665 unsigned crtc_mask)
Maarten Lankhorste8861672016-02-24 11:24:26 +010013666{
Daniel Vetter5a21b662016-05-24 17:13:53 +020013667 unsigned last_vblank_count[I915_MAX_PIPES];
13668 enum pipe pipe;
13669 int ret;
Maarten Lankhorste8861672016-02-24 11:24:26 +010013670
Daniel Vetter5a21b662016-05-24 17:13:53 +020013671 if (!crtc_mask)
13672 return;
Maarten Lankhorste8861672016-02-24 11:24:26 +010013673
Daniel Vetter5a21b662016-05-24 17:13:53 +020013674 for_each_pipe(dev_priv, pipe) {
13675 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Maarten Lankhorste8861672016-02-24 11:24:26 +010013676
Daniel Vetter5a21b662016-05-24 17:13:53 +020013677 if (!((1 << pipe) & crtc_mask))
Maarten Lankhorste8861672016-02-24 11:24:26 +010013678 continue;
13679
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020013680 ret = drm_crtc_vblank_get(crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +020013681 if (WARN_ON(ret != 0)) {
13682 crtc_mask &= ~(1 << pipe);
13683 continue;
13684 }
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020013685
Daniel Vetter5a21b662016-05-24 17:13:53 +020013686 last_vblank_count[pipe] = drm_crtc_vblank_count(crtc);
13687 }
13688
13689 for_each_pipe(dev_priv, pipe) {
13690 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13691 long lret;
13692
13693 if (!((1 << pipe) & crtc_mask))
13694 continue;
13695
13696 lret = wait_event_timeout(dev->vblank[pipe].queue,
13697 last_vblank_count[pipe] !=
13698 drm_crtc_vblank_count(crtc),
13699 msecs_to_jiffies(50));
13700
13701 WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
13702
13703 drm_crtc_vblank_put(crtc);
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020013704 }
13705}
13706
Daniel Vetter5a21b662016-05-24 17:13:53 +020013707static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013708{
Daniel Vetter5a21b662016-05-24 17:13:53 +020013709 /* fb updated, need to unpin old fb */
13710 if (crtc_state->fb_changed)
13711 return true;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013712
Daniel Vetter5a21b662016-05-24 17:13:53 +020013713 /* wm changes, need vblank before final wm's */
13714 if (crtc_state->update_wm_post)
13715 return true;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013716
Daniel Vetter5a21b662016-05-24 17:13:53 +020013717 /*
13718 * cxsr is re-enabled after vblank.
13719 * This is already handled by crtc_state->update_wm_post,
13720 * but added for clarity.
13721 */
13722 if (crtc_state->disable_cxsr)
13723 return true;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013724
Daniel Vetter5a21b662016-05-24 17:13:53 +020013725 return false;
Maarten Lankhorste8861672016-02-24 11:24:26 +010013726}
13727
Daniel Vetter94f05022016-06-14 18:01:00 +020013728static void intel_atomic_commit_tail(struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020013729{
Daniel Vetter94f05022016-06-14 18:01:00 +020013730 struct drm_device *dev = state->dev;
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013731 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Jani Nikulafbee40d2014-03-31 14:27:18 +030013732 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013733 struct drm_crtc_state *old_crtc_state;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013734 struct drm_crtc *crtc;
Daniel Vetter5a21b662016-05-24 17:13:53 +020013735 struct intel_crtc_state *intel_cstate;
Daniel Vetter94f05022016-06-14 18:01:00 +020013736 struct drm_plane *plane;
13737 struct drm_plane_state *plane_state;
Daniel Vetter5a21b662016-05-24 17:13:53 +020013738 bool hw_check = intel_state->modeset;
13739 unsigned long put_domains[I915_MAX_PIPES] = {};
13740 unsigned crtc_vblank_mask = 0;
Daniel Vetter94f05022016-06-14 18:01:00 +020013741 int i, ret;
Daniel Vettera6778b32012-07-02 09:56:42 +020013742
Daniel Vetter94f05022016-06-14 18:01:00 +020013743 for_each_plane_in_state(state, plane, plane_state, i) {
13744 struct intel_plane_state *intel_plane_state =
13745 to_intel_plane_state(plane_state);
Daniel Vetterea0000f2016-06-13 16:13:46 +020013746
Daniel Vetter94f05022016-06-14 18:01:00 +020013747 if (!intel_plane_state->wait_req)
13748 continue;
13749
13750 ret = __i915_wait_request(intel_plane_state->wait_req,
13751 true, NULL, NULL);
13752 /* EIO should be eaten, and we can't get interrupted in the
13753 * worker, and blocking commits have waited already. */
13754 WARN_ON(ret);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013755 }
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013756
Daniel Vetterea0000f2016-06-13 16:13:46 +020013757 drm_atomic_helper_wait_for_dependencies(state);
13758
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013759 if (intel_state->modeset) {
13760 memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
13761 sizeof(intel_state->min_pixclk));
13762 dev_priv->active_crtcs = intel_state->active_crtcs;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013763 dev_priv->atomic_cdclk_freq = intel_state->cdclk;
Daniel Vetter5a21b662016-05-24 17:13:53 +020013764
13765 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013766 }
13767
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013768 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013769 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13770
Daniel Vetter5a21b662016-05-24 17:13:53 +020013771 if (needs_modeset(crtc->state) ||
13772 to_intel_crtc_state(crtc->state)->update_pipe) {
13773 hw_check = true;
13774
13775 put_domains[to_intel_crtc(crtc)->pipe] =
13776 modeset_get_crtc_power_domains(crtc,
13777 to_intel_crtc_state(crtc->state));
13778 }
13779
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013780 if (!needs_modeset(crtc->state))
13781 continue;
13782
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013783 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
Daniel Vetter460da9162013-03-27 00:44:51 +010013784
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013785 if (old_crtc_state->active) {
13786 intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013787 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013788 intel_crtc->active = false;
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -020013789 intel_fbc_disable(intel_crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013790 intel_disable_shared_dpll(intel_crtc);
Ville Syrjälä9bbc8258a2015-11-20 22:09:20 +020013791
13792 /*
13793 * Underruns don't always raise
13794 * interrupts, so check manually.
13795 */
13796 intel_check_cpu_fifo_underruns(dev_priv);
13797 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorstb9001112015-11-19 16:07:16 +010013798
13799 if (!crtc->state->active)
13800 intel_update_watermarks(crtc);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013801 }
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010013802 }
Daniel Vetter7758a112012-07-08 19:40:39 +020013803
Daniel Vetterea9d7582012-07-10 10:42:52 +020013804 /* Only after disabling all output pipelines that will be changed can we
13805 * update the the output configuration. */
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013806 intel_modeset_update_crtc_state(state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020013807
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013808 if (intel_state->modeset) {
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013809 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013810
13811 if (dev_priv->display.modeset_commit_cdclk &&
Clint Taylorc89e39f2016-05-13 23:41:21 +030013812 (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
Ville Syrjälä63911d72016-05-13 23:41:32 +030013813 intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco))
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013814 dev_priv->display.modeset_commit_cdclk(state);
Maarten Lankhorstf6d19732016-03-23 14:58:07 +010013815
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013816 intel_modeset_verify_disabled(dev);
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013817 }
Daniel Vetter47fab732012-10-26 10:58:18 +020013818
Daniel Vettera6778b32012-07-02 09:56:42 +020013819 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013820 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013821 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13822 bool modeset = needs_modeset(crtc->state);
Daniel Vetter5a21b662016-05-24 17:13:53 +020013823 struct intel_crtc_state *pipe_config =
13824 to_intel_crtc_state(crtc->state);
Patrik Jakobsson9f836f92015-11-16 16:20:01 +010013825
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013826 if (modeset && crtc->state->active) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013827 update_scanline_offset(to_intel_crtc(crtc));
13828 dev_priv->display.crtc_enable(crtc);
13829 }
13830
Daniel Vetter1f7528c2016-06-13 16:13:45 +020013831 /* Complete events for now disable pipes here. */
13832 if (modeset && !crtc->state->active && crtc->state->event) {
13833 spin_lock_irq(&dev->event_lock);
13834 drm_crtc_send_vblank_event(crtc, crtc->state->event);
13835 spin_unlock_irq(&dev->event_lock);
13836
13837 crtc->state->event = NULL;
13838 }
13839
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013840 if (!modeset)
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013841 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013842
Daniel Vetter5a21b662016-05-24 17:13:53 +020013843 if (crtc->state->active &&
13844 drm_atomic_get_existing_plane_state(state, crtc->primary))
Maarten Lankhorstfaf68d92016-06-14 14:24:20 +020013845 intel_fbc_enable(intel_crtc, pipe_config, to_intel_plane_state(crtc->primary->state));
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013846
Daniel Vetter1f7528c2016-06-13 16:13:45 +020013847 if (crtc->state->active)
Daniel Vetter5a21b662016-05-24 17:13:53 +020013848 drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013849
Daniel Vetter5a21b662016-05-24 17:13:53 +020013850 if (pipe_config->base.active && needs_vblank_wait(pipe_config))
13851 crtc_vblank_mask |= 1 << i;
Matt Ropered4a6a72016-02-23 17:20:13 -080013852 }
13853
Daniel Vetter94f05022016-06-14 18:01:00 +020013854 /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
13855 * already, but still need the state for the delayed optimization. To
13856 * fix this:
13857 * - wrap the optimization/post_plane_update stuff into a per-crtc work.
13858 * - schedule that vblank worker _before_ calling hw_done
13859 * - at the start of commit_tail, cancel it _synchrously
13860 * - switch over to the vblank wait helper in the core after that since
13861 * we don't need out special handling any more.
13862 */
Daniel Vetter5a21b662016-05-24 17:13:53 +020013863 if (!state->legacy_cursor_update)
13864 intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
13865
13866 /*
13867 * Now that the vblank has passed, we can go ahead and program the
13868 * optimal watermarks on platforms that need two-step watermark
13869 * programming.
13870 *
13871 * TODO: Move this (and other cleanup) to an async worker eventually.
13872 */
13873 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
13874 intel_cstate = to_intel_crtc_state(crtc->state);
13875
13876 if (dev_priv->display.optimize_watermarks)
13877 dev_priv->display.optimize_watermarks(intel_cstate);
13878 }
13879
13880 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
13881 intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
13882
13883 if (put_domains[i])
13884 modeset_put_power_domains(dev_priv, put_domains[i]);
13885
13886 intel_modeset_verify_crtc(crtc, old_crtc_state, crtc->state);
13887 }
13888
Daniel Vetter94f05022016-06-14 18:01:00 +020013889 drm_atomic_helper_commit_hw_done(state);
13890
Daniel Vetter5a21b662016-05-24 17:13:53 +020013891 if (intel_state->modeset)
13892 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
13893
13894 mutex_lock(&dev->struct_mutex);
13895 drm_atomic_helper_cleanup_planes(dev, state);
13896 mutex_unlock(&dev->struct_mutex);
13897
Daniel Vetterea0000f2016-06-13 16:13:46 +020013898 drm_atomic_helper_commit_cleanup_done(state);
13899
Maarten Lankhorstee165b12015-08-05 12:37:00 +020013900 drm_atomic_state_free(state);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013901
Mika Kuoppala75714942015-12-16 09:26:48 +020013902 /* As one of the primary mmio accessors, KMS has a high likelihood
13903 * of triggering bugs in unclaimed access. After we finish
13904 * modesetting, see if an error has been flagged, and if so
13905 * enable debugging for the next modeset - and hope we catch
13906 * the culprit.
13907 *
13908 * XXX note that we assume display power is on at this point.
13909 * This might hold true now but we need to add pm helper to check
13910 * unclaimed only when the hardware is on, as atomic commits
13911 * can happen also when the device is completely off.
13912 */
13913 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
Daniel Vetter94f05022016-06-14 18:01:00 +020013914}
13915
13916static void intel_atomic_commit_work(struct work_struct *work)
13917{
13918 struct drm_atomic_state *state = container_of(work,
13919 struct drm_atomic_state,
13920 commit_work);
13921 intel_atomic_commit_tail(state);
13922}
13923
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020013924static void intel_atomic_track_fbs(struct drm_atomic_state *state)
13925{
13926 struct drm_plane_state *old_plane_state;
13927 struct drm_plane *plane;
13928 struct drm_i915_gem_object *obj, *old_obj;
13929 struct intel_plane *intel_plane;
13930 int i;
13931
13932 mutex_lock(&state->dev->struct_mutex);
13933 for_each_plane_in_state(state, plane, old_plane_state, i) {
13934 obj = intel_fb_obj(plane->state->fb);
13935 old_obj = intel_fb_obj(old_plane_state->fb);
13936 intel_plane = to_intel_plane(plane);
13937
13938 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
13939 }
13940 mutex_unlock(&state->dev->struct_mutex);
13941}
13942
Daniel Vetter94f05022016-06-14 18:01:00 +020013943/**
13944 * intel_atomic_commit - commit validated state object
13945 * @dev: DRM device
13946 * @state: the top-level driver state object
13947 * @nonblock: nonblocking commit
13948 *
13949 * This function commits a top-level state object that has been validated
13950 * with drm_atomic_helper_check().
13951 *
13952 * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
13953 * nonblocking commits are only safe for pure plane updates. Everything else
13954 * should work though.
13955 *
13956 * RETURNS
13957 * Zero for success or -errno.
13958 */
13959static int intel_atomic_commit(struct drm_device *dev,
13960 struct drm_atomic_state *state,
13961 bool nonblock)
13962{
13963 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
13964 struct drm_i915_private *dev_priv = dev->dev_private;
13965 int ret = 0;
13966
13967 if (intel_state->modeset && nonblock) {
13968 DRM_DEBUG_KMS("nonblocking commit for modeset not yet implemented.\n");
13969 return -EINVAL;
13970 }
13971
13972 ret = drm_atomic_helper_setup_commit(state, nonblock);
13973 if (ret)
13974 return ret;
13975
13976 INIT_WORK(&state->commit_work, intel_atomic_commit_work);
13977
13978 ret = intel_atomic_prepare_commit(dev, state, nonblock);
13979 if (ret) {
13980 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
13981 return ret;
13982 }
13983
13984 drm_atomic_helper_swap_state(state, true);
13985 dev_priv->wm.distrust_bios_wm = false;
13986 dev_priv->wm.skl_results = intel_state->wm_results;
13987 intel_shared_dpll_commit(state);
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020013988 intel_atomic_track_fbs(state);
Daniel Vetter94f05022016-06-14 18:01:00 +020013989
13990 if (nonblock)
13991 queue_work(system_unbound_wq, &state->commit_work);
13992 else
13993 intel_atomic_commit_tail(state);
Mika Kuoppala75714942015-12-16 09:26:48 +020013994
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013995 return 0;
Daniel Vetterf30da182013-04-11 20:22:50 +020013996}
13997
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013998void intel_crtc_restore_mode(struct drm_crtc *crtc)
13999{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020014000 struct drm_device *dev = crtc->dev;
14001 struct drm_atomic_state *state;
Maarten Lankhorste694eb02015-07-14 16:19:12 +020014002 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030014003 int ret;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020014004
14005 state = drm_atomic_state_alloc(dev);
14006 if (!state) {
Ville Syrjälä78108b72016-05-27 20:59:19 +030014007 DRM_DEBUG_KMS("[CRTC:%d:%s] crtc restore failed, out of memory",
14008 crtc->base.id, crtc->name);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020014009 return;
14010 }
14011
Maarten Lankhorste694eb02015-07-14 16:19:12 +020014012 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020014013
Maarten Lankhorste694eb02015-07-14 16:19:12 +020014014retry:
14015 crtc_state = drm_atomic_get_crtc_state(state, crtc);
14016 ret = PTR_ERR_OR_ZERO(crtc_state);
14017 if (!ret) {
14018 if (!crtc_state->active)
14019 goto out;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020014020
Maarten Lankhorste694eb02015-07-14 16:19:12 +020014021 crtc_state->mode_changed = true;
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020014022 ret = drm_atomic_commit(state);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020014023 }
14024
Maarten Lankhorste694eb02015-07-14 16:19:12 +020014025 if (ret == -EDEADLK) {
14026 drm_atomic_state_clear(state);
14027 drm_modeset_backoff(state->acquire_ctx);
14028 goto retry;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030014029 }
14030
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030014031 if (ret)
Maarten Lankhorste694eb02015-07-14 16:19:12 +020014032out:
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030014033 drm_atomic_state_free(state);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000014034}
14035
Daniel Vetter25c5b262012-07-08 22:08:04 +020014036#undef for_each_intel_crtc_masked
14037
Chris Wilsonf6e5b162011-04-12 18:06:51 +010014038static const struct drm_crtc_funcs intel_crtc_funcs = {
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000014039 .gamma_set = drm_atomic_helper_legacy_gamma_set,
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020014040 .set_config = drm_atomic_helper_set_config,
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000014041 .set_property = drm_atomic_helper_crtc_set_property,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010014042 .destroy = intel_crtc_destroy,
Chris Wilson527b6ab2016-06-24 13:44:03 +010014043 .page_flip = intel_crtc_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080014044 .atomic_duplicate_state = intel_crtc_duplicate_state,
14045 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010014046};
14047
Matt Roper6beb8c232014-12-01 15:40:14 -080014048/**
14049 * intel_prepare_plane_fb - Prepare fb for usage on plane
14050 * @plane: drm plane to prepare for
14051 * @fb: framebuffer to prepare for presentation
14052 *
14053 * Prepares a framebuffer for usage on a display plane. Generally this
14054 * involves pinning the underlying object and updating the frontbuffer tracking
14055 * bits. Some older platforms need special physical address handling for
14056 * cursor planes.
14057 *
Maarten Lankhorstf9356752015-08-18 13:40:05 +020014058 * Must be called with struct_mutex held.
14059 *
Matt Roper6beb8c232014-12-01 15:40:14 -080014060 * Returns 0 on success, negative error code on failure.
14061 */
14062int
14063intel_prepare_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000014064 const struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070014065{
14066 struct drm_device *dev = plane->dev;
Maarten Lankhorst844f9112015-09-02 10:42:40 +020014067 struct drm_framebuffer *fb = new_state->fb;
Matt Roper6beb8c232014-12-01 15:40:14 -080014068 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014069 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
Chris Wilsonc37efb92016-06-17 08:28:47 +010014070 struct reservation_object *resv;
Matt Roper6beb8c232014-12-01 15:40:14 -080014071 int ret = 0;
Matt Roper465c1202014-05-29 08:06:54 -070014072
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014073 if (!obj && !old_obj)
Matt Roper465c1202014-05-29 08:06:54 -070014074 return 0;
14075
Maarten Lankhorst5008e872015-08-18 13:40:05 +020014076 if (old_obj) {
14077 struct drm_crtc_state *crtc_state =
14078 drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
14079
14080 /* Big Hammer, we also need to ensure that any pending
14081 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
14082 * current scanout is retired before unpinning the old
14083 * framebuffer. Note that we rely on userspace rendering
14084 * into the buffer attached to the pipe they are waiting
14085 * on. If not, userspace generates a GPU hang with IPEHR
14086 * point to the MI_WAIT_FOR_EVENT.
14087 *
14088 * This should only fail upon a hung GPU, in which case we
14089 * can safely continue.
14090 */
14091 if (needs_modeset(crtc_state))
14092 ret = i915_gem_object_wait_rendering(old_obj, true);
Chris Wilsonf4457ae2016-04-13 17:35:08 +010014093 if (ret) {
14094 /* GPU hangs should have been swallowed by the wait */
14095 WARN_ON(ret == -EIO);
Maarten Lankhorstf9356752015-08-18 13:40:05 +020014096 return ret;
Chris Wilsonf4457ae2016-04-13 17:35:08 +010014097 }
Maarten Lankhorst5008e872015-08-18 13:40:05 +020014098 }
14099
Chris Wilsonc37efb92016-06-17 08:28:47 +010014100 if (!obj)
14101 return 0;
14102
Daniel Vetter5a21b662016-05-24 17:13:53 +020014103 /* For framebuffer backed by dmabuf, wait for fence */
Chris Wilsonc37efb92016-06-17 08:28:47 +010014104 resv = i915_gem_object_get_dmabuf_resv(obj);
14105 if (resv) {
Daniel Vetter5a21b662016-05-24 17:13:53 +020014106 long lret;
14107
Chris Wilsonc37efb92016-06-17 08:28:47 +010014108 lret = reservation_object_wait_timeout_rcu(resv, false, true,
Daniel Vetter5a21b662016-05-24 17:13:53 +020014109 MAX_SCHEDULE_TIMEOUT);
14110 if (lret == -ERESTARTSYS)
14111 return lret;
14112
14113 WARN(lret < 0, "waiting returns %li\n", lret);
14114 }
14115
Chris Wilsonc37efb92016-06-17 08:28:47 +010014116 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
Matt Roper6beb8c232014-12-01 15:40:14 -080014117 INTEL_INFO(dev)->cursor_needs_physical) {
14118 int align = IS_I830(dev) ? 16 * 1024 : 256;
14119 ret = i915_gem_object_attach_phys(obj, align);
14120 if (ret)
14121 DRM_DEBUG_KMS("failed to attach phys object\n");
14122 } else {
Ville Syrjälä3465c582016-02-15 22:54:43 +020014123 ret = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
Matt Roper6beb8c232014-12-01 15:40:14 -080014124 }
14125
Chris Wilsonc37efb92016-06-17 08:28:47 +010014126 if (ret == 0) {
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020014127 struct intel_plane_state *plane_state =
14128 to_intel_plane_state(new_state);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020014129
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020014130 i915_gem_request_assign(&plane_state->wait_req,
14131 obj->last_write_req);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020014132 }
Matt Roper6beb8c232014-12-01 15:40:14 -080014133
Matt Roper6beb8c232014-12-01 15:40:14 -080014134 return ret;
14135}
14136
Matt Roper38f3ce32014-12-02 07:45:25 -080014137/**
14138 * intel_cleanup_plane_fb - Cleans up an fb after plane use
14139 * @plane: drm plane to clean up for
14140 * @fb: old framebuffer that was on plane
14141 *
14142 * Cleans up a framebuffer that has just been removed from a plane.
Maarten Lankhorstf9356752015-08-18 13:40:05 +020014143 *
14144 * Must be called with struct_mutex held.
Matt Roper38f3ce32014-12-02 07:45:25 -080014145 */
14146void
14147intel_cleanup_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000014148 const struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080014149{
14150 struct drm_device *dev = plane->dev;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020014151 struct intel_plane_state *old_intel_state;
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014152 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
14153 struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
Matt Roper38f3ce32014-12-02 07:45:25 -080014154
Maarten Lankhorst7580d772015-08-18 13:40:06 +020014155 old_intel_state = to_intel_plane_state(old_state);
14156
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014157 if (!obj && !old_obj)
Matt Roper38f3ce32014-12-02 07:45:25 -080014158 return;
14159
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014160 if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
14161 !INTEL_INFO(dev)->cursor_needs_physical))
Ville Syrjälä3465c582016-02-15 22:54:43 +020014162 intel_unpin_fb_obj(old_state->fb, old_state->rotation);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014163
Maarten Lankhorst7580d772015-08-18 13:40:06 +020014164 i915_gem_request_assign(&old_intel_state->wait_req, NULL);
Matt Roper465c1202014-05-29 08:06:54 -070014165}
14166
Chandra Konduru6156a452015-04-27 13:48:39 -070014167int
14168skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
14169{
14170 int max_scale;
14171 struct drm_device *dev;
14172 struct drm_i915_private *dev_priv;
14173 int crtc_clock, cdclk;
14174
Maarten Lankhorstbf8a0af2015-11-24 11:29:02 +010014175 if (!intel_crtc || !crtc_state->base.enable)
Chandra Konduru6156a452015-04-27 13:48:39 -070014176 return DRM_PLANE_HELPER_NO_SCALING;
14177
14178 dev = intel_crtc->base.dev;
14179 dev_priv = dev->dev_private;
14180 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014181 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
Chandra Konduru6156a452015-04-27 13:48:39 -070014182
Tvrtko Ursulin54bf1ce2015-10-20 17:17:07 +010014183 if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
Chandra Konduru6156a452015-04-27 13:48:39 -070014184 return DRM_PLANE_HELPER_NO_SCALING;
14185
14186 /*
14187 * skl max scale is lower of:
14188 * close to 3 but not 3, -1 is for that purpose
14189 * or
14190 * cdclk/crtc_clock
14191 */
14192 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
14193
14194 return max_scale;
14195}
14196
Matt Roper465c1202014-05-29 08:06:54 -070014197static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030014198intel_check_primary_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014199 struct intel_crtc_state *crtc_state,
Gustavo Padovan3c692a42014-09-05 17:04:49 -030014200 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070014201{
Matt Roper2b875c22014-12-01 15:40:13 -080014202 struct drm_crtc *crtc = state->base.crtc;
14203 struct drm_framebuffer *fb = state->base.fb;
Chandra Konduru6156a452015-04-27 13:48:39 -070014204 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014205 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
14206 bool can_position = false;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030014207
Ville Syrjälä693bdc22016-01-15 20:46:53 +020014208 if (INTEL_INFO(plane->dev)->gen >= 9) {
14209 /* use scaler when colorkey is not required */
14210 if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
14211 min_scale = 1;
14212 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
14213 }
Sonika Jindald8106362015-04-10 14:37:28 +053014214 can_position = true;
Chandra Konduru6156a452015-04-27 13:48:39 -070014215 }
Sonika Jindald8106362015-04-10 14:37:28 +053014216
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014217 return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
14218 &state->dst, &state->clip,
Ville Syrjälä9b8b0132016-06-17 17:13:10 +030014219 state->base.rotation,
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014220 min_scale, max_scale,
14221 can_position, true,
14222 &state->visible);
Matt Roper465c1202014-05-29 08:06:54 -070014223}
14224
Daniel Vetter5a21b662016-05-24 17:13:53 +020014225static void intel_begin_crtc_commit(struct drm_crtc *crtc,
14226 struct drm_crtc_state *old_crtc_state)
14227{
14228 struct drm_device *dev = crtc->dev;
14229 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14230 struct intel_crtc_state *old_intel_state =
14231 to_intel_crtc_state(old_crtc_state);
14232 bool modeset = needs_modeset(crtc->state);
14233
14234 /* Perform vblank evasion around commit operation */
14235 intel_pipe_update_start(intel_crtc);
14236
14237 if (modeset)
14238 return;
14239
14240 if (crtc->state->color_mgmt_changed || to_intel_crtc_state(crtc->state)->update_pipe) {
14241 intel_color_set_csc(crtc->state);
14242 intel_color_load_luts(crtc->state);
14243 }
14244
14245 if (to_intel_crtc_state(crtc->state)->update_pipe)
14246 intel_update_pipe_config(intel_crtc, old_intel_state);
14247 else if (INTEL_INFO(dev)->gen >= 9)
14248 skl_detach_scalers(intel_crtc);
14249}
14250
14251static void intel_finish_crtc_commit(struct drm_crtc *crtc,
14252 struct drm_crtc_state *old_crtc_state)
14253{
14254 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14255
14256 intel_pipe_update_end(intel_crtc, NULL);
14257}
14258
Matt Ropercf4c7c12014-12-04 10:27:42 -080014259/**
Matt Roper4a3b8762014-12-23 10:41:51 -080014260 * intel_plane_destroy - destroy a plane
14261 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080014262 *
Matt Roper4a3b8762014-12-23 10:41:51 -080014263 * Common destruction function for all types of planes (primary, cursor,
14264 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080014265 */
Matt Roper4a3b8762014-12-23 10:41:51 -080014266void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070014267{
Ville Syrjälä69ae5612016-05-27 20:59:22 +030014268 if (!plane)
14269 return;
14270
Matt Roper465c1202014-05-29 08:06:54 -070014271 drm_plane_cleanup(plane);
Ville Syrjälä69ae5612016-05-27 20:59:22 +030014272 kfree(to_intel_plane(plane));
Matt Roper465c1202014-05-29 08:06:54 -070014273}
14274
Matt Roper65a3fea2015-01-21 16:35:42 -080014275const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070014276 .update_plane = drm_atomic_helper_update_plane,
14277 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070014278 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080014279 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080014280 .atomic_get_property = intel_plane_atomic_get_property,
14281 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080014282 .atomic_duplicate_state = intel_plane_duplicate_state,
14283 .atomic_destroy_state = intel_plane_destroy_state,
14284
Matt Roper465c1202014-05-29 08:06:54 -070014285};
14286
14287static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
14288 int pipe)
14289{
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014290 struct intel_plane *primary = NULL;
14291 struct intel_plane_state *state = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070014292 const uint32_t *intel_primary_formats;
Thierry Reding45e37432015-08-12 16:54:28 +020014293 unsigned int num_formats;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014294 int ret;
Matt Roper465c1202014-05-29 08:06:54 -070014295
14296 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014297 if (!primary)
14298 goto fail;
Matt Roper465c1202014-05-29 08:06:54 -070014299
Matt Roper8e7d6882015-01-21 16:35:41 -080014300 state = intel_create_plane_state(&primary->base);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014301 if (!state)
14302 goto fail;
Matt Roper8e7d6882015-01-21 16:35:41 -080014303 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080014304
Matt Roper465c1202014-05-29 08:06:54 -070014305 primary->can_scale = false;
14306 primary->max_downscale = 1;
Chandra Konduru6156a452015-04-27 13:48:39 -070014307 if (INTEL_INFO(dev)->gen >= 9) {
14308 primary->can_scale = true;
Chandra Konduruaf99ced2015-05-11 14:35:47 -070014309 state->scaler_id = -1;
Chandra Konduru6156a452015-04-27 13:48:39 -070014310 }
Matt Roper465c1202014-05-29 08:06:54 -070014311 primary->pipe = pipe;
14312 primary->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030014313 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080014314 primary->check_plane = intel_check_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070014315 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
14316 primary->plane = !pipe;
14317
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014318 if (INTEL_INFO(dev)->gen >= 9) {
14319 intel_primary_formats = skl_primary_formats;
14320 num_formats = ARRAY_SIZE(skl_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010014321
14322 primary->update_plane = skylake_update_primary_plane;
14323 primary->disable_plane = skylake_disable_primary_plane;
14324 } else if (HAS_PCH_SPLIT(dev)) {
14325 intel_primary_formats = i965_primary_formats;
14326 num_formats = ARRAY_SIZE(i965_primary_formats);
14327
14328 primary->update_plane = ironlake_update_primary_plane;
14329 primary->disable_plane = i9xx_disable_primary_plane;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014330 } else if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau568db4f2015-05-12 16:13:18 +010014331 intel_primary_formats = i965_primary_formats;
14332 num_formats = ARRAY_SIZE(i965_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010014333
14334 primary->update_plane = i9xx_update_primary_plane;
14335 primary->disable_plane = i9xx_disable_primary_plane;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014336 } else {
14337 intel_primary_formats = i8xx_primary_formats;
14338 num_formats = ARRAY_SIZE(i8xx_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010014339
14340 primary->update_plane = i9xx_update_primary_plane;
14341 primary->disable_plane = i9xx_disable_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070014342 }
14343
Ville Syrjälä38573dc2016-05-27 20:59:23 +030014344 if (INTEL_INFO(dev)->gen >= 9)
14345 ret = drm_universal_plane_init(dev, &primary->base, 0,
14346 &intel_plane_funcs,
14347 intel_primary_formats, num_formats,
14348 DRM_PLANE_TYPE_PRIMARY,
14349 "plane 1%c", pipe_name(pipe));
14350 else if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
14351 ret = drm_universal_plane_init(dev, &primary->base, 0,
14352 &intel_plane_funcs,
14353 intel_primary_formats, num_formats,
14354 DRM_PLANE_TYPE_PRIMARY,
14355 "primary %c", pipe_name(pipe));
14356 else
14357 ret = drm_universal_plane_init(dev, &primary->base, 0,
14358 &intel_plane_funcs,
14359 intel_primary_formats, num_formats,
14360 DRM_PLANE_TYPE_PRIMARY,
14361 "plane %c", plane_name(primary->plane));
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014362 if (ret)
14363 goto fail;
Sonika Jindal48404c12014-08-22 14:06:04 +053014364
Sonika Jindal3b7a5112015-04-10 14:37:29 +053014365 if (INTEL_INFO(dev)->gen >= 4)
14366 intel_create_rotation_property(dev, primary);
Sonika Jindal48404c12014-08-22 14:06:04 +053014367
Matt Roperea2c67b2014-12-23 10:41:52 -080014368 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
14369
Matt Roper465c1202014-05-29 08:06:54 -070014370 return &primary->base;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014371
14372fail:
14373 kfree(state);
14374 kfree(primary);
14375
14376 return NULL;
Matt Roper465c1202014-05-29 08:06:54 -070014377}
14378
Sonika Jindal3b7a5112015-04-10 14:37:29 +053014379void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
14380{
14381 if (!dev->mode_config.rotation_property) {
14382 unsigned long flags = BIT(DRM_ROTATE_0) |
14383 BIT(DRM_ROTATE_180);
14384
14385 if (INTEL_INFO(dev)->gen >= 9)
14386 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
14387
14388 dev->mode_config.rotation_property =
14389 drm_mode_create_rotation_property(dev, flags);
14390 }
14391 if (dev->mode_config.rotation_property)
14392 drm_object_attach_property(&plane->base.base,
14393 dev->mode_config.rotation_property,
14394 plane->base.state->rotation);
14395}
14396
Matt Roper3d7d6512014-06-10 08:28:13 -070014397static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030014398intel_check_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014399 struct intel_crtc_state *crtc_state,
Gustavo Padovan852e7872014-09-05 17:22:31 -030014400 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070014401{
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014402 struct drm_crtc *crtc = crtc_state->base.crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080014403 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014404 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Ville Syrjäläb29ec922015-12-18 19:24:39 +020014405 enum pipe pipe = to_intel_plane(plane)->pipe;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014406 unsigned stride;
14407 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030014408
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014409 ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
14410 &state->dst, &state->clip,
Ville Syrjälä9b8b0132016-06-17 17:13:10 +030014411 state->base.rotation,
Gustavo Padovan852e7872014-09-05 17:22:31 -030014412 DRM_PLANE_HELPER_NO_SCALING,
14413 DRM_PLANE_HELPER_NO_SCALING,
14414 true, true, &state->visible);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014415 if (ret)
14416 return ret;
14417
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014418 /* if we want to turn off the cursor ignore width and height */
14419 if (!obj)
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014420 return 0;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014421
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014422 /* Check for which cursor types we support */
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014423 if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
Matt Roperea2c67b2014-12-23 10:41:52 -080014424 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
14425 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014426 return -EINVAL;
14427 }
14428
Matt Roperea2c67b2014-12-23 10:41:52 -080014429 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
14430 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014431 DRM_DEBUG_KMS("buffer is too small\n");
14432 return -ENOMEM;
14433 }
14434
Ville Syrjälä3a656b52015-03-09 21:08:37 +020014435 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014436 DRM_DEBUG_KMS("cursor cannot be tiled\n");
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014437 return -EINVAL;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014438 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014439
Ville Syrjäläb29ec922015-12-18 19:24:39 +020014440 /*
14441 * There's something wrong with the cursor on CHV pipe C.
14442 * If it straddles the left edge of the screen then
14443 * moving it away from the edge or disabling it often
14444 * results in a pipe underrun, and often that can lead to
14445 * dead pipe (constant underrun reported, and it scans
14446 * out just a solid color). To recover from that, the
14447 * display power well must be turned off and on again.
14448 * Refuse the put the cursor into that compromised position.
14449 */
14450 if (IS_CHERRYVIEW(plane->dev) && pipe == PIPE_C &&
14451 state->visible && state->base.crtc_x < 0) {
14452 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
14453 return -EINVAL;
14454 }
14455
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014456 return 0;
Gustavo Padovan852e7872014-09-05 17:22:31 -030014457}
14458
Matt Roperf4a2cf22014-12-01 15:40:12 -080014459static void
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014460intel_disable_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst7fabf5e2015-06-15 12:33:47 +020014461 struct drm_crtc *crtc)
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014462{
Maarten Lankhorstf2858022016-01-07 11:54:09 +010014463 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14464
14465 intel_crtc->cursor_addr = 0;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014466 intel_crtc_update_cursor(crtc, NULL);
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014467}
14468
14469static void
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014470intel_update_cursor_plane(struct drm_plane *plane,
14471 const struct intel_crtc_state *crtc_state,
14472 const struct intel_plane_state *state)
Gustavo Padovan852e7872014-09-05 17:22:31 -030014473{
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014474 struct drm_crtc *crtc = crtc_state->base.crtc;
14475 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roperea2c67b2014-12-23 10:41:52 -080014476 struct drm_device *dev = plane->dev;
Matt Roper2b875c22014-12-01 15:40:13 -080014477 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080014478 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070014479
Matt Roperf4a2cf22014-12-01 15:40:12 -080014480 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080014481 addr = 0;
Matt Roperf4a2cf22014-12-01 15:40:12 -080014482 else if (!INTEL_INFO(dev)->cursor_needs_physical)
Gustavo Padovana912f122014-12-01 15:40:10 -080014483 addr = i915_gem_obj_ggtt_offset(obj);
Matt Roperf4a2cf22014-12-01 15:40:12 -080014484 else
Gustavo Padovana912f122014-12-01 15:40:10 -080014485 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080014486
Gustavo Padovana912f122014-12-01 15:40:10 -080014487 intel_crtc->cursor_addr = addr;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014488 intel_crtc_update_cursor(crtc, state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014489}
Gustavo Padovan852e7872014-09-05 17:22:31 -030014490
Matt Roper3d7d6512014-06-10 08:28:13 -070014491static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
14492 int pipe)
14493{
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014494 struct intel_plane *cursor = NULL;
14495 struct intel_plane_state *state = NULL;
14496 int ret;
Matt Roper3d7d6512014-06-10 08:28:13 -070014497
14498 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014499 if (!cursor)
14500 goto fail;
Matt Roper3d7d6512014-06-10 08:28:13 -070014501
Matt Roper8e7d6882015-01-21 16:35:41 -080014502 state = intel_create_plane_state(&cursor->base);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014503 if (!state)
14504 goto fail;
Matt Roper8e7d6882015-01-21 16:35:41 -080014505 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080014506
Matt Roper3d7d6512014-06-10 08:28:13 -070014507 cursor->can_scale = false;
14508 cursor->max_downscale = 1;
14509 cursor->pipe = pipe;
14510 cursor->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030014511 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080014512 cursor->check_plane = intel_check_cursor_plane;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014513 cursor->update_plane = intel_update_cursor_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014514 cursor->disable_plane = intel_disable_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070014515
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014516 ret = drm_universal_plane_init(dev, &cursor->base, 0,
14517 &intel_plane_funcs,
14518 intel_cursor_formats,
14519 ARRAY_SIZE(intel_cursor_formats),
Ville Syrjälä38573dc2016-05-27 20:59:23 +030014520 DRM_PLANE_TYPE_CURSOR,
14521 "cursor %c", pipe_name(pipe));
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014522 if (ret)
14523 goto fail;
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014524
14525 if (INTEL_INFO(dev)->gen >= 4) {
14526 if (!dev->mode_config.rotation_property)
14527 dev->mode_config.rotation_property =
14528 drm_mode_create_rotation_property(dev,
14529 BIT(DRM_ROTATE_0) |
14530 BIT(DRM_ROTATE_180));
14531 if (dev->mode_config.rotation_property)
14532 drm_object_attach_property(&cursor->base.base,
14533 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080014534 state->base.rotation);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014535 }
14536
Chandra Konduruaf99ced2015-05-11 14:35:47 -070014537 if (INTEL_INFO(dev)->gen >=9)
14538 state->scaler_id = -1;
14539
Matt Roperea2c67b2014-12-23 10:41:52 -080014540 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
14541
Matt Roper3d7d6512014-06-10 08:28:13 -070014542 return &cursor->base;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014543
14544fail:
14545 kfree(state);
14546 kfree(cursor);
14547
14548 return NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070014549}
14550
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014551static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
14552 struct intel_crtc_state *crtc_state)
14553{
14554 int i;
14555 struct intel_scaler *intel_scaler;
14556 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
14557
14558 for (i = 0; i < intel_crtc->num_scalers; i++) {
14559 intel_scaler = &scaler_state->scalers[i];
14560 intel_scaler->in_use = 0;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014561 intel_scaler->mode = PS_SCALER_MODE_DYN;
14562 }
14563
14564 scaler_state->scaler_id = -1;
14565}
14566
Hannes Ederb358d0a2008-12-18 21:18:47 +010014567static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080014568{
Jani Nikulafbee40d2014-03-31 14:27:18 +030014569 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080014570 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014571 struct intel_crtc_state *crtc_state = NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070014572 struct drm_plane *primary = NULL;
14573 struct drm_plane *cursor = NULL;
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +000014574 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080014575
Daniel Vetter955382f2013-09-19 14:05:45 +020014576 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080014577 if (intel_crtc == NULL)
14578 return;
14579
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014580 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
14581 if (!crtc_state)
14582 goto fail;
Ander Conselvan de Oliveira550acef2015-04-21 17:13:24 +030014583 intel_crtc->config = crtc_state;
14584 intel_crtc->base.state = &crtc_state->base;
Matt Roper07878242015-02-25 11:43:26 -080014585 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014586
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014587 /* initialize shared scalers */
14588 if (INTEL_INFO(dev)->gen >= 9) {
14589 if (pipe == PIPE_C)
14590 intel_crtc->num_scalers = 1;
14591 else
14592 intel_crtc->num_scalers = SKL_NUM_SCALERS;
14593
14594 skl_init_scalers(dev, intel_crtc, crtc_state);
14595 }
14596
Matt Roper465c1202014-05-29 08:06:54 -070014597 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014598 if (!primary)
14599 goto fail;
14600
14601 cursor = intel_cursor_plane_create(dev, pipe);
14602 if (!cursor)
14603 goto fail;
14604
Matt Roper465c1202014-05-29 08:06:54 -070014605 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Ville Syrjälä4d5d72b72016-05-27 20:59:21 +030014606 cursor, &intel_crtc_funcs,
14607 "pipe %c", pipe_name(pipe));
Matt Roper3d7d6512014-06-10 08:28:13 -070014608 if (ret)
14609 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080014610
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014611 /*
14612 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020014613 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014614 */
Jesse Barnes80824002009-09-10 15:28:06 -070014615 intel_crtc->pipe = pipe;
14616 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010014617 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080014618 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010014619 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070014620 }
14621
Chris Wilson4b0e3332014-05-30 16:35:26 +030014622 intel_crtc->cursor_base = ~0;
14623 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030014624 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030014625
Ville Syrjälä852eb002015-06-24 22:00:07 +030014626 intel_crtc->wm.cxsr_allowed = true;
14627
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080014628 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
14629 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
14630 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
14631 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
14632
Jesse Barnes79e53942008-11-07 14:24:08 -080014633 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020014634
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +000014635 intel_color_init(&intel_crtc->base);
14636
Daniel Vetter87b6b102014-05-15 15:33:46 +020014637 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014638 return;
14639
14640fail:
Ville Syrjälä69ae5612016-05-27 20:59:22 +030014641 intel_plane_destroy(primary);
14642 intel_plane_destroy(cursor);
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014643 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014644 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080014645}
14646
Jesse Barnes752aa882013-10-31 18:55:49 +020014647enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14648{
14649 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014650 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020014651
Rob Clark51fd3712013-11-19 12:10:12 -050014652 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020014653
Ville Syrjäläd3babd32014-11-07 11:16:01 +020014654 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020014655 return INVALID_PIPE;
14656
14657 return to_intel_crtc(encoder->crtc)->pipe;
14658}
14659
Carl Worth08d7b3d2009-04-29 14:43:54 -070014660int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000014661 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070014662{
Carl Worth08d7b3d2009-04-29 14:43:54 -070014663 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040014664 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020014665 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014666
Rob Clark7707e652014-07-17 23:30:04 -040014667 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Chris Wilson71240ed2016-06-24 14:00:24 +010014668 if (!drmmode_crtc)
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030014669 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014670
Rob Clark7707e652014-07-17 23:30:04 -040014671 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020014672 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014673
Daniel Vetterc05422d2009-08-11 16:05:30 +020014674 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014675}
14676
Daniel Vetter66a92782012-07-12 20:08:18 +020014677static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080014678{
Daniel Vetter66a92782012-07-12 20:08:18 +020014679 struct drm_device *dev = encoder->base.dev;
14680 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080014681 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080014682 int entry = 0;
14683
Damien Lespiaub2784e12014-08-05 11:29:37 +010014684 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020014685 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020014686 index_mask |= (1 << entry);
14687
Jesse Barnes79e53942008-11-07 14:24:08 -080014688 entry++;
14689 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010014690
Jesse Barnes79e53942008-11-07 14:24:08 -080014691 return index_mask;
14692}
14693
Chris Wilson4d302442010-12-14 19:21:29 +000014694static bool has_edp_a(struct drm_device *dev)
14695{
14696 struct drm_i915_private *dev_priv = dev->dev_private;
14697
14698 if (!IS_MOBILE(dev))
14699 return false;
14700
14701 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14702 return false;
14703
Damien Lespiaue3589902014-02-07 19:12:50 +000014704 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000014705 return false;
14706
14707 return true;
14708}
14709
Jesse Barnes84b4e042014-06-25 08:24:29 -070014710static bool intel_crt_present(struct drm_device *dev)
14711{
14712 struct drm_i915_private *dev_priv = dev->dev_private;
14713
Damien Lespiau884497e2013-12-03 13:56:23 +000014714 if (INTEL_INFO(dev)->gen >= 9)
14715 return false;
14716
Damien Lespiaucf404ce2014-10-01 20:04:15 +010014717 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
Jesse Barnes84b4e042014-06-25 08:24:29 -070014718 return false;
14719
14720 if (IS_CHERRYVIEW(dev))
14721 return false;
14722
Ville Syrjälä65e472e2015-12-01 23:28:55 +020014723 if (HAS_PCH_LPT_H(dev) && I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
14724 return false;
14725
Ville Syrjälä70ac54d2015-12-01 23:29:56 +020014726 /* DDI E can't be used if DDI A requires 4 lanes */
14727 if (HAS_DDI(dev) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
14728 return false;
14729
Ville Syrjäläe4abb732015-12-01 23:31:33 +020014730 if (!dev_priv->vbt.int_crt_support)
Jesse Barnes84b4e042014-06-25 08:24:29 -070014731 return false;
14732
14733 return true;
14734}
14735
Jesse Barnes79e53942008-11-07 14:24:08 -080014736static void intel_setup_outputs(struct drm_device *dev)
14737{
Eric Anholt725e30a2009-01-22 13:01:02 -080014738 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010014739 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014740 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080014741
Imre Deak97a824e12016-06-21 11:51:47 +030014742 /*
14743 * intel_edp_init_connector() depends on this completing first, to
14744 * prevent the registeration of both eDP and LVDS and the incorrect
14745 * sharing of the PPS.
14746 */
Daniel Vetterc9093352013-06-06 22:22:47 +020014747 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014748
Jesse Barnes84b4e042014-06-25 08:24:29 -070014749 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020014750 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014751
Vandana Kannanc776eb22014-08-19 12:05:01 +053014752 if (IS_BROXTON(dev)) {
14753 /*
14754 * FIXME: Broxton doesn't support port detection via the
14755 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14756 * detect the ports.
14757 */
14758 intel_ddi_init(dev, PORT_A);
14759 intel_ddi_init(dev, PORT_B);
14760 intel_ddi_init(dev, PORT_C);
Shashank Sharmac6c794a2016-03-22 12:01:50 +020014761
14762 intel_dsi_init(dev);
Vandana Kannanc776eb22014-08-19 12:05:01 +053014763 } else if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014764 int found;
14765
Jesse Barnesde31fac2015-03-06 15:53:32 -080014766 /*
14767 * Haswell uses DDI functions to detect digital outputs.
14768 * On SKL pre-D0 the strap isn't connected, so we assume
14769 * it's there.
14770 */
Ville Syrjälä77179402015-09-18 20:03:35 +030014771 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080014772 /* WaIgnoreDDIAStrap: skl */
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070014773 if (found || IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014774 intel_ddi_init(dev, PORT_A);
14775
14776 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14777 * register */
14778 found = I915_READ(SFUSE_STRAP);
14779
14780 if (found & SFUSE_STRAP_DDIB_DETECTED)
14781 intel_ddi_init(dev, PORT_B);
14782 if (found & SFUSE_STRAP_DDIC_DETECTED)
14783 intel_ddi_init(dev, PORT_C);
14784 if (found & SFUSE_STRAP_DDID_DETECTED)
14785 intel_ddi_init(dev, PORT_D);
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014786 /*
14787 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
14788 */
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070014789 if ((IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) &&
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014790 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
14791 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
14792 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
14793 intel_ddi_init(dev, PORT_E);
14794
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014795 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014796 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020014797 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020014798
14799 if (has_edp_a(dev))
14800 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014801
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014802 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080014803 /* PCH SDVOB multiplex with HDMIB */
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014804 found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014805 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014806 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014807 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014808 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014809 }
14810
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014811 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014812 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014813
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014814 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014815 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014816
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014817 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014818 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014819
Daniel Vetter270b3042012-10-27 15:52:05 +020014820 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014821 intel_dp_init(dev, PCH_DP_D, PORT_D);
Wayne Boyer666a4532015-12-09 12:29:35 -080014822 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Ville Syrjälä22f350422016-06-03 12:17:43 +030014823 bool has_edp, has_port;
Chris Wilson457c52d2016-06-01 08:27:50 +010014824
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014825 /*
14826 * The DP_DETECTED bit is the latched state of the DDC
14827 * SDA pin at boot. However since eDP doesn't require DDC
14828 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14829 * eDP ports may have been muxed to an alternate function.
14830 * Thus we can't rely on the DP_DETECTED bit alone to detect
14831 * eDP ports. Consult the VBT as well as DP_DETECTED to
14832 * detect eDP ports.
Ville Syrjälä22f350422016-06-03 12:17:43 +030014833 *
14834 * Sadly the straps seem to be missing sometimes even for HDMI
14835 * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
14836 * and VBT for the presence of the port. Additionally we can't
14837 * trust the port type the VBT declares as we've seen at least
14838 * HDMI ports that the VBT claim are DP or eDP.
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014839 */
Chris Wilson457c52d2016-06-01 08:27:50 +010014840 has_edp = intel_dp_is_edp(dev, PORT_B);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014841 has_port = intel_bios_is_port_present(dev_priv, PORT_B);
14842 if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
Chris Wilson457c52d2016-06-01 08:27:50 +010014843 has_edp &= intel_dp_init(dev, VLV_DP_B, PORT_B);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014844 if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014845 intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014846
Chris Wilson457c52d2016-06-01 08:27:50 +010014847 has_edp = intel_dp_is_edp(dev, PORT_C);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014848 has_port = intel_bios_is_port_present(dev_priv, PORT_C);
14849 if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
Chris Wilson457c52d2016-06-01 08:27:50 +010014850 has_edp &= intel_dp_init(dev, VLV_DP_C, PORT_C);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014851 if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014852 intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053014853
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014854 if (IS_CHERRYVIEW(dev)) {
Ville Syrjälä22f350422016-06-03 12:17:43 +030014855 /*
14856 * eDP not supported on port D,
14857 * so no need to worry about it
14858 */
14859 has_port = intel_bios_is_port_present(dev_priv, PORT_D);
14860 if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014861 intel_dp_init(dev, CHV_DP_D, PORT_D);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014862 if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
14863 intel_hdmi_init(dev, CHV_HDMID, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014864 }
14865
Jani Nikula3cfca972013-08-27 15:12:26 +030014866 intel_dsi_init(dev);
Daniel Vetter09da55d2015-07-07 11:44:32 +020014867 } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014868 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080014869
Paulo Zanonie2debe92013-02-18 19:00:27 -030014870 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014871 DRM_DEBUG_KMS("probing SDVOB\n");
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014872 found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014873 if (!found && IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014874 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014875 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014876 }
Ma Ling27185ae2009-08-24 13:50:23 +080014877
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014878 if (!found && IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014879 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080014880 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014881
14882 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014883
Paulo Zanonie2debe92013-02-18 19:00:27 -030014884 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014885 DRM_DEBUG_KMS("probing SDVOC\n");
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014886 found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014887 }
Ma Ling27185ae2009-08-24 13:50:23 +080014888
Paulo Zanonie2debe92013-02-18 19:00:27 -030014889 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014890
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014891 if (IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014892 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014893 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014894 }
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014895 if (IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014896 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080014897 }
Ma Ling27185ae2009-08-24 13:50:23 +080014898
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014899 if (IS_G4X(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030014900 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014901 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070014902 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014903 intel_dvo_init(dev);
14904
Zhenyu Wang103a1962009-11-27 11:44:36 +080014905 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014906 intel_tv_init(dev);
14907
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -080014908 intel_psr_init(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070014909
Damien Lespiaub2784e12014-08-05 11:29:37 +010014910 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010014911 encoder->base.possible_crtcs = encoder->crtc_mask;
14912 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020014913 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080014914 }
Chris Wilson47356eb2011-01-11 17:06:04 +000014915
Paulo Zanonidde86e22012-12-01 12:04:25 -020014916 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020014917
14918 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014919}
14920
14921static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14922{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014923 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080014924 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080014925
Daniel Vetteref2d6332014-02-10 18:00:38 +010014926 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014927 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010014928 WARN_ON(!intel_fb->obj->framebuffer_references--);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014929 drm_gem_object_unreference(&intel_fb->obj->base);
14930 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080014931 kfree(intel_fb);
14932}
14933
14934static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000014935 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080014936 unsigned int *handle)
14937{
14938 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000014939 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014940
Chris Wilsoncc917ab2015-10-13 14:22:26 +010014941 if (obj->userptr.mm) {
14942 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
14943 return -EINVAL;
14944 }
14945
Chris Wilson05394f32010-11-08 19:18:58 +000014946 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080014947}
14948
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014949static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
14950 struct drm_file *file,
14951 unsigned flags, unsigned color,
14952 struct drm_clip_rect *clips,
14953 unsigned num_clips)
14954{
14955 struct drm_device *dev = fb->dev;
14956 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
14957 struct drm_i915_gem_object *obj = intel_fb->obj;
14958
14959 mutex_lock(&dev->struct_mutex);
Paulo Zanoni74b4ea12015-07-14 16:29:14 -030014960 intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014961 mutex_unlock(&dev->struct_mutex);
14962
14963 return 0;
14964}
14965
Jesse Barnes79e53942008-11-07 14:24:08 -080014966static const struct drm_framebuffer_funcs intel_fb_funcs = {
14967 .destroy = intel_user_framebuffer_destroy,
14968 .create_handle = intel_user_framebuffer_create_handle,
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014969 .dirty = intel_user_framebuffer_dirty,
Jesse Barnes79e53942008-11-07 14:24:08 -080014970};
14971
Damien Lespiaub3218032015-02-27 11:15:18 +000014972static
14973u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14974 uint32_t pixel_format)
14975{
14976 u32 gen = INTEL_INFO(dev)->gen;
14977
14978 if (gen >= 9) {
Ville Syrjäläac484962016-01-20 21:05:26 +020014979 int cpp = drm_format_plane_cpp(pixel_format, 0);
14980
Damien Lespiaub3218032015-02-27 11:15:18 +000014981 /* "The stride in bytes must not exceed the of the size of 8K
14982 * pixels and 32K bytes."
14983 */
Ville Syrjäläac484962016-01-20 21:05:26 +020014984 return min(8192 * cpp, 32768);
Wayne Boyer666a4532015-12-09 12:29:35 -080014985 } else if (gen >= 5 && !IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014986 return 32*1024;
14987 } else if (gen >= 4) {
14988 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14989 return 16*1024;
14990 else
14991 return 32*1024;
14992 } else if (gen >= 3) {
14993 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14994 return 8*1024;
14995 else
14996 return 16*1024;
14997 } else {
14998 /* XXX DSPC is limited to 4k tiled */
14999 return 8*1024;
15000 }
15001}
15002
Daniel Vetterb5ea6422014-03-02 21:18:00 +010015003static int intel_framebuffer_init(struct drm_device *dev,
15004 struct intel_framebuffer *intel_fb,
15005 struct drm_mode_fb_cmd2 *mode_cmd,
15006 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080015007{
Ville Syrjälä7b49f942016-01-12 21:08:32 +020015008 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +000015009 unsigned int aligned_height;
Jesse Barnes79e53942008-11-07 14:24:08 -080015010 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000015011 u32 pitch_limit, stride_alignment;
Jesse Barnes79e53942008-11-07 14:24:08 -080015012
Daniel Vetterdd4916c2013-10-09 21:23:51 +020015013 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
15014
Daniel Vetter2a80ead2015-02-10 17:16:06 +000015015 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
15016 /* Enforce that fb modifier and tiling mode match, but only for
15017 * X-tiled. This is needed for FBC. */
15018 if (!!(obj->tiling_mode == I915_TILING_X) !=
15019 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
15020 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
15021 return -EINVAL;
15022 }
15023 } else {
15024 if (obj->tiling_mode == I915_TILING_X)
15025 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
15026 else if (obj->tiling_mode == I915_TILING_Y) {
15027 DRM_DEBUG("No Y tiling for legacy addfb\n");
15028 return -EINVAL;
15029 }
15030 }
15031
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000015032 /* Passed in modifier sanity checking. */
15033 switch (mode_cmd->modifier[0]) {
15034 case I915_FORMAT_MOD_Y_TILED:
15035 case I915_FORMAT_MOD_Yf_TILED:
15036 if (INTEL_INFO(dev)->gen < 9) {
15037 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
15038 mode_cmd->modifier[0]);
15039 return -EINVAL;
15040 }
15041 case DRM_FORMAT_MOD_NONE:
15042 case I915_FORMAT_MOD_X_TILED:
15043 break;
15044 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070015045 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
15046 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010015047 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015048 }
Chris Wilson57cd6502010-08-08 12:34:44 +010015049
Ville Syrjälä7b49f942016-01-12 21:08:32 +020015050 stride_alignment = intel_fb_stride_alignment(dev_priv,
15051 mode_cmd->modifier[0],
Damien Lespiaub3218032015-02-27 11:15:18 +000015052 mode_cmd->pixel_format);
15053 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
15054 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
15055 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010015056 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015057 }
Chris Wilson57cd6502010-08-08 12:34:44 +010015058
Damien Lespiaub3218032015-02-27 11:15:18 +000015059 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
15060 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010015061 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000015062 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
15063 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000015064 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010015065 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020015066 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015067 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020015068
Daniel Vetter2a80ead2015-02-10 17:16:06 +000015069 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015070 mode_cmd->pitches[0] != obj->stride) {
15071 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
15072 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020015073 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015074 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020015075
Ville Syrjälä57779d02012-10-31 17:50:14 +020015076 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080015077 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020015078 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020015079 case DRM_FORMAT_RGB565:
15080 case DRM_FORMAT_XRGB8888:
15081 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020015082 break;
15083 case DRM_FORMAT_XRGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015084 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000015085 DRM_DEBUG("unsupported pixel format: %s\n",
15086 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020015087 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015088 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020015089 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +020015090 case DRM_FORMAT_ABGR8888:
Wayne Boyer666a4532015-12-09 12:29:35 -080015091 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
15092 INTEL_INFO(dev)->gen < 9) {
Damien Lespiau6c0fd452015-05-19 12:29:16 +010015093 DRM_DEBUG("unsupported pixel format: %s\n",
15094 drm_get_format_name(mode_cmd->pixel_format));
15095 return -EINVAL;
15096 }
15097 break;
15098 case DRM_FORMAT_XBGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020015099 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020015100 case DRM_FORMAT_XBGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015101 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000015102 DRM_DEBUG("unsupported pixel format: %s\n",
15103 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020015104 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015105 }
Jesse Barnesb5626742011-06-24 12:19:27 -070015106 break;
Damien Lespiau75312082015-05-15 19:06:01 +010015107 case DRM_FORMAT_ABGR2101010:
Wayne Boyer666a4532015-12-09 12:29:35 -080015108 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Damien Lespiau75312082015-05-15 19:06:01 +010015109 DRM_DEBUG("unsupported pixel format: %s\n",
15110 drm_get_format_name(mode_cmd->pixel_format));
15111 return -EINVAL;
15112 }
15113 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020015114 case DRM_FORMAT_YUYV:
15115 case DRM_FORMAT_UYVY:
15116 case DRM_FORMAT_YVYU:
15117 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015118 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000015119 DRM_DEBUG("unsupported pixel format: %s\n",
15120 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020015121 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015122 }
Chris Wilson57cd6502010-08-08 12:34:44 +010015123 break;
15124 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000015125 DRM_DEBUG("unsupported pixel format: %s\n",
15126 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010015127 return -EINVAL;
15128 }
15129
Ville Syrjälä90f9a332012-10-31 17:50:19 +020015130 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
15131 if (mode_cmd->offsets[0] != 0)
15132 return -EINVAL;
15133
Damien Lespiauec2c9812015-01-20 12:51:45 +000015134 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +000015135 mode_cmd->pixel_format,
15136 mode_cmd->modifier[0]);
Daniel Vetter53155c02013-10-09 21:55:33 +020015137 /* FIXME drm helper for size checks (especially planar formats)? */
15138 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
15139 return -EINVAL;
15140
Daniel Vetterc7d73f62012-12-13 23:38:38 +010015141 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
15142 intel_fb->obj = obj;
15143
Ville Syrjälä2d7a2152016-02-15 22:54:47 +020015144 intel_fill_fb_info(dev_priv, &intel_fb->base);
15145
Jesse Barnes79e53942008-11-07 14:24:08 -080015146 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
15147 if (ret) {
15148 DRM_ERROR("framebuffer init failed %d\n", ret);
15149 return ret;
15150 }
15151
Ville Syrjälä0b05e1e2016-01-14 15:22:09 +020015152 intel_fb->obj->framebuffer_references++;
15153
Jesse Barnes79e53942008-11-07 14:24:08 -080015154 return 0;
15155}
15156
Jesse Barnes79e53942008-11-07 14:24:08 -080015157static struct drm_framebuffer *
15158intel_user_framebuffer_create(struct drm_device *dev,
15159 struct drm_file *filp,
Ville Syrjälä1eb83452015-11-11 19:11:29 +020015160 const struct drm_mode_fb_cmd2 *user_mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080015161{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020015162 struct drm_framebuffer *fb;
Chris Wilson05394f32010-11-08 19:18:58 +000015163 struct drm_i915_gem_object *obj;
Ville Syrjälä76dc3762015-11-11 19:11:28 +020015164 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
Jesse Barnes79e53942008-11-07 14:24:08 -080015165
Chris Wilsona8ad0bd2016-05-09 11:04:54 +010015166 obj = to_intel_bo(drm_gem_object_lookup(filp, mode_cmd.handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000015167 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010015168 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080015169
Daniel Vetter92907cb2015-11-23 09:04:05 +010015170 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020015171 if (IS_ERR(fb))
15172 drm_gem_object_unreference_unlocked(&obj->base);
15173
15174 return fb;
Jesse Barnes79e53942008-11-07 14:24:08 -080015175}
15176
Daniel Vetter06957262015-08-10 13:34:08 +020015177#ifndef CONFIG_DRM_FBDEV_EMULATION
Daniel Vetter0632fef2013-10-08 17:44:49 +020015178static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020015179{
15180}
15181#endif
15182
Jesse Barnes79e53942008-11-07 14:24:08 -080015183static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080015184 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020015185 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080015186 .atomic_check = intel_atomic_check,
15187 .atomic_commit = intel_atomic_commit,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +020015188 .atomic_state_alloc = intel_atomic_state_alloc,
15189 .atomic_state_clear = intel_atomic_state_clear,
Jesse Barnes79e53942008-11-07 14:24:08 -080015190};
15191
Imre Deak88212942016-03-16 13:38:53 +020015192/**
15193 * intel_init_display_hooks - initialize the display modesetting hooks
15194 * @dev_priv: device private
15195 */
15196void intel_init_display_hooks(struct drm_i915_private *dev_priv)
Jesse Barnese70236a2009-09-21 10:42:27 -070015197{
Imre Deak88212942016-03-16 13:38:53 +020015198 if (INTEL_INFO(dev_priv)->gen >= 9) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000015199 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015200 dev_priv->display.get_initial_plane_config =
15201 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000015202 dev_priv->display.crtc_compute_clock =
15203 haswell_crtc_compute_clock;
15204 dev_priv->display.crtc_enable = haswell_crtc_enable;
15205 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020015206 } else if (HAS_DDI(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010015207 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015208 dev_priv->display.get_initial_plane_config =
15209 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020015210 dev_priv->display.crtc_compute_clock =
15211 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020015212 dev_priv->display.crtc_enable = haswell_crtc_enable;
15213 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020015214 } else if (HAS_PCH_SPLIT(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010015215 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015216 dev_priv->display.get_initial_plane_config =
15217 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020015218 dev_priv->display.crtc_compute_clock =
15219 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020015220 dev_priv->display.crtc_enable = ironlake_crtc_enable;
15221 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +020015222 } else if (IS_CHERRYVIEW(dev_priv)) {
Jesse Barnes89b667f2013-04-18 14:51:36 -070015223 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015224 dev_priv->display.get_initial_plane_config =
15225 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +020015226 dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
15227 dev_priv->display.crtc_enable = valleyview_crtc_enable;
15228 dev_priv->display.crtc_disable = i9xx_crtc_disable;
15229 } else if (IS_VALLEYVIEW(dev_priv)) {
15230 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15231 dev_priv->display.get_initial_plane_config =
15232 i9xx_get_initial_plane_config;
15233 dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070015234 dev_priv->display.crtc_enable = valleyview_crtc_enable;
15235 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +020015236 } else if (IS_G4X(dev_priv)) {
15237 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15238 dev_priv->display.get_initial_plane_config =
15239 i9xx_get_initial_plane_config;
15240 dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
15241 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15242 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +020015243 } else if (IS_PINEVIEW(dev_priv)) {
15244 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15245 dev_priv->display.get_initial_plane_config =
15246 i9xx_get_initial_plane_config;
15247 dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
15248 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15249 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +020015250 } else if (!IS_GEN2(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010015251 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015252 dev_priv->display.get_initial_plane_config =
15253 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020015254 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020015255 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15256 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +020015257 } else {
15258 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15259 dev_priv->display.get_initial_plane_config =
15260 i9xx_get_initial_plane_config;
15261 dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
15262 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15263 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Eric Anholtf564048e2011-03-30 13:01:02 -070015264 }
Jesse Barnese70236a2009-09-21 10:42:27 -070015265
Jesse Barnese70236a2009-09-21 10:42:27 -070015266 /* Returns the core display clock speed */
Imre Deak88212942016-03-16 13:38:53 +020015267 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030015268 dev_priv->display.get_display_clock_speed =
15269 skylake_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015270 else if (IS_BROXTON(dev_priv))
Bob Paauweacd3f3d2015-06-23 14:14:26 -070015271 dev_priv->display.get_display_clock_speed =
15272 broxton_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015273 else if (IS_BROADWELL(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030015274 dev_priv->display.get_display_clock_speed =
15275 broadwell_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015276 else if (IS_HASWELL(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030015277 dev_priv->display.get_display_clock_speed =
15278 haswell_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015279 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070015280 dev_priv->display.get_display_clock_speed =
15281 valleyview_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015282 else if (IS_GEN5(dev_priv))
Ville Syrjäläb37a6432015-03-31 14:11:54 +030015283 dev_priv->display.get_display_clock_speed =
15284 ilk_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015285 else if (IS_I945G(dev_priv) || IS_BROADWATER(dev_priv) ||
15286 IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015287 dev_priv->display.get_display_clock_speed =
15288 i945_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015289 else if (IS_GM45(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030015290 dev_priv->display.get_display_clock_speed =
15291 gm45_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015292 else if (IS_CRESTLINE(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030015293 dev_priv->display.get_display_clock_speed =
15294 i965gm_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015295 else if (IS_PINEVIEW(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030015296 dev_priv->display.get_display_clock_speed =
15297 pnv_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015298 else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030015299 dev_priv->display.get_display_clock_speed =
15300 g33_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015301 else if (IS_I915G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015302 dev_priv->display.get_display_clock_speed =
15303 i915_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015304 else if (IS_I945GM(dev_priv) || IS_845G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015305 dev_priv->display.get_display_clock_speed =
15306 i9xx_misc_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015307 else if (IS_I915GM(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015308 dev_priv->display.get_display_clock_speed =
15309 i915gm_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015310 else if (IS_I865G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015311 dev_priv->display.get_display_clock_speed =
15312 i865_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015313 else if (IS_I85X(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015314 dev_priv->display.get_display_clock_speed =
Ville Syrjälä1b1d2712015-05-22 11:22:31 +030015315 i85x_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030015316 else { /* 830 */
Imre Deak88212942016-03-16 13:38:53 +020015317 WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
Jesse Barnese70236a2009-09-21 10:42:27 -070015318 dev_priv->display.get_display_clock_speed =
15319 i830_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030015320 }
Jesse Barnese70236a2009-09-21 10:42:27 -070015321
Imre Deak88212942016-03-16 13:38:53 +020015322 if (IS_GEN5(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015323 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020015324 } else if (IS_GEN6(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015325 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020015326 } else if (IS_IVYBRIDGE(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015327 /* FIXME: detect B0+ stepping and use auto training */
15328 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020015329 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015330 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Ville Syrjälä445e7802016-05-11 22:44:42 +030015331 }
15332
15333 if (IS_BROADWELL(dev_priv)) {
15334 dev_priv->display.modeset_commit_cdclk =
15335 broadwell_modeset_commit_cdclk;
15336 dev_priv->display.modeset_calc_cdclk =
15337 broadwell_modeset_calc_cdclk;
Imre Deak88212942016-03-16 13:38:53 +020015338 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020015339 dev_priv->display.modeset_commit_cdclk =
15340 valleyview_modeset_commit_cdclk;
15341 dev_priv->display.modeset_calc_cdclk =
15342 valleyview_modeset_calc_cdclk;
Imre Deak88212942016-03-16 13:38:53 +020015343 } else if (IS_BROXTON(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020015344 dev_priv->display.modeset_commit_cdclk =
Imre Deak324513c2016-06-13 16:44:36 +030015345 bxt_modeset_commit_cdclk;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020015346 dev_priv->display.modeset_calc_cdclk =
Imre Deak324513c2016-06-13 16:44:36 +030015347 bxt_modeset_calc_cdclk;
Clint Taylorc89e39f2016-05-13 23:41:21 +030015348 } else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
15349 dev_priv->display.modeset_commit_cdclk =
15350 skl_modeset_commit_cdclk;
15351 dev_priv->display.modeset_calc_cdclk =
15352 skl_modeset_calc_cdclk;
Jesse Barnese70236a2009-09-21 10:42:27 -070015353 }
Daniel Vetter5a21b662016-05-24 17:13:53 +020015354
15355 switch (INTEL_INFO(dev_priv)->gen) {
15356 case 2:
15357 dev_priv->display.queue_flip = intel_gen2_queue_flip;
15358 break;
15359
15360 case 3:
15361 dev_priv->display.queue_flip = intel_gen3_queue_flip;
15362 break;
15363
15364 case 4:
15365 case 5:
15366 dev_priv->display.queue_flip = intel_gen4_queue_flip;
15367 break;
15368
15369 case 6:
15370 dev_priv->display.queue_flip = intel_gen6_queue_flip;
15371 break;
15372 case 7:
15373 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
15374 dev_priv->display.queue_flip = intel_gen7_queue_flip;
15375 break;
15376 case 9:
15377 /* Drop through - unsupported since execlist only. */
15378 default:
15379 /* Default just returns -ENODEV to indicate unsupported */
15380 dev_priv->display.queue_flip = intel_default_queue_flip;
15381 }
Jesse Barnese70236a2009-09-21 10:42:27 -070015382}
15383
Jesse Barnesb690e962010-07-19 13:53:12 -070015384/*
15385 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
15386 * resume, or other times. This quirk makes sure that's the case for
15387 * affected systems.
15388 */
Akshay Joshi0206e352011-08-16 15:34:10 -040015389static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070015390{
15391 struct drm_i915_private *dev_priv = dev->dev_private;
15392
15393 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015394 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070015395}
15396
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030015397static void quirk_pipeb_force(struct drm_device *dev)
15398{
15399 struct drm_i915_private *dev_priv = dev->dev_private;
15400
15401 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
15402 DRM_INFO("applying pipe b force quirk\n");
15403}
15404
Keith Packard435793d2011-07-12 14:56:22 -070015405/*
15406 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
15407 */
15408static void quirk_ssc_force_disable(struct drm_device *dev)
15409{
15410 struct drm_i915_private *dev_priv = dev->dev_private;
15411 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015412 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070015413}
15414
Carsten Emde4dca20e2012-03-15 15:56:26 +010015415/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010015416 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
15417 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010015418 */
15419static void quirk_invert_brightness(struct drm_device *dev)
15420{
15421 struct drm_i915_private *dev_priv = dev->dev_private;
15422 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015423 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070015424}
15425
Scot Doyle9c72cc62014-07-03 23:27:50 +000015426/* Some VBT's incorrectly indicate no backlight is present */
15427static void quirk_backlight_present(struct drm_device *dev)
15428{
15429 struct drm_i915_private *dev_priv = dev->dev_private;
15430 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
15431 DRM_INFO("applying backlight present quirk\n");
15432}
15433
Jesse Barnesb690e962010-07-19 13:53:12 -070015434struct intel_quirk {
15435 int device;
15436 int subsystem_vendor;
15437 int subsystem_device;
15438 void (*hook)(struct drm_device *dev);
15439};
15440
Egbert Eich5f85f172012-10-14 15:46:38 +020015441/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
15442struct intel_dmi_quirk {
15443 void (*hook)(struct drm_device *dev);
15444 const struct dmi_system_id (*dmi_id_list)[];
15445};
15446
15447static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
15448{
15449 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
15450 return 1;
15451}
15452
15453static const struct intel_dmi_quirk intel_dmi_quirks[] = {
15454 {
15455 .dmi_id_list = &(const struct dmi_system_id[]) {
15456 {
15457 .callback = intel_dmi_reverse_brightness,
15458 .ident = "NCR Corporation",
15459 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
15460 DMI_MATCH(DMI_PRODUCT_NAME, ""),
15461 },
15462 },
15463 { } /* terminating entry */
15464 },
15465 .hook = quirk_invert_brightness,
15466 },
15467};
15468
Ben Widawskyc43b5632012-04-16 14:07:40 -070015469static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070015470 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
15471 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
15472
Jesse Barnesb690e962010-07-19 13:53:12 -070015473 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
15474 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
15475
Ville Syrjälä5f080c02014-08-15 01:22:06 +030015476 /* 830 needs to leave pipe A & dpll A up */
15477 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
15478
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030015479 /* 830 needs to leave pipe B & dpll B up */
15480 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
15481
Keith Packard435793d2011-07-12 14:56:22 -070015482 /* Lenovo U160 cannot use SSC on LVDS */
15483 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020015484
15485 /* Sony Vaio Y cannot use SSC on LVDS */
15486 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010015487
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010015488 /* Acer Aspire 5734Z must invert backlight brightness */
15489 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
15490
15491 /* Acer/eMachines G725 */
15492 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
15493
15494 /* Acer/eMachines e725 */
15495 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
15496
15497 /* Acer/Packard Bell NCL20 */
15498 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
15499
15500 /* Acer Aspire 4736Z */
15501 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020015502
15503 /* Acer Aspire 5336 */
15504 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000015505
15506 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
15507 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000015508
Scot Doyledfb3d47b2014-08-21 16:08:02 +000015509 /* Acer C720 Chromebook (Core i3 4005U) */
15510 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
15511
jens steinb2a96012014-10-28 20:25:53 +010015512 /* Apple Macbook 2,1 (Core 2 T7400) */
15513 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
15514
Jani Nikula1b9448b2015-11-05 11:49:59 +020015515 /* Apple Macbook 4,1 */
15516 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
15517
Scot Doyled4967d82014-07-03 23:27:52 +000015518 /* Toshiba CB35 Chromebook (Celeron 2955U) */
15519 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000015520
15521 /* HP Chromebook 14 (Celeron 2955U) */
15522 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020015523
15524 /* Dell Chromebook 11 */
15525 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jani Nikula9be64ee2015-10-30 14:50:24 +020015526
15527 /* Dell Chromebook 11 (2015 version) */
15528 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070015529};
15530
15531static void intel_init_quirks(struct drm_device *dev)
15532{
15533 struct pci_dev *d = dev->pdev;
15534 int i;
15535
15536 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
15537 struct intel_quirk *q = &intel_quirks[i];
15538
15539 if (d->device == q->device &&
15540 (d->subsystem_vendor == q->subsystem_vendor ||
15541 q->subsystem_vendor == PCI_ANY_ID) &&
15542 (d->subsystem_device == q->subsystem_device ||
15543 q->subsystem_device == PCI_ANY_ID))
15544 q->hook(dev);
15545 }
Egbert Eich5f85f172012-10-14 15:46:38 +020015546 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
15547 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
15548 intel_dmi_quirks[i].hook(dev);
15549 }
Jesse Barnesb690e962010-07-19 13:53:12 -070015550}
15551
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015552/* Disable the VGA plane that we never use */
15553static void i915_disable_vga(struct drm_device *dev)
15554{
15555 struct drm_i915_private *dev_priv = dev->dev_private;
15556 u8 sr1;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020015557 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015558
Ville Syrjälä2b37c612014-01-22 21:32:38 +020015559 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015560 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070015561 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015562 sr1 = inb(VGA_SR_DATA);
15563 outb(sr1 | 1<<5, VGA_SR_DATA);
15564 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
15565 udelay(300);
15566
Ville Syrjälä01f5a622014-12-16 18:38:37 +020015567 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015568 POSTING_READ(vga_reg);
15569}
15570
Daniel Vetterf8175862012-04-10 15:50:11 +020015571void intel_modeset_init_hw(struct drm_device *dev)
15572{
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010015573 struct drm_i915_private *dev_priv = dev->dev_private;
15574
Ville Syrjäläb6283052015-06-03 15:45:07 +030015575 intel_update_cdclk(dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010015576
15577 dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
15578
Daniel Vetterf8175862012-04-10 15:50:11 +020015579 intel_init_clock_gating(dev);
Chris Wilsondc979972016-05-10 14:10:04 +010015580 intel_enable_gt_powersave(dev_priv);
Daniel Vetterf8175862012-04-10 15:50:11 +020015581}
15582
Matt Roperd93c0372015-12-03 11:37:41 -080015583/*
15584 * Calculate what we think the watermarks should be for the state we've read
15585 * out of the hardware and then immediately program those watermarks so that
15586 * we ensure the hardware settings match our internal state.
15587 *
15588 * We can calculate what we think WM's should be by creating a duplicate of the
15589 * current state (which was constructed during hardware readout) and running it
15590 * through the atomic check code to calculate new watermark values in the
15591 * state object.
15592 */
15593static void sanitize_watermarks(struct drm_device *dev)
15594{
15595 struct drm_i915_private *dev_priv = to_i915(dev);
15596 struct drm_atomic_state *state;
15597 struct drm_crtc *crtc;
15598 struct drm_crtc_state *cstate;
15599 struct drm_modeset_acquire_ctx ctx;
15600 int ret;
15601 int i;
15602
15603 /* Only supported on platforms that use atomic watermark design */
Matt Ropered4a6a72016-02-23 17:20:13 -080015604 if (!dev_priv->display.optimize_watermarks)
Matt Roperd93c0372015-12-03 11:37:41 -080015605 return;
15606
15607 /*
15608 * We need to hold connection_mutex before calling duplicate_state so
15609 * that the connector loop is protected.
15610 */
15611 drm_modeset_acquire_init(&ctx, 0);
15612retry:
Matt Roper0cd12622016-01-12 07:13:37 -080015613 ret = drm_modeset_lock_all_ctx(dev, &ctx);
Matt Roperd93c0372015-12-03 11:37:41 -080015614 if (ret == -EDEADLK) {
15615 drm_modeset_backoff(&ctx);
15616 goto retry;
15617 } else if (WARN_ON(ret)) {
Matt Roper0cd12622016-01-12 07:13:37 -080015618 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015619 }
15620
15621 state = drm_atomic_helper_duplicate_state(dev, &ctx);
15622 if (WARN_ON(IS_ERR(state)))
Matt Roper0cd12622016-01-12 07:13:37 -080015623 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015624
Matt Ropered4a6a72016-02-23 17:20:13 -080015625 /*
15626 * Hardware readout is the only time we don't want to calculate
15627 * intermediate watermarks (since we don't trust the current
15628 * watermarks).
15629 */
15630 to_intel_atomic_state(state)->skip_intermediate_wm = true;
15631
Matt Roperd93c0372015-12-03 11:37:41 -080015632 ret = intel_atomic_check(dev, state);
15633 if (ret) {
15634 /*
15635 * If we fail here, it means that the hardware appears to be
15636 * programmed in a way that shouldn't be possible, given our
15637 * understanding of watermark requirements. This might mean a
15638 * mistake in the hardware readout code or a mistake in the
15639 * watermark calculations for a given platform. Raise a WARN
15640 * so that this is noticeable.
15641 *
15642 * If this actually happens, we'll have to just leave the
15643 * BIOS-programmed watermarks untouched and hope for the best.
15644 */
15645 WARN(true, "Could not determine valid watermarks for inherited state\n");
Matt Roper0cd12622016-01-12 07:13:37 -080015646 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015647 }
15648
15649 /* Write calculated watermark values back */
Matt Roperd93c0372015-12-03 11:37:41 -080015650 for_each_crtc_in_state(state, crtc, cstate, i) {
15651 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
15652
Matt Ropered4a6a72016-02-23 17:20:13 -080015653 cs->wm.need_postvbl_update = true;
15654 dev_priv->display.optimize_watermarks(cs);
Matt Roperd93c0372015-12-03 11:37:41 -080015655 }
15656
15657 drm_atomic_state_free(state);
Matt Roper0cd12622016-01-12 07:13:37 -080015658fail:
Matt Roperd93c0372015-12-03 11:37:41 -080015659 drm_modeset_drop_locks(&ctx);
15660 drm_modeset_acquire_fini(&ctx);
15661}
15662
Jesse Barnes79e53942008-11-07 14:24:08 -080015663void intel_modeset_init(struct drm_device *dev)
15664{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +030015665 struct drm_i915_private *dev_priv = to_i915(dev);
15666 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Damien Lespiau1fe47782014-03-03 17:31:47 +000015667 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015668 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080015669 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080015670
15671 drm_mode_config_init(dev);
15672
15673 dev->mode_config.min_width = 0;
15674 dev->mode_config.min_height = 0;
15675
Dave Airlie019d96c2011-09-29 16:20:42 +010015676 dev->mode_config.preferred_depth = 24;
15677 dev->mode_config.prefer_shadow = 1;
15678
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000015679 dev->mode_config.allow_fb_modifiers = true;
15680
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020015681 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080015682
Jesse Barnesb690e962010-07-19 13:53:12 -070015683 intel_init_quirks(dev);
15684
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030015685 intel_init_pm(dev);
15686
Ben Widawskye3c74752013-04-05 13:12:39 -070015687 if (INTEL_INFO(dev)->num_pipes == 0)
15688 return;
15689
Lukas Wunner69f92f62015-07-15 13:57:35 +020015690 /*
15691 * There may be no VBT; and if the BIOS enabled SSC we can
15692 * just keep using it to avoid unnecessary flicker. Whereas if the
15693 * BIOS isn't using it, don't assume it will work even if the VBT
15694 * indicates as much.
15695 */
15696 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
15697 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15698 DREF_SSC1_ENABLE);
15699
15700 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
15701 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
15702 bios_lvds_use_ssc ? "en" : "dis",
15703 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
15704 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
15705 }
15706 }
15707
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015708 if (IS_GEN2(dev)) {
15709 dev->mode_config.max_width = 2048;
15710 dev->mode_config.max_height = 2048;
15711 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070015712 dev->mode_config.max_width = 4096;
15713 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080015714 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015715 dev->mode_config.max_width = 8192;
15716 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080015717 }
Damien Lespiau068be562014-03-28 14:17:49 +000015718
Ville Syrjälädc41c152014-08-13 11:57:05 +030015719 if (IS_845G(dev) || IS_I865G(dev)) {
15720 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
15721 dev->mode_config.cursor_height = 1023;
15722 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000015723 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
15724 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
15725 } else {
15726 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
15727 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
15728 }
15729
Joonas Lahtinen72e96d62016-03-30 16:57:10 +030015730 dev->mode_config.fb_base = ggtt->mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080015731
Zhao Yakui28c97732009-10-09 11:39:41 +080015732 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015733 INTEL_INFO(dev)->num_pipes,
15734 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080015735
Damien Lespiau055e3932014-08-18 13:49:10 +010015736 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015737 intel_crtc_init(dev, pipe);
Damien Lespiau3bdcfc02015-02-28 14:54:09 +000015738 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +000015739 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015740 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030015741 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000015742 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015743 }
Jesse Barnes79e53942008-11-07 14:24:08 -080015744 }
15745
Ville Syrjäläbfa7df02015-09-24 23:29:18 +030015746 intel_update_czclk(dev_priv);
15747 intel_update_cdclk(dev);
15748
Daniel Vettere72f9fb2013-06-05 13:34:06 +020015749 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010015750
Ville Syrjäläb2045352016-05-13 23:41:27 +030015751 if (dev_priv->max_cdclk_freq == 0)
15752 intel_update_max_cdclk(dev);
15753
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015754 /* Just disable it once at startup */
15755 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015756 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000015757
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015758 drm_modeset_lock_all(dev);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015759 intel_modeset_setup_hw_state(dev);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015760 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015761
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015762 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015763 struct intel_initial_plane_config plane_config = {};
15764
Jesse Barnes46f297f2014-03-07 08:57:48 -080015765 if (!crtc->active)
15766 continue;
15767
Jesse Barnes46f297f2014-03-07 08:57:48 -080015768 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080015769 * Note that reserving the BIOS fb up front prevents us
15770 * from stuffing other stolen allocations like the ring
15771 * on top. This prevents some ugliness at boot time, and
15772 * can even allow for smooth boot transitions if the BIOS
15773 * fb is large enough for the active pipe configuration.
15774 */
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015775 dev_priv->display.get_initial_plane_config(crtc,
15776 &plane_config);
15777
15778 /*
15779 * If the fb is shared between multiple heads, we'll
15780 * just get the first one.
15781 */
15782 intel_find_initial_plane_obj(crtc, &plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015783 }
Matt Roperd93c0372015-12-03 11:37:41 -080015784
15785 /*
15786 * Make sure hardware watermarks really match the state we read out.
15787 * Note that we need to do this after reconstructing the BIOS fb's
15788 * since the watermark calculation done here will use pstate->fb.
15789 */
15790 sanitize_watermarks(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010015791}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080015792
Daniel Vetter7fad7982012-07-04 17:51:47 +020015793static void intel_enable_pipe_a(struct drm_device *dev)
15794{
15795 struct intel_connector *connector;
15796 struct drm_connector *crt = NULL;
15797 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015798 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020015799
15800 /* We can't just switch on the pipe A, we need to set things up with a
15801 * proper mode and output configuration. As a gross hack, enable pipe A
15802 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015803 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020015804 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15805 crt = &connector->base;
15806 break;
15807 }
15808 }
15809
15810 if (!crt)
15811 return;
15812
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015813 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020015814 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020015815}
15816
Daniel Vetterfa555832012-10-10 23:14:00 +020015817static bool
15818intel_check_plane_mapping(struct intel_crtc *crtc)
15819{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015820 struct drm_device *dev = crtc->base.dev;
15821 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä649636e2015-09-22 19:50:01 +030015822 u32 val;
Daniel Vetterfa555832012-10-10 23:14:00 +020015823
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015824 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020015825 return true;
15826
Ville Syrjälä649636e2015-09-22 19:50:01 +030015827 val = I915_READ(DSPCNTR(!crtc->plane));
Daniel Vetterfa555832012-10-10 23:14:00 +020015828
15829 if ((val & DISPLAY_PLANE_ENABLE) &&
15830 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15831 return false;
15832
15833 return true;
15834}
15835
Ville Syrjälä02e93c32015-08-26 19:39:19 +030015836static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
15837{
15838 struct drm_device *dev = crtc->base.dev;
15839 struct intel_encoder *encoder;
15840
15841 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15842 return true;
15843
15844 return false;
15845}
15846
Ville Syrjälädd756192016-02-17 21:28:45 +020015847static bool intel_encoder_has_connectors(struct intel_encoder *encoder)
15848{
15849 struct drm_device *dev = encoder->base.dev;
15850 struct intel_connector *connector;
15851
15852 for_each_connector_on_encoder(dev, &encoder->base, connector)
15853 return true;
15854
15855 return false;
15856}
15857
Daniel Vetter24929352012-07-02 20:28:59 +020015858static void intel_sanitize_crtc(struct intel_crtc *crtc)
15859{
15860 struct drm_device *dev = crtc->base.dev;
15861 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula4d1de972016-03-18 17:05:42 +020015862 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter24929352012-07-02 20:28:59 +020015863
Daniel Vetter24929352012-07-02 20:28:59 +020015864 /* Clear any frame start delays used for debugging left by the BIOS */
Jani Nikula4d1de972016-03-18 17:05:42 +020015865 if (!transcoder_is_dsi(cpu_transcoder)) {
15866 i915_reg_t reg = PIPECONF(cpu_transcoder);
15867
15868 I915_WRITE(reg,
15869 I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15870 }
Daniel Vetter24929352012-07-02 20:28:59 +020015871
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015872 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010015873 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030015874 if (crtc->active) {
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015875 struct intel_plane *plane;
15876
Daniel Vetter96256042015-02-13 21:03:42 +010015877 drm_crtc_vblank_on(&crtc->base);
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015878
15879 /* Disable everything but the primary plane */
15880 for_each_intel_plane_on_crtc(dev, crtc, plane) {
15881 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
15882 continue;
15883
15884 plane->disable_plane(&plane->base, &crtc->base);
15885 }
Daniel Vetter96256042015-02-13 21:03:42 +010015886 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015887
Daniel Vetter24929352012-07-02 20:28:59 +020015888 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020015889 * disable the crtc (and hence change the state) if it is wrong. Note
15890 * that gen4+ has a fixed plane -> pipe mapping. */
15891 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020015892 bool plane;
15893
Ville Syrjälä78108b72016-05-27 20:59:19 +030015894 DRM_DEBUG_KMS("[CRTC:%d:%s] wrong plane connection detected!\n",
15895 crtc->base.base.id, crtc->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015896
15897 /* Pipe has the wrong plane attached and the plane is active.
15898 * Temporarily change the plane mapping and disable everything
15899 * ... */
15900 plane = crtc->plane;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015901 to_intel_plane_state(crtc->base.primary->state)->visible = true;
Daniel Vetter24929352012-07-02 20:28:59 +020015902 crtc->plane = !plane;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015903 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015904 crtc->plane = plane;
Daniel Vetter24929352012-07-02 20:28:59 +020015905 }
Daniel Vetter24929352012-07-02 20:28:59 +020015906
Daniel Vetter7fad7982012-07-04 17:51:47 +020015907 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15908 crtc->pipe == PIPE_A && !crtc->active) {
15909 /* BIOS forgot to enable pipe A, this mostly happens after
15910 * resume. Force-enable the pipe to fix this, the update_dpms
15911 * call below we restore the pipe to the right state, but leave
15912 * the required bits on. */
15913 intel_enable_pipe_a(dev);
15914 }
15915
Daniel Vetter24929352012-07-02 20:28:59 +020015916 /* Adjust the state of the output pipe according to whether we
15917 * have active connectors/encoders. */
Maarten Lankhorst842e0302016-03-02 15:48:01 +010015918 if (crtc->active && !intel_crtc_has_encoders(crtc))
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015919 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015920
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030015921 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010015922 /*
15923 * We start out with underrun reporting disabled to avoid races.
15924 * For correct bookkeeping mark this on active crtcs.
15925 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020015926 * Also on gmch platforms we dont have any hardware bits to
15927 * disable the underrun reporting. Which means we need to start
15928 * out with underrun reporting disabled also on inactive pipes,
15929 * since otherwise we'll complain about the garbage we read when
15930 * e.g. coming up after runtime pm.
15931 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010015932 * No protection against concurrent access is required - at
15933 * worst a fifo underrun happens which also sets this to false.
15934 */
15935 crtc->cpu_fifo_underrun_disabled = true;
15936 crtc->pch_fifo_underrun_disabled = true;
15937 }
Daniel Vetter24929352012-07-02 20:28:59 +020015938}
15939
15940static void intel_sanitize_encoder(struct intel_encoder *encoder)
15941{
15942 struct intel_connector *connector;
15943 struct drm_device *dev = encoder->base.dev;
15944
15945 /* We need to check both for a crtc link (meaning that the
15946 * encoder is active and trying to read from a pipe) and the
15947 * pipe itself being active. */
15948 bool has_active_crtc = encoder->base.crtc &&
15949 to_intel_crtc(encoder->base.crtc)->active;
15950
Ville Syrjälädd756192016-02-17 21:28:45 +020015951 if (intel_encoder_has_connectors(encoder) && !has_active_crtc) {
Daniel Vetter24929352012-07-02 20:28:59 +020015952 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15953 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015954 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015955
15956 /* Connector is active, but has no active pipe. This is
15957 * fallout from our resume register restoring. Disable
15958 * the encoder manually again. */
15959 if (encoder->base.crtc) {
15960 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15961 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015962 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015963 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030015964 if (encoder->post_disable)
15965 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020015966 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020015967 encoder->base.crtc = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015968
15969 /* Inconsistent output/port/pipe state happens presumably due to
15970 * a bug in one of the get_hw_state functions. Or someplace else
15971 * in our code, like the register restore mess on resume. Clamp
15972 * things to off as a safer default. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015973 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015974 if (connector->encoder != encoder)
15975 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020015976 connector->base.dpms = DRM_MODE_DPMS_OFF;
15977 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015978 }
15979 }
15980 /* Enabled encoders without active connectors will be fixed in
15981 * the crtc fixup. */
15982}
15983
Imre Deak04098752014-02-18 00:02:16 +020015984void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015985{
15986 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020015987 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015988
Imre Deak04098752014-02-18 00:02:16 +020015989 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15990 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15991 i915_disable_vga(dev);
15992 }
15993}
15994
15995void i915_redisable_vga(struct drm_device *dev)
15996{
15997 struct drm_i915_private *dev_priv = dev->dev_private;
15998
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015999 /* This function can be called both from intel_modeset_setup_hw_state or
16000 * at a very early point in our resume sequence, where the power well
16001 * structures are not yet restored. Since this function is at a very
16002 * paranoid "someone might have enabled VGA while we were not looking"
16003 * level, just check if the power well is enabled instead of trying to
16004 * follow the "don't touch the power well if we don't need it" policy
16005 * the rest of the driver uses. */
Imre Deak6392f842016-02-12 18:55:13 +020016006 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030016007 return;
16008
Imre Deak04098752014-02-18 00:02:16 +020016009 i915_redisable_vga_power_on(dev);
Imre Deak6392f842016-02-12 18:55:13 +020016010
16011 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010016012}
16013
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030016014static bool primary_get_hw_state(struct intel_plane *plane)
Ville Syrjälä98ec7732014-04-30 17:43:01 +030016015{
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030016016 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030016017
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030016018 return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020016019}
Ville Syrjälä98ec7732014-04-30 17:43:01 +030016020
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030016021/* FIXME read out full plane state for all planes */
16022static void readout_plane_state(struct intel_crtc *crtc)
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020016023{
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020016024 struct drm_plane *primary = crtc->base.primary;
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030016025 struct intel_plane_state *plane_state =
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020016026 to_intel_plane_state(primary->state);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020016027
Matt Roper19b8d382015-09-24 15:53:17 -070016028 plane_state->visible = crtc->active &&
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020016029 primary_get_hw_state(to_intel_plane(primary));
16030
16031 if (plane_state->visible)
16032 crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030016033}
16034
Daniel Vetter30e984d2013-06-05 13:34:17 +020016035static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020016036{
16037 struct drm_i915_private *dev_priv = dev->dev_private;
16038 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020016039 struct intel_crtc *crtc;
16040 struct intel_encoder *encoder;
16041 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020016042 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020016043
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016044 dev_priv->active_crtcs = 0;
16045
Damien Lespiaud3fcc802014-05-13 23:32:22 +010016046 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016047 struct intel_crtc_state *crtc_state = crtc->config;
16048 int pixclk = 0;
Daniel Vetter3b117c82013-04-17 20:15:07 +020016049
Daniel Vetterec2dc6a2016-05-09 16:34:09 +020016050 __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016051 memset(crtc_state, 0, sizeof(*crtc_state));
16052 crtc_state->base.crtc = &crtc->base;
Daniel Vetter24929352012-07-02 20:28:59 +020016053
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016054 crtc_state->base.active = crtc_state->base.enable =
16055 dev_priv->display.get_pipe_config(crtc, crtc_state);
16056
16057 crtc->base.enabled = crtc_state->base.enable;
16058 crtc->active = crtc_state->base.active;
16059
16060 if (crtc_state->base.active) {
16061 dev_priv->active_crtcs |= 1 << crtc->pipe;
16062
Clint Taylorc89e39f2016-05-13 23:41:21 +030016063 if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016064 pixclk = ilk_pipe_pixel_rate(crtc_state);
Ville Syrjälä9558d152016-05-13 23:41:20 +030016065 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016066 pixclk = crtc_state->base.adjusted_mode.crtc_clock;
16067 else
16068 WARN_ON(dev_priv->display.modeset_calc_cdclk);
Ville Syrjälä9558d152016-05-13 23:41:20 +030016069
16070 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
16071 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
16072 pixclk = DIV_ROUND_UP(pixclk * 100, 95);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016073 }
16074
16075 dev_priv->min_pixclk[crtc->pipe] = pixclk;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030016076
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030016077 readout_plane_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020016078
Ville Syrjälä78108b72016-05-27 20:59:19 +030016079 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
16080 crtc->base.base.id, crtc->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020016081 crtc->active ? "enabled" : "disabled");
16082 }
16083
Daniel Vetter53589012013-06-05 13:34:16 +020016084 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
16085 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
16086
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020016087 pll->on = pll->funcs.get_hw_state(dev_priv, pll,
16088 &pll->config.hw_state);
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020016089 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010016090 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010016091 if (crtc->active && crtc->config->shared_dpll == pll)
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020016092 pll->config.crtc_mask |= 1 << crtc->pipe;
Daniel Vetter53589012013-06-05 13:34:16 +020016093 }
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010016094 pll->active_mask = pll->config.crtc_mask;
Daniel Vetter53589012013-06-05 13:34:16 +020016095
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020016096 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020016097 pll->name, pll->config.crtc_mask, pll->on);
Daniel Vetter53589012013-06-05 13:34:16 +020016098 }
16099
Damien Lespiaub2784e12014-08-05 11:29:37 +010016100 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020016101 pipe = 0;
16102
16103 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070016104 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
16105 encoder->base.crtc = &crtc->base;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020016106 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020016107 } else {
16108 encoder->base.crtc = NULL;
16109 }
16110
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010016111 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020016112 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030016113 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020016114 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010016115 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020016116 }
16117
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020016118 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020016119 if (connector->get_hw_state(connector)) {
16120 connector->base.dpms = DRM_MODE_DPMS_ON;
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010016121
16122 encoder = connector->encoder;
16123 connector->base.encoder = &encoder->base;
16124
16125 if (encoder->base.crtc &&
16126 encoder->base.crtc->state->active) {
16127 /*
16128 * This has to be done during hardware readout
16129 * because anything calling .crtc_disable may
16130 * rely on the connector_mask being accurate.
16131 */
16132 encoder->base.crtc->state->connector_mask |=
16133 1 << drm_connector_index(&connector->base);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +010016134 encoder->base.crtc->state->encoder_mask |=
16135 1 << drm_encoder_index(&encoder->base);
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010016136 }
16137
Daniel Vetter24929352012-07-02 20:28:59 +020016138 } else {
16139 connector->base.dpms = DRM_MODE_DPMS_OFF;
16140 connector->base.encoder = NULL;
16141 }
16142 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
16143 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030016144 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020016145 connector->base.encoder ? "enabled" : "disabled");
16146 }
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030016147
16148 for_each_intel_crtc(dev, crtc) {
16149 crtc->base.hwmode = crtc->config->base.adjusted_mode;
16150
16151 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
16152 if (crtc->base.state->active) {
16153 intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
16154 intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
16155 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
16156
16157 /*
16158 * The initial mode needs to be set in order to keep
16159 * the atomic core happy. It wants a valid mode if the
16160 * crtc's enabled, so we do the above call.
16161 *
16162 * At this point some state updated by the connectors
16163 * in their ->detect() callback has not run yet, so
16164 * no recalculation can be done yet.
16165 *
16166 * Even if we could do a recalculation and modeset
16167 * right now it would cause a double modeset if
16168 * fbdev or userspace chooses a different initial mode.
16169 *
16170 * If that happens, someone indicated they wanted a
16171 * mode change, which means it's safe to do a full
16172 * recalculation.
16173 */
16174 crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
Ville Syrjälä9eca68322015-09-10 18:59:10 +030016175
16176 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
16177 update_scanline_offset(crtc);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030016178 }
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020016179
16180 intel_pipe_config_sanity_check(dev_priv, crtc->config);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030016181 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020016182}
16183
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016184/* Scan out the current hw modeset state,
16185 * and sanitizes it to the current state
16186 */
16187static void
16188intel_modeset_setup_hw_state(struct drm_device *dev)
Daniel Vetter30e984d2013-06-05 13:34:17 +020016189{
16190 struct drm_i915_private *dev_priv = dev->dev_private;
16191 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020016192 struct intel_crtc *crtc;
16193 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020016194 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020016195
16196 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020016197
16198 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010016199 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020016200 intel_sanitize_encoder(encoder);
16201 }
16202
Damien Lespiau055e3932014-08-18 13:49:10 +010016203 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020016204 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
16205 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020016206 intel_dump_pipe_config(crtc, crtc->config,
16207 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020016208 }
Daniel Vetter9a935852012-07-05 22:34:27 +020016209
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020016210 intel_modeset_update_connector_atomic_state(dev);
16211
Daniel Vetter35c95372013-07-17 06:55:04 +020016212 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
16213 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
16214
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010016215 if (!pll->on || pll->active_mask)
Daniel Vetter35c95372013-07-17 06:55:04 +020016216 continue;
16217
16218 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
16219
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020016220 pll->funcs.disable(dev_priv, pll);
Daniel Vetter35c95372013-07-17 06:55:04 +020016221 pll->on = false;
16222 }
16223
Wayne Boyer666a4532015-12-09 12:29:35 -080016224 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Ville Syrjälä6eb1a682015-06-24 22:00:03 +030016225 vlv_wm_get_hw_state(dev);
16226 else if (IS_GEN9(dev))
Pradeep Bhat30789992014-11-04 17:06:45 +000016227 skl_wm_get_hw_state(dev);
16228 else if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030016229 ilk_wm_get_hw_state(dev);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020016230
16231 for_each_intel_crtc(dev, crtc) {
16232 unsigned long put_domains;
16233
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +010016234 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020016235 if (WARN_ON(put_domains))
16236 modeset_put_power_domains(dev_priv, put_domains);
16237 }
16238 intel_display_set_init_power(dev_priv, false);
Paulo Zanoni010cf732016-01-19 11:35:48 -020016239
16240 intel_fbc_init_pipe_state(dev_priv);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016241}
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030016242
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016243void intel_display_resume(struct drm_device *dev)
16244{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016245 struct drm_i915_private *dev_priv = to_i915(dev);
16246 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
16247 struct drm_modeset_acquire_ctx ctx;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016248 int ret;
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016249 bool setup = false;
Daniel Vetterf30da182013-04-11 20:22:50 +020016250
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016251 dev_priv->modeset_restore_state = NULL;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016252
Maarten Lankhorstea49c9a2016-02-16 15:27:42 +010016253 /*
16254 * This is a cludge because with real atomic modeset mode_config.mutex
16255 * won't be taken. Unfortunately some probed state like
16256 * audio_codec_enable is still protected by mode_config.mutex, so lock
16257 * it here for now.
16258 */
16259 mutex_lock(&dev->mode_config.mutex);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016260 drm_modeset_acquire_init(&ctx, 0);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016261
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016262retry:
16263 ret = drm_modeset_lock_all_ctx(dev, &ctx);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016264
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016265 if (ret == 0 && !setup) {
16266 setup = true;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016267
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016268 intel_modeset_setup_hw_state(dev);
16269 i915_redisable_vga(dev);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010016270 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020016271
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016272 if (ret == 0 && state) {
16273 struct drm_crtc_state *crtc_state;
16274 struct drm_crtc *crtc;
16275 int i;
16276
16277 state->acquire_ctx = &ctx;
16278
Ville Syrjäläe3d54572016-05-13 10:10:42 -070016279 /* ignore any reset values/BIOS leftovers in the WM registers */
16280 to_intel_atomic_state(state)->skip_intermediate_wm = true;
16281
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016282 for_each_crtc_in_state(state, crtc, crtc_state, i) {
16283 /*
16284 * Force recalculation even if we restore
16285 * current state. With fast modeset this may not result
16286 * in a modeset when the state is compatible.
16287 */
16288 crtc_state->mode_changed = true;
16289 }
16290
16291 ret = drm_atomic_commit(state);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016292 }
16293
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016294 if (ret == -EDEADLK) {
16295 drm_modeset_backoff(&ctx);
16296 goto retry;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016297 }
16298
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016299 drm_modeset_drop_locks(&ctx);
16300 drm_modeset_acquire_fini(&ctx);
Maarten Lankhorstea49c9a2016-02-16 15:27:42 +010016301 mutex_unlock(&dev->mode_config.mutex);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016302
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016303 if (ret) {
16304 DRM_ERROR("Restoring old state failed with %i\n", ret);
16305 drm_atomic_state_free(state);
16306 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010016307}
16308
16309void intel_modeset_gem_init(struct drm_device *dev)
16310{
Chris Wilsondc979972016-05-10 14:10:04 +010016311 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080016312 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070016313 struct drm_i915_gem_object *obj;
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010016314 int ret;
Jesse Barnes484b41d2014-03-07 08:57:55 -080016315
Chris Wilsondc979972016-05-10 14:10:04 +010016316 intel_init_gt_powersave(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +030016317
Chris Wilson1833b132012-05-09 11:56:28 +010016318 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020016319
Chris Wilson1ee8da62016-05-12 12:43:23 +010016320 intel_setup_overlay(dev_priv);
Jesse Barnes484b41d2014-03-07 08:57:55 -080016321
16322 /*
16323 * Make sure any fbs we allocated at startup are properly
16324 * pinned & fenced. When we do the allocation it's too early
16325 * for this.
16326 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010016327 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070016328 obj = intel_fb_obj(c->primary->fb);
16329 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080016330 continue;
16331
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010016332 mutex_lock(&dev->struct_mutex);
Ville Syrjälä3465c582016-02-15 22:54:43 +020016333 ret = intel_pin_and_fence_fb_obj(c->primary->fb,
16334 c->primary->state->rotation);
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010016335 mutex_unlock(&dev->struct_mutex);
16336 if (ret) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080016337 DRM_ERROR("failed to pin boot fb on pipe %d\n",
16338 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100016339 drm_framebuffer_unreference(c->primary->fb);
Daniel Vetter5a21b662016-05-24 17:13:53 +020016340 c->primary->fb = NULL;
Maarten Lankhorst36750f22015-06-01 12:49:54 +020016341 c->primary->crtc = c->primary->state->crtc = NULL;
Daniel Vetter5a21b662016-05-24 17:13:53 +020016342 update_state_fb(c->primary);
Maarten Lankhorst36750f22015-06-01 12:49:54 +020016343 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
Jesse Barnes484b41d2014-03-07 08:57:55 -080016344 }
16345 }
Chris Wilson1ebaa0b2016-06-24 14:00:15 +010016346}
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020016347
Chris Wilson1ebaa0b2016-06-24 14:00:15 +010016348int intel_connector_register(struct drm_connector *connector)
16349{
16350 struct intel_connector *intel_connector = to_intel_connector(connector);
16351 int ret;
16352
16353 ret = intel_backlight_device_register(intel_connector);
16354 if (ret)
16355 goto err;
16356
16357 return 0;
16358
16359err:
16360 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080016361}
16362
Chris Wilsonc191eca2016-06-17 11:40:33 +010016363void intel_connector_unregister(struct drm_connector *connector)
Imre Deak4932e2c2014-02-11 17:12:48 +020016364{
Chris Wilsone63d87c2016-06-17 11:40:34 +010016365 struct intel_connector *intel_connector = to_intel_connector(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020016366
Chris Wilsone63d87c2016-06-17 11:40:34 +010016367 intel_backlight_device_unregister(intel_connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020016368 intel_panel_destroy_backlight(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020016369}
16370
Jesse Barnes79e53942008-11-07 14:24:08 -080016371void intel_modeset_cleanup(struct drm_device *dev)
16372{
Jesse Barnes652c3932009-08-17 13:31:43 -070016373 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes652c3932009-08-17 13:31:43 -070016374
Chris Wilsondc979972016-05-10 14:10:04 +010016375 intel_disable_gt_powersave(dev_priv);
Imre Deak2eb52522014-11-19 15:30:05 +020016376
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016377 /*
16378 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020016379 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016380 * experience fancy races otherwise.
16381 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020016382 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070016383
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016384 /*
16385 * Due to the hpd irq storm handling the hotplug work can re-arm the
16386 * poll handlers. Hence disable polling after hpd handling is shut down.
16387 */
Keith Packardf87ea762010-10-03 19:36:26 -070016388 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016389
Jesse Barnes723bfd72010-10-07 16:01:13 -070016390 intel_unregister_dsm_handler();
16391
Paulo Zanonic937ab3e52016-01-19 11:35:46 -020016392 intel_fbc_global_disable(dev_priv);
Kristian Høgsberg69341a52009-11-11 12:19:17 -050016393
Chris Wilson1630fe72011-07-08 12:22:42 +010016394 /* flush any delayed tasks or pending work */
16395 flush_scheduled_work();
16396
Jesse Barnes79e53942008-11-07 14:24:08 -080016397 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010016398
Chris Wilson1ee8da62016-05-12 12:43:23 +010016399 intel_cleanup_overlay(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +030016400
Chris Wilsondc979972016-05-10 14:10:04 +010016401 intel_cleanup_gt_powersave(dev_priv);
Daniel Vetterf5949142016-01-13 11:55:28 +010016402
16403 intel_teardown_gmbus(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080016404}
16405
Chris Wilsondf0e9242010-09-09 16:20:55 +010016406void intel_connector_attach_encoder(struct intel_connector *connector,
16407 struct intel_encoder *encoder)
16408{
16409 connector->encoder = encoder;
16410 drm_mode_connector_attach_encoder(&connector->base,
16411 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080016412}
Dave Airlie28d52042009-09-21 14:33:58 +100016413
16414/*
16415 * set vga decode state - true == enable VGA decode
16416 */
16417int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
16418{
16419 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000016420 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100016421 u16 gmch_ctrl;
16422
Chris Wilson75fa0412014-02-07 18:37:02 -020016423 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
16424 DRM_ERROR("failed to read control word\n");
16425 return -EIO;
16426 }
16427
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020016428 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
16429 return 0;
16430
Dave Airlie28d52042009-09-21 14:33:58 +100016431 if (state)
16432 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
16433 else
16434 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020016435
16436 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
16437 DRM_ERROR("failed to write control word\n");
16438 return -EIO;
16439 }
16440
Dave Airlie28d52042009-09-21 14:33:58 +100016441 return 0;
16442}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016443
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016444struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016445
16446 u32 power_well_driver;
16447
Chris Wilson63b66e52013-08-08 15:12:06 +020016448 int num_transcoders;
16449
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016450 struct intel_cursor_error_state {
16451 u32 control;
16452 u32 position;
16453 u32 base;
16454 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010016455 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016456
16457 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020016458 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016459 u32 source;
Imre Deakf301b1e2014-04-18 15:55:04 +030016460 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010016461 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016462
16463 struct intel_plane_error_state {
16464 u32 control;
16465 u32 stride;
16466 u32 size;
16467 u32 pos;
16468 u32 addr;
16469 u32 surface;
16470 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010016471 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020016472
16473 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020016474 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020016475 enum transcoder cpu_transcoder;
16476
16477 u32 conf;
16478
16479 u32 htotal;
16480 u32 hblank;
16481 u32 hsync;
16482 u32 vtotal;
16483 u32 vblank;
16484 u32 vsync;
16485 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016486};
16487
16488struct intel_display_error_state *
Chris Wilsonc0336662016-05-06 15:40:21 +010016489intel_display_capture_error_state(struct drm_i915_private *dev_priv)
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016490{
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016491 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020016492 int transcoders[] = {
16493 TRANSCODER_A,
16494 TRANSCODER_B,
16495 TRANSCODER_C,
16496 TRANSCODER_EDP,
16497 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016498 int i;
16499
Chris Wilsonc0336662016-05-06 15:40:21 +010016500 if (INTEL_INFO(dev_priv)->num_pipes == 0)
Chris Wilson63b66e52013-08-08 15:12:06 +020016501 return NULL;
16502
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016503 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016504 if (error == NULL)
16505 return NULL;
16506
Chris Wilsonc0336662016-05-06 15:40:21 +010016507 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016508 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
16509
Damien Lespiau055e3932014-08-18 13:49:10 +010016510 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020016511 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020016512 __intel_display_power_is_enabled(dev_priv,
16513 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020016514 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016515 continue;
16516
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030016517 error->cursor[i].control = I915_READ(CURCNTR(i));
16518 error->cursor[i].position = I915_READ(CURPOS(i));
16519 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016520
16521 error->plane[i].control = I915_READ(DSPCNTR(i));
16522 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Chris Wilsonc0336662016-05-06 15:40:21 +010016523 if (INTEL_GEN(dev_priv) <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030016524 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016525 error->plane[i].pos = I915_READ(DSPPOS(i));
16526 }
Chris Wilsonc0336662016-05-06 15:40:21 +010016527 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
Paulo Zanonica291362013-03-06 20:03:14 -030016528 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc0336662016-05-06 15:40:21 +010016529 if (INTEL_GEN(dev_priv) >= 4) {
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016530 error->plane[i].surface = I915_READ(DSPSURF(i));
16531 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
16532 }
16533
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016534 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e2014-04-18 15:55:04 +030016535
Chris Wilsonc0336662016-05-06 15:40:21 +010016536 if (HAS_GMCH_DISPLAY(dev_priv))
Imre Deakf301b1e2014-04-18 15:55:04 +030016537 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020016538 }
16539
Jani Nikula4d1de972016-03-18 17:05:42 +020016540 /* Note: this does not include DSI transcoders. */
Chris Wilsonc0336662016-05-06 15:40:21 +010016541 error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +030016542 if (HAS_DDI(dev_priv))
Chris Wilson63b66e52013-08-08 15:12:06 +020016543 error->num_transcoders++; /* Account for eDP. */
16544
16545 for (i = 0; i < error->num_transcoders; i++) {
16546 enum transcoder cpu_transcoder = transcoders[i];
16547
Imre Deakddf9c532013-11-27 22:02:02 +020016548 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020016549 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020016550 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020016551 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016552 continue;
16553
Chris Wilson63b66e52013-08-08 15:12:06 +020016554 error->transcoder[i].cpu_transcoder = cpu_transcoder;
16555
16556 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
16557 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
16558 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
16559 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
16560 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
16561 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
16562 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016563 }
16564
16565 return error;
16566}
16567
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016568#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
16569
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016570void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016571intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016572 struct drm_device *dev,
16573 struct intel_display_error_state *error)
16574{
Damien Lespiau055e3932014-08-18 13:49:10 +010016575 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016576 int i;
16577
Chris Wilson63b66e52013-08-08 15:12:06 +020016578 if (!error)
16579 return;
16580
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016581 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020016582 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016583 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016584 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010016585 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016586 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020016587 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020016588 onoff(error->pipe[i].power_domain_on));
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016589 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e2014-04-18 15:55:04 +030016590 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016591
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016592 err_printf(m, "Plane [%d]:\n", i);
16593 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
16594 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016595 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016596 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
16597 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016598 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030016599 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016600 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016601 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016602 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
16603 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016604 }
16605
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016606 err_printf(m, "Cursor [%d]:\n", i);
16607 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
16608 err_printf(m, " POS: %08x\n", error->cursor[i].position);
16609 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016610 }
Chris Wilson63b66e52013-08-08 15:12:06 +020016611
16612 for (i = 0; i < error->num_transcoders; i++) {
Jani Nikulada205632016-03-15 21:51:10 +020016613 err_printf(m, "CPU transcoder: %s\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020016614 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020016615 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020016616 onoff(error->transcoder[i].power_domain_on));
Chris Wilson63b66e52013-08-08 15:12:06 +020016617 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
16618 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
16619 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
16620 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
16621 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
16622 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
16623 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
16624 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016625}