blob: f3b58829e3508e75b92a67faeeed1de4577dfd74 [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"
Imre Deakdb18b6a2016-03-24 12:41:40 +020039#include "intel_dsi.h"
Jesse Barnese5510fa2010-07-01 16:48:37 -070040#include "i915_trace.h"
Xi Ruoyao319c1d42015-03-12 20:16:32 +080041#include <drm/drm_atomic.h>
Matt Roperc196e1d2015-01-21 16:35:48 -080042#include <drm/drm_atomic_helper.h>
David Howells760285e2012-10-02 18:01:07 +010043#include <drm/drm_dp_helper.h>
44#include <drm/drm_crtc_helper.h>
Matt Roper465c1202014-05-29 08:06:54 -070045#include <drm/drm_plane_helper.h>
46#include <drm/drm_rect.h>
Keith Packardc0f372b32011-11-16 22:24:52 -080047#include <linux/dma_remapping.h>
Alex Goinsfd8e0582015-11-25 18:43:38 -080048#include <linux/reservation.h>
49#include <linux/dma-buf.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080050
Matt Roper465c1202014-05-29 08:06:54 -070051/* Primary plane formats for gen <= 3 */
Damien Lespiau568db4f2015-05-12 16:13:18 +010052static const uint32_t i8xx_primary_formats[] = {
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010053 DRM_FORMAT_C8,
54 DRM_FORMAT_RGB565,
Matt Roper465c1202014-05-29 08:06:54 -070055 DRM_FORMAT_XRGB1555,
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010056 DRM_FORMAT_XRGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070057};
58
59/* Primary plane formats for gen >= 4 */
Damien Lespiau568db4f2015-05-12 16:13:18 +010060static const uint32_t i965_primary_formats[] = {
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010061 DRM_FORMAT_C8,
62 DRM_FORMAT_RGB565,
63 DRM_FORMAT_XRGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070064 DRM_FORMAT_XBGR8888,
Damien Lespiau6c0fd452015-05-19 12:29:16 +010065 DRM_FORMAT_XRGB2101010,
66 DRM_FORMAT_XBGR2101010,
67};
68
69static const uint32_t skl_primary_formats[] = {
70 DRM_FORMAT_C8,
71 DRM_FORMAT_RGB565,
72 DRM_FORMAT_XRGB8888,
73 DRM_FORMAT_XBGR8888,
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010074 DRM_FORMAT_ARGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070075 DRM_FORMAT_ABGR8888,
76 DRM_FORMAT_XRGB2101010,
Matt Roper465c1202014-05-29 08:06:54 -070077 DRM_FORMAT_XBGR2101010,
Kumar, Maheshea916ea2015-09-03 16:17:09 +053078 DRM_FORMAT_YUYV,
79 DRM_FORMAT_YVYU,
80 DRM_FORMAT_UYVY,
81 DRM_FORMAT_VYUY,
Matt Roper465c1202014-05-29 08:06:54 -070082};
83
Matt Roper3d7d6512014-06-10 08:28:13 -070084/* Cursor formats */
85static const uint32_t intel_cursor_formats[] = {
86 DRM_FORMAT_ARGB8888,
87};
88
Jesse Barnesf1f644d2013-06-27 00:39:25 +030089static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020090 struct intel_crtc_state *pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +030091static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020092 struct intel_crtc_state *pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030093
Jesse Barneseb1bfe82014-02-12 12:26:25 -080094static int intel_framebuffer_init(struct drm_device *dev,
95 struct intel_framebuffer *ifb,
96 struct drm_mode_fb_cmd2 *mode_cmd,
97 struct drm_i915_gem_object *obj);
Daniel Vetter5b18e572014-04-24 23:55:06 +020098static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
99static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +0200100static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +0200101static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -0700102 struct intel_link_m_n *m_n,
103 struct intel_link_m_n *m2_n2);
Daniel Vetter29407aa2014-04-24 23:55:08 +0200104static void ironlake_set_pipeconf(struct drm_crtc *crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +0200105static void haswell_set_pipeconf(struct drm_crtc *crtc);
Jani Nikula391bf042016-03-18 17:05:40 +0200106static void haswell_set_pipemisc(struct drm_crtc *crtc);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200107static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200108 const struct intel_crtc_state *pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200109static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200110 const struct intel_crtc_state *pipe_config);
Maarten Lankhorst613d2b22015-07-21 13:28:58 +0200111static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
112static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
Chandra Konduru549e2bf2015-04-07 15:28:38 -0700113static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
114 struct intel_crtc_state *crtc_state);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +0200115static void skylake_pfit_enable(struct intel_crtc *crtc);
116static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
117static void ironlake_pfit_enable(struct intel_crtc *crtc);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +0200118static void intel_modeset_setup_hw_state(struct drm_device *dev);
Ville Syrjälä2622a082016-03-09 19:07:26 +0200119static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
Damien Lespiaue7457a92013-08-08 22:28:59 +0100120
Jesse Barnes79e53942008-11-07 14:24:08 -0800121typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -0400122 int min, max;
Jesse Barnes79e53942008-11-07 14:24:08 -0800123} intel_range_t;
124
125typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -0400126 int dot_limit;
127 int p2_slow, p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800128} intel_p2_t;
129
Ma Lingd4906092009-03-18 20:13:27 +0800130typedef struct intel_limit intel_limit_t;
131struct intel_limit {
Akshay Joshi0206e352011-08-16 15:34:10 -0400132 intel_range_t dot, vco, n, m, m1, m2, p, p1;
133 intel_p2_t p2;
Ma Lingd4906092009-03-18 20:13:27 +0800134};
Jesse Barnes79e53942008-11-07 14:24:08 -0800135
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300136/* returns HPLL frequency in kHz */
137static int valleyview_get_vco(struct drm_i915_private *dev_priv)
138{
139 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
140
141 /* Obtain SKU information */
142 mutex_lock(&dev_priv->sb_lock);
143 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
144 CCK_FUSE_HPLL_FREQ_MASK;
145 mutex_unlock(&dev_priv->sb_lock);
146
147 return vco_freq[hpll_freq] * 1000;
148}
149
Ville Syrjäläc30fec62016-03-04 21:43:02 +0200150int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
151 const char *name, u32 reg, int ref_freq)
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300152{
153 u32 val;
154 int divider;
155
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300156 mutex_lock(&dev_priv->sb_lock);
157 val = vlv_cck_read(dev_priv, reg);
158 mutex_unlock(&dev_priv->sb_lock);
159
160 divider = val & CCK_FREQUENCY_VALUES;
161
162 WARN((val & CCK_FREQUENCY_STATUS) !=
163 (divider << CCK_FREQUENCY_STATUS_SHIFT),
164 "%s change in progress\n", name);
165
Ville Syrjäläc30fec62016-03-04 21:43:02 +0200166 return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
167}
168
169static int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
170 const char *name, u32 reg)
171{
172 if (dev_priv->hpll_freq == 0)
173 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
174
175 return vlv_get_cck_clock(dev_priv, name, reg,
176 dev_priv->hpll_freq);
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300177}
178
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200179static int
180intel_pch_rawclk(struct drm_i915_private *dev_priv)
Daniel Vetterd2acd212012-10-20 20:57:43 +0200181{
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200182 return (I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK) * 1000;
Daniel Vetterd2acd212012-10-20 20:57:43 +0200183}
184
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200185static int
186intel_vlv_hrawclk(struct drm_i915_private *dev_priv)
Jani Nikula79e50a42015-08-26 10:58:20 +0300187{
Ville Syrjälä35d38d12016-03-02 17:22:16 +0200188 return vlv_get_cck_clock_hpll(dev_priv, "hrawclk",
189 CCK_DISPLAY_REF_CLOCK_CONTROL);
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200190}
191
192static int
193intel_g4x_hrawclk(struct drm_i915_private *dev_priv)
194{
Jani Nikula79e50a42015-08-26 10:58:20 +0300195 uint32_t clkcfg;
196
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200197 /* hrawclock is 1/4 the FSB frequency */
Jani Nikula79e50a42015-08-26 10:58:20 +0300198 clkcfg = I915_READ(CLKCFG);
199 switch (clkcfg & CLKCFG_FSB_MASK) {
200 case CLKCFG_FSB_400:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200201 return 100000;
Jani Nikula79e50a42015-08-26 10:58:20 +0300202 case CLKCFG_FSB_533:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200203 return 133333;
Jani Nikula79e50a42015-08-26 10:58:20 +0300204 case CLKCFG_FSB_667:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200205 return 166667;
Jani Nikula79e50a42015-08-26 10:58:20 +0300206 case CLKCFG_FSB_800:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200207 return 200000;
Jani Nikula79e50a42015-08-26 10:58:20 +0300208 case CLKCFG_FSB_1067:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200209 return 266667;
Jani Nikula79e50a42015-08-26 10:58:20 +0300210 case CLKCFG_FSB_1333:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200211 return 333333;
Jani Nikula79e50a42015-08-26 10:58:20 +0300212 /* these two are just a guess; one of them might be right */
213 case CLKCFG_FSB_1600:
214 case CLKCFG_FSB_1600_ALT:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200215 return 400000;
Jani Nikula79e50a42015-08-26 10:58:20 +0300216 default:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200217 return 133333;
Jani Nikula79e50a42015-08-26 10:58:20 +0300218 }
219}
220
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200221static void intel_update_rawclk(struct drm_i915_private *dev_priv)
222{
223 if (HAS_PCH_SPLIT(dev_priv))
224 dev_priv->rawclk_freq = intel_pch_rawclk(dev_priv);
225 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
226 dev_priv->rawclk_freq = intel_vlv_hrawclk(dev_priv);
227 else if (IS_G4X(dev_priv) || IS_PINEVIEW(dev_priv))
228 dev_priv->rawclk_freq = intel_g4x_hrawclk(dev_priv);
229 else
230 return; /* no rawclk on other platforms, or no need to know it */
231
232 DRM_DEBUG_DRIVER("rawclk rate: %d kHz\n", dev_priv->rawclk_freq);
233}
234
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300235static void intel_update_czclk(struct drm_i915_private *dev_priv)
236{
Wayne Boyer666a4532015-12-09 12:29:35 -0800237 if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300238 return;
239
240 dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
241 CCK_CZ_CLOCK_CONTROL);
242
243 DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
244}
245
Chris Wilson021357a2010-09-07 20:54:59 +0100246static inline u32 /* units of 100MHz */
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200247intel_fdi_link_freq(struct drm_i915_private *dev_priv,
248 const struct intel_crtc_state *pipe_config)
Chris Wilson021357a2010-09-07 20:54:59 +0100249{
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200250 if (HAS_DDI(dev_priv))
251 return pipe_config->port_clock; /* SPLL */
252 else if (IS_GEN5(dev_priv))
253 return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
Ville Syrjäläe3b247d2016-02-17 21:41:09 +0200254 else
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200255 return 270000;
Chris Wilson021357a2010-09-07 20:54:59 +0100256}
257
Daniel Vetter5d536e22013-07-06 12:52:06 +0200258static const intel_limit_t intel_limits_i8xx_dac = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400259 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200260 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200261 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400262 .m = { .min = 96, .max = 140 },
263 .m1 = { .min = 18, .max = 26 },
264 .m2 = { .min = 6, .max = 16 },
265 .p = { .min = 4, .max = 128 },
266 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700267 .p2 = { .dot_limit = 165000,
268 .p2_slow = 4, .p2_fast = 2 },
Keith Packarde4b36692009-06-05 19:22:17 -0700269};
270
Daniel Vetter5d536e22013-07-06 12:52:06 +0200271static const intel_limit_t intel_limits_i8xx_dvo = {
272 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200273 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200274 .n = { .min = 2, .max = 16 },
Daniel Vetter5d536e22013-07-06 12:52:06 +0200275 .m = { .min = 96, .max = 140 },
276 .m1 = { .min = 18, .max = 26 },
277 .m2 = { .min = 6, .max = 16 },
278 .p = { .min = 4, .max = 128 },
279 .p1 = { .min = 2, .max = 33 },
280 .p2 = { .dot_limit = 165000,
281 .p2_slow = 4, .p2_fast = 4 },
282};
283
Keith Packarde4b36692009-06-05 19:22:17 -0700284static const intel_limit_t intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400285 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200286 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200287 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400288 .m = { .min = 96, .max = 140 },
289 .m1 = { .min = 18, .max = 26 },
290 .m2 = { .min = 6, .max = 16 },
291 .p = { .min = 4, .max = 128 },
292 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700293 .p2 = { .dot_limit = 165000,
294 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700295};
Eric Anholt273e27c2011-03-30 13:01:10 -0700296
Keith Packarde4b36692009-06-05 19:22:17 -0700297static const intel_limit_t intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400298 .dot = { .min = 20000, .max = 400000 },
299 .vco = { .min = 1400000, .max = 2800000 },
300 .n = { .min = 1, .max = 6 },
301 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100302 .m1 = { .min = 8, .max = 18 },
303 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400304 .p = { .min = 5, .max = 80 },
305 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700306 .p2 = { .dot_limit = 200000,
307 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700308};
309
310static const intel_limit_t intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400311 .dot = { .min = 20000, .max = 400000 },
312 .vco = { .min = 1400000, .max = 2800000 },
313 .n = { .min = 1, .max = 6 },
314 .m = { .min = 70, .max = 120 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100315 .m1 = { .min = 8, .max = 18 },
316 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400317 .p = { .min = 7, .max = 98 },
318 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700319 .p2 = { .dot_limit = 112000,
320 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700321};
322
Eric Anholt273e27c2011-03-30 13:01:10 -0700323
Keith Packarde4b36692009-06-05 19:22:17 -0700324static const intel_limit_t intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700325 .dot = { .min = 25000, .max = 270000 },
326 .vco = { .min = 1750000, .max = 3500000},
327 .n = { .min = 1, .max = 4 },
328 .m = { .min = 104, .max = 138 },
329 .m1 = { .min = 17, .max = 23 },
330 .m2 = { .min = 5, .max = 11 },
331 .p = { .min = 10, .max = 30 },
332 .p1 = { .min = 1, .max = 3},
333 .p2 = { .dot_limit = 270000,
334 .p2_slow = 10,
335 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800336 },
Keith Packarde4b36692009-06-05 19:22:17 -0700337};
338
339static const intel_limit_t intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700340 .dot = { .min = 22000, .max = 400000 },
341 .vco = { .min = 1750000, .max = 3500000},
342 .n = { .min = 1, .max = 4 },
343 .m = { .min = 104, .max = 138 },
344 .m1 = { .min = 16, .max = 23 },
345 .m2 = { .min = 5, .max = 11 },
346 .p = { .min = 5, .max = 80 },
347 .p1 = { .min = 1, .max = 8},
348 .p2 = { .dot_limit = 165000,
349 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700350};
351
352static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700353 .dot = { .min = 20000, .max = 115000 },
354 .vco = { .min = 1750000, .max = 3500000 },
355 .n = { .min = 1, .max = 3 },
356 .m = { .min = 104, .max = 138 },
357 .m1 = { .min = 17, .max = 23 },
358 .m2 = { .min = 5, .max = 11 },
359 .p = { .min = 28, .max = 112 },
360 .p1 = { .min = 2, .max = 8 },
361 .p2 = { .dot_limit = 0,
362 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800363 },
Keith Packarde4b36692009-06-05 19:22:17 -0700364};
365
366static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700367 .dot = { .min = 80000, .max = 224000 },
368 .vco = { .min = 1750000, .max = 3500000 },
369 .n = { .min = 1, .max = 3 },
370 .m = { .min = 104, .max = 138 },
371 .m1 = { .min = 17, .max = 23 },
372 .m2 = { .min = 5, .max = 11 },
373 .p = { .min = 14, .max = 42 },
374 .p1 = { .min = 2, .max = 6 },
375 .p2 = { .dot_limit = 0,
376 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800377 },
Keith Packarde4b36692009-06-05 19:22:17 -0700378};
379
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500380static const intel_limit_t intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400381 .dot = { .min = 20000, .max = 400000},
382 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700383 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400384 .n = { .min = 3, .max = 6 },
385 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700386 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400387 .m1 = { .min = 0, .max = 0 },
388 .m2 = { .min = 0, .max = 254 },
389 .p = { .min = 5, .max = 80 },
390 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700391 .p2 = { .dot_limit = 200000,
392 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700393};
394
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500395static const intel_limit_t intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400396 .dot = { .min = 20000, .max = 400000 },
397 .vco = { .min = 1700000, .max = 3500000 },
398 .n = { .min = 3, .max = 6 },
399 .m = { .min = 2, .max = 256 },
400 .m1 = { .min = 0, .max = 0 },
401 .m2 = { .min = 0, .max = 254 },
402 .p = { .min = 7, .max = 112 },
403 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700404 .p2 = { .dot_limit = 112000,
405 .p2_slow = 14, .p2_fast = 14 },
Keith Packarde4b36692009-06-05 19:22:17 -0700406};
407
Eric Anholt273e27c2011-03-30 13:01:10 -0700408/* Ironlake / Sandybridge
409 *
410 * We calculate clock using (register_value + 2) for N/M1/M2, so here
411 * the range value for them is (actual_value - 2).
412 */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800413static const intel_limit_t intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700414 .dot = { .min = 25000, .max = 350000 },
415 .vco = { .min = 1760000, .max = 3510000 },
416 .n = { .min = 1, .max = 5 },
417 .m = { .min = 79, .max = 127 },
418 .m1 = { .min = 12, .max = 22 },
419 .m2 = { .min = 5, .max = 9 },
420 .p = { .min = 5, .max = 80 },
421 .p1 = { .min = 1, .max = 8 },
422 .p2 = { .dot_limit = 225000,
423 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700424};
425
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800426static const intel_limit_t intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700427 .dot = { .min = 25000, .max = 350000 },
428 .vco = { .min = 1760000, .max = 3510000 },
429 .n = { .min = 1, .max = 3 },
430 .m = { .min = 79, .max = 118 },
431 .m1 = { .min = 12, .max = 22 },
432 .m2 = { .min = 5, .max = 9 },
433 .p = { .min = 28, .max = 112 },
434 .p1 = { .min = 2, .max = 8 },
435 .p2 = { .dot_limit = 225000,
436 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800437};
438
439static const intel_limit_t intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700440 .dot = { .min = 25000, .max = 350000 },
441 .vco = { .min = 1760000, .max = 3510000 },
442 .n = { .min = 1, .max = 3 },
443 .m = { .min = 79, .max = 127 },
444 .m1 = { .min = 12, .max = 22 },
445 .m2 = { .min = 5, .max = 9 },
446 .p = { .min = 14, .max = 56 },
447 .p1 = { .min = 2, .max = 8 },
448 .p2 = { .dot_limit = 225000,
449 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800450};
451
Eric Anholt273e27c2011-03-30 13:01:10 -0700452/* LVDS 100mhz refclk limits. */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800453static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700454 .dot = { .min = 25000, .max = 350000 },
455 .vco = { .min = 1760000, .max = 3510000 },
456 .n = { .min = 1, .max = 2 },
457 .m = { .min = 79, .max = 126 },
458 .m1 = { .min = 12, .max = 22 },
459 .m2 = { .min = 5, .max = 9 },
460 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400461 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700462 .p2 = { .dot_limit = 225000,
463 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800464};
465
466static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700467 .dot = { .min = 25000, .max = 350000 },
468 .vco = { .min = 1760000, .max = 3510000 },
469 .n = { .min = 1, .max = 3 },
470 .m = { .min = 79, .max = 126 },
471 .m1 = { .min = 12, .max = 22 },
472 .m2 = { .min = 5, .max = 9 },
473 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400474 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700475 .p2 = { .dot_limit = 225000,
476 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800477};
478
Ville Syrjälädc730512013-09-24 21:26:30 +0300479static const intel_limit_t intel_limits_vlv = {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300480 /*
481 * These are the data rate limits (measured in fast clocks)
482 * since those are the strictest limits we have. The fast
483 * clock and actual rate limits are more relaxed, so checking
484 * them would make no difference.
485 */
486 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200487 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700488 .n = { .min = 1, .max = 7 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700489 .m1 = { .min = 2, .max = 3 },
490 .m2 = { .min = 11, .max = 156 },
Ville Syrjäläb99ab662013-09-24 21:26:26 +0300491 .p1 = { .min = 2, .max = 3 },
Ville Syrjälä5fdc9c492013-09-24 21:26:29 +0300492 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700493};
494
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300495static const intel_limit_t intel_limits_chv = {
496 /*
497 * These are the data rate limits (measured in fast clocks)
498 * since those are the strictest limits we have. The fast
499 * clock and actual rate limits are more relaxed, so checking
500 * them would make no difference.
501 */
502 .dot = { .min = 25000 * 5, .max = 540000 * 5},
Ville Syrjälä17fe1022015-02-26 21:01:52 +0200503 .vco = { .min = 4800000, .max = 6480000 },
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300504 .n = { .min = 1, .max = 1 },
505 .m1 = { .min = 2, .max = 2 },
506 .m2 = { .min = 24 << 22, .max = 175 << 22 },
507 .p1 = { .min = 2, .max = 4 },
508 .p2 = { .p2_slow = 1, .p2_fast = 14 },
509};
510
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200511static const intel_limit_t intel_limits_bxt = {
512 /* FIXME: find real dot limits */
513 .dot = { .min = 0, .max = INT_MAX },
Vandana Kannane6292552015-07-01 17:02:57 +0530514 .vco = { .min = 4800000, .max = 6700000 },
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200515 .n = { .min = 1, .max = 1 },
516 .m1 = { .min = 2, .max = 2 },
517 /* FIXME: find real m2 limits */
518 .m2 = { .min = 2 << 22, .max = 255 << 22 },
519 .p1 = { .min = 2, .max = 4 },
520 .p2 = { .p2_slow = 1, .p2_fast = 20 },
521};
522
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200523static bool
524needs_modeset(struct drm_crtc_state *state)
525{
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200526 return drm_atomic_crtc_needs_modeset(state);
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200527}
528
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300529/**
530 * Returns whether any output on the specified pipe is of the specified type
531 */
Damien Lespiau40935612014-10-29 11:16:59 +0000532bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300533{
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300534 struct drm_device *dev = crtc->base.dev;
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300535 struct intel_encoder *encoder;
536
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300537 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300538 if (encoder->type == type)
539 return true;
540
541 return false;
542}
543
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200544/**
545 * Returns whether any output on the specified pipe will have the specified
546 * type after a staged modeset is complete, i.e., the same as
547 * intel_pipe_has_type() but looking at encoder->new_crtc instead of
548 * encoder->crtc.
549 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200550static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
551 int type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200552{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200553 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +0300554 struct drm_connector *connector;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200555 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200556 struct intel_encoder *encoder;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200557 int i, num_connectors = 0;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200558
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +0300559 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200560 if (connector_state->crtc != crtc_state->base.crtc)
561 continue;
562
563 num_connectors++;
564
565 encoder = to_intel_encoder(connector_state->best_encoder);
566 if (encoder->type == type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200567 return true;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200568 }
569
570 WARN_ON(num_connectors == 0);
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200571
572 return false;
573}
574
Imre Deakdccbea32015-06-22 23:35:51 +0300575/*
576 * Platform specific helpers to calculate the port PLL loopback- (clock.m),
577 * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
578 * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
579 * The helpers' return value is the rate of the clock that is fed to the
580 * display engine's pipe which can be the above fast dot clock rate or a
581 * divided-down version of it.
582 */
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500583/* m1 is reserved as 0 in Pineview, n is a ring counter */
Imre Deakdccbea32015-06-22 23:35:51 +0300584static int pnv_calc_dpll_params(int refclk, intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800585{
Shaohua Li21778322009-02-23 15:19:16 +0800586 clock->m = clock->m2 + 2;
587 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200588 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300589 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300590 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
591 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300592
593 return clock->dot;
Shaohua Li21778322009-02-23 15:19:16 +0800594}
595
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200596static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
597{
598 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
599}
600
Imre Deakdccbea32015-06-22 23:35:51 +0300601static int i9xx_calc_dpll_params(int refclk, intel_clock_t *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800602{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200603 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800604 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200605 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300606 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300607 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
608 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300609
610 return clock->dot;
Jesse Barnes79e53942008-11-07 14:24:08 -0800611}
612
Imre Deakdccbea32015-06-22 23:35:51 +0300613static int vlv_calc_dpll_params(int refclk, intel_clock_t *clock)
Imre Deak589eca62015-06-22 23:35:50 +0300614{
615 clock->m = clock->m1 * clock->m2;
616 clock->p = clock->p1 * clock->p2;
617 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300618 return 0;
Imre Deak589eca62015-06-22 23:35:50 +0300619 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
620 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300621
622 return clock->dot / 5;
Imre Deak589eca62015-06-22 23:35:50 +0300623}
624
Imre Deakdccbea32015-06-22 23:35:51 +0300625int chv_calc_dpll_params(int refclk, intel_clock_t *clock)
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300626{
627 clock->m = clock->m1 * clock->m2;
628 clock->p = clock->p1 * clock->p2;
629 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300630 return 0;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300631 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
632 clock->n << 22);
633 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300634
635 return clock->dot / 5;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300636}
637
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800638#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800639/**
640 * Returns whether the given set of divisors are valid for a given refclk with
641 * the given connectors.
642 */
643
Chris Wilson1b894b52010-12-14 20:04:54 +0000644static bool intel_PLL_is_valid(struct drm_device *dev,
645 const intel_limit_t *limit,
646 const intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800647{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300648 if (clock->n < limit->n.min || limit->n.max < clock->n)
649 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800650 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400651 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800652 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400653 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800654 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400655 INTELPllInvalid("m1 out of range\n");
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300656
Wayne Boyer666a4532015-12-09 12:29:35 -0800657 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) &&
658 !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev))
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300659 if (clock->m1 <= clock->m2)
660 INTELPllInvalid("m1 <= m2\n");
661
Wayne Boyer666a4532015-12-09 12:29:35 -0800662 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev)) {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300663 if (clock->p < limit->p.min || limit->p.max < clock->p)
664 INTELPllInvalid("p out of range\n");
665 if (clock->m < limit->m.min || limit->m.max < clock->m)
666 INTELPllInvalid("m out of range\n");
667 }
668
Jesse Barnes79e53942008-11-07 14:24:08 -0800669 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400670 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800671 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
672 * connector, etc., rather than just a single range.
673 */
674 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400675 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800676
677 return true;
678}
679
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300680static int
681i9xx_select_p2_div(const intel_limit_t *limit,
682 const struct intel_crtc_state *crtc_state,
683 int target)
Jesse Barnes79e53942008-11-07 14:24:08 -0800684{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300685 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800686
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200687 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800688 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100689 * For LVDS just rely on its current settings for dual-channel.
690 * We haven't figured out how to reliably set up different
691 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800692 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100693 if (intel_is_dual_link_lvds(dev))
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300694 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800695 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300696 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800697 } else {
698 if (target < limit->p2.dot_limit)
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300699 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800700 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300701 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800702 }
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300703}
704
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200705/*
706 * Returns a set of divisors for the desired target clock with the given
707 * refclk, or FALSE. The returned values represent the clock equation:
708 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
709 *
710 * Target and reference clocks are specified in kHz.
711 *
712 * If match_clock is provided, then best_clock P divider must match the P
713 * divider from @match_clock used for LVDS downclocking.
714 */
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300715static bool
716i9xx_find_best_dpll(const intel_limit_t *limit,
717 struct intel_crtc_state *crtc_state,
718 int target, int refclk, intel_clock_t *match_clock,
719 intel_clock_t *best_clock)
720{
721 struct drm_device *dev = crtc_state->base.crtc->dev;
722 intel_clock_t clock;
723 int err = target;
Jesse Barnes79e53942008-11-07 14:24:08 -0800724
Akshay Joshi0206e352011-08-16 15:34:10 -0400725 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800726
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300727 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
728
Zhao Yakui42158662009-11-20 11:24:18 +0800729 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
730 clock.m1++) {
731 for (clock.m2 = limit->m2.min;
732 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200733 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800734 break;
735 for (clock.n = limit->n.min;
736 clock.n <= limit->n.max; clock.n++) {
737 for (clock.p1 = limit->p1.min;
738 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800739 int this_err;
740
Imre Deakdccbea32015-06-22 23:35:51 +0300741 i9xx_calc_dpll_params(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000742 if (!intel_PLL_is_valid(dev, limit,
743 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800744 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800745 if (match_clock &&
746 clock.p != match_clock->p)
747 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800748
749 this_err = abs(clock.dot - target);
750 if (this_err < err) {
751 *best_clock = clock;
752 err = this_err;
753 }
754 }
755 }
756 }
757 }
758
759 return (err != target);
760}
761
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200762/*
763 * Returns a set of divisors for the desired target clock with the given
764 * refclk, or FALSE. The returned values represent the clock equation:
765 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
766 *
767 * Target and reference clocks are specified in kHz.
768 *
769 * If match_clock is provided, then best_clock P divider must match the P
770 * divider from @match_clock used for LVDS downclocking.
771 */
Ma Lingd4906092009-03-18 20:13:27 +0800772static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200773pnv_find_best_dpll(const intel_limit_t *limit,
774 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200775 int target, int refclk, intel_clock_t *match_clock,
776 intel_clock_t *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200777{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300778 struct drm_device *dev = crtc_state->base.crtc->dev;
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200779 intel_clock_t clock;
780 int err = target;
781
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200782 memset(best_clock, 0, sizeof(*best_clock));
783
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300784 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
785
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200786 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
787 clock.m1++) {
788 for (clock.m2 = limit->m2.min;
789 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200790 for (clock.n = limit->n.min;
791 clock.n <= limit->n.max; clock.n++) {
792 for (clock.p1 = limit->p1.min;
793 clock.p1 <= limit->p1.max; clock.p1++) {
794 int this_err;
795
Imre Deakdccbea32015-06-22 23:35:51 +0300796 pnv_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800797 if (!intel_PLL_is_valid(dev, limit,
798 &clock))
799 continue;
800 if (match_clock &&
801 clock.p != match_clock->p)
802 continue;
803
804 this_err = abs(clock.dot - target);
805 if (this_err < err) {
806 *best_clock = clock;
807 err = this_err;
808 }
809 }
810 }
811 }
812 }
813
814 return (err != target);
815}
816
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +0200817/*
818 * Returns a set of divisors for the desired target clock with the given
819 * refclk, or FALSE. The returned values represent the clock equation:
820 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200821 *
822 * Target and reference clocks are specified in kHz.
823 *
824 * If match_clock is provided, then best_clock P divider must match the P
825 * divider from @match_clock used for LVDS downclocking.
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +0200826 */
Ma Lingd4906092009-03-18 20:13:27 +0800827static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200828g4x_find_best_dpll(const intel_limit_t *limit,
829 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200830 int target, int refclk, intel_clock_t *match_clock,
831 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800832{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300833 struct drm_device *dev = crtc_state->base.crtc->dev;
Ma Lingd4906092009-03-18 20:13:27 +0800834 intel_clock_t clock;
835 int max_n;
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300836 bool found = false;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400837 /* approximately equals target * 0.00585 */
838 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800839
840 memset(best_clock, 0, sizeof(*best_clock));
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300841
842 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
843
Ma Lingd4906092009-03-18 20:13:27 +0800844 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200845 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800846 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200847 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800848 for (clock.m1 = limit->m1.max;
849 clock.m1 >= limit->m1.min; clock.m1--) {
850 for (clock.m2 = limit->m2.max;
851 clock.m2 >= limit->m2.min; clock.m2--) {
852 for (clock.p1 = limit->p1.max;
853 clock.p1 >= limit->p1.min; clock.p1--) {
854 int this_err;
855
Imre Deakdccbea32015-06-22 23:35:51 +0300856 i9xx_calc_dpll_params(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000857 if (!intel_PLL_is_valid(dev, limit,
858 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800859 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000860
861 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800862 if (this_err < err_most) {
863 *best_clock = clock;
864 err_most = this_err;
865 max_n = clock.n;
866 found = true;
867 }
868 }
869 }
870 }
871 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800872 return found;
873}
Ma Lingd4906092009-03-18 20:13:27 +0800874
Imre Deakd5dd62b2015-03-17 11:40:03 +0200875/*
876 * Check if the calculated PLL configuration is more optimal compared to the
877 * best configuration and error found so far. Return the calculated error.
878 */
879static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
880 const intel_clock_t *calculated_clock,
881 const intel_clock_t *best_clock,
882 unsigned int best_error_ppm,
883 unsigned int *error_ppm)
884{
Imre Deak9ca3ba02015-03-17 11:40:05 +0200885 /*
886 * For CHV ignore the error and consider only the P value.
887 * Prefer a bigger P value based on HW requirements.
888 */
889 if (IS_CHERRYVIEW(dev)) {
890 *error_ppm = 0;
891
892 return calculated_clock->p > best_clock->p;
893 }
894
Imre Deak24be4e42015-03-17 11:40:04 +0200895 if (WARN_ON_ONCE(!target_freq))
896 return false;
897
Imre Deakd5dd62b2015-03-17 11:40:03 +0200898 *error_ppm = div_u64(1000000ULL *
899 abs(target_freq - calculated_clock->dot),
900 target_freq);
901 /*
902 * Prefer a better P value over a better (smaller) error if the error
903 * is small. Ensure this preference for future configurations too by
904 * setting the error to 0.
905 */
906 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
907 *error_ppm = 0;
908
909 return true;
910 }
911
912 return *error_ppm + 10 < best_error_ppm;
913}
914
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200915/*
916 * Returns a set of divisors for the desired target clock with the given
917 * refclk, or FALSE. The returned values represent the clock equation:
918 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
919 */
Zhenyu Wang2c072452009-06-05 15:38:42 +0800920static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200921vlv_find_best_dpll(const intel_limit_t *limit,
922 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200923 int target, int refclk, intel_clock_t *match_clock,
924 intel_clock_t *best_clock)
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700925{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200926 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300927 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300928 intel_clock_t clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300929 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300930 /* min update 19.2 MHz */
931 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300932 bool found = false;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700933
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300934 target *= 5; /* fast clock */
935
936 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700937
938 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300939 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300940 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300941 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300942 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300943 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700944 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300945 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Imre Deakd5dd62b2015-03-17 11:40:03 +0200946 unsigned int ppm;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300947
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300948 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
949 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300950
Imre Deakdccbea32015-06-22 23:35:51 +0300951 vlv_calc_dpll_params(refclk, &clock);
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300952
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300953 if (!intel_PLL_is_valid(dev, limit,
954 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300955 continue;
956
Imre Deakd5dd62b2015-03-17 11:40:03 +0200957 if (!vlv_PLL_is_optimal(dev, target,
958 &clock,
959 best_clock,
960 bestppm, &ppm))
961 continue;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300962
Imre Deakd5dd62b2015-03-17 11:40:03 +0200963 *best_clock = clock;
964 bestppm = ppm;
965 found = true;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700966 }
967 }
968 }
969 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700970
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300971 return found;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700972}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700973
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200974/*
975 * Returns a set of divisors for the desired target clock with the given
976 * refclk, or FALSE. The returned values represent the clock equation:
977 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
978 */
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300979static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200980chv_find_best_dpll(const intel_limit_t *limit,
981 struct intel_crtc_state *crtc_state,
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300982 int target, int refclk, intel_clock_t *match_clock,
983 intel_clock_t *best_clock)
984{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200985 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300986 struct drm_device *dev = crtc->base.dev;
Imre Deak9ca3ba02015-03-17 11:40:05 +0200987 unsigned int best_error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300988 intel_clock_t clock;
989 uint64_t m2;
990 int found = false;
991
992 memset(best_clock, 0, sizeof(*best_clock));
Imre Deak9ca3ba02015-03-17 11:40:05 +0200993 best_error_ppm = 1000000;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300994
995 /*
996 * Based on hardware doc, the n always set to 1, and m1 always
997 * set to 2. If requires to support 200Mhz refclk, we need to
998 * revisit this because n may not 1 anymore.
999 */
1000 clock.n = 1, clock.m1 = 2;
1001 target *= 5; /* fast clock */
1002
1003 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
1004 for (clock.p2 = limit->p2.p2_fast;
1005 clock.p2 >= limit->p2.p2_slow;
1006 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Imre Deak9ca3ba02015-03-17 11:40:05 +02001007 unsigned int error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001008
1009 clock.p = clock.p1 * clock.p2;
1010
1011 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
1012 clock.n) << 22, refclk * clock.m1);
1013
1014 if (m2 > INT_MAX/clock.m1)
1015 continue;
1016
1017 clock.m2 = m2;
1018
Imre Deakdccbea32015-06-22 23:35:51 +03001019 chv_calc_dpll_params(refclk, &clock);
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001020
1021 if (!intel_PLL_is_valid(dev, limit, &clock))
1022 continue;
1023
Imre Deak9ca3ba02015-03-17 11:40:05 +02001024 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
1025 best_error_ppm, &error_ppm))
1026 continue;
1027
1028 *best_clock = clock;
1029 best_error_ppm = error_ppm;
1030 found = true;
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001031 }
1032 }
1033
1034 return found;
1035}
1036
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001037bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
1038 intel_clock_t *best_clock)
1039{
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02001040 int refclk = 100000;
1041 const intel_limit_t *limit = &intel_limits_bxt;
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001042
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02001043 return chv_find_best_dpll(limit, crtc_state,
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001044 target_clock, refclk, NULL, best_clock);
1045}
1046
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001047bool intel_crtc_active(struct drm_crtc *crtc)
1048{
1049 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1050
1051 /* Be paranoid as we can arrive here with only partial
1052 * state retrieved from the hardware during setup.
1053 *
Damien Lespiau241bfc32013-09-25 16:45:37 +01001054 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001055 * as Haswell has gained clock readout/fastboot support.
1056 *
Dave Airlie66e514c2014-04-03 07:51:54 +10001057 * We can ditch the crtc->primary->fb check as soon as we can
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001058 * properly reconstruct framebuffers.
Matt Roperc3d1f432015-03-09 10:19:23 -07001059 *
1060 * FIXME: The intel_crtc->active here should be switched to
1061 * crtc->state->active once we have proper CRTC states wired up
1062 * for atomic.
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001063 */
Matt Roperc3d1f432015-03-09 10:19:23 -07001064 return intel_crtc->active && crtc->primary->state->fb &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001065 intel_crtc->config->base.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001066}
1067
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001068enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1069 enum pipe pipe)
1070{
1071 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1072 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1073
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001074 return intel_crtc->config->cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001075}
1076
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001077static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1078{
1079 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001080 i915_reg_t reg = PIPEDSL(pipe);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001081 u32 line1, line2;
1082 u32 line_mask;
1083
1084 if (IS_GEN2(dev))
1085 line_mask = DSL_LINEMASK_GEN2;
1086 else
1087 line_mask = DSL_LINEMASK_GEN3;
1088
1089 line1 = I915_READ(reg) & line_mask;
Daniel Vetter6adfb1e2015-07-07 09:10:40 +02001090 msleep(5);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001091 line2 = I915_READ(reg) & line_mask;
1092
1093 return line1 == line2;
1094}
1095
Keith Packardab7ad7f2010-10-03 00:33:06 -07001096/*
1097 * intel_wait_for_pipe_off - wait for pipe to turn off
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001098 * @crtc: crtc whose pipe to wait for
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001099 *
1100 * After disabling a pipe, we can't wait for vblank in the usual way,
1101 * spinning on the vblank interrupt status bit, since we won't actually
1102 * see an interrupt when the pipe is disabled.
1103 *
Keith Packardab7ad7f2010-10-03 00:33:06 -07001104 * On Gen4 and above:
1105 * wait for the pipe register state bit to turn off
1106 *
1107 * Otherwise:
1108 * wait for the display line value to settle (it usually
1109 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +01001110 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001111 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001112static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001113{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001114 struct drm_device *dev = crtc->base.dev;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001115 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001116 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001117 enum pipe pipe = crtc->pipe;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001118
Keith Packardab7ad7f2010-10-03 00:33:06 -07001119 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001120 i915_reg_t reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001121
Keith Packardab7ad7f2010-10-03 00:33:06 -07001122 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +01001123 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
1124 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001125 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001126 } else {
Keith Packardab7ad7f2010-10-03 00:33:06 -07001127 /* Wait for the display line to settle */
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001128 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001129 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001130 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001131}
1132
Jesse Barnesb24e7172011-01-04 15:09:30 -08001133/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +02001134void assert_pll(struct drm_i915_private *dev_priv,
1135 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001136{
Jesse Barnesb24e7172011-01-04 15:09:30 -08001137 u32 val;
1138 bool cur_state;
1139
Ville Syrjälä649636e2015-09-22 19:50:01 +03001140 val = I915_READ(DPLL(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001141 cur_state = !!(val & DPLL_VCO_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001142 I915_STATE_WARN(cur_state != state,
Jesse Barnesb24e7172011-01-04 15:09:30 -08001143 "PLL state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001144 onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001145}
Jesse Barnesb24e7172011-01-04 15:09:30 -08001146
Jani Nikula23538ef2013-08-27 15:12:22 +03001147/* XXX: the dsi pll is shared between MIPI DSI ports */
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +00001148void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
Jani Nikula23538ef2013-08-27 15:12:22 +03001149{
1150 u32 val;
1151 bool cur_state;
1152
Ville Syrjäläa5805162015-05-26 20:42:30 +03001153 mutex_lock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001154 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
Ville Syrjäläa5805162015-05-26 20:42:30 +03001155 mutex_unlock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001156
1157 cur_state = val & DSI_PLL_VCO_EN;
Rob Clarke2c719b2014-12-15 13:56:32 -05001158 I915_STATE_WARN(cur_state != state,
Jani Nikula23538ef2013-08-27 15:12:22 +03001159 "DSI PLL state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001160 onoff(state), onoff(cur_state));
Jani Nikula23538ef2013-08-27 15:12:22 +03001161}
Jani Nikula23538ef2013-08-27 15:12:22 +03001162
Jesse Barnes040484a2011-01-03 12:14:26 -08001163static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1164 enum pipe pipe, bool state)
1165{
Jesse Barnes040484a2011-01-03 12:14:26 -08001166 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001167 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1168 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001169
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001170 if (HAS_DDI(dev_priv)) {
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001171 /* DDI does not have a specific FDI_TX register */
Ville Syrjälä649636e2015-09-22 19:50:01 +03001172 u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
Paulo Zanoniad80a812012-10-24 16:06:19 -02001173 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001174 } else {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001175 u32 val = I915_READ(FDI_TX_CTL(pipe));
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001176 cur_state = !!(val & FDI_TX_ENABLE);
1177 }
Rob Clarke2c719b2014-12-15 13:56:32 -05001178 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001179 "FDI TX state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001180 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001181}
1182#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1183#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1184
1185static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1186 enum pipe pipe, bool state)
1187{
Jesse Barnes040484a2011-01-03 12:14:26 -08001188 u32 val;
1189 bool cur_state;
1190
Ville Syrjälä649636e2015-09-22 19:50:01 +03001191 val = I915_READ(FDI_RX_CTL(pipe));
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001192 cur_state = !!(val & FDI_RX_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001193 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001194 "FDI RX state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001195 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001196}
1197#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1198#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1199
1200static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1201 enum pipe pipe)
1202{
Jesse Barnes040484a2011-01-03 12:14:26 -08001203 u32 val;
1204
1205 /* ILK FDI PLL is always enabled */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001206 if (INTEL_INFO(dev_priv)->gen == 5)
Jesse Barnes040484a2011-01-03 12:14:26 -08001207 return;
1208
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001209 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001210 if (HAS_DDI(dev_priv))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001211 return;
1212
Ville Syrjälä649636e2015-09-22 19:50:01 +03001213 val = I915_READ(FDI_TX_CTL(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001214 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 -08001215}
1216
Daniel Vetter55607e82013-06-16 21:42:39 +02001217void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1218 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001219{
Jesse Barnes040484a2011-01-03 12:14:26 -08001220 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001221 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001222
Ville Syrjälä649636e2015-09-22 19:50:01 +03001223 val = I915_READ(FDI_RX_CTL(pipe));
Daniel Vetter55607e82013-06-16 21:42:39 +02001224 cur_state = !!(val & FDI_RX_PLL_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001225 I915_STATE_WARN(cur_state != state,
Daniel Vetter55607e82013-06-16 21:42:39 +02001226 "FDI RX PLL assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001227 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001228}
1229
Daniel Vetterb680c372014-09-19 18:27:27 +02001230void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1231 enum pipe pipe)
Jesse Barnesea0760c2011-01-04 15:09:32 -08001232{
Jani Nikulabedd4db2014-08-22 15:04:13 +03001233 struct drm_device *dev = dev_priv->dev;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001234 i915_reg_t pp_reg;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001235 u32 val;
1236 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001237 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001238
Jani Nikulabedd4db2014-08-22 15:04:13 +03001239 if (WARN_ON(HAS_DDI(dev)))
1240 return;
1241
1242 if (HAS_PCH_SPLIT(dev)) {
1243 u32 port_sel;
1244
Jesse Barnesea0760c2011-01-04 15:09:32 -08001245 pp_reg = PCH_PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001246 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1247
1248 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1249 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1250 panel_pipe = PIPE_B;
1251 /* XXX: else fix for eDP */
Wayne Boyer666a4532015-12-09 12:29:35 -08001252 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Jani Nikulabedd4db2014-08-22 15:04:13 +03001253 /* presumably write lock depends on pipe, not port select */
1254 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1255 panel_pipe = pipe;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001256 } else {
1257 pp_reg = PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001258 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1259 panel_pipe = PIPE_B;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001260 }
1261
1262 val = I915_READ(pp_reg);
1263 if (!(val & PANEL_POWER_ON) ||
Jani Nikulaec49ba22014-08-21 15:06:25 +03001264 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
Jesse Barnesea0760c2011-01-04 15:09:32 -08001265 locked = false;
1266
Rob Clarke2c719b2014-12-15 13:56:32 -05001267 I915_STATE_WARN(panel_pipe == pipe && locked,
Jesse Barnesea0760c2011-01-04 15:09:32 -08001268 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001269 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001270}
1271
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001272static void assert_cursor(struct drm_i915_private *dev_priv,
1273 enum pipe pipe, bool state)
1274{
1275 struct drm_device *dev = dev_priv->dev;
1276 bool cur_state;
1277
Paulo Zanonid9d82082014-02-27 16:30:56 -03001278 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä0b87c242015-09-22 19:47:51 +03001279 cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
Paulo Zanonid9d82082014-02-27 16:30:56 -03001280 else
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03001281 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001282
Rob Clarke2c719b2014-12-15 13:56:32 -05001283 I915_STATE_WARN(cur_state != state,
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001284 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001285 pipe_name(pipe), onoff(state), onoff(cur_state));
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001286}
1287#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1288#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1289
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001290void assert_pipe(struct drm_i915_private *dev_priv,
1291 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001292{
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001293 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001294 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1295 pipe);
Imre Deak4feed0e2016-02-12 18:55:14 +02001296 enum intel_display_power_domain power_domain;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001297
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001298 /* if we need the pipe quirk it must be always on */
1299 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1300 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetter8e636782012-01-22 01:36:48 +01001301 state = true;
1302
Imre Deak4feed0e2016-02-12 18:55:14 +02001303 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
1304 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001305 u32 val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni69310162013-01-29 16:35:19 -02001306 cur_state = !!(val & PIPECONF_ENABLE);
Imre Deak4feed0e2016-02-12 18:55:14 +02001307
1308 intel_display_power_put(dev_priv, power_domain);
1309 } else {
1310 cur_state = false;
Paulo Zanoni69310162013-01-29 16:35:19 -02001311 }
1312
Rob Clarke2c719b2014-12-15 13:56:32 -05001313 I915_STATE_WARN(cur_state != state,
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001314 "pipe %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001315 pipe_name(pipe), onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001316}
1317
Chris Wilson931872f2012-01-16 23:01:13 +00001318static void assert_plane(struct drm_i915_private *dev_priv,
1319 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001320{
Jesse Barnesb24e7172011-01-04 15:09:30 -08001321 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001322 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001323
Ville Syrjälä649636e2015-09-22 19:50:01 +03001324 val = I915_READ(DSPCNTR(plane));
Chris Wilson931872f2012-01-16 23:01:13 +00001325 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001326 I915_STATE_WARN(cur_state != state,
Chris Wilson931872f2012-01-16 23:01:13 +00001327 "plane %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001328 plane_name(plane), onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001329}
1330
Chris Wilson931872f2012-01-16 23:01:13 +00001331#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1332#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1333
Jesse Barnesb24e7172011-01-04 15:09:30 -08001334static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1335 enum pipe pipe)
1336{
Ville Syrjälä653e1022013-06-04 13:49:05 +03001337 struct drm_device *dev = dev_priv->dev;
Ville Syrjälä649636e2015-09-22 19:50:01 +03001338 int i;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001339
Ville Syrjälä653e1022013-06-04 13:49:05 +03001340 /* Primary planes are fixed to pipes on gen4+ */
1341 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001342 u32 val = I915_READ(DSPCNTR(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001343 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
Adam Jackson28c057942011-10-07 14:38:42 -04001344 "plane %c assertion failure, should be disabled but not\n",
1345 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001346 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001347 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001348
Jesse Barnesb24e7172011-01-04 15:09:30 -08001349 /* Need to check both planes against the pipe */
Damien Lespiau055e3932014-08-18 13:49:10 +01001350 for_each_pipe(dev_priv, i) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001351 u32 val = I915_READ(DSPCNTR(i));
1352 enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
Jesse Barnesb24e7172011-01-04 15:09:30 -08001353 DISPPLANE_SEL_PIPE_SHIFT;
Rob Clarke2c719b2014-12-15 13:56:32 -05001354 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001355 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1356 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001357 }
1358}
1359
Jesse Barnes19332d72013-03-28 09:55:38 -07001360static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1361 enum pipe pipe)
1362{
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001363 struct drm_device *dev = dev_priv->dev;
Ville Syrjälä649636e2015-09-22 19:50:01 +03001364 int sprite;
Jesse Barnes19332d72013-03-28 09:55:38 -07001365
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001366 if (INTEL_INFO(dev)->gen >= 9) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001367 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001368 u32 val = I915_READ(PLANE_CTL(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001369 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001370 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1371 sprite, pipe_name(pipe));
1372 }
Wayne Boyer666a4532015-12-09 12:29:35 -08001373 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001374 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001375 u32 val = I915_READ(SPCNTR(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001376 I915_STATE_WARN(val & SP_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001377 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +00001378 sprite_name(pipe, sprite), pipe_name(pipe));
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001379 }
1380 } else if (INTEL_INFO(dev)->gen >= 7) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001381 u32 val = I915_READ(SPRCTL(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001382 I915_STATE_WARN(val & SPRITE_ENABLE,
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001383 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001384 plane_name(pipe), pipe_name(pipe));
1385 } else if (INTEL_INFO(dev)->gen >= 5) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001386 u32 val = I915_READ(DVSCNTR(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001387 I915_STATE_WARN(val & DVS_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001388 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1389 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001390 }
1391}
1392
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001393static void assert_vblank_disabled(struct drm_crtc *crtc)
1394{
Rob Clarke2c719b2014-12-15 13:56:32 -05001395 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001396 drm_crtc_vblank_put(crtc);
1397}
1398
Ander Conselvan de Oliveira7abd4b32016-03-08 17:46:15 +02001399void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1400 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001401{
Jesse Barnes92f25842011-01-04 15:09:34 -08001402 u32 val;
1403 bool enabled;
1404
Ville Syrjälä649636e2015-09-22 19:50:01 +03001405 val = I915_READ(PCH_TRANSCONF(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001406 enabled = !!(val & TRANS_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001407 I915_STATE_WARN(enabled,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001408 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1409 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001410}
1411
Keith Packard4e634382011-08-06 10:39:45 -07001412static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1413 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001414{
1415 if ((val & DP_PORT_EN) == 0)
1416 return false;
1417
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001418 if (HAS_PCH_CPT(dev_priv)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001419 u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
Keith Packardf0575e92011-07-25 22:12:43 -07001420 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1421 return false;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001422 } else if (IS_CHERRYVIEW(dev_priv)) {
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001423 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1424 return false;
Keith Packardf0575e92011-07-25 22:12:43 -07001425 } else {
1426 if ((val & DP_PIPE_MASK) != (pipe << 30))
1427 return false;
1428 }
1429 return true;
1430}
1431
Keith Packard1519b992011-08-06 10:35:34 -07001432static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1433 enum pipe pipe, u32 val)
1434{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001435 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001436 return false;
1437
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001438 if (HAS_PCH_CPT(dev_priv)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001439 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001440 return false;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001441 } else if (IS_CHERRYVIEW(dev_priv)) {
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001442 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1443 return false;
Keith Packard1519b992011-08-06 10:35:34 -07001444 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001445 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001446 return false;
1447 }
1448 return true;
1449}
1450
1451static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1452 enum pipe pipe, u32 val)
1453{
1454 if ((val & LVDS_PORT_EN) == 0)
1455 return false;
1456
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001457 if (HAS_PCH_CPT(dev_priv)) {
Keith Packard1519b992011-08-06 10:35:34 -07001458 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1459 return false;
1460 } else {
1461 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1462 return false;
1463 }
1464 return true;
1465}
1466
1467static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1468 enum pipe pipe, u32 val)
1469{
1470 if ((val & ADPA_DAC_ENABLE) == 0)
1471 return false;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001472 if (HAS_PCH_CPT(dev_priv)) {
Keith Packard1519b992011-08-06 10:35:34 -07001473 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1474 return false;
1475 } else {
1476 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1477 return false;
1478 }
1479 return true;
1480}
1481
Jesse Barnes291906f2011-02-02 12:28:03 -08001482static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001483 enum pipe pipe, i915_reg_t reg,
1484 u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001485{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001486 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001487 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001488 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001489 i915_mmio_reg_offset(reg), pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001490
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001491 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001492 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001493 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001494}
1495
1496static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001497 enum pipe pipe, i915_reg_t reg)
Jesse Barnes291906f2011-02-02 12:28:03 -08001498{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001499 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001500 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001501 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001502 i915_mmio_reg_offset(reg), pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001503
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001504 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001505 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001506 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001507}
1508
1509static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1510 enum pipe pipe)
1511{
Jesse Barnes291906f2011-02-02 12:28:03 -08001512 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001513
Keith Packardf0575e92011-07-25 22:12:43 -07001514 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1515 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1516 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001517
Ville Syrjälä649636e2015-09-22 19:50:01 +03001518 val = I915_READ(PCH_ADPA);
Rob Clarke2c719b2014-12-15 13:56:32 -05001519 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001520 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001521 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001522
Ville Syrjälä649636e2015-09-22 19:50:01 +03001523 val = I915_READ(PCH_LVDS);
Rob Clarke2c719b2014-12-15 13:56:32 -05001524 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001525 "PCH LVDS 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
Paulo Zanonie2debe92013-02-18 19:00:27 -03001528 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1529 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1530 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001531}
1532
Ville Syrjäläd288f652014-10-28 13:20:22 +02001533static void vlv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001534 const struct intel_crtc_state *pipe_config)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001535{
Daniel Vetter426115c2013-07-11 22:13:42 +02001536 struct drm_device *dev = crtc->base.dev;
1537 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001538 enum pipe pipe = crtc->pipe;
1539 i915_reg_t reg = DPLL(pipe);
Ville Syrjäläd288f652014-10-28 13:20:22 +02001540 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001541
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001542 assert_pipe_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001543
Daniel Vetter87442f72013-06-06 00:52:17 +02001544 /* PLL is protected by panel, make sure we can write it */
Ville Syrjälä7d1a83c2016-03-15 16:39:58 +02001545 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001546
Daniel Vetter426115c2013-07-11 22:13:42 +02001547 I915_WRITE(reg, dpll);
1548 POSTING_READ(reg);
1549 udelay(150);
1550
1551 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001552 DRM_ERROR("DPLL %d failed to lock\n", pipe);
Daniel Vetter426115c2013-07-11 22:13:42 +02001553
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001554 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1555 POSTING_READ(DPLL_MD(pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001556}
1557
Ville Syrjäläd288f652014-10-28 13:20:22 +02001558static void chv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001559 const struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001560{
1561 struct drm_device *dev = crtc->base.dev;
1562 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001563 enum pipe pipe = crtc->pipe;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001564 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001565 u32 tmp;
1566
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001567 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001568
Ville Syrjälä7d1a83c2016-03-15 16:39:58 +02001569 /* PLL is protected by panel, make sure we can write it */
1570 assert_panel_unlocked(dev_priv, pipe);
1571
Ville Syrjäläa5805162015-05-26 20:42:30 +03001572 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001573
1574 /* Enable back the 10bit clock to display controller */
1575 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1576 tmp |= DPIO_DCLKP_EN;
1577 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1578
Ville Syrjälä54433e92015-05-26 20:42:31 +03001579 mutex_unlock(&dev_priv->sb_lock);
1580
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001581 /*
1582 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1583 */
1584 udelay(1);
1585
1586 /* Enable PLL */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001587 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001588
1589 /* Check PLL is locked */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001590 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001591 DRM_ERROR("PLL %d failed to lock\n", pipe);
1592
Ville Syrjäläc2317752016-03-15 16:39:56 +02001593 if (pipe != PIPE_A) {
1594 /*
1595 * WaPixelRepeatModeFixForC0:chv
1596 *
1597 * DPLLCMD is AWOL. Use chicken bits to propagate
1598 * the value from DPLLBMD to either pipe B or C.
1599 */
1600 I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
1601 I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
1602 I915_WRITE(CBR4_VLV, 0);
1603 dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
1604
1605 /*
1606 * DPLLB VGA mode also seems to cause problems.
1607 * We should always have it disabled.
1608 */
1609 WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
1610 } else {
1611 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1612 POSTING_READ(DPLL_MD(pipe));
1613 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001614}
1615
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001616static int intel_num_dvo_pipes(struct drm_device *dev)
1617{
1618 struct intel_crtc *crtc;
1619 int count = 0;
1620
1621 for_each_intel_crtc(dev, crtc)
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001622 count += crtc->base.state->active &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001623 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001624
1625 return count;
1626}
1627
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001628static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001629{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001630 struct drm_device *dev = crtc->base.dev;
1631 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001632 i915_reg_t reg = DPLL(crtc->pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001633 u32 dpll = crtc->config->dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001634
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001635 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001636
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001637 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001638 if (IS_MOBILE(dev) && !IS_I830(dev))
1639 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001640
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001641 /* Enable DVO 2x clock on both PLLs if necessary */
1642 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1643 /*
1644 * It appears to be important that we don't enable this
1645 * for the current pipe before otherwise configuring the
1646 * PLL. No idea how this should be handled if multiple
1647 * DVO outputs are enabled simultaneosly.
1648 */
1649 dpll |= DPLL_DVO_2X_MODE;
1650 I915_WRITE(DPLL(!crtc->pipe),
1651 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1652 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001653
Ville Syrjäläc2b63372015-10-07 22:08:25 +03001654 /*
1655 * Apparently we need to have VGA mode enabled prior to changing
1656 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1657 * dividers, even though the register value does change.
1658 */
1659 I915_WRITE(reg, 0);
1660
Ville Syrjälä8e7a65a2015-10-07 22:08:24 +03001661 I915_WRITE(reg, dpll);
1662
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001663 /* Wait for the clocks to stabilize. */
1664 POSTING_READ(reg);
1665 udelay(150);
1666
1667 if (INTEL_INFO(dev)->gen >= 4) {
1668 I915_WRITE(DPLL_MD(crtc->pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001669 crtc->config->dpll_hw_state.dpll_md);
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001670 } else {
1671 /* The pixel multiplier can only be updated once the
1672 * DPLL is enabled and the clocks are stable.
1673 *
1674 * So write it again.
1675 */
1676 I915_WRITE(reg, dpll);
1677 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001678
1679 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001680 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001681 POSTING_READ(reg);
1682 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001683 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001684 POSTING_READ(reg);
1685 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001686 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001687 POSTING_READ(reg);
1688 udelay(150); /* wait for warmup */
1689}
1690
1691/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001692 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001693 * @dev_priv: i915 private structure
1694 * @pipe: pipe PLL to disable
1695 *
1696 * Disable the PLL for @pipe, making sure the pipe is off first.
1697 *
1698 * Note! This is for pre-ILK only.
1699 */
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001700static void i9xx_disable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001701{
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001702 struct drm_device *dev = crtc->base.dev;
1703 struct drm_i915_private *dev_priv = dev->dev_private;
1704 enum pipe pipe = crtc->pipe;
1705
1706 /* Disable DVO 2x clock on both PLLs if necessary */
1707 if (IS_I830(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001708 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001709 !intel_num_dvo_pipes(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001710 I915_WRITE(DPLL(PIPE_B),
1711 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1712 I915_WRITE(DPLL(PIPE_A),
1713 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1714 }
1715
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001716 /* Don't disable pipe or pipe PLLs if needed */
1717 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1718 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001719 return;
1720
1721 /* Make sure the pipe isn't still relying on us */
1722 assert_pipe_disabled(dev_priv, pipe);
1723
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001724 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
Daniel Vetter50b44a42013-06-05 13:34:33 +02001725 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001726}
1727
Jesse Barnesf6071162013-10-01 10:41:38 -07001728static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1729{
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001730 u32 val;
Jesse Barnesf6071162013-10-01 10:41:38 -07001731
1732 /* Make sure the pipe isn't still relying on us */
1733 assert_pipe_disabled(dev_priv, pipe);
1734
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02001735 val = DPLL_INTEGRATED_REF_CLK_VLV |
1736 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1737 if (pipe != PIPE_A)
1738 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1739
Jesse Barnesf6071162013-10-01 10:41:38 -07001740 I915_WRITE(DPLL(pipe), val);
1741 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001742}
1743
1744static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1745{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001746 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001747 u32 val;
1748
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001749 /* Make sure the pipe isn't still relying on us */
1750 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001751
Ville Syrjälä60bfe442015-06-29 15:25:49 +03001752 val = DPLL_SSC_REF_CLK_CHV |
1753 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001754 if (pipe != PIPE_A)
1755 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02001756
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001757 I915_WRITE(DPLL(pipe), val);
1758 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001759
Ville Syrjäläa5805162015-05-26 20:42:30 +03001760 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd7520482014-04-09 13:28:59 +03001761
1762 /* Disable 10bit clock to display controller */
1763 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1764 val &= ~DPIO_DCLKP_EN;
1765 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1766
Ville Syrjäläa5805162015-05-26 20:42:30 +03001767 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001768}
1769
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001770void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001771 struct intel_digital_port *dport,
1772 unsigned int expected_mask)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001773{
1774 u32 port_mask;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001775 i915_reg_t dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001776
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001777 switch (dport->port) {
1778 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001779 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001780 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001781 break;
1782 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001783 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001784 dpll_reg = DPLL(0);
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001785 expected_mask <<= 4;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001786 break;
1787 case PORT_D:
1788 port_mask = DPLL_PORTD_READY_MASK;
1789 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001790 break;
1791 default:
1792 BUG();
1793 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001794
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001795 if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
1796 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1797 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
Jesse Barnes89b667f2013-04-18 14:51:36 -07001798}
1799
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001800static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1801 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001802{
Daniel Vetter23670b322012-11-01 09:15:30 +01001803 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001804 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001805 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001806 i915_reg_t reg;
1807 uint32_t val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001808
Jesse Barnes040484a2011-01-03 12:14:26 -08001809 /* Make sure PCH DPLL is enabled */
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02001810 assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
Jesse Barnes040484a2011-01-03 12:14:26 -08001811
1812 /* FDI must be feeding us bits for PCH ports */
1813 assert_fdi_tx_enabled(dev_priv, pipe);
1814 assert_fdi_rx_enabled(dev_priv, pipe);
1815
Daniel Vetter23670b322012-11-01 09:15:30 +01001816 if (HAS_PCH_CPT(dev)) {
1817 /* Workaround: Set the timing override bit before enabling the
1818 * pch transcoder. */
1819 reg = TRANS_CHICKEN2(pipe);
1820 val = I915_READ(reg);
1821 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1822 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001823 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001824
Daniel Vetterab9412b2013-05-03 11:49:46 +02001825 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001826 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001827 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001828
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001829 if (HAS_PCH_IBX(dev_priv)) {
Jesse Barnese9bcff52011-06-24 12:19:20 -07001830 /*
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001831 * Make the BPC in transcoder be consistent with
1832 * that in pipeconf reg. For HDMI we must use 8bpc
1833 * here for both 8bpc and 12bpc.
Jesse Barnese9bcff52011-06-24 12:19:20 -07001834 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001835 val &= ~PIPECONF_BPC_MASK;
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001836 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
1837 val |= PIPECONF_8BPC;
1838 else
1839 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001840 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001841
1842 val &= ~TRANS_INTERLACE_MASK;
1843 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001844 if (HAS_PCH_IBX(dev_priv) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001845 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001846 val |= TRANS_LEGACY_INTERLACED_ILK;
1847 else
1848 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001849 else
1850 val |= TRANS_PROGRESSIVE;
1851
Jesse Barnes040484a2011-01-03 12:14:26 -08001852 I915_WRITE(reg, val | TRANS_ENABLE);
1853 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001854 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08001855}
1856
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001857static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001858 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001859{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001860 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001861
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001862 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001863 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001864 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001865
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001866 /* Workaround: set timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001867 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01001868 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001869 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001870
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001871 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001872 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001873
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001874 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1875 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001876 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001877 else
1878 val |= TRANS_PROGRESSIVE;
1879
Daniel Vetterab9412b2013-05-03 11:49:46 +02001880 I915_WRITE(LPT_TRANSCONF, val);
1881 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02001882 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001883}
1884
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001885static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1886 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001887{
Daniel Vetter23670b322012-11-01 09:15:30 +01001888 struct drm_device *dev = dev_priv->dev;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001889 i915_reg_t reg;
1890 uint32_t val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001891
1892 /* FDI relies on the transcoder */
1893 assert_fdi_tx_disabled(dev_priv, pipe);
1894 assert_fdi_rx_disabled(dev_priv, pipe);
1895
Jesse Barnes291906f2011-02-02 12:28:03 -08001896 /* Ports must be off as well */
1897 assert_pch_ports_disabled(dev_priv, pipe);
1898
Daniel Vetterab9412b2013-05-03 11:49:46 +02001899 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001900 val = I915_READ(reg);
1901 val &= ~TRANS_ENABLE;
1902 I915_WRITE(reg, val);
1903 /* wait for PCH transcoder off, transcoder state */
1904 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001905 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01001906
Ville Syrjäläc4656132015-10-29 21:25:56 +02001907 if (HAS_PCH_CPT(dev)) {
Daniel Vetter23670b322012-11-01 09:15:30 +01001908 /* Workaround: Clear the timing override chicken bit again. */
1909 reg = TRANS_CHICKEN2(pipe);
1910 val = I915_READ(reg);
1911 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1912 I915_WRITE(reg, val);
1913 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001914}
1915
Paulo Zanoniab4d9662012-10-31 18:12:55 -02001916static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001917{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001918 u32 val;
1919
Daniel Vetterab9412b2013-05-03 11:49:46 +02001920 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001921 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02001922 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001923 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02001924 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001925 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001926
1927 /* Workaround: clear timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001928 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01001929 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001930 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Jesse Barnes92f25842011-01-04 15:09:34 -08001931}
1932
1933/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001934 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02001935 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08001936 *
Paulo Zanoni03722642014-01-17 13:51:09 -02001937 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001938 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08001939 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02001940static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001941{
Paulo Zanoni03722642014-01-17 13:51:09 -02001942 struct drm_device *dev = crtc->base.dev;
1943 struct drm_i915_private *dev_priv = dev->dev_private;
1944 enum pipe pipe = crtc->pipe;
Ville Syrjälä1a70a7282015-10-29 21:25:50 +02001945 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter1a240d42012-11-29 22:18:51 +01001946 enum pipe pch_transcoder;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001947 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001948 u32 val;
1949
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03001950 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
1951
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001952 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001953 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001954 assert_sprites_disabled(dev_priv, pipe);
1955
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001956 if (HAS_PCH_LPT(dev_priv))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001957 pch_transcoder = TRANSCODER_A;
1958 else
1959 pch_transcoder = pipe;
1960
Jesse Barnesb24e7172011-01-04 15:09:30 -08001961 /*
1962 * A pipe without a PLL won't actually be able to drive bits from
1963 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1964 * need the check.
1965 */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001966 if (HAS_GMCH_DISPLAY(dev_priv))
Jani Nikulaa65347b2015-11-27 12:21:46 +02001967 if (crtc->config->has_dsi_encoder)
Jani Nikula23538ef2013-08-27 15:12:22 +03001968 assert_dsi_pll_enabled(dev_priv);
1969 else
1970 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001971 else {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001972 if (crtc->config->has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08001973 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001974 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001975 assert_fdi_tx_pll_enabled(dev_priv,
1976 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08001977 }
1978 /* FIXME: assert CPU port conditions for SNB+ */
1979 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08001980
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001981 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001982 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02001983 if (val & PIPECONF_ENABLE) {
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001984 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1985 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
Chris Wilson00d70b12011-03-17 07:18:29 +00001986 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02001987 }
Chris Wilson00d70b12011-03-17 07:18:29 +00001988
1989 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02001990 POSTING_READ(reg);
Ville Syrjäläb7792d82015-12-14 18:23:43 +02001991
1992 /*
1993 * Until the pipe starts DSL will read as 0, which would cause
1994 * an apparent vblank timestamp jump, which messes up also the
1995 * frame count when it's derived from the timestamps. So let's
1996 * wait for the pipe to start properly before we call
1997 * drm_crtc_vblank_on()
1998 */
1999 if (dev->max_vblank_count == 0 &&
2000 wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
2001 DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08002002}
2003
2004/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002005 * intel_disable_pipe - disable a pipe, asserting requirements
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002006 * @crtc: crtc whose pipes is to be disabled
Jesse Barnesb24e7172011-01-04 15:09:30 -08002007 *
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002008 * Disable the pipe of @crtc, making sure that various hardware
2009 * specific requirements are met, if applicable, e.g. plane
2010 * disabled, panel fitter off, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002011 *
2012 * Will wait until the pipe has shut down before returning.
2013 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002014static void intel_disable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002015{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002016 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002017 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002018 enum pipe pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002019 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002020 u32 val;
2021
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03002022 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
2023
Jesse Barnesb24e7172011-01-04 15:09:30 -08002024 /*
2025 * Make sure planes won't keep trying to pump pixels to us,
2026 * or we might hang the display.
2027 */
2028 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002029 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07002030 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002031
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002032 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002033 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00002034 if ((val & PIPECONF_ENABLE) == 0)
2035 return;
2036
Ville Syrjälä67adc642014-08-15 01:21:57 +03002037 /*
2038 * Double wide has implications for planes
2039 * so best keep it disabled when not needed.
2040 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002041 if (crtc->config->double_wide)
Ville Syrjälä67adc642014-08-15 01:21:57 +03002042 val &= ~PIPECONF_DOUBLE_WIDE;
2043
2044 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002045 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2046 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Ville Syrjälä67adc642014-08-15 01:21:57 +03002047 val &= ~PIPECONF_ENABLE;
2048
2049 I915_WRITE(reg, val);
2050 if ((val & PIPECONF_ENABLE) == 0)
2051 intel_wait_for_pipe_off(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002052}
2053
Chris Wilson693db182013-03-05 14:52:39 +00002054static bool need_vtd_wa(struct drm_device *dev)
2055{
2056#ifdef CONFIG_INTEL_IOMMU
2057 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2058 return true;
2059#endif
2060 return false;
2061}
2062
Ville Syrjälä832be822016-01-12 21:08:33 +02002063static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
2064{
2065 return IS_GEN2(dev_priv) ? 2048 : 4096;
2066}
2067
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002068static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
2069 uint64_t fb_modifier, unsigned int cpp)
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002070{
2071 switch (fb_modifier) {
2072 case DRM_FORMAT_MOD_NONE:
2073 return cpp;
2074 case I915_FORMAT_MOD_X_TILED:
2075 if (IS_GEN2(dev_priv))
2076 return 128;
2077 else
2078 return 512;
2079 case I915_FORMAT_MOD_Y_TILED:
2080 if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
2081 return 128;
2082 else
2083 return 512;
2084 case I915_FORMAT_MOD_Yf_TILED:
2085 switch (cpp) {
2086 case 1:
2087 return 64;
2088 case 2:
2089 case 4:
2090 return 128;
2091 case 8:
2092 case 16:
2093 return 256;
2094 default:
2095 MISSING_CASE(cpp);
2096 return cpp;
2097 }
2098 break;
2099 default:
2100 MISSING_CASE(fb_modifier);
2101 return cpp;
2102 }
2103}
2104
Ville Syrjälä832be822016-01-12 21:08:33 +02002105unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
2106 uint64_t fb_modifier, unsigned int cpp)
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002107{
Ville Syrjälä832be822016-01-12 21:08:33 +02002108 if (fb_modifier == DRM_FORMAT_MOD_NONE)
2109 return 1;
2110 else
2111 return intel_tile_size(dev_priv) /
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002112 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002113}
2114
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002115/* Return the tile dimensions in pixel units */
2116static void intel_tile_dims(const struct drm_i915_private *dev_priv,
2117 unsigned int *tile_width,
2118 unsigned int *tile_height,
2119 uint64_t fb_modifier,
2120 unsigned int cpp)
2121{
2122 unsigned int tile_width_bytes =
2123 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
2124
2125 *tile_width = tile_width_bytes / cpp;
2126 *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
2127}
2128
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002129unsigned int
2130intel_fb_align_height(struct drm_device *dev, unsigned int height,
Ville Syrjälä832be822016-01-12 21:08:33 +02002131 uint32_t pixel_format, uint64_t fb_modifier)
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002132{
Ville Syrjälä832be822016-01-12 21:08:33 +02002133 unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
2134 unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
2135
2136 return ALIGN(height, tile_height);
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002137}
2138
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002139unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
2140{
2141 unsigned int size = 0;
2142 int i;
2143
2144 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
2145 size += rot_info->plane[i].width * rot_info->plane[i].height;
2146
2147 return size;
2148}
2149
Daniel Vetter75c82a52015-10-14 16:51:04 +02002150static void
Ville Syrjälä3465c582016-02-15 22:54:43 +02002151intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
2152 const struct drm_framebuffer *fb,
2153 unsigned int rotation)
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002154{
Ville Syrjälä2d7a2152016-02-15 22:54:47 +02002155 if (intel_rotation_90_or_270(rotation)) {
2156 *view = i915_ggtt_view_rotated;
2157 view->params.rotated = to_intel_framebuffer(fb)->rot_info;
2158 } else {
2159 *view = i915_ggtt_view_normal;
2160 }
2161}
2162
2163static void
2164intel_fill_fb_info(struct drm_i915_private *dev_priv,
2165 struct drm_framebuffer *fb)
2166{
2167 struct intel_rotation_info *info = &to_intel_framebuffer(fb)->rot_info;
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002168 unsigned int tile_size, tile_width, tile_height, cpp;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002169
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002170 tile_size = intel_tile_size(dev_priv);
2171
2172 cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002173 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2174 fb->modifier[0], cpp);
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002175
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002176 info->plane[0].width = DIV_ROUND_UP(fb->pitches[0], tile_width * cpp);
2177 info->plane[0].height = DIV_ROUND_UP(fb->height, tile_height);
Tvrtko Ursulin84fe03f2015-06-23 14:26:46 +01002178
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01002179 if (info->pixel_format == DRM_FORMAT_NV12) {
Ville Syrjälä832be822016-01-12 21:08:33 +02002180 cpp = drm_format_plane_cpp(fb->pixel_format, 1);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002181 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2182 fb->modifier[1], cpp);
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002183
Ville Syrjälä2d7a2152016-02-15 22:54:47 +02002184 info->uv_offset = fb->offsets[1];
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002185 info->plane[1].width = DIV_ROUND_UP(fb->pitches[1], tile_width * cpp);
2186 info->plane[1].height = DIV_ROUND_UP(fb->height / 2, tile_height);
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01002187 }
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002188}
2189
Ville Syrjälä603525d2016-01-12 21:08:37 +02002190static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002191{
2192 if (INTEL_INFO(dev_priv)->gen >= 9)
2193 return 256 * 1024;
Ville Syrjälä985b8bb2015-06-11 16:31:15 +03002194 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
Wayne Boyer666a4532015-12-09 12:29:35 -08002195 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002196 return 128 * 1024;
2197 else if (INTEL_INFO(dev_priv)->gen >= 4)
2198 return 4 * 1024;
2199 else
Ville Syrjälä44c59052015-06-11 16:31:16 +03002200 return 0;
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002201}
2202
Ville Syrjälä603525d2016-01-12 21:08:37 +02002203static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
2204 uint64_t fb_modifier)
2205{
2206 switch (fb_modifier) {
2207 case DRM_FORMAT_MOD_NONE:
2208 return intel_linear_alignment(dev_priv);
2209 case I915_FORMAT_MOD_X_TILED:
2210 if (INTEL_INFO(dev_priv)->gen >= 9)
2211 return 256 * 1024;
2212 return 0;
2213 case I915_FORMAT_MOD_Y_TILED:
2214 case I915_FORMAT_MOD_Yf_TILED:
2215 return 1 * 1024 * 1024;
2216 default:
2217 MISSING_CASE(fb_modifier);
2218 return 0;
2219 }
2220}
2221
Chris Wilson127bd2a2010-07-23 23:32:05 +01002222int
Ville Syrjälä3465c582016-02-15 22:54:43 +02002223intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
2224 unsigned int rotation)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002225{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002226 struct drm_device *dev = fb->dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00002227 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002228 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002229 struct i915_ggtt_view view;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002230 u32 alignment;
2231 int ret;
2232
Matt Roperebcdd392014-07-09 16:22:11 -07002233 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2234
Ville Syrjälä603525d2016-01-12 21:08:37 +02002235 alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002236
Ville Syrjälä3465c582016-02-15 22:54:43 +02002237 intel_fill_fb_ggtt_view(&view, fb, rotation);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002238
Chris Wilson693db182013-03-05 14:52:39 +00002239 /* Note that the w/a also requires 64 PTE of padding following the
2240 * bo. We currently fill all unused PTE with the shadow page and so
2241 * we should always have valid PTE following the scanout preventing
2242 * the VT-d warning.
2243 */
2244 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2245 alignment = 256 * 1024;
2246
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002247 /*
2248 * Global gtt pte registers are special registers which actually forward
2249 * writes to a chunk of system memory. Which means that there is no risk
2250 * that the register values disappear as soon as we call
2251 * intel_runtime_pm_put(), so it is correct to wrap only the
2252 * pin/unpin/fence and not more.
2253 */
2254 intel_runtime_pm_get(dev_priv);
2255
Maarten Lankhorst7580d772015-08-18 13:40:06 +02002256 ret = i915_gem_object_pin_to_display_plane(obj, alignment,
2257 &view);
Chris Wilson48b956c2010-09-14 12:50:34 +01002258 if (ret)
Maarten Lankhorstb26a6b32015-09-23 13:27:09 +02002259 goto err_pm;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002260
2261 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2262 * fence, whereas 965+ only requires a fence if using
2263 * framebuffer compression. For simplicity, we always install
2264 * a fence as the cost is not that onerous.
2265 */
Vivek Kasireddy98072162015-10-29 18:54:38 -07002266 if (view.type == I915_GGTT_VIEW_NORMAL) {
2267 ret = i915_gem_object_get_fence(obj);
2268 if (ret == -EDEADLK) {
2269 /*
2270 * -EDEADLK means there are no free fences
2271 * no pending flips.
2272 *
2273 * This is propagated to atomic, but it uses
2274 * -EDEADLK to force a locking recovery, so
2275 * change the returned error to -EBUSY.
2276 */
2277 ret = -EBUSY;
2278 goto err_unpin;
2279 } else if (ret)
2280 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002281
Vivek Kasireddy98072162015-10-29 18:54:38 -07002282 i915_gem_object_pin_fence(obj);
2283 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002284
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002285 intel_runtime_pm_put(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002286 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002287
2288err_unpin:
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002289 i915_gem_object_unpin_from_display_plane(obj, &view);
Maarten Lankhorstb26a6b32015-09-23 13:27:09 +02002290err_pm:
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002291 intel_runtime_pm_put(dev_priv);
Chris Wilson48b956c2010-09-14 12:50:34 +01002292 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002293}
2294
Ville Syrjälä3465c582016-02-15 22:54:43 +02002295static void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
Chris Wilson1690e1e2011-12-14 13:57:08 +01002296{
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002297 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002298 struct i915_ggtt_view view;
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002299
Matt Roperebcdd392014-07-09 16:22:11 -07002300 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2301
Ville Syrjälä3465c582016-02-15 22:54:43 +02002302 intel_fill_fb_ggtt_view(&view, fb, rotation);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002303
Vivek Kasireddy98072162015-10-29 18:54:38 -07002304 if (view.type == I915_GGTT_VIEW_NORMAL)
2305 i915_gem_object_unpin_fence(obj);
2306
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002307 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002308}
2309
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002310/*
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002311 * Adjust the tile offset by moving the difference into
2312 * the x/y offsets.
2313 *
2314 * Input tile dimensions and pitch must already be
2315 * rotated to match x and y, and in pixel units.
2316 */
2317static u32 intel_adjust_tile_offset(int *x, int *y,
2318 unsigned int tile_width,
2319 unsigned int tile_height,
2320 unsigned int tile_size,
2321 unsigned int pitch_tiles,
2322 u32 old_offset,
2323 u32 new_offset)
2324{
2325 unsigned int tiles;
2326
2327 WARN_ON(old_offset & (tile_size - 1));
2328 WARN_ON(new_offset & (tile_size - 1));
2329 WARN_ON(new_offset > old_offset);
2330
2331 tiles = (old_offset - new_offset) / tile_size;
2332
2333 *y += tiles / pitch_tiles * tile_height;
2334 *x += tiles % pitch_tiles * tile_width;
2335
2336 return new_offset;
2337}
2338
2339/*
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002340 * Computes the linear offset to the base tile and adjusts
2341 * x, y. bytes per pixel is assumed to be a power-of-two.
2342 *
2343 * In the 90/270 rotated case, x and y are assumed
2344 * to be already rotated to match the rotated GTT view, and
2345 * pitch is the tile_height aligned framebuffer height.
2346 */
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002347u32 intel_compute_tile_offset(int *x, int *y,
2348 const struct drm_framebuffer *fb, int plane,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002349 unsigned int pitch,
2350 unsigned int rotation)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002351{
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002352 const struct drm_i915_private *dev_priv = to_i915(fb->dev);
2353 uint64_t fb_modifier = fb->modifier[plane];
2354 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002355 u32 offset, offset_aligned, alignment;
2356
2357 alignment = intel_surf_alignment(dev_priv, fb_modifier);
2358 if (alignment)
2359 alignment--;
2360
Ville Syrjäläb5c65332016-01-12 21:08:31 +02002361 if (fb_modifier != DRM_FORMAT_MOD_NONE) {
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002362 unsigned int tile_size, tile_width, tile_height;
2363 unsigned int tile_rows, tiles, pitch_tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002364
Ville Syrjäläd8433102016-01-12 21:08:35 +02002365 tile_size = intel_tile_size(dev_priv);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002366 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2367 fb_modifier, cpp);
2368
2369 if (intel_rotation_90_or_270(rotation)) {
2370 pitch_tiles = pitch / tile_height;
2371 swap(tile_width, tile_height);
2372 } else {
2373 pitch_tiles = pitch / (tile_width * cpp);
2374 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002375
Ville Syrjäläd8433102016-01-12 21:08:35 +02002376 tile_rows = *y / tile_height;
2377 *y %= tile_height;
Chris Wilsonbc752862013-02-21 20:04:31 +00002378
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002379 tiles = *x / tile_width;
2380 *x %= tile_width;
Ville Syrjäläd8433102016-01-12 21:08:35 +02002381
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002382 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2383 offset_aligned = offset & ~alignment;
Chris Wilsonbc752862013-02-21 20:04:31 +00002384
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002385 intel_adjust_tile_offset(x, y, tile_width, tile_height,
2386 tile_size, pitch_tiles,
2387 offset, offset_aligned);
2388 } else {
Chris Wilsonbc752862013-02-21 20:04:31 +00002389 offset = *y * pitch + *x * cpp;
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002390 offset_aligned = offset & ~alignment;
2391
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002392 *y = (offset & alignment) / pitch;
2393 *x = ((offset & alignment) - *y * pitch) / cpp;
Chris Wilsonbc752862013-02-21 20:04:31 +00002394 }
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002395
2396 return offset_aligned;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002397}
2398
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002399static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002400{
2401 switch (format) {
2402 case DISPPLANE_8BPP:
2403 return DRM_FORMAT_C8;
2404 case DISPPLANE_BGRX555:
2405 return DRM_FORMAT_XRGB1555;
2406 case DISPPLANE_BGRX565:
2407 return DRM_FORMAT_RGB565;
2408 default:
2409 case DISPPLANE_BGRX888:
2410 return DRM_FORMAT_XRGB8888;
2411 case DISPPLANE_RGBX888:
2412 return DRM_FORMAT_XBGR8888;
2413 case DISPPLANE_BGRX101010:
2414 return DRM_FORMAT_XRGB2101010;
2415 case DISPPLANE_RGBX101010:
2416 return DRM_FORMAT_XBGR2101010;
2417 }
2418}
2419
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002420static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2421{
2422 switch (format) {
2423 case PLANE_CTL_FORMAT_RGB_565:
2424 return DRM_FORMAT_RGB565;
2425 default:
2426 case PLANE_CTL_FORMAT_XRGB_8888:
2427 if (rgb_order) {
2428 if (alpha)
2429 return DRM_FORMAT_ABGR8888;
2430 else
2431 return DRM_FORMAT_XBGR8888;
2432 } else {
2433 if (alpha)
2434 return DRM_FORMAT_ARGB8888;
2435 else
2436 return DRM_FORMAT_XRGB8888;
2437 }
2438 case PLANE_CTL_FORMAT_XRGB_2101010:
2439 if (rgb_order)
2440 return DRM_FORMAT_XBGR2101010;
2441 else
2442 return DRM_FORMAT_XRGB2101010;
2443 }
2444}
2445
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002446static bool
Daniel Vetterf6936e22015-03-26 12:17:05 +01002447intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2448 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002449{
2450 struct drm_device *dev = crtc->base.dev;
Paulo Zanoni3badb492015-09-23 12:52:23 -03002451 struct drm_i915_private *dev_priv = to_i915(dev);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002452 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002453 struct drm_i915_gem_object *obj = NULL;
2454 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Damien Lespiau2d140302015-02-05 17:22:18 +00002455 struct drm_framebuffer *fb = &plane_config->fb->base;
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002456 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2457 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2458 PAGE_SIZE);
2459
2460 size_aligned -= base_aligned;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002461
Chris Wilsonff2652e2014-03-10 08:07:02 +00002462 if (plane_config->size == 0)
2463 return false;
2464
Paulo Zanoni3badb492015-09-23 12:52:23 -03002465 /* If the FB is too big, just don't use it since fbdev is not very
2466 * important and we should probably use that space with FBC or other
2467 * features. */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002468 if (size_aligned * 2 > ggtt->stolen_usable_size)
Paulo Zanoni3badb492015-09-23 12:52:23 -03002469 return false;
2470
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002471 mutex_lock(&dev->struct_mutex);
2472
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002473 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2474 base_aligned,
2475 base_aligned,
2476 size_aligned);
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002477 if (!obj) {
2478 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002479 return false;
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002480 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002481
Damien Lespiau49af4492015-01-20 12:51:44 +00002482 obj->tiling_mode = plane_config->tiling;
2483 if (obj->tiling_mode == I915_TILING_X)
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002484 obj->stride = fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002485
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002486 mode_cmd.pixel_format = fb->pixel_format;
2487 mode_cmd.width = fb->width;
2488 mode_cmd.height = fb->height;
2489 mode_cmd.pitches[0] = fb->pitches[0];
Daniel Vetter18c52472015-02-10 17:16:09 +00002490 mode_cmd.modifier[0] = fb->modifier[0];
2491 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002492
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002493 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002494 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002495 DRM_DEBUG_KMS("intel fb init failed\n");
2496 goto out_unref_obj;
2497 }
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002498
Jesse Barnes46f297f2014-03-07 08:57:48 -08002499 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002500
Daniel Vetterf6936e22015-03-26 12:17:05 +01002501 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002502 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002503
2504out_unref_obj:
2505 drm_gem_object_unreference(&obj->base);
2506 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002507 return false;
2508}
2509
Matt Roperafd65eb2015-02-03 13:10:04 -08002510/* Update plane->state->fb to match plane->fb after driver-internal updates */
2511static void
2512update_state_fb(struct drm_plane *plane)
2513{
2514 if (plane->fb == plane->state->fb)
2515 return;
2516
2517 if (plane->state->fb)
2518 drm_framebuffer_unreference(plane->state->fb);
2519 plane->state->fb = plane->fb;
2520 if (plane->state->fb)
2521 drm_framebuffer_reference(plane->state->fb);
2522}
2523
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002524static void
Daniel Vetterf6936e22015-03-26 12:17:05 +01002525intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2526 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002527{
2528 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnesd9ceb812014-10-09 12:57:43 -07002529 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002530 struct drm_crtc *c;
2531 struct intel_crtc *i;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002532 struct drm_i915_gem_object *obj;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002533 struct drm_plane *primary = intel_crtc->base.primary;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002534 struct drm_plane_state *plane_state = primary->state;
Matt Roper200757f2015-12-03 11:37:36 -08002535 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
2536 struct intel_plane *intel_plane = to_intel_plane(primary);
Matt Roper0a8d8a82015-12-03 11:37:38 -08002537 struct intel_plane_state *intel_state =
2538 to_intel_plane_state(plane_state);
Daniel Vetter88595ac2015-03-26 12:42:24 +01002539 struct drm_framebuffer *fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002540
Damien Lespiau2d140302015-02-05 17:22:18 +00002541 if (!plane_config->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002542 return;
2543
Daniel Vetterf6936e22015-03-26 12:17:05 +01002544 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002545 fb = &plane_config->fb->base;
2546 goto valid_fb;
Damien Lespiauf55548b2015-02-05 18:30:20 +00002547 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002548
Damien Lespiau2d140302015-02-05 17:22:18 +00002549 kfree(plane_config->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002550
2551 /*
2552 * Failed to alloc the obj, check to see if we should share
2553 * an fb with another CRTC instead
2554 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002555 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002556 i = to_intel_crtc(c);
2557
2558 if (c == &intel_crtc->base)
2559 continue;
2560
Matt Roper2ff8fde2014-07-08 07:50:07 -07002561 if (!i->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002562 continue;
2563
Daniel Vetter88595ac2015-03-26 12:42:24 +01002564 fb = c->primary->fb;
2565 if (!fb)
Matt Roper2ff8fde2014-07-08 07:50:07 -07002566 continue;
2567
Daniel Vetter88595ac2015-03-26 12:42:24 +01002568 obj = intel_fb_obj(fb);
Matt Roper2ff8fde2014-07-08 07:50:07 -07002569 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002570 drm_framebuffer_reference(fb);
2571 goto valid_fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002572 }
2573 }
Daniel Vetter88595ac2015-03-26 12:42:24 +01002574
Matt Roper200757f2015-12-03 11:37:36 -08002575 /*
2576 * We've failed to reconstruct the BIOS FB. Current display state
2577 * indicates that the primary plane is visible, but has a NULL FB,
2578 * which will lead to problems later if we don't fix it up. The
2579 * simplest solution is to just disable the primary plane now and
2580 * pretend the BIOS never had it enabled.
2581 */
2582 to_intel_plane_state(plane_state)->visible = false;
2583 crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
Ville Syrjälä2622a082016-03-09 19:07:26 +02002584 intel_pre_disable_primary_noatomic(&intel_crtc->base);
Matt Roper200757f2015-12-03 11:37:36 -08002585 intel_plane->disable_plane(primary, &intel_crtc->base);
2586
Daniel Vetter88595ac2015-03-26 12:42:24 +01002587 return;
2588
2589valid_fb:
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002590 plane_state->src_x = 0;
2591 plane_state->src_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002592 plane_state->src_w = fb->width << 16;
2593 plane_state->src_h = fb->height << 16;
2594
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002595 plane_state->crtc_x = 0;
2596 plane_state->crtc_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002597 plane_state->crtc_w = fb->width;
2598 plane_state->crtc_h = fb->height;
2599
Matt Roper0a8d8a82015-12-03 11:37:38 -08002600 intel_state->src.x1 = plane_state->src_x;
2601 intel_state->src.y1 = plane_state->src_y;
2602 intel_state->src.x2 = plane_state->src_x + plane_state->src_w;
2603 intel_state->src.y2 = plane_state->src_y + plane_state->src_h;
2604 intel_state->dst.x1 = plane_state->crtc_x;
2605 intel_state->dst.y1 = plane_state->crtc_y;
2606 intel_state->dst.x2 = plane_state->crtc_x + plane_state->crtc_w;
2607 intel_state->dst.y2 = plane_state->crtc_y + plane_state->crtc_h;
2608
Daniel Vetter88595ac2015-03-26 12:42:24 +01002609 obj = intel_fb_obj(fb);
2610 if (obj->tiling_mode != I915_TILING_NONE)
2611 dev_priv->preserve_bios_swizzle = true;
2612
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002613 drm_framebuffer_reference(fb);
2614 primary->fb = primary->state->fb = fb;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002615 primary->crtc = primary->state->crtc = &intel_crtc->base;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002616 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
Ville Syrjäläa9ff8712015-06-24 21:59:34 +03002617 obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002618}
2619
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002620static void i9xx_update_primary_plane(struct drm_plane *primary,
2621 const struct intel_crtc_state *crtc_state,
2622 const struct intel_plane_state *plane_state)
Jesse Barnes81255562010-08-02 12:07:50 -07002623{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002624 struct drm_device *dev = primary->dev;
Jesse Barnes81255562010-08-02 12:07:50 -07002625 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002626 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2627 struct drm_framebuffer *fb = plane_state->base.fb;
2628 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Jesse Barnes81255562010-08-02 12:07:50 -07002629 int plane = intel_crtc->plane;
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002630 u32 linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002631 u32 dspcntr;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002632 i915_reg_t reg = DSPCNTR(plane);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002633 unsigned int rotation = plane_state->base.rotation;
Ville Syrjäläac484962016-01-20 21:05:26 +02002634 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002635 int x = plane_state->src.x1 >> 16;
2636 int y = plane_state->src.y1 >> 16;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002637
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002638 dspcntr = DISPPLANE_GAMMA_ENABLE;
2639
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002640 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002641
2642 if (INTEL_INFO(dev)->gen < 4) {
2643 if (intel_crtc->pipe == PIPE_B)
2644 dspcntr |= DISPPLANE_SEL_PIPE_B;
2645
2646 /* pipesrc and dspsize control the size that is scaled from,
2647 * which should always be the user's requested size.
2648 */
2649 I915_WRITE(DSPSIZE(plane),
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002650 ((crtc_state->pipe_src_h - 1) << 16) |
2651 (crtc_state->pipe_src_w - 1));
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002652 I915_WRITE(DSPPOS(plane), 0);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002653 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2654 I915_WRITE(PRIMSIZE(plane),
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002655 ((crtc_state->pipe_src_h - 1) << 16) |
2656 (crtc_state->pipe_src_w - 1));
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002657 I915_WRITE(PRIMPOS(plane), 0);
2658 I915_WRITE(PRIMCNSTALPHA(plane), 0);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002659 }
2660
Ville Syrjälä57779d02012-10-31 17:50:14 +02002661 switch (fb->pixel_format) {
2662 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002663 dspcntr |= DISPPLANE_8BPP;
2664 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002665 case DRM_FORMAT_XRGB1555:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002666 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002667 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002668 case DRM_FORMAT_RGB565:
2669 dspcntr |= DISPPLANE_BGRX565;
2670 break;
2671 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002672 dspcntr |= DISPPLANE_BGRX888;
2673 break;
2674 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002675 dspcntr |= DISPPLANE_RGBX888;
2676 break;
2677 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002678 dspcntr |= DISPPLANE_BGRX101010;
2679 break;
2680 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002681 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002682 break;
2683 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002684 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002685 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002686
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002687 if (INTEL_INFO(dev)->gen >= 4 &&
2688 obj->tiling_mode != I915_TILING_NONE)
2689 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07002690
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002691 if (IS_G4X(dev))
2692 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2693
Ville Syrjäläac484962016-01-20 21:05:26 +02002694 linear_offset = y * fb->pitches[0] + x * cpp;
Jesse Barnes81255562010-08-02 12:07:50 -07002695
Daniel Vetterc2c75132012-07-05 12:17:30 +02002696 if (INTEL_INFO(dev)->gen >= 4) {
2697 intel_crtc->dspaddr_offset =
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002698 intel_compute_tile_offset(&x, &y, fb, 0,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002699 fb->pitches[0], rotation);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002700 linear_offset -= intel_crtc->dspaddr_offset;
2701 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002702 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002703 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002704
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002705 if (rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302706 dspcntr |= DISPPLANE_ROTATE_180;
2707
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002708 x += (crtc_state->pipe_src_w - 1);
2709 y += (crtc_state->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302710
2711 /* Finding the last pixel of the last line of the display
2712 data and adding to linear_offset*/
2713 linear_offset +=
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002714 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
Ville Syrjäläac484962016-01-20 21:05:26 +02002715 (crtc_state->pipe_src_w - 1) * cpp;
Sonika Jindal48404c12014-08-22 14:06:04 +05302716 }
2717
Paulo Zanoni2db33662015-09-14 15:20:03 -03002718 intel_crtc->adjusted_x = x;
2719 intel_crtc->adjusted_y = y;
2720
Sonika Jindal48404c12014-08-22 14:06:04 +05302721 I915_WRITE(reg, dspcntr);
2722
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002723 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002724 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002725 I915_WRITE(DSPSURF(plane),
2726 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002727 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002728 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002729 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002730 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002731 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002732}
2733
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002734static void i9xx_disable_primary_plane(struct drm_plane *primary,
2735 struct drm_crtc *crtc)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002736{
2737 struct drm_device *dev = crtc->dev;
2738 struct drm_i915_private *dev_priv = dev->dev_private;
2739 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002740 int plane = intel_crtc->plane;
2741
2742 I915_WRITE(DSPCNTR(plane), 0);
2743 if (INTEL_INFO(dev_priv)->gen >= 4)
2744 I915_WRITE(DSPSURF(plane), 0);
2745 else
2746 I915_WRITE(DSPADDR(plane), 0);
2747 POSTING_READ(DSPCNTR(plane));
2748}
2749
2750static void ironlake_update_primary_plane(struct drm_plane *primary,
2751 const struct intel_crtc_state *crtc_state,
2752 const struct intel_plane_state *plane_state)
2753{
2754 struct drm_device *dev = primary->dev;
2755 struct drm_i915_private *dev_priv = dev->dev_private;
2756 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2757 struct drm_framebuffer *fb = plane_state->base.fb;
2758 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002759 int plane = intel_crtc->plane;
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002760 u32 linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002761 u32 dspcntr;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002762 i915_reg_t reg = DSPCNTR(plane);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002763 unsigned int rotation = plane_state->base.rotation;
Ville Syrjäläac484962016-01-20 21:05:26 +02002764 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002765 int x = plane_state->src.x1 >> 16;
2766 int y = plane_state->src.y1 >> 16;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002767
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002768 dspcntr = DISPPLANE_GAMMA_ENABLE;
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002769 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002770
2771 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2772 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
2773
Ville Syrjälä57779d02012-10-31 17:50:14 +02002774 switch (fb->pixel_format) {
2775 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002776 dspcntr |= DISPPLANE_8BPP;
2777 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002778 case DRM_FORMAT_RGB565:
2779 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002780 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002781 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002782 dspcntr |= DISPPLANE_BGRX888;
2783 break;
2784 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002785 dspcntr |= DISPPLANE_RGBX888;
2786 break;
2787 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002788 dspcntr |= DISPPLANE_BGRX101010;
2789 break;
2790 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002791 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002792 break;
2793 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002794 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002795 }
2796
2797 if (obj->tiling_mode != I915_TILING_NONE)
2798 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002799
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002800 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002801 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002802
Ville Syrjäläac484962016-01-20 21:05:26 +02002803 linear_offset = y * fb->pitches[0] + x * cpp;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002804 intel_crtc->dspaddr_offset =
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002805 intel_compute_tile_offset(&x, &y, fb, 0,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002806 fb->pitches[0], rotation);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002807 linear_offset -= intel_crtc->dspaddr_offset;
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002808 if (rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302809 dspcntr |= DISPPLANE_ROTATE_180;
2810
2811 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002812 x += (crtc_state->pipe_src_w - 1);
2813 y += (crtc_state->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302814
2815 /* Finding the last pixel of the last line of the display
2816 data and adding to linear_offset*/
2817 linear_offset +=
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002818 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
Ville Syrjäläac484962016-01-20 21:05:26 +02002819 (crtc_state->pipe_src_w - 1) * cpp;
Sonika Jindal48404c12014-08-22 14:06:04 +05302820 }
2821 }
2822
Paulo Zanoni2db33662015-09-14 15:20:03 -03002823 intel_crtc->adjusted_x = x;
2824 intel_crtc->adjusted_y = y;
2825
Sonika Jindal48404c12014-08-22 14:06:04 +05302826 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002827
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002828 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002829 I915_WRITE(DSPSURF(plane),
2830 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002831 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002832 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2833 } else {
2834 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2835 I915_WRITE(DSPLINOFF(plane), linear_offset);
2836 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002837 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002838}
2839
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002840u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
2841 uint64_t fb_modifier, uint32_t pixel_format)
Damien Lespiaub3218032015-02-27 11:15:18 +00002842{
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002843 if (fb_modifier == DRM_FORMAT_MOD_NONE) {
2844 return 64;
2845 } else {
2846 int cpp = drm_format_plane_cpp(pixel_format, 0);
Damien Lespiaub3218032015-02-27 11:15:18 +00002847
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002848 return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
Damien Lespiaub3218032015-02-27 11:15:18 +00002849 }
2850}
2851
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002852u32 intel_plane_obj_offset(struct intel_plane *intel_plane,
2853 struct drm_i915_gem_object *obj,
2854 unsigned int plane)
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002855{
Daniel Vetterce7f1722015-10-14 16:51:06 +02002856 struct i915_ggtt_view view;
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002857 struct i915_vma *vma;
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002858 u64 offset;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002859
Ville Syrjäläe7941292016-01-19 18:23:17 +02002860 intel_fill_fb_ggtt_view(&view, intel_plane->base.state->fb,
Ville Syrjälä3465c582016-02-15 22:54:43 +02002861 intel_plane->base.state->rotation);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002862
Daniel Vetterce7f1722015-10-14 16:51:06 +02002863 vma = i915_gem_obj_to_ggtt_view(obj, &view);
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002864 if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
Daniel Vetterce7f1722015-10-14 16:51:06 +02002865 view.type))
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002866 return -1;
2867
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002868 offset = vma->node.start;
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002869
2870 if (plane == 1) {
Ville Syrjälä7723f47d2016-01-20 21:05:22 +02002871 offset += vma->ggtt_view.params.rotated.uv_start_page *
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002872 PAGE_SIZE;
2873 }
2874
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002875 WARN_ON(upper_32_bits(offset));
2876
2877 return lower_32_bits(offset);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002878}
2879
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002880static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
2881{
2882 struct drm_device *dev = intel_crtc->base.dev;
2883 struct drm_i915_private *dev_priv = dev->dev_private;
2884
2885 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
2886 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
2887 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002888}
2889
Chandra Kondurua1b22782015-04-07 15:28:45 -07002890/*
2891 * This function detaches (aka. unbinds) unused scalers in hardware
2892 */
Maarten Lankhorst05832362015-06-15 12:33:48 +02002893static void skl_detach_scalers(struct intel_crtc *intel_crtc)
Chandra Kondurua1b22782015-04-07 15:28:45 -07002894{
Chandra Kondurua1b22782015-04-07 15:28:45 -07002895 struct intel_crtc_scaler_state *scaler_state;
2896 int i;
2897
Chandra Kondurua1b22782015-04-07 15:28:45 -07002898 scaler_state = &intel_crtc->config->scaler_state;
2899
2900 /* loop through and disable scalers that aren't in use */
2901 for (i = 0; i < intel_crtc->num_scalers; i++) {
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002902 if (!scaler_state->scalers[i].in_use)
2903 skl_detach_scaler(intel_crtc, i);
Chandra Kondurua1b22782015-04-07 15:28:45 -07002904 }
2905}
2906
Chandra Konduru6156a452015-04-27 13:48:39 -07002907u32 skl_plane_ctl_format(uint32_t pixel_format)
2908{
Chandra Konduru6156a452015-04-27 13:48:39 -07002909 switch (pixel_format) {
Damien Lespiaud161cf72015-05-12 16:13:17 +01002910 case DRM_FORMAT_C8:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002911 return PLANE_CTL_FORMAT_INDEXED;
Chandra Konduru6156a452015-04-27 13:48:39 -07002912 case DRM_FORMAT_RGB565:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002913 return PLANE_CTL_FORMAT_RGB_565;
Chandra Konduru6156a452015-04-27 13:48:39 -07002914 case DRM_FORMAT_XBGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002915 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
Chandra Konduru6156a452015-04-27 13:48:39 -07002916 case DRM_FORMAT_XRGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002917 return PLANE_CTL_FORMAT_XRGB_8888;
Chandra Konduru6156a452015-04-27 13:48:39 -07002918 /*
2919 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2920 * to be already pre-multiplied. We need to add a knob (or a different
2921 * DRM_FORMAT) for user-space to configure that.
2922 */
2923 case DRM_FORMAT_ABGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002924 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
Chandra Konduru6156a452015-04-27 13:48:39 -07002925 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002926 case DRM_FORMAT_ARGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002927 return PLANE_CTL_FORMAT_XRGB_8888 |
Chandra Konduru6156a452015-04-27 13:48:39 -07002928 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002929 case DRM_FORMAT_XRGB2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002930 return PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002931 case DRM_FORMAT_XBGR2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002932 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002933 case DRM_FORMAT_YUYV:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002934 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
Chandra Konduru6156a452015-04-27 13:48:39 -07002935 case DRM_FORMAT_YVYU:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002936 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
Chandra Konduru6156a452015-04-27 13:48:39 -07002937 case DRM_FORMAT_UYVY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002938 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002939 case DRM_FORMAT_VYUY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002940 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002941 default:
Damien Lespiau4249eee2015-05-12 16:13:16 +01002942 MISSING_CASE(pixel_format);
Chandra Konduru6156a452015-04-27 13:48:39 -07002943 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002944
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002945 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002946}
2947
2948u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
2949{
Chandra Konduru6156a452015-04-27 13:48:39 -07002950 switch (fb_modifier) {
2951 case DRM_FORMAT_MOD_NONE:
2952 break;
2953 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002954 return PLANE_CTL_TILED_X;
Chandra Konduru6156a452015-04-27 13:48:39 -07002955 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002956 return PLANE_CTL_TILED_Y;
Chandra Konduru6156a452015-04-27 13:48:39 -07002957 case I915_FORMAT_MOD_Yf_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002958 return PLANE_CTL_TILED_YF;
Chandra Konduru6156a452015-04-27 13:48:39 -07002959 default:
2960 MISSING_CASE(fb_modifier);
2961 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002962
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002963 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002964}
2965
2966u32 skl_plane_ctl_rotation(unsigned int rotation)
2967{
Chandra Konduru6156a452015-04-27 13:48:39 -07002968 switch (rotation) {
2969 case BIT(DRM_ROTATE_0):
2970 break;
Sonika Jindal1e8df162015-05-20 13:40:48 +05302971 /*
2972 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
2973 * while i915 HW rotation is clockwise, thats why this swapping.
2974 */
Chandra Konduru6156a452015-04-27 13:48:39 -07002975 case BIT(DRM_ROTATE_90):
Sonika Jindal1e8df162015-05-20 13:40:48 +05302976 return PLANE_CTL_ROTATE_270;
Chandra Konduru6156a452015-04-27 13:48:39 -07002977 case BIT(DRM_ROTATE_180):
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002978 return PLANE_CTL_ROTATE_180;
Chandra Konduru6156a452015-04-27 13:48:39 -07002979 case BIT(DRM_ROTATE_270):
Sonika Jindal1e8df162015-05-20 13:40:48 +05302980 return PLANE_CTL_ROTATE_90;
Chandra Konduru6156a452015-04-27 13:48:39 -07002981 default:
2982 MISSING_CASE(rotation);
2983 }
2984
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002985 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002986}
2987
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002988static void skylake_update_primary_plane(struct drm_plane *plane,
2989 const struct intel_crtc_state *crtc_state,
2990 const struct intel_plane_state *plane_state)
Damien Lespiau70d21f02013-07-03 21:06:04 +01002991{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002992 struct drm_device *dev = plane->dev;
Damien Lespiau70d21f02013-07-03 21:06:04 +01002993 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002994 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2995 struct drm_framebuffer *fb = plane_state->base.fb;
2996 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Damien Lespiau70d21f02013-07-03 21:06:04 +01002997 int pipe = intel_crtc->pipe;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05302998 u32 plane_ctl, stride_div, stride;
2999 u32 tile_height, plane_offset, plane_size;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003000 unsigned int rotation = plane_state->base.rotation;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303001 int x_offset, y_offset;
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02003002 u32 surf_addr;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003003 int scaler_id = plane_state->scaler_id;
3004 int src_x = plane_state->src.x1 >> 16;
3005 int src_y = plane_state->src.y1 >> 16;
3006 int src_w = drm_rect_width(&plane_state->src) >> 16;
3007 int src_h = drm_rect_height(&plane_state->src) >> 16;
3008 int dst_x = plane_state->dst.x1;
3009 int dst_y = plane_state->dst.y1;
3010 int dst_w = drm_rect_width(&plane_state->dst);
3011 int dst_h = drm_rect_height(&plane_state->dst);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003012
3013 plane_ctl = PLANE_CTL_ENABLE |
3014 PLANE_CTL_PIPE_GAMMA_ENABLE |
3015 PLANE_CTL_PIPE_CSC_ENABLE;
3016
Chandra Konduru6156a452015-04-27 13:48:39 -07003017 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3018 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003019 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Chandra Konduru6156a452015-04-27 13:48:39 -07003020 plane_ctl |= skl_plane_ctl_rotation(rotation);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003021
Ville Syrjälä7b49f942016-01-12 21:08:32 +02003022 stride_div = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
Damien Lespiaub3218032015-02-27 11:15:18 +00003023 fb->pixel_format);
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01003024 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj, 0);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303025
Paulo Zanonia42e5a22015-09-30 17:05:43 -03003026 WARN_ON(drm_rect_width(&plane_state->src) == 0);
Chandra Konduru6156a452015-04-27 13:48:39 -07003027
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303028 if (intel_rotation_90_or_270(rotation)) {
Ville Syrjälä832be822016-01-12 21:08:33 +02003029 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
3030
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303031 /* stride = Surface height in tiles */
Ville Syrjälä832be822016-01-12 21:08:33 +02003032 tile_height = intel_tile_height(dev_priv, fb->modifier[0], cpp);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303033 stride = DIV_ROUND_UP(fb->height, tile_height);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003034 x_offset = stride * tile_height - src_y - src_h;
3035 y_offset = src_x;
Chandra Konduru6156a452015-04-27 13:48:39 -07003036 plane_size = (src_w - 1) << 16 | (src_h - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303037 } else {
3038 stride = fb->pitches[0] / stride_div;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003039 x_offset = src_x;
3040 y_offset = src_y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003041 plane_size = (src_h - 1) << 16 | (src_w - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303042 }
3043 plane_offset = y_offset << 16 | x_offset;
Damien Lespiaub3218032015-02-27 11:15:18 +00003044
Paulo Zanoni2db33662015-09-14 15:20:03 -03003045 intel_crtc->adjusted_x = x_offset;
3046 intel_crtc->adjusted_y = y_offset;
3047
Damien Lespiau70d21f02013-07-03 21:06:04 +01003048 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303049 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3050 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3051 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
Chandra Konduru6156a452015-04-27 13:48:39 -07003052
3053 if (scaler_id >= 0) {
3054 uint32_t ps_ctrl = 0;
3055
3056 WARN_ON(!dst_w || !dst_h);
3057 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3058 crtc_state->scaler_state.scalers[scaler_id].mode;
3059 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3060 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3061 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3062 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3063 I915_WRITE(PLANE_POS(pipe, 0), 0);
3064 } else {
3065 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3066 }
3067
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003068 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003069
3070 POSTING_READ(PLANE_SURF(pipe, 0));
3071}
3072
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003073static void skylake_disable_primary_plane(struct drm_plane *primary,
3074 struct drm_crtc *crtc)
3075{
3076 struct drm_device *dev = crtc->dev;
3077 struct drm_i915_private *dev_priv = dev->dev_private;
3078 int pipe = to_intel_crtc(crtc)->pipe;
3079
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003080 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3081 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3082 POSTING_READ(PLANE_SURF(pipe, 0));
3083}
3084
Jesse Barnes17638cd2011-06-24 12:19:23 -07003085/* Assume fb object is pinned & idle & fenced and just update base pointers */
3086static int
3087intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3088 int x, int y, enum mode_set_atomic state)
3089{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003090 /* Support for kgdboc is disabled, this needs a major rework. */
3091 DRM_ERROR("legacy panic handler not supported any more.\n");
Jesse Barnes17638cd2011-06-24 12:19:23 -07003092
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003093 return -ENODEV;
Jesse Barnes81255562010-08-02 12:07:50 -07003094}
3095
Ville Syrjälä75147472014-11-24 18:28:11 +02003096static void intel_complete_page_flips(struct drm_device *dev)
Ville Syrjälä96a02912013-02-18 19:08:49 +02003097{
Ville Syrjälä96a02912013-02-18 19:08:49 +02003098 struct drm_crtc *crtc;
3099
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003100 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003101 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3102 enum plane plane = intel_crtc->plane;
3103
3104 intel_prepare_page_flip(dev, plane);
3105 intel_finish_page_flip_plane(dev, plane);
3106 }
Ville Syrjälä75147472014-11-24 18:28:11 +02003107}
3108
3109static void intel_update_primary_planes(struct drm_device *dev)
3110{
Ville Syrjälä75147472014-11-24 18:28:11 +02003111 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003112
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003113 for_each_crtc(dev, crtc) {
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003114 struct intel_plane *plane = to_intel_plane(crtc->primary);
3115 struct intel_plane_state *plane_state;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003116
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003117 drm_modeset_lock_crtc(crtc, &plane->base);
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003118 plane_state = to_intel_plane_state(plane->base.state);
3119
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003120 if (plane_state->visible)
3121 plane->update_plane(&plane->base,
3122 to_intel_crtc_state(crtc->state),
3123 plane_state);
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003124
3125 drm_modeset_unlock_crtc(crtc);
Ville Syrjälä96a02912013-02-18 19:08:49 +02003126 }
3127}
3128
Ville Syrjälä75147472014-11-24 18:28:11 +02003129void intel_prepare_reset(struct drm_device *dev)
3130{
3131 /* no reset support for gen2 */
3132 if (IS_GEN2(dev))
3133 return;
3134
3135 /* reset doesn't touch the display */
3136 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3137 return;
3138
3139 drm_modeset_lock_all(dev);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003140 /*
3141 * Disabling the crtcs gracefully seems nicer. Also the
3142 * g33 docs say we should at least disable all the planes.
3143 */
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02003144 intel_display_suspend(dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003145}
3146
3147void intel_finish_reset(struct drm_device *dev)
3148{
3149 struct drm_i915_private *dev_priv = to_i915(dev);
3150
3151 /*
3152 * Flips in the rings will be nuked by the reset,
3153 * so complete all pending flips so that user space
3154 * will get its events and not get stuck.
3155 */
3156 intel_complete_page_flips(dev);
3157
3158 /* no reset support for gen2 */
3159 if (IS_GEN2(dev))
3160 return;
3161
3162 /* reset doesn't touch the display */
3163 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3164 /*
3165 * Flips in the rings have been nuked by the reset,
3166 * so update the base address of all primary
3167 * planes to the the last fb to make sure we're
3168 * showing the correct fb after a reset.
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003169 *
3170 * FIXME: Atomic will make this obsolete since we won't schedule
3171 * CS-based flips (which might get lost in gpu resets) any more.
Ville Syrjälä75147472014-11-24 18:28:11 +02003172 */
3173 intel_update_primary_planes(dev);
3174 return;
3175 }
3176
3177 /*
3178 * The display has been reset as well,
3179 * so need a full re-initialization.
3180 */
3181 intel_runtime_pm_disable_interrupts(dev_priv);
3182 intel_runtime_pm_enable_interrupts(dev_priv);
3183
3184 intel_modeset_init_hw(dev);
3185
3186 spin_lock_irq(&dev_priv->irq_lock);
3187 if (dev_priv->display.hpd_irq_setup)
3188 dev_priv->display.hpd_irq_setup(dev);
3189 spin_unlock_irq(&dev_priv->irq_lock);
3190
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +02003191 intel_display_resume(dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003192
3193 intel_hpd_init(dev_priv);
3194
3195 drm_modeset_unlock_all(dev);
3196}
3197
Chris Wilson7d5e3792014-03-04 13:15:08 +00003198static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3199{
3200 struct drm_device *dev = crtc->dev;
3201 struct drm_i915_private *dev_priv = dev->dev_private;
3202 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003203 bool pending;
3204
3205 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3206 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3207 return false;
3208
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003209 spin_lock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003210 pending = to_intel_crtc(crtc)->unpin_work != NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003211 spin_unlock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003212
3213 return pending;
3214}
3215
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003216static void intel_update_pipe_config(struct intel_crtc *crtc,
3217 struct intel_crtc_state *old_crtc_state)
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003218{
3219 struct drm_device *dev = crtc->base.dev;
3220 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003221 struct intel_crtc_state *pipe_config =
3222 to_intel_crtc_state(crtc->base.state);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003223
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003224 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
3225 crtc->base.mode = crtc->base.state->mode;
3226
3227 DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
3228 old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
3229 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003230
3231 /*
3232 * Update pipe size and adjust fitter if needed: the reason for this is
3233 * that in compute_mode_changes we check the native mode (not the pfit
3234 * mode) to see if we can flip rather than do a full mode set. In the
3235 * fastboot case, we'll flip, but if we don't update the pipesrc and
3236 * pfit state, we'll end up with a big fb scanned out into the wrong
3237 * sized surface.
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003238 */
3239
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003240 I915_WRITE(PIPESRC(crtc->pipe),
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003241 ((pipe_config->pipe_src_w - 1) << 16) |
3242 (pipe_config->pipe_src_h - 1));
3243
3244 /* on skylake this is done by detaching scalers */
3245 if (INTEL_INFO(dev)->gen >= 9) {
3246 skl_detach_scalers(crtc);
3247
3248 if (pipe_config->pch_pfit.enabled)
3249 skylake_pfit_enable(crtc);
3250 } else if (HAS_PCH_SPLIT(dev)) {
3251 if (pipe_config->pch_pfit.enabled)
3252 ironlake_pfit_enable(crtc);
3253 else if (old_crtc_state->pch_pfit.enabled)
3254 ironlake_pfit_disable(crtc, true);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003255 }
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003256}
3257
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003258static void intel_fdi_normal_train(struct drm_crtc *crtc)
3259{
3260 struct drm_device *dev = crtc->dev;
3261 struct drm_i915_private *dev_priv = dev->dev_private;
3262 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3263 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003264 i915_reg_t reg;
3265 u32 temp;
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003266
3267 /* enable normal train */
3268 reg = FDI_TX_CTL(pipe);
3269 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07003270 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003271 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3272 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003273 } else {
3274 temp &= ~FDI_LINK_TRAIN_NONE;
3275 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003276 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003277 I915_WRITE(reg, temp);
3278
3279 reg = FDI_RX_CTL(pipe);
3280 temp = I915_READ(reg);
3281 if (HAS_PCH_CPT(dev)) {
3282 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3283 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3284 } else {
3285 temp &= ~FDI_LINK_TRAIN_NONE;
3286 temp |= FDI_LINK_TRAIN_NONE;
3287 }
3288 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3289
3290 /* wait one idle pattern time */
3291 POSTING_READ(reg);
3292 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003293
3294 /* IVB wants error correction enabled */
3295 if (IS_IVYBRIDGE(dev))
3296 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3297 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003298}
3299
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003300/* The FDI link training functions for ILK/Ibexpeak. */
3301static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3302{
3303 struct drm_device *dev = crtc->dev;
3304 struct drm_i915_private *dev_priv = dev->dev_private;
3305 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3306 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003307 i915_reg_t reg;
3308 u32 temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003309
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003310 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003311 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003312
Adam Jacksone1a44742010-06-25 15:32:14 -04003313 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3314 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003315 reg = FDI_RX_IMR(pipe);
3316 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003317 temp &= ~FDI_RX_SYMBOL_LOCK;
3318 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003319 I915_WRITE(reg, temp);
3320 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003321 udelay(150);
3322
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003323 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003324 reg = FDI_TX_CTL(pipe);
3325 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003326 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003327 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003328 temp &= ~FDI_LINK_TRAIN_NONE;
3329 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003330 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003331
Chris Wilson5eddb702010-09-11 13:48:45 +01003332 reg = FDI_RX_CTL(pipe);
3333 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003334 temp &= ~FDI_LINK_TRAIN_NONE;
3335 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003336 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3337
3338 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003339 udelay(150);
3340
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003341 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003342 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3343 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3344 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003345
Chris Wilson5eddb702010-09-11 13:48:45 +01003346 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003347 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003348 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003349 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3350
3351 if ((temp & FDI_RX_BIT_LOCK)) {
3352 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003353 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003354 break;
3355 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003356 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003357 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003358 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003359
3360 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003361 reg = FDI_TX_CTL(pipe);
3362 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003363 temp &= ~FDI_LINK_TRAIN_NONE;
3364 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003365 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003366
Chris Wilson5eddb702010-09-11 13:48:45 +01003367 reg = FDI_RX_CTL(pipe);
3368 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003369 temp &= ~FDI_LINK_TRAIN_NONE;
3370 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003371 I915_WRITE(reg, temp);
3372
3373 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003374 udelay(150);
3375
Chris Wilson5eddb702010-09-11 13:48:45 +01003376 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003377 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003378 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003379 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3380
3381 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003382 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003383 DRM_DEBUG_KMS("FDI train 2 done.\n");
3384 break;
3385 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003386 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003387 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003388 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003389
3390 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003391
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003392}
3393
Akshay Joshi0206e352011-08-16 15:34:10 -04003394static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003395 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3396 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3397 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3398 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3399};
3400
3401/* The FDI link training functions for SNB/Cougarpoint. */
3402static void gen6_fdi_link_train(struct drm_crtc *crtc)
3403{
3404 struct drm_device *dev = crtc->dev;
3405 struct drm_i915_private *dev_priv = dev->dev_private;
3406 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3407 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003408 i915_reg_t reg;
3409 u32 temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003410
Adam Jacksone1a44742010-06-25 15:32:14 -04003411 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3412 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003413 reg = FDI_RX_IMR(pipe);
3414 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003415 temp &= ~FDI_RX_SYMBOL_LOCK;
3416 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003417 I915_WRITE(reg, temp);
3418
3419 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003420 udelay(150);
3421
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003422 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003423 reg = FDI_TX_CTL(pipe);
3424 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003425 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003426 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003427 temp &= ~FDI_LINK_TRAIN_NONE;
3428 temp |= FDI_LINK_TRAIN_PATTERN_1;
3429 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3430 /* SNB-B */
3431 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003432 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003433
Daniel Vetterd74cf322012-10-26 10:58:13 +02003434 I915_WRITE(FDI_RX_MISC(pipe),
3435 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3436
Chris Wilson5eddb702010-09-11 13:48:45 +01003437 reg = FDI_RX_CTL(pipe);
3438 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003439 if (HAS_PCH_CPT(dev)) {
3440 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3441 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3442 } else {
3443 temp &= ~FDI_LINK_TRAIN_NONE;
3444 temp |= FDI_LINK_TRAIN_PATTERN_1;
3445 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003446 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3447
3448 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003449 udelay(150);
3450
Akshay Joshi0206e352011-08-16 15:34:10 -04003451 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003452 reg = FDI_TX_CTL(pipe);
3453 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003454 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3455 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003456 I915_WRITE(reg, temp);
3457
3458 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003459 udelay(500);
3460
Sean Paulfa37d392012-03-02 12:53:39 -05003461 for (retry = 0; retry < 5; retry++) {
3462 reg = FDI_RX_IIR(pipe);
3463 temp = I915_READ(reg);
3464 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3465 if (temp & FDI_RX_BIT_LOCK) {
3466 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3467 DRM_DEBUG_KMS("FDI train 1 done.\n");
3468 break;
3469 }
3470 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003471 }
Sean Paulfa37d392012-03-02 12:53:39 -05003472 if (retry < 5)
3473 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003474 }
3475 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003476 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003477
3478 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003479 reg = FDI_TX_CTL(pipe);
3480 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003481 temp &= ~FDI_LINK_TRAIN_NONE;
3482 temp |= FDI_LINK_TRAIN_PATTERN_2;
3483 if (IS_GEN6(dev)) {
3484 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3485 /* SNB-B */
3486 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3487 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003488 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003489
Chris Wilson5eddb702010-09-11 13:48:45 +01003490 reg = FDI_RX_CTL(pipe);
3491 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003492 if (HAS_PCH_CPT(dev)) {
3493 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3494 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3495 } else {
3496 temp &= ~FDI_LINK_TRAIN_NONE;
3497 temp |= FDI_LINK_TRAIN_PATTERN_2;
3498 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003499 I915_WRITE(reg, temp);
3500
3501 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003502 udelay(150);
3503
Akshay Joshi0206e352011-08-16 15:34:10 -04003504 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003505 reg = FDI_TX_CTL(pipe);
3506 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003507 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3508 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003509 I915_WRITE(reg, temp);
3510
3511 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003512 udelay(500);
3513
Sean Paulfa37d392012-03-02 12:53:39 -05003514 for (retry = 0; retry < 5; retry++) {
3515 reg = FDI_RX_IIR(pipe);
3516 temp = I915_READ(reg);
3517 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3518 if (temp & FDI_RX_SYMBOL_LOCK) {
3519 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3520 DRM_DEBUG_KMS("FDI train 2 done.\n");
3521 break;
3522 }
3523 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003524 }
Sean Paulfa37d392012-03-02 12:53:39 -05003525 if (retry < 5)
3526 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003527 }
3528 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003529 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003530
3531 DRM_DEBUG_KMS("FDI train done.\n");
3532}
3533
Jesse Barnes357555c2011-04-28 15:09:55 -07003534/* Manual link training for Ivy Bridge A0 parts */
3535static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3536{
3537 struct drm_device *dev = crtc->dev;
3538 struct drm_i915_private *dev_priv = dev->dev_private;
3539 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3540 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003541 i915_reg_t reg;
3542 u32 temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003543
3544 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3545 for train result */
3546 reg = FDI_RX_IMR(pipe);
3547 temp = I915_READ(reg);
3548 temp &= ~FDI_RX_SYMBOL_LOCK;
3549 temp &= ~FDI_RX_BIT_LOCK;
3550 I915_WRITE(reg, temp);
3551
3552 POSTING_READ(reg);
3553 udelay(150);
3554
Daniel Vetter01a415f2012-10-27 15:58:40 +02003555 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3556 I915_READ(FDI_RX_IIR(pipe)));
3557
Jesse Barnes139ccd32013-08-19 11:04:55 -07003558 /* Try each vswing and preemphasis setting twice before moving on */
3559 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3560 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003561 reg = FDI_TX_CTL(pipe);
3562 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003563 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3564 temp &= ~FDI_TX_ENABLE;
3565 I915_WRITE(reg, temp);
3566
3567 reg = FDI_RX_CTL(pipe);
3568 temp = I915_READ(reg);
3569 temp &= ~FDI_LINK_TRAIN_AUTO;
3570 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3571 temp &= ~FDI_RX_ENABLE;
3572 I915_WRITE(reg, temp);
3573
3574 /* enable CPU FDI TX and PCH FDI RX */
3575 reg = FDI_TX_CTL(pipe);
3576 temp = I915_READ(reg);
3577 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003578 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003579 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003580 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003581 temp |= snb_b_fdi_train_param[j/2];
3582 temp |= FDI_COMPOSITE_SYNC;
3583 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3584
3585 I915_WRITE(FDI_RX_MISC(pipe),
3586 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3587
3588 reg = FDI_RX_CTL(pipe);
3589 temp = I915_READ(reg);
3590 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3591 temp |= FDI_COMPOSITE_SYNC;
3592 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3593
3594 POSTING_READ(reg);
3595 udelay(1); /* should be 0.5us */
3596
3597 for (i = 0; i < 4; i++) {
3598 reg = FDI_RX_IIR(pipe);
3599 temp = I915_READ(reg);
3600 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3601
3602 if (temp & FDI_RX_BIT_LOCK ||
3603 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3604 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3605 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3606 i);
3607 break;
3608 }
3609 udelay(1); /* should be 0.5us */
3610 }
3611 if (i == 4) {
3612 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3613 continue;
3614 }
3615
3616 /* Train 2 */
3617 reg = FDI_TX_CTL(pipe);
3618 temp = I915_READ(reg);
3619 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3620 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3621 I915_WRITE(reg, temp);
3622
3623 reg = FDI_RX_CTL(pipe);
3624 temp = I915_READ(reg);
3625 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3626 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003627 I915_WRITE(reg, temp);
3628
3629 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003630 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003631
Jesse Barnes139ccd32013-08-19 11:04:55 -07003632 for (i = 0; i < 4; i++) {
3633 reg = FDI_RX_IIR(pipe);
3634 temp = I915_READ(reg);
3635 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003636
Jesse Barnes139ccd32013-08-19 11:04:55 -07003637 if (temp & FDI_RX_SYMBOL_LOCK ||
3638 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3639 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3640 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3641 i);
3642 goto train_done;
3643 }
3644 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003645 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003646 if (i == 4)
3647 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003648 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003649
Jesse Barnes139ccd32013-08-19 11:04:55 -07003650train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003651 DRM_DEBUG_KMS("FDI train done.\n");
3652}
3653
Daniel Vetter88cefb62012-08-12 19:27:14 +02003654static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003655{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003656 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003657 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003658 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003659 i915_reg_t reg;
3660 u32 temp;
Jesse Barnesc64e3112010-09-10 11:27:03 -07003661
Jesse Barnes0e23b992010-09-10 11:10:00 -07003662 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003663 reg = FDI_RX_CTL(pipe);
3664 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003665 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003666 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003667 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003668 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3669
3670 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003671 udelay(200);
3672
3673 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003674 temp = I915_READ(reg);
3675 I915_WRITE(reg, temp | FDI_PCDCLK);
3676
3677 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003678 udelay(200);
3679
Paulo Zanoni20749732012-11-23 15:30:38 -02003680 /* Enable CPU FDI TX PLL, always on for Ironlake */
3681 reg = FDI_TX_CTL(pipe);
3682 temp = I915_READ(reg);
3683 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3684 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003685
Paulo Zanoni20749732012-11-23 15:30:38 -02003686 POSTING_READ(reg);
3687 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003688 }
3689}
3690
Daniel Vetter88cefb62012-08-12 19:27:14 +02003691static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3692{
3693 struct drm_device *dev = intel_crtc->base.dev;
3694 struct drm_i915_private *dev_priv = dev->dev_private;
3695 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003696 i915_reg_t reg;
3697 u32 temp;
Daniel Vetter88cefb62012-08-12 19:27:14 +02003698
3699 /* Switch from PCDclk to Rawclk */
3700 reg = FDI_RX_CTL(pipe);
3701 temp = I915_READ(reg);
3702 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3703
3704 /* Disable CPU FDI TX PLL */
3705 reg = FDI_TX_CTL(pipe);
3706 temp = I915_READ(reg);
3707 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3708
3709 POSTING_READ(reg);
3710 udelay(100);
3711
3712 reg = FDI_RX_CTL(pipe);
3713 temp = I915_READ(reg);
3714 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3715
3716 /* Wait for the clocks to turn off. */
3717 POSTING_READ(reg);
3718 udelay(100);
3719}
3720
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003721static void ironlake_fdi_disable(struct drm_crtc *crtc)
3722{
3723 struct drm_device *dev = crtc->dev;
3724 struct drm_i915_private *dev_priv = dev->dev_private;
3725 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3726 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003727 i915_reg_t reg;
3728 u32 temp;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003729
3730 /* disable CPU FDI tx and PCH FDI rx */
3731 reg = FDI_TX_CTL(pipe);
3732 temp = I915_READ(reg);
3733 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3734 POSTING_READ(reg);
3735
3736 reg = FDI_RX_CTL(pipe);
3737 temp = I915_READ(reg);
3738 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003739 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003740 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3741
3742 POSTING_READ(reg);
3743 udelay(100);
3744
3745 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003746 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003747 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003748
3749 /* still set train pattern 1 */
3750 reg = FDI_TX_CTL(pipe);
3751 temp = I915_READ(reg);
3752 temp &= ~FDI_LINK_TRAIN_NONE;
3753 temp |= FDI_LINK_TRAIN_PATTERN_1;
3754 I915_WRITE(reg, temp);
3755
3756 reg = FDI_RX_CTL(pipe);
3757 temp = I915_READ(reg);
3758 if (HAS_PCH_CPT(dev)) {
3759 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3760 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3761 } else {
3762 temp &= ~FDI_LINK_TRAIN_NONE;
3763 temp |= FDI_LINK_TRAIN_PATTERN_1;
3764 }
3765 /* BPC in FDI rx is consistent with that in PIPECONF */
3766 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003767 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003768 I915_WRITE(reg, temp);
3769
3770 POSTING_READ(reg);
3771 udelay(100);
3772}
3773
Chris Wilson5dce5b932014-01-20 10:17:36 +00003774bool intel_has_pending_fb_unpin(struct drm_device *dev)
3775{
3776 struct intel_crtc *crtc;
3777
3778 /* Note that we don't need to be called with mode_config.lock here
3779 * as our list of CRTC objects is static for the lifetime of the
3780 * device and so cannot disappear as we iterate. Similarly, we can
3781 * happily treat the predicates as racy, atomic checks as userspace
3782 * cannot claim and pin a new fb without at least acquring the
3783 * struct_mutex and so serialising with us.
3784 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003785 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003786 if (atomic_read(&crtc->unpin_work_count) == 0)
3787 continue;
3788
3789 if (crtc->unpin_work)
3790 intel_wait_for_vblank(dev, crtc->pipe);
3791
3792 return true;
3793 }
3794
3795 return false;
3796}
3797
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003798static void page_flip_completed(struct intel_crtc *intel_crtc)
3799{
3800 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3801 struct intel_unpin_work *work = intel_crtc->unpin_work;
3802
3803 /* ensure that the unpin work is consistent wrt ->pending. */
3804 smp_rmb();
3805 intel_crtc->unpin_work = NULL;
3806
3807 if (work->event)
3808 drm_send_vblank_event(intel_crtc->base.dev,
3809 intel_crtc->pipe,
3810 work->event);
3811
3812 drm_crtc_vblank_put(&intel_crtc->base);
3813
3814 wake_up_all(&dev_priv->pending_flip_queue);
3815 queue_work(dev_priv->wq, &work->work);
3816
3817 trace_i915_flip_complete(intel_crtc->plane,
3818 work->pending_flip_obj);
3819}
3820
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003821static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003822{
Chris Wilson0f911282012-04-17 10:05:38 +01003823 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003824 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003825 long ret;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003826
Daniel Vetter2c10d572012-12-20 21:24:07 +01003827 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003828
3829 ret = wait_event_interruptible_timeout(
3830 dev_priv->pending_flip_queue,
3831 !intel_crtc_has_pending_flip(crtc),
3832 60*HZ);
3833
3834 if (ret < 0)
3835 return ret;
3836
3837 if (ret == 0) {
Chris Wilson9c787942014-09-05 07:13:25 +01003838 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter2c10d572012-12-20 21:24:07 +01003839
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003840 spin_lock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003841 if (intel_crtc->unpin_work) {
3842 WARN_ONCE(1, "Removing stuck page flip\n");
3843 page_flip_completed(intel_crtc);
3844 }
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003845 spin_unlock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003846 }
Chris Wilson5bb61642012-09-27 21:25:58 +01003847
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003848 return 0;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003849}
3850
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003851static void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
3852{
3853 u32 temp;
3854
3855 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3856
3857 mutex_lock(&dev_priv->sb_lock);
3858
3859 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3860 temp |= SBI_SSCCTL_DISABLE;
3861 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
3862
3863 mutex_unlock(&dev_priv->sb_lock);
3864}
3865
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003866/* Program iCLKIP clock to the desired frequency */
3867static void lpt_program_iclkip(struct drm_crtc *crtc)
3868{
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003869 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003870 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003871 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3872 u32 temp;
3873
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003874 lpt_disable_iclkip(dev_priv);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003875
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003876 /* The iCLK virtual clock root frequency is in MHz,
3877 * but the adjusted_mode->crtc_clock in in KHz. To get the
3878 * divisors, it is necessary to divide one by another, so we
3879 * convert the virtual clock precision to KHz here for higher
3880 * precision.
3881 */
3882 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003883 u32 iclk_virtual_root_freq = 172800 * 1000;
3884 u32 iclk_pi_range = 64;
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003885 u32 desired_divisor;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003886
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003887 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
3888 clock << auxdiv);
3889 divsel = (desired_divisor / iclk_pi_range) - 2;
3890 phaseinc = desired_divisor % iclk_pi_range;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003891
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003892 /*
3893 * Near 20MHz is a corner case which is
3894 * out of range for the 7-bit divisor
3895 */
3896 if (divsel <= 0x7f)
3897 break;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003898 }
3899
3900 /* This should not happen with any sane values */
3901 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3902 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3903 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3904 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3905
3906 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 +03003907 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003908 auxdiv,
3909 divsel,
3910 phasedir,
3911 phaseinc);
3912
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003913 mutex_lock(&dev_priv->sb_lock);
3914
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003915 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003916 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003917 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3918 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3919 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3920 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3921 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3922 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003923 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003924
3925 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003926 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003927 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3928 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003929 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003930
3931 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003932 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003933 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003934 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003935
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003936 mutex_unlock(&dev_priv->sb_lock);
3937
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003938 /* Wait for initialization time */
3939 udelay(24);
3940
3941 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
3942}
3943
Ville Syrjälä8802e5b2016-02-17 21:41:12 +02003944int lpt_get_iclkip(struct drm_i915_private *dev_priv)
3945{
3946 u32 divsel, phaseinc, auxdiv;
3947 u32 iclk_virtual_root_freq = 172800 * 1000;
3948 u32 iclk_pi_range = 64;
3949 u32 desired_divisor;
3950 u32 temp;
3951
3952 if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
3953 return 0;
3954
3955 mutex_lock(&dev_priv->sb_lock);
3956
3957 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3958 if (temp & SBI_SSCCTL_DISABLE) {
3959 mutex_unlock(&dev_priv->sb_lock);
3960 return 0;
3961 }
3962
3963 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
3964 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
3965 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
3966 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
3967 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
3968
3969 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
3970 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
3971 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
3972
3973 mutex_unlock(&dev_priv->sb_lock);
3974
3975 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
3976
3977 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
3978 desired_divisor << auxdiv);
3979}
3980
Daniel Vetter275f01b22013-05-03 11:49:47 +02003981static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3982 enum pipe pch_transcoder)
3983{
3984 struct drm_device *dev = crtc->base.dev;
3985 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003986 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02003987
3988 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3989 I915_READ(HTOTAL(cpu_transcoder)));
3990 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
3991 I915_READ(HBLANK(cpu_transcoder)));
3992 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
3993 I915_READ(HSYNC(cpu_transcoder)));
3994
3995 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
3996 I915_READ(VTOTAL(cpu_transcoder)));
3997 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
3998 I915_READ(VBLANK(cpu_transcoder)));
3999 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4000 I915_READ(VSYNC(cpu_transcoder)));
4001 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4002 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4003}
4004
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004005static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004006{
4007 struct drm_i915_private *dev_priv = dev->dev_private;
4008 uint32_t temp;
4009
4010 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004011 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004012 return;
4013
4014 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4015 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4016
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004017 temp &= ~FDI_BC_BIFURCATION_SELECT;
4018 if (enable)
4019 temp |= FDI_BC_BIFURCATION_SELECT;
4020
4021 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004022 I915_WRITE(SOUTH_CHICKEN1, temp);
4023 POSTING_READ(SOUTH_CHICKEN1);
4024}
4025
4026static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4027{
4028 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004029
4030 switch (intel_crtc->pipe) {
4031 case PIPE_A:
4032 break;
4033 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004034 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004035 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004036 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004037 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004038
4039 break;
4040 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004041 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004042
4043 break;
4044 default:
4045 BUG();
4046 }
4047}
4048
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004049/* Return which DP Port should be selected for Transcoder DP control */
4050static enum port
4051intel_trans_dp_port_sel(struct drm_crtc *crtc)
4052{
4053 struct drm_device *dev = crtc->dev;
4054 struct intel_encoder *encoder;
4055
4056 for_each_encoder_on_crtc(dev, crtc, encoder) {
4057 if (encoder->type == INTEL_OUTPUT_DISPLAYPORT ||
4058 encoder->type == INTEL_OUTPUT_EDP)
4059 return enc_to_dig_port(&encoder->base)->port;
4060 }
4061
4062 return -1;
4063}
4064
Jesse Barnesf67a5592011-01-05 10:31:48 -08004065/*
4066 * Enable PCH resources required for PCH ports:
4067 * - PCH PLLs
4068 * - FDI training & RX/TX
4069 * - update transcoder timings
4070 * - DP transcoding bits
4071 * - transcoder
4072 */
4073static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08004074{
4075 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004076 struct drm_i915_private *dev_priv = dev->dev_private;
4077 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4078 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004079 u32 temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004080
Daniel Vetterab9412b2013-05-03 11:49:46 +02004081 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004082
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004083 if (IS_IVYBRIDGE(dev))
4084 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4085
Daniel Vettercd986ab2012-10-26 10:58:12 +02004086 /* Write the TU size bits before fdi link training, so that error
4087 * detection works. */
4088 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4089 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4090
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004091 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07004092 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004093
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004094 /* We need to program the right clock selection before writing the pixel
4095 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004096 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004097 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004098
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004099 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004100 temp |= TRANS_DPLL_ENABLE(pipe);
4101 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02004102 if (intel_crtc->config->shared_dpll ==
4103 intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004104 temp |= sel;
4105 else
4106 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004107 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004108 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004109
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004110 /* XXX: pch pll's can be enabled any time before we enable the PCH
4111 * transcoder, and we actually should do this to not upset any PCH
4112 * transcoder that already use the clock when we share it.
4113 *
4114 * Note that enable_shared_dpll tries to do the right thing, but
4115 * get_shared_dpll unconditionally resets the pll - we need that to have
4116 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02004117 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004118
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004119 /* set transcoder timing, panel must allow it */
4120 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02004121 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004122
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004123 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004124
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004125 /* For PCH DP, enable TRANS_DP_CTL */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004126 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004127 const struct drm_display_mode *adjusted_mode =
4128 &intel_crtc->config->base.adjusted_mode;
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004129 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004130 i915_reg_t reg = TRANS_DP_CTL(pipe);
Chris Wilson5eddb702010-09-11 13:48:45 +01004131 temp = I915_READ(reg);
4132 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004133 TRANS_DP_SYNC_MASK |
4134 TRANS_DP_BPC_MASK);
Ville Syrjäläe3ef4472015-05-05 17:17:31 +03004135 temp |= TRANS_DP_OUTPUT_ENABLE;
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004136 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004137
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004138 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004139 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004140 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004141 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004142
4143 switch (intel_trans_dp_port_sel(crtc)) {
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004144 case PORT_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004145 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004146 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004147 case PORT_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004148 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004149 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004150 case PORT_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004151 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004152 break;
4153 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004154 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004155 }
4156
Chris Wilson5eddb702010-09-11 13:48:45 +01004157 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004158 }
4159
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004160 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004161}
4162
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004163static void lpt_pch_enable(struct drm_crtc *crtc)
4164{
4165 struct drm_device *dev = crtc->dev;
4166 struct drm_i915_private *dev_priv = dev->dev_private;
4167 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004168 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004169
Daniel Vetterab9412b2013-05-03 11:49:46 +02004170 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004171
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004172 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004173
Paulo Zanoni0540e482012-10-31 18:12:40 -02004174 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02004175 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004176
Paulo Zanoni937bb612012-10-31 18:12:47 -02004177 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004178}
4179
Daniel Vettera1520312013-05-03 11:49:50 +02004180static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004181{
4182 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004183 i915_reg_t dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004184 u32 temp;
4185
4186 temp = I915_READ(dslreg);
4187 udelay(500);
4188 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004189 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004190 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004191 }
4192}
4193
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004194static int
4195skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4196 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4197 int src_w, int src_h, int dst_w, int dst_h)
Chandra Kondurua1b22782015-04-07 15:28:45 -07004198{
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004199 struct intel_crtc_scaler_state *scaler_state =
4200 &crtc_state->scaler_state;
4201 struct intel_crtc *intel_crtc =
4202 to_intel_crtc(crtc_state->base.crtc);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004203 int need_scaling;
Chandra Konduru6156a452015-04-27 13:48:39 -07004204
4205 need_scaling = intel_rotation_90_or_270(rotation) ?
4206 (src_h != dst_w || src_w != dst_h):
4207 (src_w != dst_w || src_h != dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004208
4209 /*
4210 * if plane is being disabled or scaler is no more required or force detach
4211 * - free scaler binded to this plane/crtc
4212 * - in order to do this, update crtc->scaler_usage
4213 *
4214 * Here scaler state in crtc_state is set free so that
4215 * scaler can be assigned to other user. Actual register
4216 * update to free the scaler is done in plane/panel-fit programming.
4217 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4218 */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004219 if (force_detach || !need_scaling) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004220 if (*scaler_id >= 0) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004221 scaler_state->scaler_users &= ~(1 << scaler_user);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004222 scaler_state->scalers[*scaler_id].in_use = 0;
4223
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004224 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4225 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4226 intel_crtc->pipe, scaler_user, *scaler_id,
Chandra Kondurua1b22782015-04-07 15:28:45 -07004227 scaler_state->scaler_users);
4228 *scaler_id = -1;
4229 }
4230 return 0;
4231 }
4232
4233 /* range checks */
4234 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4235 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4236
4237 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4238 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004239 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
Chandra Kondurua1b22782015-04-07 15:28:45 -07004240 "size is out of scaler range\n",
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004241 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004242 return -EINVAL;
4243 }
4244
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004245 /* mark this plane as a scaler user in crtc_state */
4246 scaler_state->scaler_users |= (1 << scaler_user);
4247 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4248 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4249 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4250 scaler_state->scaler_users);
4251
4252 return 0;
4253}
4254
4255/**
4256 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4257 *
4258 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004259 *
4260 * Return
4261 * 0 - scaler_usage updated successfully
4262 * error - requested scaling cannot be supported or other error condition
4263 */
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004264int skl_update_scaler_crtc(struct intel_crtc_state *state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004265{
4266 struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03004267 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004268
4269 DRM_DEBUG_KMS("Updating scaler for [CRTC:%i] scaler_user index %u.%u\n",
4270 intel_crtc->base.base.id, intel_crtc->pipe, SKL_CRTC_INDEX);
4271
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004272 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
Ville Syrjäläfa5a7972015-10-15 17:01:58 +03004273 &state->scaler_state.scaler_id, BIT(DRM_ROTATE_0),
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004274 state->pipe_src_w, state->pipe_src_h,
Ville Syrjäläaad941d2015-09-25 16:38:56 +03004275 adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004276}
4277
4278/**
4279 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4280 *
4281 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004282 * @plane_state: atomic plane state to update
4283 *
4284 * Return
4285 * 0 - scaler_usage updated successfully
4286 * error - requested scaling cannot be supported or other error condition
4287 */
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004288static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4289 struct intel_plane_state *plane_state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004290{
4291
4292 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004293 struct intel_plane *intel_plane =
4294 to_intel_plane(plane_state->base.plane);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004295 struct drm_framebuffer *fb = plane_state->base.fb;
4296 int ret;
4297
4298 bool force_detach = !fb || !plane_state->visible;
4299
4300 DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
4301 intel_plane->base.base.id, intel_crtc->pipe,
4302 drm_plane_index(&intel_plane->base));
4303
4304 ret = skl_update_scaler(crtc_state, force_detach,
4305 drm_plane_index(&intel_plane->base),
4306 &plane_state->scaler_id,
4307 plane_state->base.rotation,
4308 drm_rect_width(&plane_state->src) >> 16,
4309 drm_rect_height(&plane_state->src) >> 16,
4310 drm_rect_width(&plane_state->dst),
4311 drm_rect_height(&plane_state->dst));
4312
4313 if (ret || plane_state->scaler_id < 0)
4314 return ret;
4315
Chandra Kondurua1b22782015-04-07 15:28:45 -07004316 /* check colorkey */
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004317 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004318 DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004319 intel_plane->base.base.id);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004320 return -EINVAL;
4321 }
4322
4323 /* Check src format */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004324 switch (fb->pixel_format) {
4325 case DRM_FORMAT_RGB565:
4326 case DRM_FORMAT_XBGR8888:
4327 case DRM_FORMAT_XRGB8888:
4328 case DRM_FORMAT_ABGR8888:
4329 case DRM_FORMAT_ARGB8888:
4330 case DRM_FORMAT_XRGB2101010:
4331 case DRM_FORMAT_XBGR2101010:
4332 case DRM_FORMAT_YUYV:
4333 case DRM_FORMAT_YVYU:
4334 case DRM_FORMAT_UYVY:
4335 case DRM_FORMAT_VYUY:
4336 break;
4337 default:
4338 DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
4339 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4340 return -EINVAL;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004341 }
4342
Chandra Kondurua1b22782015-04-07 15:28:45 -07004343 return 0;
4344}
4345
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004346static void skylake_scaler_disable(struct intel_crtc *crtc)
4347{
4348 int i;
4349
4350 for (i = 0; i < crtc->num_scalers; i++)
4351 skl_detach_scaler(crtc, i);
4352}
4353
4354static void skylake_pfit_enable(struct intel_crtc *crtc)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004355{
4356 struct drm_device *dev = crtc->base.dev;
4357 struct drm_i915_private *dev_priv = dev->dev_private;
4358 int pipe = crtc->pipe;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004359 struct intel_crtc_scaler_state *scaler_state =
4360 &crtc->config->scaler_state;
4361
4362 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4363
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004364 if (crtc->config->pch_pfit.enabled) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004365 int id;
4366
4367 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4368 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4369 return;
4370 }
4371
4372 id = scaler_state->scaler_id;
4373 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4374 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4375 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4376 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4377
4378 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004379 }
4380}
4381
Jesse Barnesb074cec2013-04-25 12:55:02 -07004382static void ironlake_pfit_enable(struct intel_crtc *crtc)
4383{
4384 struct drm_device *dev = crtc->base.dev;
4385 struct drm_i915_private *dev_priv = dev->dev_private;
4386 int pipe = crtc->pipe;
4387
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004388 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004389 /* Force use of hard-coded filter coefficients
4390 * as some pre-programmed values are broken,
4391 * e.g. x201.
4392 */
4393 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4394 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4395 PF_PIPE_SEL_IVB(pipe));
4396 else
4397 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004398 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4399 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004400 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004401}
4402
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004403void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004404{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004405 struct drm_device *dev = crtc->base.dev;
4406 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid77e4532013-09-24 13:52:55 -03004407
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004408 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004409 return;
4410
Maarten Lankhorst307e4492016-03-23 14:33:28 +01004411 /*
4412 * We can only enable IPS after we enable a plane and wait for a vblank
4413 * This function is called from post_plane_update, which is run after
4414 * a vblank wait.
4415 */
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004416
Paulo Zanonid77e4532013-09-24 13:52:55 -03004417 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004418 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004419 mutex_lock(&dev_priv->rps.hw_lock);
4420 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4421 mutex_unlock(&dev_priv->rps.hw_lock);
4422 /* Quoting Art Runyan: "its not safe to expect any particular
4423 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004424 * mailbox." Moreover, the mailbox may return a bogus state,
4425 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004426 */
4427 } else {
4428 I915_WRITE(IPS_CTL, IPS_ENABLE);
4429 /* The bit only becomes 1 in the next vblank, so this wait here
4430 * is essentially intel_wait_for_vblank. If we don't have this
4431 * and don't wait for vblanks until the end of crtc_enable, then
4432 * the HW state readout code will complain that the expected
4433 * IPS_CTL value is not the one we read. */
4434 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4435 DRM_ERROR("Timed out waiting for IPS enable\n");
4436 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004437}
4438
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004439void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004440{
4441 struct drm_device *dev = crtc->base.dev;
4442 struct drm_i915_private *dev_priv = dev->dev_private;
4443
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004444 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004445 return;
4446
4447 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004448 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004449 mutex_lock(&dev_priv->rps.hw_lock);
4450 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4451 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004452 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4453 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4454 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004455 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004456 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004457 POSTING_READ(IPS_CTL);
4458 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004459
4460 /* We need to wait for a vblank before we can disable the plane. */
4461 intel_wait_for_vblank(dev, crtc->pipe);
4462}
4463
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004464static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004465{
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004466 if (intel_crtc->overlay) {
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004467 struct drm_device *dev = intel_crtc->base.dev;
4468 struct drm_i915_private *dev_priv = dev->dev_private;
4469
4470 mutex_lock(&dev->struct_mutex);
4471 dev_priv->mm.interruptible = false;
4472 (void) intel_overlay_switch_off(intel_crtc->overlay);
4473 dev_priv->mm.interruptible = true;
4474 mutex_unlock(&dev->struct_mutex);
4475 }
4476
4477 /* Let userspace switch the overlay on again. In most cases userspace
4478 * has to recompute where to put it anyway.
4479 */
4480}
4481
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004482/**
4483 * intel_post_enable_primary - Perform operations after enabling primary plane
4484 * @crtc: the CRTC whose primary plane was just enabled
4485 *
4486 * Performs potentially sleeping operations that must be done after the primary
4487 * plane is enabled, such as updating FBC and IPS. Note that this may be
4488 * called due to an explicit primary plane update, or due to an implicit
4489 * re-enable that is caused when a sprite plane is updated to no longer
4490 * completely hide the primary plane.
4491 */
4492static void
4493intel_post_enable_primary(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004494{
4495 struct drm_device *dev = crtc->dev;
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004496 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004497 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4498 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004499
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004500 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004501 * FIXME IPS should be fine as long as one plane is
4502 * enabled, but in practice it seems to have problems
4503 * when going from primary only to sprite only and vice
4504 * versa.
4505 */
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004506 hsw_enable_ips(intel_crtc);
4507
Daniel Vetterf99d7062014-06-19 16:01:59 +02004508 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004509 * Gen2 reports pipe underruns whenever all planes are disabled.
4510 * So don't enable underrun reporting before at least some planes
4511 * are enabled.
4512 * FIXME: Need to fix the logic to work when we turn off all planes
4513 * but leave the pipe running.
Daniel Vetterf99d7062014-06-19 16:01:59 +02004514 */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004515 if (IS_GEN2(dev))
4516 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4517
Ville Syrjäläaca7b682015-10-30 19:22:21 +02004518 /* Underruns don't always raise interrupts, so check manually. */
4519 intel_check_cpu_fifo_underruns(dev_priv);
4520 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004521}
4522
Ville Syrjälä2622a082016-03-09 19:07:26 +02004523/* FIXME move all this to pre_plane_update() with proper state tracking */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004524static void
4525intel_pre_disable_primary(struct drm_crtc *crtc)
4526{
4527 struct drm_device *dev = crtc->dev;
4528 struct drm_i915_private *dev_priv = dev->dev_private;
4529 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4530 int pipe = intel_crtc->pipe;
4531
4532 /*
4533 * Gen2 reports pipe underruns whenever all planes are disabled.
4534 * So diasble underrun reporting before all the planes get disabled.
4535 * FIXME: Need to fix the logic to work when we turn off all planes
4536 * but leave the pipe running.
4537 */
4538 if (IS_GEN2(dev))
4539 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4540
4541 /*
Ville Syrjälä2622a082016-03-09 19:07:26 +02004542 * FIXME IPS should be fine as long as one plane is
4543 * enabled, but in practice it seems to have problems
4544 * when going from primary only to sprite only and vice
4545 * versa.
4546 */
4547 hsw_disable_ips(intel_crtc);
4548}
4549
4550/* FIXME get rid of this and use pre_plane_update */
4551static void
4552intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
4553{
4554 struct drm_device *dev = crtc->dev;
4555 struct drm_i915_private *dev_priv = dev->dev_private;
4556 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4557 int pipe = intel_crtc->pipe;
4558
4559 intel_pre_disable_primary(crtc);
4560
4561 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004562 * Vblank time updates from the shadow to live plane control register
4563 * are blocked if the memory self-refresh mode is active at that
4564 * moment. So to make sure the plane gets truly disabled, disable
4565 * first the self-refresh mode. The self-refresh enable bit in turn
4566 * will be checked/applied by the HW only at the next frame start
4567 * event which is after the vblank start event, so we need to have a
4568 * wait-for-vblank between disabling the plane and the pipe.
4569 */
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004570 if (HAS_GMCH_DISPLAY(dev)) {
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004571 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004572 dev_priv->wm.vlv.cxsr = false;
4573 intel_wait_for_vblank(dev, pipe);
4574 }
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004575}
4576
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004577static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004578{
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004579 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
4580 struct drm_atomic_state *old_state = old_crtc_state->base.state;
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004581 struct intel_crtc_state *pipe_config =
4582 to_intel_crtc_state(crtc->base.state);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004583 struct drm_device *dev = crtc->base.dev;
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004584 struct drm_plane *primary = crtc->base.primary;
4585 struct drm_plane_state *old_pri_state =
4586 drm_atomic_get_existing_plane_state(old_state, primary);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004587
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004588 intel_frontbuffer_flip(dev, pipe_config->fb_bits);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004589
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01004590 crtc->wm.cxsr_allowed = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +03004591
Ville Syrjäläcaed3612016-03-09 19:07:25 +02004592 if (pipe_config->update_wm_post && pipe_config->base.active)
Ville Syrjäläf015c552015-06-24 22:00:02 +03004593 intel_update_watermarks(&crtc->base);
4594
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004595 if (old_pri_state) {
4596 struct intel_plane_state *primary_state =
4597 to_intel_plane_state(primary->state);
4598 struct intel_plane_state *old_primary_state =
4599 to_intel_plane_state(old_pri_state);
4600
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +01004601 intel_fbc_post_update(crtc);
4602
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004603 if (primary_state->visible &&
4604 (needs_modeset(&pipe_config->base) ||
4605 !old_primary_state->visible))
4606 intel_post_enable_primary(&crtc->base);
4607 }
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004608}
4609
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004610static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004611{
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004612 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004613 struct drm_device *dev = crtc->base.dev;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +02004614 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01004615 struct intel_crtc_state *pipe_config =
4616 to_intel_crtc_state(crtc->base.state);
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004617 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4618 struct drm_plane *primary = crtc->base.primary;
4619 struct drm_plane_state *old_pri_state =
4620 drm_atomic_get_existing_plane_state(old_state, primary);
4621 bool modeset = needs_modeset(&pipe_config->base);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004622
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004623 if (old_pri_state) {
4624 struct intel_plane_state *primary_state =
4625 to_intel_plane_state(primary->state);
4626 struct intel_plane_state *old_primary_state =
4627 to_intel_plane_state(old_pri_state);
4628
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +01004629 intel_fbc_pre_update(crtc);
4630
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004631 if (old_primary_state->visible &&
4632 (modeset || !primary_state->visible))
4633 intel_pre_disable_primary(&crtc->base);
4634 }
Ville Syrjälä852eb002015-06-24 22:00:07 +03004635
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01004636 if (pipe_config->disable_cxsr) {
Ville Syrjälä852eb002015-06-24 22:00:07 +03004637 crtc->wm.cxsr_allowed = false;
Maarten Lankhorst2dfd1782016-02-03 16:53:25 +01004638
Ville Syrjälä2622a082016-03-09 19:07:26 +02004639 /*
4640 * Vblank time updates from the shadow to live plane control register
4641 * are blocked if the memory self-refresh mode is active at that
4642 * moment. So to make sure the plane gets truly disabled, disable
4643 * first the self-refresh mode. The self-refresh enable bit in turn
4644 * will be checked/applied by the HW only at the next frame start
4645 * event which is after the vblank start event, so we need to have a
4646 * wait-for-vblank between disabling the plane and the pipe.
4647 */
4648 if (old_crtc_state->base.active) {
Maarten Lankhorst2dfd1782016-02-03 16:53:25 +01004649 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä2622a082016-03-09 19:07:26 +02004650 dev_priv->wm.vlv.cxsr = false;
4651 intel_wait_for_vblank(dev, crtc->pipe);
4652 }
Ville Syrjälä852eb002015-06-24 22:00:07 +03004653 }
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004654
Matt Ropered4a6a72016-02-23 17:20:13 -08004655 /*
4656 * IVB workaround: must disable low power watermarks for at least
4657 * one frame before enabling scaling. LP watermarks can be re-enabled
4658 * when scaling is disabled.
4659 *
4660 * WaCxSRDisabledForSpriteScaling:ivb
4661 */
4662 if (pipe_config->disable_lp_wm) {
4663 ilk_disable_lp_wm(dev);
4664 intel_wait_for_vblank(dev, crtc->pipe);
4665 }
4666
4667 /*
4668 * If we're doing a modeset, we're done. No need to do any pre-vblank
4669 * watermark programming here.
4670 */
4671 if (needs_modeset(&pipe_config->base))
4672 return;
4673
4674 /*
4675 * For platforms that support atomic watermarks, program the
4676 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
4677 * will be the intermediate values that are safe for both pre- and
4678 * post- vblank; when vblank happens, the 'active' values will be set
4679 * to the final 'target' values and we'll do this again to get the
4680 * optimal watermarks. For gen9+ platforms, the values we program here
4681 * will be the final target values which will get automatically latched
4682 * at vblank time; no further programming will be necessary.
4683 *
4684 * If a platform hasn't been transitioned to atomic watermarks yet,
4685 * we'll continue to update watermarks the old way, if flags tell
4686 * us to.
4687 */
4688 if (dev_priv->display.initial_watermarks != NULL)
4689 dev_priv->display.initial_watermarks(pipe_config);
Ville Syrjäläcaed3612016-03-09 19:07:25 +02004690 else if (pipe_config->update_wm_pre)
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004691 intel_update_watermarks(&crtc->base);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004692}
4693
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004694static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004695{
4696 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004697 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004698 struct drm_plane *p;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004699 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004700
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004701 intel_crtc_dpms_overlay_disable(intel_crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004702
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004703 drm_for_each_plane_mask(p, dev, plane_mask)
4704 to_intel_plane(p)->disable_plane(p, crtc);
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004705
Daniel Vetterf99d7062014-06-19 16:01:59 +02004706 /*
4707 * FIXME: Once we grow proper nuclear flip support out of this we need
4708 * to compute the mask of flip planes precisely. For the time being
4709 * consider this a flip to a NULL plane.
4710 */
4711 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004712}
4713
Jesse Barnesf67a5592011-01-05 10:31:48 -08004714static void ironlake_crtc_enable(struct drm_crtc *crtc)
4715{
4716 struct drm_device *dev = crtc->dev;
4717 struct drm_i915_private *dev_priv = dev->dev_private;
4718 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004719 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004720 int pipe = intel_crtc->pipe;
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004721 struct intel_crtc_state *pipe_config =
4722 to_intel_crtc_state(crtc->state);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004723
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004724 if (WARN_ON(intel_crtc->active))
Jesse Barnesf67a5592011-01-05 10:31:48 -08004725 return;
4726
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004727 /*
4728 * Sometimes spurious CPU pipe underruns happen during FDI
4729 * training, at least with VGA+HDMI cloning. Suppress them.
4730 *
4731 * On ILK we get an occasional spurious CPU pipe underruns
4732 * between eDP port A enable and vdd enable. Also PCH port
4733 * enable seems to result in the occasional CPU pipe underrun.
4734 *
4735 * Spurious PCH underruns also occur during PCH enabling.
4736 */
4737 if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
4738 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004739 if (intel_crtc->config->has_pch_encoder)
Ville Syrjälä81b088c2015-10-30 19:21:31 +02004740 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
4741
4742 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02004743 intel_prepare_shared_dpll(intel_crtc);
4744
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004745 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05304746 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004747
4748 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02004749 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004750
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004751 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02004752 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004753 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004754 }
4755
4756 ironlake_set_pipeconf(crtc);
4757
Jesse Barnesf67a5592011-01-05 10:31:48 -08004758 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004759
Daniel Vetterf6736a12013-06-05 13:34:30 +02004760 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004761 if (encoder->pre_enable)
4762 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004763
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004764 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004765 /* Note: FDI PLL enabling _must_ be done before we enable the
4766 * cpu pipes, hence this is separate from all the other fdi/pch
4767 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004768 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004769 } else {
4770 assert_fdi_tx_disabled(dev_priv, pipe);
4771 assert_fdi_rx_disabled(dev_priv, pipe);
4772 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004773
Jesse Barnesb074cec2013-04-25 12:55:02 -07004774 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004775
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004776 /*
4777 * On ILK+ LUT must be loaded before the pipe is running but with
4778 * clocks enabled
4779 */
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004780 intel_color_load_luts(&pipe_config->base);
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004781
Imre Deak1d5bf5d2016-02-29 22:10:33 +02004782 if (dev_priv->display.initial_watermarks != NULL)
4783 dev_priv->display.initial_watermarks(intel_crtc->config);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004784 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004785
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004786 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004787 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004788
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004789 assert_vblank_disabled(crtc);
4790 drm_crtc_vblank_on(crtc);
4791
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004792 for_each_encoder_on_crtc(dev, crtc, encoder)
4793 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004794
4795 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004796 cpt_verify_modeset(dev, intel_crtc->pipe);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004797
4798 /* Must wait for vblank to avoid spurious PCH FIFO underruns */
4799 if (intel_crtc->config->has_pch_encoder)
4800 intel_wait_for_vblank(dev, pipe);
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004801 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004802 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004803}
4804
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004805/* IPS only exists on ULT machines and is tied to pipe A. */
4806static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4807{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004808 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004809}
4810
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004811static void haswell_crtc_enable(struct drm_crtc *crtc)
4812{
4813 struct drm_device *dev = crtc->dev;
4814 struct drm_i915_private *dev_priv = dev->dev_private;
4815 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4816 struct intel_encoder *encoder;
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004817 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
Jani Nikula4d1de972016-03-18 17:05:42 +02004818 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004819 struct intel_crtc_state *pipe_config =
4820 to_intel_crtc_state(crtc->state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004821
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004822 if (WARN_ON(intel_crtc->active))
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004823 return;
4824
Ville Syrjälä81b088c2015-10-30 19:21:31 +02004825 if (intel_crtc->config->has_pch_encoder)
4826 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4827 false);
4828
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02004829 if (intel_crtc->config->shared_dpll)
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004830 intel_enable_shared_dpll(intel_crtc);
4831
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004832 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05304833 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02004834
Jani Nikula4d1de972016-03-18 17:05:42 +02004835 if (!intel_crtc->config->has_dsi_encoder)
4836 intel_set_pipe_timings(intel_crtc);
4837
Jani Nikulabc58be62016-03-18 17:05:39 +02004838 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02004839
Jani Nikula4d1de972016-03-18 17:05:42 +02004840 if (cpu_transcoder != TRANSCODER_EDP &&
4841 !transcoder_is_dsi(cpu_transcoder)) {
4842 I915_WRITE(PIPE_MULT(cpu_transcoder),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004843 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07004844 }
4845
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004846 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02004847 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004848 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02004849 }
4850
Jani Nikula4d1de972016-03-18 17:05:42 +02004851 if (!intel_crtc->config->has_dsi_encoder)
4852 haswell_set_pipeconf(crtc);
4853
Jani Nikula391bf042016-03-18 17:05:40 +02004854 haswell_set_pipemisc(crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02004855
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004856 intel_color_set_csc(&pipe_config->base);
Daniel Vetter229fca92014-04-24 23:55:09 +02004857
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004858 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004859
Daniel Vetter6b698512015-11-28 11:05:39 +01004860 if (intel_crtc->config->has_pch_encoder)
4861 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4862 else
4863 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4864
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304865 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004866 if (encoder->pre_enable)
4867 encoder->pre_enable(encoder);
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304868 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004869
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004870 if (intel_crtc->config->has_pch_encoder)
Imre Deak4fe94672014-06-25 22:01:49 +03004871 dev_priv->display.fdi_link_train(crtc);
Imre Deak4fe94672014-06-25 22:01:49 +03004872
Jani Nikulaa65347b2015-11-27 12:21:46 +02004873 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304874 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004875
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07004876 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004877 skylake_pfit_enable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08004878 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07004879 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004880
4881 /*
4882 * On ILK+ LUT must be loaded before the pipe is running but with
4883 * clocks enabled
4884 */
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004885 intel_color_load_luts(&pipe_config->base);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004886
Paulo Zanoni1f544382012-10-24 11:32:00 -02004887 intel_ddi_set_pipe_settings(crtc);
Jani Nikulaa65347b2015-11-27 12:21:46 +02004888 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304889 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004890
Imre Deak1d5bf5d2016-02-29 22:10:33 +02004891 if (dev_priv->display.initial_watermarks != NULL)
4892 dev_priv->display.initial_watermarks(pipe_config);
4893 else
4894 intel_update_watermarks(crtc);
Jani Nikula4d1de972016-03-18 17:05:42 +02004895
4896 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
4897 if (!intel_crtc->config->has_dsi_encoder)
4898 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004899
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004900 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004901 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004902
Jani Nikulaa65347b2015-11-27 12:21:46 +02004903 if (intel_crtc->config->dp_encoder_is_mst)
Dave Airlie0e32b392014-05-02 14:02:48 +10004904 intel_ddi_set_vc_payload_alloc(crtc, true);
4905
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004906 assert_vblank_disabled(crtc);
4907 drm_crtc_vblank_on(crtc);
4908
Jani Nikula8807e552013-08-30 19:40:32 +03004909 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004910 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004911 intel_opregion_notify_encoder(encoder, true);
4912 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004913
Daniel Vetter6b698512015-11-28 11:05:39 +01004914 if (intel_crtc->config->has_pch_encoder) {
4915 intel_wait_for_vblank(dev, pipe);
4916 intel_wait_for_vblank(dev, pipe);
4917 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004918 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4919 true);
Daniel Vetter6b698512015-11-28 11:05:39 +01004920 }
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004921
Paulo Zanonie4916942013-09-20 16:21:19 -03004922 /* If we change the relative order between pipe/planes enabling, we need
4923 * to change the workaround. */
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004924 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
4925 if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
4926 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4927 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4928 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004929}
4930
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02004931static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004932{
4933 struct drm_device *dev = crtc->base.dev;
4934 struct drm_i915_private *dev_priv = dev->dev_private;
4935 int pipe = crtc->pipe;
4936
4937 /* To avoid upsetting the power well on haswell only disable the pfit if
4938 * it's in use. The hw state code will make sure we get this right. */
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02004939 if (force || crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004940 I915_WRITE(PF_CTL(pipe), 0);
4941 I915_WRITE(PF_WIN_POS(pipe), 0);
4942 I915_WRITE(PF_WIN_SZ(pipe), 0);
4943 }
4944}
4945
Jesse Barnes6be4a602010-09-10 10:26:01 -07004946static void ironlake_crtc_disable(struct drm_crtc *crtc)
4947{
4948 struct drm_device *dev = crtc->dev;
4949 struct drm_i915_private *dev_priv = dev->dev_private;
4950 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004951 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004952 int pipe = intel_crtc->pipe;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004953
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004954 /*
4955 * Sometimes spurious CPU pipe underruns happen when the
4956 * pipe is already disabled, but FDI RX/TX is still enabled.
4957 * Happens at least with VGA+HDMI cloning. Suppress them.
4958 */
4959 if (intel_crtc->config->has_pch_encoder) {
4960 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004961 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004962 }
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004963
Daniel Vetterea9d7582012-07-10 10:42:52 +02004964 for_each_encoder_on_crtc(dev, crtc, encoder)
4965 encoder->disable(encoder);
4966
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004967 drm_crtc_vblank_off(crtc);
4968 assert_vblank_disabled(crtc);
4969
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03004970 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004971
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02004972 ironlake_pfit_disable(intel_crtc, false);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004973
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004974 if (intel_crtc->config->has_pch_encoder)
Ville Syrjälä5a74f702015-05-05 17:17:38 +03004975 ironlake_fdi_disable(crtc);
4976
Daniel Vetterbf49ec82012-09-06 22:15:40 +02004977 for_each_encoder_on_crtc(dev, crtc, encoder)
4978 if (encoder->post_disable)
4979 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004980
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004981 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02004982 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004983
Daniel Vetterd925c592013-06-05 13:34:04 +02004984 if (HAS_PCH_CPT(dev)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004985 i915_reg_t reg;
4986 u32 temp;
4987
Daniel Vetterd925c592013-06-05 13:34:04 +02004988 /* disable TRANS_DP_CTL */
4989 reg = TRANS_DP_CTL(pipe);
4990 temp = I915_READ(reg);
4991 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
4992 TRANS_DP_PORT_SEL_MASK);
4993 temp |= TRANS_DP_PORT_SEL_NONE;
4994 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004995
Daniel Vetterd925c592013-06-05 13:34:04 +02004996 /* disable DPLL_SEL */
4997 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004998 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02004999 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005000 }
Daniel Vetterd925c592013-06-05 13:34:04 +02005001
Daniel Vetterd925c592013-06-05 13:34:04 +02005002 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005003 }
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005004
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005005 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005006 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005007}
5008
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005009static void haswell_crtc_disable(struct drm_crtc *crtc)
5010{
5011 struct drm_device *dev = crtc->dev;
5012 struct drm_i915_private *dev_priv = dev->dev_private;
5013 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5014 struct intel_encoder *encoder;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005015 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005016
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005017 if (intel_crtc->config->has_pch_encoder)
5018 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5019 false);
5020
Jani Nikula8807e552013-08-30 19:40:32 +03005021 for_each_encoder_on_crtc(dev, crtc, encoder) {
5022 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005023 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005024 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005025
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005026 drm_crtc_vblank_off(crtc);
5027 assert_vblank_disabled(crtc);
5028
Jani Nikula4d1de972016-03-18 17:05:42 +02005029 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
5030 if (!intel_crtc->config->has_dsi_encoder)
5031 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005032
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005033 if (intel_crtc->config->dp_encoder_is_mst)
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03005034 intel_ddi_set_vc_payload_alloc(crtc, false);
5035
Jani Nikulaa65347b2015-11-27 12:21:46 +02005036 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305037 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005038
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07005039 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02005040 skylake_scaler_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005041 else
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005042 ironlake_pfit_disable(intel_crtc, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005043
Jani Nikulaa65347b2015-11-27 12:21:46 +02005044 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305045 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005046
Imre Deak97b040a2014-06-25 22:01:50 +03005047 for_each_encoder_on_crtc(dev, crtc, encoder)
5048 if (encoder->post_disable)
5049 encoder->post_disable(encoder);
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005050
Ville Syrjälä92966a32015-12-08 16:05:48 +02005051 if (intel_crtc->config->has_pch_encoder) {
5052 lpt_disable_pch_transcoder(dev_priv);
Ville Syrjälä503a74e2015-12-04 22:22:14 +02005053 lpt_disable_iclkip(dev_priv);
Ville Syrjälä92966a32015-12-08 16:05:48 +02005054 intel_ddi_fdi_disable(crtc);
5055
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005056 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5057 true);
Ville Syrjälä92966a32015-12-08 16:05:48 +02005058 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005059}
5060
Jesse Barnes2dd24552013-04-25 12:55:01 -07005061static void i9xx_pfit_enable(struct intel_crtc *crtc)
5062{
5063 struct drm_device *dev = crtc->base.dev;
5064 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005065 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07005066
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02005067 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005068 return;
5069
Daniel Vetterc0b03412013-05-28 12:05:54 +02005070 /*
5071 * The panel fitter should only be adjusted whilst the pipe is disabled,
5072 * according to register description and PRM.
5073 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005074 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5075 assert_pipe_disabled(dev_priv, crtc->pipe);
5076
Jesse Barnesb074cec2013-04-25 12:55:02 -07005077 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5078 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005079
5080 /* Border color in case we don't scale up to the full screen. Black by
5081 * default, change to something else for debugging. */
5082 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005083}
5084
Dave Airlied05410f2014-06-05 13:22:59 +10005085static enum intel_display_power_domain port_to_power_domain(enum port port)
5086{
5087 switch (port) {
5088 case PORT_A:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005089 return POWER_DOMAIN_PORT_DDI_A_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005090 case PORT_B:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005091 return POWER_DOMAIN_PORT_DDI_B_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005092 case PORT_C:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005093 return POWER_DOMAIN_PORT_DDI_C_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005094 case PORT_D:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005095 return POWER_DOMAIN_PORT_DDI_D_LANES;
Xiong Zhangd8e19f92015-08-13 18:00:12 +08005096 case PORT_E:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005097 return POWER_DOMAIN_PORT_DDI_E_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005098 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005099 MISSING_CASE(port);
Dave Airlied05410f2014-06-05 13:22:59 +10005100 return POWER_DOMAIN_PORT_OTHER;
5101 }
5102}
5103
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005104static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
5105{
5106 switch (port) {
5107 case PORT_A:
5108 return POWER_DOMAIN_AUX_A;
5109 case PORT_B:
5110 return POWER_DOMAIN_AUX_B;
5111 case PORT_C:
5112 return POWER_DOMAIN_AUX_C;
5113 case PORT_D:
5114 return POWER_DOMAIN_AUX_D;
5115 case PORT_E:
5116 /* FIXME: Check VBT for actual wiring of PORT E */
5117 return POWER_DOMAIN_AUX_D;
5118 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005119 MISSING_CASE(port);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005120 return POWER_DOMAIN_AUX_A;
5121 }
5122}
5123
Imre Deak319be8a2014-03-04 19:22:57 +02005124enum intel_display_power_domain
5125intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02005126{
Imre Deak319be8a2014-03-04 19:22:57 +02005127 struct drm_device *dev = intel_encoder->base.dev;
5128 struct intel_digital_port *intel_dig_port;
5129
5130 switch (intel_encoder->type) {
5131 case INTEL_OUTPUT_UNKNOWN:
5132 /* Only DDI platforms should ever use this output type */
5133 WARN_ON_ONCE(!HAS_DDI(dev));
5134 case INTEL_OUTPUT_DISPLAYPORT:
5135 case INTEL_OUTPUT_HDMI:
5136 case INTEL_OUTPUT_EDP:
5137 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10005138 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10005139 case INTEL_OUTPUT_DP_MST:
5140 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5141 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02005142 case INTEL_OUTPUT_ANALOG:
5143 return POWER_DOMAIN_PORT_CRT;
5144 case INTEL_OUTPUT_DSI:
5145 return POWER_DOMAIN_PORT_DSI;
5146 default:
5147 return POWER_DOMAIN_PORT_OTHER;
5148 }
5149}
5150
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005151enum intel_display_power_domain
5152intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
5153{
5154 struct drm_device *dev = intel_encoder->base.dev;
5155 struct intel_digital_port *intel_dig_port;
5156
5157 switch (intel_encoder->type) {
5158 case INTEL_OUTPUT_UNKNOWN:
Imre Deak651174a2015-11-18 15:57:24 +02005159 case INTEL_OUTPUT_HDMI:
5160 /*
5161 * Only DDI platforms should ever use these output types.
5162 * We can get here after the HDMI detect code has already set
5163 * the type of the shared encoder. Since we can't be sure
5164 * what's the status of the given connectors, play safe and
5165 * run the DP detection too.
5166 */
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005167 WARN_ON_ONCE(!HAS_DDI(dev));
5168 case INTEL_OUTPUT_DISPLAYPORT:
5169 case INTEL_OUTPUT_EDP:
5170 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
5171 return port_to_aux_power_domain(intel_dig_port->port);
5172 case INTEL_OUTPUT_DP_MST:
5173 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5174 return port_to_aux_power_domain(intel_dig_port->port);
5175 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005176 MISSING_CASE(intel_encoder->type);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005177 return POWER_DOMAIN_AUX_A;
5178 }
5179}
5180
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005181static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
5182 struct intel_crtc_state *crtc_state)
Imre Deak319be8a2014-03-04 19:22:57 +02005183{
5184 struct drm_device *dev = crtc->dev;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005185 struct drm_encoder *encoder;
Imre Deak319be8a2014-03-04 19:22:57 +02005186 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5187 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02005188 unsigned long mask;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005189 enum transcoder transcoder = crtc_state->cpu_transcoder;
Imre Deak77d22dc2014-03-05 16:20:52 +02005190
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005191 if (!crtc_state->base.active)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005192 return 0;
5193
Imre Deak77d22dc2014-03-05 16:20:52 +02005194 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5195 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005196 if (crtc_state->pch_pfit.enabled ||
5197 crtc_state->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02005198 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5199
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005200 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
5201 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5202
Imre Deak319be8a2014-03-04 19:22:57 +02005203 mask |= BIT(intel_display_port_power_domain(intel_encoder));
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005204 }
Imre Deak319be8a2014-03-04 19:22:57 +02005205
Maarten Lankhorst15e7ec22016-03-14 09:27:54 +01005206 if (crtc_state->shared_dpll)
5207 mask |= BIT(POWER_DOMAIN_PLLS);
5208
Imre Deak77d22dc2014-03-05 16:20:52 +02005209 return mask;
5210}
5211
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005212static unsigned long
5213modeset_get_crtc_power_domains(struct drm_crtc *crtc,
5214 struct intel_crtc_state *crtc_state)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005215{
5216 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5217 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5218 enum intel_display_power_domain domain;
5219 unsigned long domains, new_domains, old_domains;
5220
5221 old_domains = intel_crtc->enabled_power_domains;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005222 intel_crtc->enabled_power_domains = new_domains =
5223 get_crtc_power_domains(crtc, crtc_state);
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005224
5225 domains = new_domains & ~old_domains;
5226
5227 for_each_power_domain(domain, domains)
5228 intel_display_power_get(dev_priv, domain);
5229
5230 return old_domains & ~new_domains;
5231}
5232
5233static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
5234 unsigned long domains)
5235{
5236 enum intel_display_power_domain domain;
5237
5238 for_each_power_domain(domain, domains)
5239 intel_display_power_put(dev_priv, domain);
5240}
5241
Mika Kaholaadafdc62015-08-18 14:36:59 +03005242static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
5243{
5244 int max_cdclk_freq = dev_priv->max_cdclk_freq;
5245
5246 if (INTEL_INFO(dev_priv)->gen >= 9 ||
5247 IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
5248 return max_cdclk_freq;
5249 else if (IS_CHERRYVIEW(dev_priv))
5250 return max_cdclk_freq*95/100;
5251 else if (INTEL_INFO(dev_priv)->gen < 4)
5252 return 2*max_cdclk_freq*90/100;
5253 else
5254 return max_cdclk_freq*90/100;
5255}
5256
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005257static void intel_update_max_cdclk(struct drm_device *dev)
5258{
5259 struct drm_i915_private *dev_priv = dev->dev_private;
5260
Rodrigo Vivief11bdb2015-10-28 04:16:45 -07005261 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005262 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
5263
5264 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
5265 dev_priv->max_cdclk_freq = 675000;
5266 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
5267 dev_priv->max_cdclk_freq = 540000;
5268 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
5269 dev_priv->max_cdclk_freq = 450000;
5270 else
5271 dev_priv->max_cdclk_freq = 337500;
Matt Roper281c1142016-04-05 14:37:19 -07005272 } else if (IS_BROXTON(dev)) {
5273 dev_priv->max_cdclk_freq = 624000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005274 } else if (IS_BROADWELL(dev)) {
5275 /*
5276 * FIXME with extra cooling we can allow
5277 * 540 MHz for ULX and 675 Mhz for ULT.
5278 * How can we know if extra cooling is
5279 * available? PCI ID, VTB, something else?
5280 */
5281 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5282 dev_priv->max_cdclk_freq = 450000;
5283 else if (IS_BDW_ULX(dev))
5284 dev_priv->max_cdclk_freq = 450000;
5285 else if (IS_BDW_ULT(dev))
5286 dev_priv->max_cdclk_freq = 540000;
5287 else
5288 dev_priv->max_cdclk_freq = 675000;
Mika Kahola0904dea2015-06-12 10:11:32 +03005289 } else if (IS_CHERRYVIEW(dev)) {
5290 dev_priv->max_cdclk_freq = 320000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005291 } else if (IS_VALLEYVIEW(dev)) {
5292 dev_priv->max_cdclk_freq = 400000;
5293 } else {
5294 /* otherwise assume cdclk is fixed */
5295 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5296 }
5297
Mika Kaholaadafdc62015-08-18 14:36:59 +03005298 dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
5299
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005300 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5301 dev_priv->max_cdclk_freq);
Mika Kaholaadafdc62015-08-18 14:36:59 +03005302
5303 DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
5304 dev_priv->max_dotclk_freq);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005305}
5306
5307static void intel_update_cdclk(struct drm_device *dev)
5308{
5309 struct drm_i915_private *dev_priv = dev->dev_private;
5310
5311 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
5312 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5313 dev_priv->cdclk_freq);
5314
5315 /*
5316 * Program the gmbus_freq based on the cdclk frequency.
5317 * BSpec erroneously claims we should aim for 4MHz, but
5318 * in fact 1MHz is the correct frequency.
5319 */
Wayne Boyer666a4532015-12-09 12:29:35 -08005320 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005321 /*
5322 * Program the gmbus_freq based on the cdclk frequency.
5323 * BSpec erroneously claims we should aim for 4MHz, but
5324 * in fact 1MHz is the correct frequency.
5325 */
5326 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
5327 }
5328
5329 if (dev_priv->max_cdclk_freq == 0)
5330 intel_update_max_cdclk(dev);
5331}
5332
Damien Lespiau70d0c572015-06-04 18:21:29 +01005333static void broxton_set_cdclk(struct drm_device *dev, int frequency)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305334{
5335 struct drm_i915_private *dev_priv = dev->dev_private;
5336 uint32_t divider;
5337 uint32_t ratio;
5338 uint32_t current_freq;
5339 int ret;
5340
5341 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
5342 switch (frequency) {
5343 case 144000:
5344 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5345 ratio = BXT_DE_PLL_RATIO(60);
5346 break;
5347 case 288000:
5348 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5349 ratio = BXT_DE_PLL_RATIO(60);
5350 break;
5351 case 384000:
5352 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5353 ratio = BXT_DE_PLL_RATIO(60);
5354 break;
5355 case 576000:
5356 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5357 ratio = BXT_DE_PLL_RATIO(60);
5358 break;
5359 case 624000:
5360 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5361 ratio = BXT_DE_PLL_RATIO(65);
5362 break;
5363 case 19200:
5364 /*
5365 * Bypass frequency with DE PLL disabled. Init ratio, divider
5366 * to suppress GCC warning.
5367 */
5368 ratio = 0;
5369 divider = 0;
5370 break;
5371 default:
5372 DRM_ERROR("unsupported CDCLK freq %d", frequency);
5373
5374 return;
5375 }
5376
5377 mutex_lock(&dev_priv->rps.hw_lock);
5378 /* Inform power controller of upcoming frequency change */
5379 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5380 0x80000000);
5381 mutex_unlock(&dev_priv->rps.hw_lock);
5382
5383 if (ret) {
5384 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
5385 ret, frequency);
5386 return;
5387 }
5388
5389 current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
5390 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
5391 current_freq = current_freq * 500 + 1000;
5392
5393 /*
5394 * DE PLL has to be disabled when
5395 * - setting to 19.2MHz (bypass, PLL isn't used)
5396 * - before setting to 624MHz (PLL needs toggling)
5397 * - before setting to any frequency from 624MHz (PLL needs toggling)
5398 */
5399 if (frequency == 19200 || frequency == 624000 ||
5400 current_freq == 624000) {
5401 I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5402 /* Timeout 200us */
5403 if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5404 1))
5405 DRM_ERROR("timout waiting for DE PLL unlock\n");
5406 }
5407
5408 if (frequency != 19200) {
5409 uint32_t val;
5410
5411 val = I915_READ(BXT_DE_PLL_CTL);
5412 val &= ~BXT_DE_PLL_RATIO_MASK;
5413 val |= ratio;
5414 I915_WRITE(BXT_DE_PLL_CTL, val);
5415
5416 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5417 /* Timeout 200us */
5418 if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5419 DRM_ERROR("timeout waiting for DE PLL lock\n");
5420
5421 val = I915_READ(CDCLK_CTL);
5422 val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
5423 val |= divider;
5424 /*
5425 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5426 * enable otherwise.
5427 */
5428 val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5429 if (frequency >= 500000)
5430 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5431
5432 val &= ~CDCLK_FREQ_DECIMAL_MASK;
5433 /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5434 val |= (frequency - 1000) / 500;
5435 I915_WRITE(CDCLK_CTL, val);
5436 }
5437
5438 mutex_lock(&dev_priv->rps.hw_lock);
5439 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5440 DIV_ROUND_UP(frequency, 25000));
5441 mutex_unlock(&dev_priv->rps.hw_lock);
5442
5443 if (ret) {
5444 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
5445 ret, frequency);
5446 return;
5447 }
5448
Damien Lespiaua47871b2015-06-04 18:21:34 +01005449 intel_update_cdclk(dev);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305450}
5451
5452void broxton_init_cdclk(struct drm_device *dev)
5453{
5454 struct drm_i915_private *dev_priv = dev->dev_private;
5455 uint32_t val;
5456
5457 /*
5458 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
5459 * or else the reset will hang because there is no PCH to respond.
5460 * Move the handshake programming to initialization sequence.
5461 * Previously was left up to BIOS.
5462 */
5463 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5464 val &= ~RESET_PCH_HANDSHAKE_ENABLE;
5465 I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
5466
5467 /* Enable PG1 for cdclk */
5468 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5469
5470 /* check if cd clock is enabled */
5471 if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
5472 DRM_DEBUG_KMS("Display already initialized\n");
5473 return;
5474 }
5475
5476 /*
5477 * FIXME:
5478 * - The initial CDCLK needs to be read from VBT.
5479 * Need to make this change after VBT has changes for BXT.
5480 * - check if setting the max (or any) cdclk freq is really necessary
5481 * here, it belongs to modeset time
5482 */
5483 broxton_set_cdclk(dev, 624000);
5484
5485 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005486 POSTING_READ(DBUF_CTL);
5487
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305488 udelay(10);
5489
5490 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5491 DRM_ERROR("DBuf power enable timeout!\n");
5492}
5493
5494void broxton_uninit_cdclk(struct drm_device *dev)
5495{
5496 struct drm_i915_private *dev_priv = dev->dev_private;
5497
5498 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005499 POSTING_READ(DBUF_CTL);
5500
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305501 udelay(10);
5502
5503 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5504 DRM_ERROR("DBuf power disable timeout!\n");
5505
5506 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
5507 broxton_set_cdclk(dev, 19200);
5508
5509 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5510}
5511
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005512static const struct skl_cdclk_entry {
5513 unsigned int freq;
5514 unsigned int vco;
5515} skl_cdclk_frequencies[] = {
5516 { .freq = 308570, .vco = 8640 },
5517 { .freq = 337500, .vco = 8100 },
5518 { .freq = 432000, .vco = 8640 },
5519 { .freq = 450000, .vco = 8100 },
5520 { .freq = 540000, .vco = 8100 },
5521 { .freq = 617140, .vco = 8640 },
5522 { .freq = 675000, .vco = 8100 },
5523};
5524
5525static unsigned int skl_cdclk_decimal(unsigned int freq)
5526{
5527 return (freq - 1000) / 500;
5528}
5529
5530static unsigned int skl_cdclk_get_vco(unsigned int freq)
5531{
5532 unsigned int i;
5533
5534 for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
5535 const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
5536
5537 if (e->freq == freq)
5538 return e->vco;
5539 }
5540
5541 return 8100;
5542}
5543
5544static void
5545skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
5546{
5547 unsigned int min_freq;
5548 u32 val;
5549
5550 /* select the minimum CDCLK before enabling DPLL 0 */
5551 val = I915_READ(CDCLK_CTL);
5552 val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
5553 val |= CDCLK_FREQ_337_308;
5554
5555 if (required_vco == 8640)
5556 min_freq = 308570;
5557 else
5558 min_freq = 337500;
5559
5560 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
5561
5562 I915_WRITE(CDCLK_CTL, val);
5563 POSTING_READ(CDCLK_CTL);
5564
5565 /*
5566 * We always enable DPLL0 with the lowest link rate possible, but still
5567 * taking into account the VCO required to operate the eDP panel at the
5568 * desired frequency. The usual DP link rates operate with a VCO of
5569 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5570 * The modeset code is responsible for the selection of the exact link
5571 * rate later on, with the constraint of choosing a frequency that
5572 * works with required_vco.
5573 */
5574 val = I915_READ(DPLL_CTRL1);
5575
5576 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5577 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5578 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
5579 if (required_vco == 8640)
5580 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5581 SKL_DPLL0);
5582 else
5583 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5584 SKL_DPLL0);
5585
5586 I915_WRITE(DPLL_CTRL1, val);
5587 POSTING_READ(DPLL_CTRL1);
5588
5589 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5590
5591 if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
5592 DRM_ERROR("DPLL0 not locked\n");
5593}
5594
5595static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5596{
5597 int ret;
5598 u32 val;
5599
5600 /* inform PCU we want to change CDCLK */
5601 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5602 mutex_lock(&dev_priv->rps.hw_lock);
5603 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5604 mutex_unlock(&dev_priv->rps.hw_lock);
5605
5606 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5607}
5608
5609static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5610{
5611 unsigned int i;
5612
5613 for (i = 0; i < 15; i++) {
5614 if (skl_cdclk_pcu_ready(dev_priv))
5615 return true;
5616 udelay(10);
5617 }
5618
5619 return false;
5620}
5621
5622static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
5623{
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005624 struct drm_device *dev = dev_priv->dev;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005625 u32 freq_select, pcu_ack;
5626
5627 DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
5628
5629 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5630 DRM_ERROR("failed to inform PCU about cdclk change\n");
5631 return;
5632 }
5633
5634 /* set CDCLK_CTL */
5635 switch(freq) {
5636 case 450000:
5637 case 432000:
5638 freq_select = CDCLK_FREQ_450_432;
5639 pcu_ack = 1;
5640 break;
5641 case 540000:
5642 freq_select = CDCLK_FREQ_540;
5643 pcu_ack = 2;
5644 break;
5645 case 308570:
5646 case 337500:
5647 default:
5648 freq_select = CDCLK_FREQ_337_308;
5649 pcu_ack = 0;
5650 break;
5651 case 617140:
5652 case 675000:
5653 freq_select = CDCLK_FREQ_675_617;
5654 pcu_ack = 3;
5655 break;
5656 }
5657
5658 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
5659 POSTING_READ(CDCLK_CTL);
5660
5661 /* inform PCU of the change */
5662 mutex_lock(&dev_priv->rps.hw_lock);
5663 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5664 mutex_unlock(&dev_priv->rps.hw_lock);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005665
5666 intel_update_cdclk(dev);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005667}
5668
5669void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5670{
5671 /* disable DBUF power */
5672 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
5673 POSTING_READ(DBUF_CTL);
5674
5675 udelay(10);
5676
5677 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5678 DRM_ERROR("DBuf power disable timeout\n");
5679
Imre Deakab96c1ee2015-11-04 19:24:18 +02005680 /* disable DPLL0 */
5681 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
5682 if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
5683 DRM_ERROR("Couldn't disable DPLL0\n");
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005684}
5685
5686void skl_init_cdclk(struct drm_i915_private *dev_priv)
5687{
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005688 unsigned int required_vco;
5689
Gary Wang39d9b852015-08-28 16:40:34 +08005690 /* DPLL0 not enabled (happens on early BIOS versions) */
5691 if (!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE)) {
5692 /* enable DPLL0 */
5693 required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
5694 skl_dpll0_enable(dev_priv, required_vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005695 }
5696
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005697 /* set CDCLK to the frequency the BIOS chose */
5698 skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
5699
5700 /* enable DBUF power */
5701 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
5702 POSTING_READ(DBUF_CTL);
5703
5704 udelay(10);
5705
5706 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5707 DRM_ERROR("DBuf power enable timeout\n");
5708}
5709
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305710int skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
5711{
5712 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
5713 uint32_t cdctl = I915_READ(CDCLK_CTL);
5714 int freq = dev_priv->skl_boot_cdclk;
5715
Shobhit Kumarf1b391a2015-11-05 18:05:32 +05305716 /*
5717 * check if the pre-os intialized the display
5718 * There is SWF18 scratchpad register defined which is set by the
5719 * pre-os which can be used by the OS drivers to check the status
5720 */
5721 if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
5722 goto sanitize;
5723
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305724 /* Is PLL enabled and locked ? */
5725 if (!((lcpll1 & LCPLL_PLL_ENABLE) && (lcpll1 & LCPLL_PLL_LOCK)))
5726 goto sanitize;
5727
5728 /* DPLL okay; verify the cdclock
5729 *
5730 * Noticed in some instances that the freq selection is correct but
5731 * decimal part is programmed wrong from BIOS where pre-os does not
5732 * enable display. Verify the same as well.
5733 */
5734 if (cdctl == ((cdctl & CDCLK_FREQ_SEL_MASK) | skl_cdclk_decimal(freq)))
5735 /* All well; nothing to sanitize */
5736 return false;
5737sanitize:
5738 /*
5739 * As of now initialize with max cdclk till
5740 * we get dynamic cdclk support
5741 * */
5742 dev_priv->skl_boot_cdclk = dev_priv->max_cdclk_freq;
5743 skl_init_cdclk(dev_priv);
5744
5745 /* we did have to sanitize */
5746 return true;
5747}
5748
Jesse Barnes30a970c2013-11-04 13:48:12 -08005749/* Adjust CDclk dividers to allow high res or save power if possible */
5750static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5751{
5752 struct drm_i915_private *dev_priv = dev->dev_private;
5753 u32 val, cmd;
5754
Vandana Kannan164dfd22014-11-24 13:37:41 +05305755 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5756 != dev_priv->cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02005757
Ville Syrjälädfcab172014-06-13 13:37:47 +03005758 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08005759 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03005760 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005761 cmd = 1;
5762 else
5763 cmd = 0;
5764
5765 mutex_lock(&dev_priv->rps.hw_lock);
5766 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5767 val &= ~DSPFREQGUAR_MASK;
5768 val |= (cmd << DSPFREQGUAR_SHIFT);
5769 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5770 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5771 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5772 50)) {
5773 DRM_ERROR("timed out waiting for CDclk change\n");
5774 }
5775 mutex_unlock(&dev_priv->rps.hw_lock);
5776
Ville Syrjälä54433e92015-05-26 20:42:31 +03005777 mutex_lock(&dev_priv->sb_lock);
5778
Ville Syrjälädfcab172014-06-13 13:37:47 +03005779 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005780 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005781
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005782 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005783
Jesse Barnes30a970c2013-11-04 13:48:12 -08005784 /* adjust cdclk divider */
5785 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Vandana Kannan87d5d252015-09-24 23:29:17 +03005786 val &= ~CCK_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005787 val |= divider;
5788 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03005789
5790 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
Vandana Kannan87d5d252015-09-24 23:29:17 +03005791 CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
Ville Syrjäläa877e802014-06-13 13:37:52 +03005792 50))
5793 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08005794 }
5795
Jesse Barnes30a970c2013-11-04 13:48:12 -08005796 /* adjust self-refresh exit latency value */
5797 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5798 val &= ~0x7f;
5799
5800 /*
5801 * For high bandwidth configs, we set a higher latency in the bunit
5802 * so that the core display fetch happens in time to avoid underruns.
5803 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03005804 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005805 val |= 4500 / 250; /* 4.5 usec */
5806 else
5807 val |= 3000 / 250; /* 3.0 usec */
5808 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
Ville Syrjälä54433e92015-05-26 20:42:31 +03005809
Ville Syrjäläa5805162015-05-26 20:42:30 +03005810 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005811
Ville Syrjäläb6283052015-06-03 15:45:07 +03005812 intel_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005813}
5814
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005815static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5816{
5817 struct drm_i915_private *dev_priv = dev->dev_private;
5818 u32 val, cmd;
5819
Vandana Kannan164dfd22014-11-24 13:37:41 +05305820 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5821 != dev_priv->cdclk_freq);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005822
5823 switch (cdclk) {
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005824 case 333333:
5825 case 320000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005826 case 266667:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005827 case 200000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005828 break;
5829 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01005830 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005831 return;
5832 }
5833
Ville Syrjälä9d0d3fd2015-03-02 20:07:17 +02005834 /*
5835 * Specs are full of misinformation, but testing on actual
5836 * hardware has shown that we just need to write the desired
5837 * CCK divider into the Punit register.
5838 */
5839 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5840
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005841 mutex_lock(&dev_priv->rps.hw_lock);
5842 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5843 val &= ~DSPFREQGUAR_MASK_CHV;
5844 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5845 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5846 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5847 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5848 50)) {
5849 DRM_ERROR("timed out waiting for CDclk change\n");
5850 }
5851 mutex_unlock(&dev_priv->rps.hw_lock);
5852
Ville Syrjäläb6283052015-06-03 15:45:07 +03005853 intel_update_cdclk(dev);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005854}
5855
Jesse Barnes30a970c2013-11-04 13:48:12 -08005856static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5857 int max_pixclk)
5858{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005859 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005860 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005861
Jesse Barnes30a970c2013-11-04 13:48:12 -08005862 /*
5863 * Really only a few cases to deal with, as only 4 CDclks are supported:
5864 * 200MHz
5865 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005866 * 320/333MHz (depends on HPLL freq)
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005867 * 400MHz (VLV only)
5868 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5869 * of the lower bin and adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005870 *
5871 * We seem to get an unstable or solid color picture at 200MHz.
5872 * Not sure what's wrong. For now use 200MHz only when all pipes
5873 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08005874 */
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005875 if (!IS_CHERRYVIEW(dev_priv) &&
5876 max_pixclk > freq_320*limit/100)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005877 return 400000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005878 else if (max_pixclk > 266667*limit/100)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005879 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005880 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005881 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005882 else
5883 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005884}
5885
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305886static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
5887 int max_pixclk)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005888{
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305889 /*
5890 * FIXME:
5891 * - remove the guardband, it's not needed on BXT
5892 * - set 19.2MHz bypass frequency if there are no active pipes
5893 */
5894 if (max_pixclk > 576000*9/10)
5895 return 624000;
5896 else if (max_pixclk > 384000*9/10)
5897 return 576000;
5898 else if (max_pixclk > 288000*9/10)
5899 return 384000;
5900 else if (max_pixclk > 144000*9/10)
5901 return 288000;
5902 else
5903 return 144000;
5904}
5905
Maarten Lankhorste8788cb2016-02-16 10:25:11 +01005906/* Compute the max pixel clock for new configuration. */
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005907static int intel_mode_max_pixclk(struct drm_device *dev,
5908 struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005909{
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005910 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
5911 struct drm_i915_private *dev_priv = dev->dev_private;
5912 struct drm_crtc *crtc;
5913 struct drm_crtc_state *crtc_state;
5914 unsigned max_pixclk = 0, i;
5915 enum pipe pipe;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005916
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005917 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
5918 sizeof(intel_state->min_pixclk));
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005919
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005920 for_each_crtc_in_state(state, crtc, crtc_state, i) {
5921 int pixclk = 0;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005922
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005923 if (crtc_state->enable)
5924 pixclk = crtc_state->adjusted_mode.crtc_clock;
5925
5926 intel_state->min_pixclk[i] = pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005927 }
5928
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005929 for_each_pipe(dev_priv, pipe)
5930 max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
5931
Jesse Barnes30a970c2013-11-04 13:48:12 -08005932 return max_pixclk;
5933}
5934
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005935static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005936{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005937 struct drm_device *dev = state->dev;
5938 struct drm_i915_private *dev_priv = dev->dev_private;
5939 int max_pixclk = intel_mode_max_pixclk(dev, state);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005940 struct intel_atomic_state *intel_state =
5941 to_intel_atomic_state(state);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005942
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005943 if (max_pixclk < 0)
5944 return max_pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005945
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005946 intel_state->cdclk = intel_state->dev_cdclk =
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005947 valleyview_calc_cdclk(dev_priv, max_pixclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305948
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005949 if (!intel_state->active_crtcs)
5950 intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
5951
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005952 return 0;
5953}
Jesse Barnes30a970c2013-11-04 13:48:12 -08005954
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005955static int broxton_modeset_calc_cdclk(struct drm_atomic_state *state)
5956{
5957 struct drm_device *dev = state->dev;
5958 struct drm_i915_private *dev_priv = dev->dev_private;
5959 int max_pixclk = intel_mode_max_pixclk(dev, state);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005960 struct intel_atomic_state *intel_state =
5961 to_intel_atomic_state(state);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005962
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005963 if (max_pixclk < 0)
5964 return max_pixclk;
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005965
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005966 intel_state->cdclk = intel_state->dev_cdclk =
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005967 broxton_calc_cdclk(dev_priv, max_pixclk);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005968
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005969 if (!intel_state->active_crtcs)
5970 intel_state->dev_cdclk = broxton_calc_cdclk(dev_priv, 0);
5971
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005972 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005973}
5974
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005975static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
5976{
5977 unsigned int credits, default_credits;
5978
5979 if (IS_CHERRYVIEW(dev_priv))
5980 default_credits = PFI_CREDIT(12);
5981 else
5982 default_credits = PFI_CREDIT(8);
5983
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03005984 if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005985 /* CHV suggested value is 31 or 63 */
5986 if (IS_CHERRYVIEW(dev_priv))
Ville Syrjäläfcc00082015-05-26 20:22:40 +03005987 credits = PFI_CREDIT_63;
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005988 else
5989 credits = PFI_CREDIT(15);
5990 } else {
5991 credits = default_credits;
5992 }
5993
5994 /*
5995 * WA - write default credits before re-programming
5996 * FIXME: should we also set the resend bit here?
5997 */
5998 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5999 default_credits);
6000
6001 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6002 credits | PFI_CREDIT_RESEND);
6003
6004 /*
6005 * FIXME is this guaranteed to clear
6006 * immediately or should we poll for it?
6007 */
6008 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
6009}
6010
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006011static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006012{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006013 struct drm_device *dev = old_state->dev;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006014 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006015 struct intel_atomic_state *old_intel_state =
6016 to_intel_atomic_state(old_state);
6017 unsigned req_cdclk = old_intel_state->dev_cdclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006018
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006019 /*
6020 * FIXME: We can end up here with all power domains off, yet
6021 * with a CDCLK frequency other than the minimum. To account
6022 * for this take the PIPE-A power domain, which covers the HW
6023 * blocks needed for the following programming. This can be
6024 * removed once it's guaranteed that we get here either with
6025 * the minimum CDCLK set, or the required power domains
6026 * enabled.
6027 */
6028 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006029
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006030 if (IS_CHERRYVIEW(dev))
6031 cherryview_set_cdclk(dev, req_cdclk);
6032 else
6033 valleyview_set_cdclk(dev, req_cdclk);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006034
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006035 vlv_program_pfi_credits(dev_priv);
Imre Deak738c05c2014-11-19 16:25:37 +02006036
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006037 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006038}
6039
Jesse Barnes89b667f2013-04-18 14:51:36 -07006040static void valleyview_crtc_enable(struct drm_crtc *crtc)
6041{
6042 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006043 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006044 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6045 struct intel_encoder *encoder;
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006046 struct intel_crtc_state *pipe_config =
6047 to_intel_crtc_state(crtc->state);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006048 int pipe = intel_crtc->pipe;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006049
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006050 if (WARN_ON(intel_crtc->active))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006051 return;
6052
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006053 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306054 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006055
6056 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02006057 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006058
Ville Syrjäläc14b0482014-10-16 20:52:34 +03006059 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
6060 struct drm_i915_private *dev_priv = dev->dev_private;
6061
6062 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6063 I915_WRITE(CHV_CANVAS(pipe), 0);
6064 }
6065
Daniel Vetter5b18e572014-04-24 23:55:06 +02006066 i9xx_set_pipeconf(intel_crtc);
6067
Jesse Barnes89b667f2013-04-18 14:51:36 -07006068 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006069
Daniel Vettera72e4c92014-09-30 10:56:47 +02006070 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006071
Jesse Barnes89b667f2013-04-18 14:51:36 -07006072 for_each_encoder_on_crtc(dev, crtc, encoder)
6073 if (encoder->pre_pll_enable)
6074 encoder->pre_pll_enable(encoder);
6075
Jani Nikulaa65347b2015-11-27 12:21:46 +02006076 if (!intel_crtc->config->has_dsi_encoder) {
Ville Syrjäläc0b4c662015-07-08 23:45:52 +03006077 if (IS_CHERRYVIEW(dev)) {
6078 chv_prepare_pll(intel_crtc, intel_crtc->config);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006079 chv_enable_pll(intel_crtc, intel_crtc->config);
Ville Syrjäläc0b4c662015-07-08 23:45:52 +03006080 } else {
6081 vlv_prepare_pll(intel_crtc, intel_crtc->config);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006082 vlv_enable_pll(intel_crtc, intel_crtc->config);
Ville Syrjäläc0b4c662015-07-08 23:45:52 +03006083 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006084 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07006085
6086 for_each_encoder_on_crtc(dev, crtc, encoder)
6087 if (encoder->pre_enable)
6088 encoder->pre_enable(encoder);
6089
Jesse Barnes2dd24552013-04-25 12:55:01 -07006090 i9xx_pfit_enable(intel_crtc);
6091
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006092 intel_color_load_luts(&pipe_config->base);
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006093
Ville Syrjäläcaed3612016-03-09 19:07:25 +02006094 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006095 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006096
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006097 assert_vblank_disabled(crtc);
6098 drm_crtc_vblank_on(crtc);
6099
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006100 for_each_encoder_on_crtc(dev, crtc, encoder)
6101 encoder->enable(encoder);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006102}
6103
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006104static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6105{
6106 struct drm_device *dev = crtc->base.dev;
6107 struct drm_i915_private *dev_priv = dev->dev_private;
6108
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006109 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6110 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006111}
6112
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006113static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006114{
6115 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006116 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006117 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006118 struct intel_encoder *encoder;
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006119 struct intel_crtc_state *pipe_config =
6120 to_intel_crtc_state(crtc->state);
Jesse Barnes79e53942008-11-07 14:24:08 -08006121 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08006122
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006123 if (WARN_ON(intel_crtc->active))
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006124 return;
6125
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006126 i9xx_set_pll_dividers(intel_crtc);
6127
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006128 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306129 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006130
6131 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02006132 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006133
Daniel Vetter5b18e572014-04-24 23:55:06 +02006134 i9xx_set_pipeconf(intel_crtc);
6135
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006136 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01006137
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006138 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006139 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006140
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02006141 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02006142 if (encoder->pre_enable)
6143 encoder->pre_enable(encoder);
6144
Daniel Vetterf6736a12013-06-05 13:34:30 +02006145 i9xx_enable_pll(intel_crtc);
6146
Jesse Barnes2dd24552013-04-25 12:55:01 -07006147 i9xx_pfit_enable(intel_crtc);
6148
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006149 intel_color_load_luts(&pipe_config->base);
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006150
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006151 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006152 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006153
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006154 assert_vblank_disabled(crtc);
6155 drm_crtc_vblank_on(crtc);
6156
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006157 for_each_encoder_on_crtc(dev, crtc, encoder)
6158 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006159}
6160
Daniel Vetter87476d62013-04-11 16:29:06 +02006161static void i9xx_pfit_disable(struct intel_crtc *crtc)
6162{
6163 struct drm_device *dev = crtc->base.dev;
6164 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02006165
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006166 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02006167 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02006168
6169 assert_pipe_disabled(dev_priv, crtc->pipe);
6170
Daniel Vetter328d8e82013-05-08 10:36:31 +02006171 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6172 I915_READ(PFIT_CONTROL));
6173 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02006174}
6175
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006176static void i9xx_crtc_disable(struct drm_crtc *crtc)
6177{
6178 struct drm_device *dev = crtc->dev;
6179 struct drm_i915_private *dev_priv = dev->dev_private;
6180 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006181 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006182 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006183
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006184 /*
6185 * On gen2 planes are double buffered but the pipe isn't, so we must
6186 * wait for planes to fully turn off before disabling the pipe.
6187 */
Ander Conselvan de Oliveira90e83e52016-03-22 10:11:24 +02006188 if (IS_GEN2(dev))
6189 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006190
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006191 for_each_encoder_on_crtc(dev, crtc, encoder)
6192 encoder->disable(encoder);
6193
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006194 drm_crtc_vblank_off(crtc);
6195 assert_vblank_disabled(crtc);
6196
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03006197 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006198
Daniel Vetter87476d62013-04-11 16:29:06 +02006199 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006200
Jesse Barnes89b667f2013-04-18 14:51:36 -07006201 for_each_encoder_on_crtc(dev, crtc, encoder)
6202 if (encoder->post_disable)
6203 encoder->post_disable(encoder);
6204
Jani Nikulaa65347b2015-11-27 12:21:46 +02006205 if (!intel_crtc->config->has_dsi_encoder) {
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006206 if (IS_CHERRYVIEW(dev))
6207 chv_disable_pll(dev_priv, pipe);
6208 else if (IS_VALLEYVIEW(dev))
6209 vlv_disable_pll(dev_priv, pipe);
6210 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03006211 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006212 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006213
Ville Syrjäläd6db9952015-07-08 23:45:49 +03006214 for_each_encoder_on_crtc(dev, crtc, encoder)
6215 if (encoder->post_pll_disable)
6216 encoder->post_pll_disable(encoder);
6217
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006218 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006219 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006220}
6221
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006222static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006223{
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006224 struct intel_encoder *encoder;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006225 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006226 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006227 enum intel_display_power_domain domain;
6228 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006229
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006230 if (!intel_crtc->active)
6231 return;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006232
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006233 if (to_intel_plane_state(crtc->primary->state)->visible) {
Maarten Lankhorstfc32b1f2015-10-19 17:09:23 +02006234 WARN_ON(intel_crtc->unpin_work);
6235
Ville Syrjälä2622a082016-03-09 19:07:26 +02006236 intel_pre_disable_primary_noatomic(crtc);
Maarten Lankhorst54a419612015-11-23 10:25:28 +01006237
6238 intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
6239 to_intel_plane_state(crtc->primary->state)->visible = false;
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006240 }
6241
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006242 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006243
6244 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was enabled, now disabled\n",
6245 crtc->base.id);
6246
6247 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
6248 crtc->state->active = false;
Matt Roper37d90782015-09-24 15:53:06 -07006249 intel_crtc->active = false;
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006250 crtc->enabled = false;
6251 crtc->state->connector_mask = 0;
6252 crtc->state->encoder_mask = 0;
6253
6254 for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
6255 encoder->base.crtc = NULL;
6256
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -02006257 intel_fbc_disable(intel_crtc);
Matt Roper37d90782015-09-24 15:53:06 -07006258 intel_update_watermarks(crtc);
Maarten Lankhorst1f7457b2015-07-13 11:55:05 +02006259 intel_disable_shared_dpll(intel_crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006260
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006261 domains = intel_crtc->enabled_power_domains;
6262 for_each_power_domain(domain, domains)
6263 intel_display_power_put(dev_priv, domain);
6264 intel_crtc->enabled_power_domains = 0;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006265
6266 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
6267 dev_priv->min_pixclk[intel_crtc->pipe] = 0;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006268}
6269
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006270/*
6271 * turn all crtc's off, but do not adjust state
6272 * This has to be paired with a call to intel_modeset_setup_hw_state.
6273 */
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006274int intel_display_suspend(struct drm_device *dev)
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006275{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006276 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006277 struct drm_atomic_state *state;
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006278 int ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006279
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006280 state = drm_atomic_helper_suspend(dev);
6281 ret = PTR_ERR_OR_ZERO(state);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006282 if (ret)
6283 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006284 else
6285 dev_priv->modeset_restore_state = state;
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006286 return ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006287}
6288
Chris Wilsonea5b2132010-08-04 13:50:23 +01006289void intel_encoder_destroy(struct drm_encoder *encoder)
6290{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006291 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01006292
Chris Wilsonea5b2132010-08-04 13:50:23 +01006293 drm_encoder_cleanup(encoder);
6294 kfree(intel_encoder);
6295}
6296
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006297/* Cross check the actual hw state with our own modeset state tracking (and it's
6298 * internal consistency). */
Maarten Lankhorstc0ead702016-03-30 10:00:05 +02006299static void intel_connector_verify_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006300{
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006301 struct drm_crtc *crtc = connector->base.state->crtc;
6302
6303 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6304 connector->base.base.id,
6305 connector->base.name);
6306
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006307 if (connector->get_hw_state(connector)) {
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006308 struct intel_encoder *encoder = connector->encoder;
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006309 struct drm_connector_state *conn_state = connector->base.state;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006310
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006311 I915_STATE_WARN(!crtc,
6312 "connector enabled without attached crtc\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006313
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006314 if (!crtc)
6315 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006316
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006317 I915_STATE_WARN(!crtc->state->active,
6318 "connector is active, but attached crtc isn't\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006319
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006320 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006321 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006322
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006323 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006324 "atomic encoder doesn't match attached encoder\n");
Dave Airlie36cd7442014-05-02 13:44:18 +10006325
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006326 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006327 "attached encoder crtc differs from connector crtc\n");
6328 } else {
Maarten Lankhorst4d688a22015-08-05 12:37:06 +02006329 I915_STATE_WARN(crtc && crtc->state->active,
6330 "attached crtc is active, but connector isn't\n");
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006331 I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
6332 "best encoder set without crtc!\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006333 }
6334}
6335
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006336int intel_connector_init(struct intel_connector *connector)
6337{
Maarten Lankhorst5350a032016-01-04 12:53:15 +01006338 drm_atomic_helper_connector_reset(&connector->base);
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006339
Maarten Lankhorst5350a032016-01-04 12:53:15 +01006340 if (!connector->base.state)
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006341 return -ENOMEM;
6342
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006343 return 0;
6344}
6345
6346struct intel_connector *intel_connector_alloc(void)
6347{
6348 struct intel_connector *connector;
6349
6350 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6351 if (!connector)
6352 return NULL;
6353
6354 if (intel_connector_init(connector) < 0) {
6355 kfree(connector);
6356 return NULL;
6357 }
6358
6359 return connector;
6360}
6361
Daniel Vetterf0947c32012-07-02 13:10:34 +02006362/* Simple connector->get_hw_state implementation for encoders that support only
6363 * one connector and no cloning and hence the encoder state determines the state
6364 * of the connector. */
6365bool intel_connector_get_hw_state(struct intel_connector *connector)
6366{
Daniel Vetter24929352012-07-02 20:28:59 +02006367 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02006368 struct intel_encoder *encoder = connector->encoder;
6369
6370 return encoder->get_hw_state(encoder, &pipe);
6371}
6372
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006373static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006374{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006375 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6376 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006377
6378 return 0;
6379}
6380
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006381static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006382 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006383{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006384 struct drm_atomic_state *state = pipe_config->base.state;
6385 struct intel_crtc *other_crtc;
6386 struct intel_crtc_state *other_crtc_state;
6387
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006388 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6389 pipe_name(pipe), pipe_config->fdi_lanes);
6390 if (pipe_config->fdi_lanes > 4) {
6391 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6392 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006393 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006394 }
6395
Paulo Zanonibafb6552013-11-02 21:07:44 -07006396 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006397 if (pipe_config->fdi_lanes > 2) {
6398 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6399 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006400 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006401 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006402 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006403 }
6404 }
6405
6406 if (INTEL_INFO(dev)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006407 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006408
6409 /* Ivybridge 3 pipe is really complicated */
6410 switch (pipe) {
6411 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006412 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006413 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006414 if (pipe_config->fdi_lanes <= 2)
6415 return 0;
6416
6417 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6418 other_crtc_state =
6419 intel_atomic_get_crtc_state(state, other_crtc);
6420 if (IS_ERR(other_crtc_state))
6421 return PTR_ERR(other_crtc_state);
6422
6423 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006424 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6425 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006426 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006427 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006428 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006429 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02006430 if (pipe_config->fdi_lanes > 2) {
6431 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6432 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006433 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02006434 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006435
6436 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6437 other_crtc_state =
6438 intel_atomic_get_crtc_state(state, other_crtc);
6439 if (IS_ERR(other_crtc_state))
6440 return PTR_ERR(other_crtc_state);
6441
6442 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006443 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006444 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006445 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006446 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006447 default:
6448 BUG();
6449 }
6450}
6451
Daniel Vettere29c22c2013-02-21 00:00:16 +01006452#define RETRY 1
6453static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006454 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02006455{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006456 struct drm_device *dev = intel_crtc->base.dev;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006457 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006458 int lane, link_bw, fdi_dotclock, ret;
6459 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006460
Daniel Vettere29c22c2013-02-21 00:00:16 +01006461retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02006462 /* FDI is a binary signal running at ~2.7GHz, encoding
6463 * each output octet as 10 bits. The actual frequency
6464 * is stored as a divider into a 100MHz clock, and the
6465 * mode pixel clock is stored in units of 1KHz.
6466 * Hence the bw of each lane in terms of the mode signal
6467 * is:
6468 */
Ville Syrjälä21a727b2016-02-17 21:41:10 +02006469 link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006470
Damien Lespiau241bfc32013-09-25 16:45:37 +01006471 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006472
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006473 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006474 pipe_config->pipe_bpp);
6475
6476 pipe_config->fdi_lanes = lane;
6477
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006478 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006479 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006480
Ville Syrjäläe3b247d2016-02-17 21:41:09 +02006481 ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006482 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01006483 pipe_config->pipe_bpp -= 2*3;
6484 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6485 pipe_config->pipe_bpp);
6486 needs_recompute = true;
6487 pipe_config->bw_constrained = true;
6488
6489 goto retry;
6490 }
6491
6492 if (needs_recompute)
6493 return RETRY;
6494
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006495 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006496}
6497
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006498static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6499 struct intel_crtc_state *pipe_config)
6500{
6501 if (pipe_config->pipe_bpp > 24)
6502 return false;
6503
6504 /* HSW can handle pixel rate up to cdclk? */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03006505 if (IS_HASWELL(dev_priv))
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006506 return true;
6507
6508 /*
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03006509 * We compare against max which means we must take
6510 * the increased cdclk requirement into account when
6511 * calculating the new cdclk.
6512 *
6513 * Should measure whether using a lower cdclk w/o IPS
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006514 */
6515 return ilk_pipe_pixel_rate(pipe_config) <=
6516 dev_priv->max_cdclk_freq * 95 / 100;
6517}
6518
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006519static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006520 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006521{
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006522 struct drm_device *dev = crtc->base.dev;
6523 struct drm_i915_private *dev_priv = dev->dev_private;
6524
Jani Nikulad330a952014-01-21 11:24:25 +02006525 pipe_config->ips_enabled = i915.enable_ips &&
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006526 hsw_crtc_supports_ips(crtc) &&
6527 pipe_config_supports_ips(dev_priv, pipe_config);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006528}
6529
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006530static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
6531{
6532 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6533
6534 /* GDG double wide on either pipe, otherwise pipe A only */
6535 return INTEL_INFO(dev_priv)->gen < 4 &&
6536 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
6537}
6538
Daniel Vettera43f6e02013-06-07 23:10:32 +02006539static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006540 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006541{
Daniel Vettera43f6e02013-06-07 23:10:32 +02006542 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02006543 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006544 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Chris Wilson89749352010-09-12 18:25:19 +01006545
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006546 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006547 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006548 int clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006549
6550 /*
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006551 * Enable double wide mode when the dot clock
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006552 * is > 90% of the (display) core speed.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006553 */
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006554 if (intel_crtc_supports_double_wide(crtc) &&
6555 adjusted_mode->crtc_clock > clock_limit) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006556 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006557 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006558 }
6559
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006560 if (adjusted_mode->crtc_clock > clock_limit) {
6561 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
6562 adjusted_mode->crtc_clock, clock_limit,
6563 yesno(pipe_config->double_wide));
Daniel Vettere29c22c2013-02-21 00:00:16 +01006564 return -EINVAL;
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006565 }
Zhenyu Wang2c072452009-06-05 15:38:42 +08006566 }
Chris Wilson89749352010-09-12 18:25:19 +01006567
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006568 /*
6569 * Pipe horizontal size must be even in:
6570 * - DVO ganged mode
6571 * - LVDS dual channel mode
6572 * - Double wide pipe
6573 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006574 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006575 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6576 pipe_config->pipe_src_w &= ~1;
6577
Damien Lespiau8693a822013-05-03 18:48:11 +01006578 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6579 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03006580 */
6581 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
Ville Syrjäläaad941d2015-09-25 16:38:56 +03006582 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006583 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03006584
Damien Lespiauf5adf942013-06-24 18:29:34 +01006585 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02006586 hsw_compute_ips_config(crtc, pipe_config);
6587
Daniel Vetter877d48d2013-04-19 11:24:43 +02006588 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02006589 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006590
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +02006591 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006592}
6593
Ville Syrjälä1652d192015-03-31 14:12:01 +03006594static int skylake_get_display_clock_speed(struct drm_device *dev)
6595{
6596 struct drm_i915_private *dev_priv = to_i915(dev);
6597 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6598 uint32_t cdctl = I915_READ(CDCLK_CTL);
6599 uint32_t linkrate;
6600
Damien Lespiau414355a2015-06-04 18:21:31 +01006601 if (!(lcpll1 & LCPLL_PLL_ENABLE))
Ville Syrjälä1652d192015-03-31 14:12:01 +03006602 return 24000; /* 24MHz is the cd freq with NSSC ref */
Ville Syrjälä1652d192015-03-31 14:12:01 +03006603
6604 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6605 return 540000;
6606
6607 linkrate = (I915_READ(DPLL_CTRL1) &
Damien Lespiau71cd8422015-04-30 16:39:17 +01006608 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006609
Damien Lespiau71cd8422015-04-30 16:39:17 +01006610 if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
6611 linkrate == DPLL_CTRL1_LINK_RATE_1080) {
Ville Syrjälä1652d192015-03-31 14:12:01 +03006612 /* vco 8640 */
6613 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6614 case CDCLK_FREQ_450_432:
6615 return 432000;
6616 case CDCLK_FREQ_337_308:
6617 return 308570;
6618 case CDCLK_FREQ_675_617:
6619 return 617140;
6620 default:
6621 WARN(1, "Unknown cd freq selection\n");
6622 }
6623 } else {
6624 /* vco 8100 */
6625 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6626 case CDCLK_FREQ_450_432:
6627 return 450000;
6628 case CDCLK_FREQ_337_308:
6629 return 337500;
6630 case CDCLK_FREQ_675_617:
6631 return 675000;
6632 default:
6633 WARN(1, "Unknown cd freq selection\n");
6634 }
6635 }
6636
6637 /* error case, do as if DPLL0 isn't enabled */
6638 return 24000;
6639}
6640
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006641static int broxton_get_display_clock_speed(struct drm_device *dev)
6642{
6643 struct drm_i915_private *dev_priv = to_i915(dev);
6644 uint32_t cdctl = I915_READ(CDCLK_CTL);
6645 uint32_t pll_ratio = I915_READ(BXT_DE_PLL_CTL) & BXT_DE_PLL_RATIO_MASK;
6646 uint32_t pll_enab = I915_READ(BXT_DE_PLL_ENABLE);
6647 int cdclk;
6648
6649 if (!(pll_enab & BXT_DE_PLL_PLL_ENABLE))
6650 return 19200;
6651
6652 cdclk = 19200 * pll_ratio / 2;
6653
6654 switch (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) {
6655 case BXT_CDCLK_CD2X_DIV_SEL_1:
6656 return cdclk; /* 576MHz or 624MHz */
6657 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
6658 return cdclk * 2 / 3; /* 384MHz */
6659 case BXT_CDCLK_CD2X_DIV_SEL_2:
6660 return cdclk / 2; /* 288MHz */
6661 case BXT_CDCLK_CD2X_DIV_SEL_4:
6662 return cdclk / 4; /* 144MHz */
6663 }
6664
6665 /* error case, do as if DE PLL isn't enabled */
6666 return 19200;
6667}
6668
Ville Syrjälä1652d192015-03-31 14:12:01 +03006669static int broadwell_get_display_clock_speed(struct drm_device *dev)
6670{
6671 struct drm_i915_private *dev_priv = dev->dev_private;
6672 uint32_t lcpll = I915_READ(LCPLL_CTL);
6673 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6674
6675 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6676 return 800000;
6677 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6678 return 450000;
6679 else if (freq == LCPLL_CLK_FREQ_450)
6680 return 450000;
6681 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6682 return 540000;
6683 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6684 return 337500;
6685 else
6686 return 675000;
6687}
6688
6689static int haswell_get_display_clock_speed(struct drm_device *dev)
6690{
6691 struct drm_i915_private *dev_priv = dev->dev_private;
6692 uint32_t lcpll = I915_READ(LCPLL_CTL);
6693 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6694
6695 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6696 return 800000;
6697 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6698 return 450000;
6699 else if (freq == LCPLL_CLK_FREQ_450)
6700 return 450000;
6701 else if (IS_HSW_ULT(dev))
6702 return 337500;
6703 else
6704 return 540000;
Jesse Barnes79e53942008-11-07 14:24:08 -08006705}
6706
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006707static int valleyview_get_display_clock_speed(struct drm_device *dev)
6708{
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03006709 return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
6710 CCK_DISPLAY_CLOCK_CONTROL);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006711}
6712
Ville Syrjäläb37a6432015-03-31 14:11:54 +03006713static int ilk_get_display_clock_speed(struct drm_device *dev)
6714{
6715 return 450000;
6716}
6717
Jesse Barnese70236a2009-09-21 10:42:27 -07006718static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006719{
Jesse Barnese70236a2009-09-21 10:42:27 -07006720 return 400000;
6721}
Jesse Barnes79e53942008-11-07 14:24:08 -08006722
Jesse Barnese70236a2009-09-21 10:42:27 -07006723static int i915_get_display_clock_speed(struct drm_device *dev)
6724{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006725 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006726}
Jesse Barnes79e53942008-11-07 14:24:08 -08006727
Jesse Barnese70236a2009-09-21 10:42:27 -07006728static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6729{
6730 return 200000;
6731}
Jesse Barnes79e53942008-11-07 14:24:08 -08006732
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006733static int pnv_get_display_clock_speed(struct drm_device *dev)
6734{
6735 u16 gcfgc = 0;
6736
6737 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6738
6739 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6740 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006741 return 266667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006742 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006743 return 333333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006744 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006745 return 444444;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006746 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6747 return 200000;
6748 default:
6749 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6750 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006751 return 133333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006752 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006753 return 166667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006754 }
6755}
6756
Jesse Barnese70236a2009-09-21 10:42:27 -07006757static int i915gm_get_display_clock_speed(struct drm_device *dev)
6758{
6759 u16 gcfgc = 0;
6760
6761 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6762
6763 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Ville Syrjäläe907f172015-03-31 14:09:47 +03006764 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006765 else {
6766 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6767 case GC_DISPLAY_CLOCK_333_MHZ:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006768 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006769 default:
6770 case GC_DISPLAY_CLOCK_190_200_MHZ:
6771 return 190000;
6772 }
6773 }
6774}
Jesse Barnes79e53942008-11-07 14:24:08 -08006775
Jesse Barnese70236a2009-09-21 10:42:27 -07006776static int i865_get_display_clock_speed(struct drm_device *dev)
6777{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006778 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006779}
6780
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006781static int i85x_get_display_clock_speed(struct drm_device *dev)
Jesse Barnese70236a2009-09-21 10:42:27 -07006782{
6783 u16 hpllcc = 0;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006784
Ville Syrjälä65cd2b32015-05-22 11:22:32 +03006785 /*
6786 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6787 * encoding is different :(
6788 * FIXME is this the right way to detect 852GM/852GMV?
6789 */
6790 if (dev->pdev->revision == 0x1)
6791 return 133333;
6792
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006793 pci_bus_read_config_word(dev->pdev->bus,
6794 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6795
Jesse Barnese70236a2009-09-21 10:42:27 -07006796 /* Assume that the hardware is in the high speed state. This
6797 * should be the default.
6798 */
6799 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6800 case GC_CLOCK_133_200:
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006801 case GC_CLOCK_133_200_2:
Jesse Barnese70236a2009-09-21 10:42:27 -07006802 case GC_CLOCK_100_200:
6803 return 200000;
6804 case GC_CLOCK_166_250:
6805 return 250000;
6806 case GC_CLOCK_100_133:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006807 return 133333;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006808 case GC_CLOCK_133_266:
6809 case GC_CLOCK_133_266_2:
6810 case GC_CLOCK_166_266:
6811 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006812 }
6813
6814 /* Shouldn't happen */
6815 return 0;
6816}
6817
6818static int i830_get_display_clock_speed(struct drm_device *dev)
6819{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006820 return 133333;
Jesse Barnes79e53942008-11-07 14:24:08 -08006821}
6822
Ville Syrjälä34edce22015-05-22 11:22:33 +03006823static unsigned int intel_hpll_vco(struct drm_device *dev)
6824{
6825 struct drm_i915_private *dev_priv = dev->dev_private;
6826 static const unsigned int blb_vco[8] = {
6827 [0] = 3200000,
6828 [1] = 4000000,
6829 [2] = 5333333,
6830 [3] = 4800000,
6831 [4] = 6400000,
6832 };
6833 static const unsigned int pnv_vco[8] = {
6834 [0] = 3200000,
6835 [1] = 4000000,
6836 [2] = 5333333,
6837 [3] = 4800000,
6838 [4] = 2666667,
6839 };
6840 static const unsigned int cl_vco[8] = {
6841 [0] = 3200000,
6842 [1] = 4000000,
6843 [2] = 5333333,
6844 [3] = 6400000,
6845 [4] = 3333333,
6846 [5] = 3566667,
6847 [6] = 4266667,
6848 };
6849 static const unsigned int elk_vco[8] = {
6850 [0] = 3200000,
6851 [1] = 4000000,
6852 [2] = 5333333,
6853 [3] = 4800000,
6854 };
6855 static const unsigned int ctg_vco[8] = {
6856 [0] = 3200000,
6857 [1] = 4000000,
6858 [2] = 5333333,
6859 [3] = 6400000,
6860 [4] = 2666667,
6861 [5] = 4266667,
6862 };
6863 const unsigned int *vco_table;
6864 unsigned int vco;
6865 uint8_t tmp = 0;
6866
6867 /* FIXME other chipsets? */
6868 if (IS_GM45(dev))
6869 vco_table = ctg_vco;
6870 else if (IS_G4X(dev))
6871 vco_table = elk_vco;
6872 else if (IS_CRESTLINE(dev))
6873 vco_table = cl_vco;
6874 else if (IS_PINEVIEW(dev))
6875 vco_table = pnv_vco;
6876 else if (IS_G33(dev))
6877 vco_table = blb_vco;
6878 else
6879 return 0;
6880
6881 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
6882
6883 vco = vco_table[tmp & 0x7];
6884 if (vco == 0)
6885 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
6886 else
6887 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
6888
6889 return vco;
6890}
6891
6892static int gm45_get_display_clock_speed(struct drm_device *dev)
6893{
6894 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6895 uint16_t tmp = 0;
6896
6897 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6898
6899 cdclk_sel = (tmp >> 12) & 0x1;
6900
6901 switch (vco) {
6902 case 2666667:
6903 case 4000000:
6904 case 5333333:
6905 return cdclk_sel ? 333333 : 222222;
6906 case 3200000:
6907 return cdclk_sel ? 320000 : 228571;
6908 default:
6909 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
6910 return 222222;
6911 }
6912}
6913
6914static int i965gm_get_display_clock_speed(struct drm_device *dev)
6915{
6916 static const uint8_t div_3200[] = { 16, 10, 8 };
6917 static const uint8_t div_4000[] = { 20, 12, 10 };
6918 static const uint8_t div_5333[] = { 24, 16, 14 };
6919 const uint8_t *div_table;
6920 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6921 uint16_t tmp = 0;
6922
6923 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6924
6925 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
6926
6927 if (cdclk_sel >= ARRAY_SIZE(div_3200))
6928 goto fail;
6929
6930 switch (vco) {
6931 case 3200000:
6932 div_table = div_3200;
6933 break;
6934 case 4000000:
6935 div_table = div_4000;
6936 break;
6937 case 5333333:
6938 div_table = div_5333;
6939 break;
6940 default:
6941 goto fail;
6942 }
6943
6944 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
6945
Damien Lespiaucaf4e252015-06-04 16:56:18 +01006946fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03006947 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
6948 return 200000;
6949}
6950
6951static int g33_get_display_clock_speed(struct drm_device *dev)
6952{
6953 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
6954 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
6955 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
6956 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
6957 const uint8_t *div_table;
6958 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6959 uint16_t tmp = 0;
6960
6961 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6962
6963 cdclk_sel = (tmp >> 4) & 0x7;
6964
6965 if (cdclk_sel >= ARRAY_SIZE(div_3200))
6966 goto fail;
6967
6968 switch (vco) {
6969 case 3200000:
6970 div_table = div_3200;
6971 break;
6972 case 4000000:
6973 div_table = div_4000;
6974 break;
6975 case 4800000:
6976 div_table = div_4800;
6977 break;
6978 case 5333333:
6979 div_table = div_5333;
6980 break;
6981 default:
6982 goto fail;
6983 }
6984
6985 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
6986
Damien Lespiaucaf4e252015-06-04 16:56:18 +01006987fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03006988 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
6989 return 190476;
6990}
6991
Zhenyu Wang2c072452009-06-05 15:38:42 +08006992static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006993intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006994{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006995 while (*num > DATA_LINK_M_N_MASK ||
6996 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08006997 *num >>= 1;
6998 *den >>= 1;
6999 }
7000}
7001
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007002static void compute_m_n(unsigned int m, unsigned int n,
7003 uint32_t *ret_m, uint32_t *ret_n)
7004{
7005 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7006 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7007 intel_reduce_m_n_ratio(ret_m, ret_n);
7008}
7009
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007010void
7011intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7012 int pixel_clock, int link_clock,
7013 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007014{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007015 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007016
7017 compute_m_n(bits_per_pixel * pixel_clock,
7018 link_clock * nlanes * 8,
7019 &m_n->gmch_m, &m_n->gmch_n);
7020
7021 compute_m_n(pixel_clock, link_clock,
7022 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08007023}
7024
Chris Wilsona7615032011-01-12 17:04:08 +00007025static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7026{
Jani Nikulad330a952014-01-21 11:24:25 +02007027 if (i915.panel_use_ssc >= 0)
7028 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007029 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07007030 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00007031}
7032
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007033static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007034{
Daniel Vetter7df00d72013-05-21 21:54:55 +02007035 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007036}
Daniel Vetterf47709a2013-03-28 10:42:02 +01007037
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007038static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7039{
7040 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007041}
7042
Daniel Vetterf47709a2013-03-28 10:42:02 +01007043static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007044 struct intel_crtc_state *crtc_state,
Jesse Barnesa7516a02011-12-15 12:30:37 -08007045 intel_clock_t *reduced_clock)
7046{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007047 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007048 u32 fp, fp2 = 0;
7049
7050 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007051 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007052 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007053 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007054 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007055 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007056 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007057 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007058 }
7059
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007060 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007061
Daniel Vetterf47709a2013-03-28 10:42:02 +01007062 crtc->lowfreq_avail = false;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007063 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07007064 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007065 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007066 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007067 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007068 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007069 }
7070}
7071
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007072static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7073 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07007074{
7075 u32 reg_val;
7076
7077 /*
7078 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7079 * and set it to a reasonable value instead.
7080 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007081 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007082 reg_val &= 0xffffff00;
7083 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007084 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007085
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007086 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007087 reg_val &= 0x8cffffff;
7088 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007089 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007090
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007091 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007092 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007093 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007094
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007095 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007096 reg_val &= 0x00ffffff;
7097 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007098 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007099}
7100
Daniel Vetterb5518422013-05-03 11:49:48 +02007101static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7102 struct intel_link_m_n *m_n)
7103{
7104 struct drm_device *dev = crtc->base.dev;
7105 struct drm_i915_private *dev_priv = dev->dev_private;
7106 int pipe = crtc->pipe;
7107
Daniel Vettere3b95f12013-05-03 11:49:49 +02007108 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7109 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7110 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7111 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007112}
7113
7114static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07007115 struct intel_link_m_n *m_n,
7116 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02007117{
7118 struct drm_device *dev = crtc->base.dev;
7119 struct drm_i915_private *dev_priv = dev->dev_private;
7120 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007121 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02007122
7123 if (INTEL_INFO(dev)->gen >= 5) {
7124 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7125 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7126 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7127 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07007128 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7129 * for gen < 8) and if DRRS is supported (to make sure the
7130 * registers are not unnecessarily accessed).
7131 */
Durgadoss R44395bf2015-02-13 15:33:02 +05307132 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007133 crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07007134 I915_WRITE(PIPE_DATA_M2(transcoder),
7135 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7136 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7137 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7138 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7139 }
Daniel Vetterb5518422013-05-03 11:49:48 +02007140 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02007141 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7142 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7143 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7144 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007145 }
7146}
7147
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307148void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007149{
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307150 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7151
7152 if (m_n == M1_N1) {
7153 dp_m_n = &crtc->config->dp_m_n;
7154 dp_m2_n2 = &crtc->config->dp_m2_n2;
7155 } else if (m_n == M2_N2) {
7156
7157 /*
7158 * M2_N2 registers are not supported. Hence m2_n2 divider value
7159 * needs to be programmed into M1_N1.
7160 */
7161 dp_m_n = &crtc->config->dp_m2_n2;
7162 } else {
7163 DRM_ERROR("Unsupported divider value\n");
7164 return;
7165 }
7166
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007167 if (crtc->config->has_pch_encoder)
7168 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007169 else
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307170 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007171}
7172
Daniel Vetter251ac862015-06-18 10:30:24 +02007173static void vlv_compute_dpll(struct intel_crtc *crtc,
7174 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007175{
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007176 pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
7177 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
7178 DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV;
7179 if (crtc->pipe != PIPE_A)
7180 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007181
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007182 pipe_config->dpll_hw_state.dpll_md =
7183 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
7184}
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007185
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007186static void chv_compute_dpll(struct intel_crtc *crtc,
7187 struct intel_crtc_state *pipe_config)
7188{
7189 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
7190 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
7191 DPLL_VCO_ENABLE;
7192 if (crtc->pipe != PIPE_A)
7193 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7194
7195 pipe_config->dpll_hw_state.dpll_md =
7196 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007197}
7198
Ville Syrjäläd288f652014-10-28 13:20:22 +02007199static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007200 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007201{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007202 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007203 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007204 int pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007205 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007206 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007207 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007208
Ville Syrjäläa5805162015-05-26 20:42:30 +03007209 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01007210
Ville Syrjäläd288f652014-10-28 13:20:22 +02007211 bestn = pipe_config->dpll.n;
7212 bestm1 = pipe_config->dpll.m1;
7213 bestm2 = pipe_config->dpll.m2;
7214 bestp1 = pipe_config->dpll.p1;
7215 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007216
Jesse Barnes89b667f2013-04-18 14:51:36 -07007217 /* See eDP HDMI DPIO driver vbios notes doc */
7218
7219 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007220 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007221 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007222
7223 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007224 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007225
7226 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007227 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007228 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007229 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007230
7231 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007232 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007233
7234 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007235 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7236 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7237 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007238 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07007239
7240 /*
7241 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7242 * but we don't support that).
7243 * Note: don't use the DAC post divider as it seems unstable.
7244 */
7245 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007246 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007247
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007248 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007249 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007250
Jesse Barnes89b667f2013-04-18 14:51:36 -07007251 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02007252 if (pipe_config->port_clock == 162000 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007253 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
7254 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007255 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b01202013-07-05 19:21:38 +03007256 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007257 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007258 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007259 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007260
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02007261 if (pipe_config->has_dp_encoder) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07007262 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007263 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007264 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007265 0x0df40000);
7266 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007267 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007268 0x0df70000);
7269 } else { /* HDMI or VGA */
7270 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007271 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007272 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007273 0x0df70000);
7274 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007275 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007276 0x0df40000);
7277 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007278
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007279 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007280 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007281 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
7282 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
Jesse Barnes89b667f2013-04-18 14:51:36 -07007283 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007284 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007285
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007286 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Ville Syrjäläa5805162015-05-26 20:42:30 +03007287 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007288}
7289
Ville Syrjäläd288f652014-10-28 13:20:22 +02007290static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007291 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007292{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007293 struct drm_device *dev = crtc->base.dev;
7294 struct drm_i915_private *dev_priv = dev->dev_private;
7295 int pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02007296 i915_reg_t dpll_reg = DPLL(crtc->pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007297 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307298 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007299 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307300 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307301 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007302
Ville Syrjäläd288f652014-10-28 13:20:22 +02007303 bestn = pipe_config->dpll.n;
7304 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7305 bestm1 = pipe_config->dpll.m1;
7306 bestm2 = pipe_config->dpll.m2 >> 22;
7307 bestp1 = pipe_config->dpll.p1;
7308 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307309 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307310 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307311 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007312
7313 /*
7314 * Enable Refclk and SSC
7315 */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007316 I915_WRITE(dpll_reg,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007317 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007318
Ville Syrjäläa5805162015-05-26 20:42:30 +03007319 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007320
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007321 /* p1 and p2 divider */
7322 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7323 5 << DPIO_CHV_S1_DIV_SHIFT |
7324 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7325 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7326 1 << DPIO_CHV_K_DIV_SHIFT);
7327
7328 /* Feedback post-divider - m2 */
7329 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7330
7331 /* Feedback refclk divider - n and m1 */
7332 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7333 DPIO_CHV_M1_DIV_BY_2 |
7334 1 << DPIO_CHV_N_DIV_SHIFT);
7335
7336 /* M2 fraction division */
Ville Syrjälä25a25df2015-07-08 23:45:47 +03007337 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007338
7339 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307340 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7341 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7342 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7343 if (bestm2_frac)
7344 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7345 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007346
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05307347 /* Program digital lock detect threshold */
7348 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7349 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7350 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7351 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7352 if (!bestm2_frac)
7353 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7354 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7355
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007356 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307357 if (vco == 5400000) {
7358 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7359 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7360 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7361 tribuf_calcntr = 0x9;
7362 } else if (vco <= 6200000) {
7363 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7364 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7365 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7366 tribuf_calcntr = 0x9;
7367 } else if (vco <= 6480000) {
7368 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7369 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7370 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7371 tribuf_calcntr = 0x8;
7372 } else {
7373 /* Not supported. Apply the same limits as in the max case */
7374 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7375 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7376 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7377 tribuf_calcntr = 0;
7378 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007379 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7380
Ville Syrjälä968040b2015-03-11 22:52:08 +02007381 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307382 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7383 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7384 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7385
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007386 /* AFC Recal */
7387 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7388 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7389 DPIO_AFC_RECAL);
7390
Ville Syrjäläa5805162015-05-26 20:42:30 +03007391 mutex_unlock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007392}
7393
Ville Syrjäläd288f652014-10-28 13:20:22 +02007394/**
7395 * vlv_force_pll_on - forcibly enable just the PLL
7396 * @dev_priv: i915 private structure
7397 * @pipe: pipe PLL to enable
7398 * @dpll: PLL configuration
7399 *
7400 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7401 * in cases where we need the PLL enabled even when @pipe is not going to
7402 * be enabled.
7403 */
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007404int vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7405 const struct dpll *dpll)
Ville Syrjäläd288f652014-10-28 13:20:22 +02007406{
7407 struct intel_crtc *crtc =
7408 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007409 struct intel_crtc_state *pipe_config;
7410
7411 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
7412 if (!pipe_config)
7413 return -ENOMEM;
7414
7415 pipe_config->base.crtc = &crtc->base;
7416 pipe_config->pixel_multiplier = 1;
7417 pipe_config->dpll = *dpll;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007418
7419 if (IS_CHERRYVIEW(dev)) {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007420 chv_compute_dpll(crtc, pipe_config);
7421 chv_prepare_pll(crtc, pipe_config);
7422 chv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007423 } else {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007424 vlv_compute_dpll(crtc, pipe_config);
7425 vlv_prepare_pll(crtc, pipe_config);
7426 vlv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007427 }
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007428
7429 kfree(pipe_config);
7430
7431 return 0;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007432}
7433
7434/**
7435 * vlv_force_pll_off - forcibly disable just the PLL
7436 * @dev_priv: i915 private structure
7437 * @pipe: pipe PLL to disable
7438 *
7439 * Disable the PLL for @pipe. To be used in cases where we need
7440 * the PLL enabled even when @pipe is not going to be enabled.
7441 */
7442void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7443{
7444 if (IS_CHERRYVIEW(dev))
7445 chv_disable_pll(to_i915(dev), pipe);
7446 else
7447 vlv_disable_pll(to_i915(dev), pipe);
7448}
7449
Daniel Vetter251ac862015-06-18 10:30:24 +02007450static void i9xx_compute_dpll(struct intel_crtc *crtc,
7451 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007452 intel_clock_t *reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007453{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007454 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007455 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007456 u32 dpll;
7457 bool is_sdvo;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007458 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007459
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007460 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307461
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007462 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7463 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007464
7465 dpll = DPLL_VGA_MODE_DIS;
7466
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007467 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007468 dpll |= DPLLB_MODE_LVDS;
7469 else
7470 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007471
Daniel Vetteref1b4602013-06-01 17:17:04 +02007472 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007473 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02007474 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007475 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02007476
7477 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007478 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007479
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007480 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007481 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007482
7483 /* compute bitmask from p1 value */
7484 if (IS_PINEVIEW(dev))
7485 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7486 else {
7487 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7488 if (IS_G4X(dev) && reduced_clock)
7489 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7490 }
7491 switch (clock->p2) {
7492 case 5:
7493 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7494 break;
7495 case 7:
7496 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7497 break;
7498 case 10:
7499 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7500 break;
7501 case 14:
7502 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7503 break;
7504 }
7505 if (INTEL_INFO(dev)->gen >= 4)
7506 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7507
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007508 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007509 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007510 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007511 intel_panel_use_ssc(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007512 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7513 else
7514 dpll |= PLL_REF_INPUT_DREFCLK;
7515
7516 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007517 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007518
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007519 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007520 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007521 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007522 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007523 }
7524}
7525
Daniel Vetter251ac862015-06-18 10:30:24 +02007526static void i8xx_compute_dpll(struct intel_crtc *crtc,
7527 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007528 intel_clock_t *reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007529{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007530 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007531 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007532 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007533 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007534
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007535 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307536
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007537 dpll = DPLL_VGA_MODE_DIS;
7538
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007539 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007540 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7541 } else {
7542 if (clock->p1 == 2)
7543 dpll |= PLL_P1_DIVIDE_BY_TWO;
7544 else
7545 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7546 if (clock->p2 == 4)
7547 dpll |= PLL_P2_DIVIDE_BY_4;
7548 }
7549
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007550 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007551 dpll |= DPLL_DVO_2X_MODE;
7552
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007553 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007554 intel_panel_use_ssc(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007555 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7556 else
7557 dpll |= PLL_REF_INPUT_DREFCLK;
7558
7559 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007560 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007561}
7562
Daniel Vetter8a654f32013-06-01 17:16:22 +02007563static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007564{
7565 struct drm_device *dev = intel_crtc->base.dev;
7566 struct drm_i915_private *dev_priv = dev->dev_private;
7567 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007568 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03007569 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007570 uint32_t crtc_vtotal, crtc_vblank_end;
7571 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007572
7573 /* We need to be careful not to changed the adjusted mode, for otherwise
7574 * the hw state checker will get angry at the mismatch. */
7575 crtc_vtotal = adjusted_mode->crtc_vtotal;
7576 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007577
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007578 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007579 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007580 crtc_vtotal -= 1;
7581 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007582
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007583 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007584 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7585 else
7586 vsyncshift = adjusted_mode->crtc_hsync_start -
7587 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007588 if (vsyncshift < 0)
7589 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007590 }
7591
7592 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007593 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007594
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007595 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007596 (adjusted_mode->crtc_hdisplay - 1) |
7597 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007598 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007599 (adjusted_mode->crtc_hblank_start - 1) |
7600 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007601 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007602 (adjusted_mode->crtc_hsync_start - 1) |
7603 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7604
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007605 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007606 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007607 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007608 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007609 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007610 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007611 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007612 (adjusted_mode->crtc_vsync_start - 1) |
7613 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7614
Paulo Zanonib5e508d2012-10-24 11:34:43 -02007615 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7616 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7617 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7618 * bits. */
7619 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7620 (pipe == PIPE_B || pipe == PIPE_C))
7621 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7622
Jani Nikulabc58be62016-03-18 17:05:39 +02007623}
7624
7625static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
7626{
7627 struct drm_device *dev = intel_crtc->base.dev;
7628 struct drm_i915_private *dev_priv = dev->dev_private;
7629 enum pipe pipe = intel_crtc->pipe;
7630
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007631 /* pipesrc controls the size that is scaled from, which should
7632 * always be the user's requested size.
7633 */
7634 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007635 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7636 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007637}
7638
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007639static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007640 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007641{
7642 struct drm_device *dev = crtc->base.dev;
7643 struct drm_i915_private *dev_priv = dev->dev_private;
7644 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7645 uint32_t tmp;
7646
7647 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007648 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7649 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007650 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007651 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7652 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007653 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007654 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7655 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007656
7657 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007658 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7659 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007660 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007661 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7662 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007663 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007664 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7665 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007666
7667 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007668 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7669 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7670 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007671 }
Jani Nikulabc58be62016-03-18 17:05:39 +02007672}
7673
7674static void intel_get_pipe_src_size(struct intel_crtc *crtc,
7675 struct intel_crtc_state *pipe_config)
7676{
7677 struct drm_device *dev = crtc->base.dev;
7678 struct drm_i915_private *dev_priv = dev->dev_private;
7679 u32 tmp;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007680
7681 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03007682 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7683 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7684
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007685 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7686 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007687}
7688
Daniel Vetterf6a83282014-02-11 15:28:57 -08007689void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007690 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03007691{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007692 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7693 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7694 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7695 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007696
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007697 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7698 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7699 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7700 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007701
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007702 mode->flags = pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007703 mode->type = DRM_MODE_TYPE_DRIVER;
Jesse Barnesbabea612013-06-26 18:57:38 +03007704
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007705 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7706 mode->flags |= pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007707
7708 mode->hsync = drm_mode_hsync(mode);
7709 mode->vrefresh = drm_mode_vrefresh(mode);
7710 drm_mode_set_name(mode);
Jesse Barnesbabea612013-06-26 18:57:38 +03007711}
7712
Daniel Vetter84b046f2013-02-19 18:48:54 +01007713static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7714{
7715 struct drm_device *dev = intel_crtc->base.dev;
7716 struct drm_i915_private *dev_priv = dev->dev_private;
7717 uint32_t pipeconf;
7718
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007719 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007720
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03007721 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7722 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7723 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02007724
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007725 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007726 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007727
Daniel Vetterff9ce462013-04-24 14:57:17 +02007728 /* only g4x and later have fancy bpc/dither controls */
Wayne Boyer666a4532015-12-09 12:29:35 -08007729 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007730 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007731 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02007732 pipeconf |= PIPECONF_DITHER_EN |
7733 PIPECONF_DITHER_TYPE_SP;
7734
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007735 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007736 case 18:
7737 pipeconf |= PIPECONF_6BPC;
7738 break;
7739 case 24:
7740 pipeconf |= PIPECONF_8BPC;
7741 break;
7742 case 30:
7743 pipeconf |= PIPECONF_10BPC;
7744 break;
7745 default:
7746 /* Case prevented by intel_choose_pipe_bpp_dither. */
7747 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01007748 }
7749 }
7750
7751 if (HAS_PIPE_CXSR(dev)) {
7752 if (intel_crtc->lowfreq_avail) {
7753 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7754 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7755 } else {
7756 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01007757 }
7758 }
7759
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007760 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007761 if (INTEL_INFO(dev)->gen < 4 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007762 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007763 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7764 else
7765 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7766 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01007767 pipeconf |= PIPECONF_PROGRESSIVE;
7768
Wayne Boyer666a4532015-12-09 12:29:35 -08007769 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
7770 intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007771 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03007772
Daniel Vetter84b046f2013-02-19 18:48:54 +01007773 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7774 POSTING_READ(PIPECONF(intel_crtc->pipe));
7775}
7776
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007777static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
7778 struct intel_crtc_state *crtc_state)
7779{
7780 struct drm_device *dev = crtc->base.dev;
7781 struct drm_i915_private *dev_priv = dev->dev_private;
7782 const intel_limit_t *limit;
7783 int refclk = 48000;
7784
7785 memset(&crtc_state->dpll_hw_state, 0,
7786 sizeof(crtc_state->dpll_hw_state));
7787
7788 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7789 if (intel_panel_use_ssc(dev_priv)) {
7790 refclk = dev_priv->vbt.lvds_ssc_freq;
7791 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7792 }
7793
7794 limit = &intel_limits_i8xx_lvds;
7795 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO)) {
7796 limit = &intel_limits_i8xx_dvo;
7797 } else {
7798 limit = &intel_limits_i8xx_dac;
7799 }
7800
7801 if (!crtc_state->clock_set &&
7802 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7803 refclk, NULL, &crtc_state->dpll)) {
7804 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7805 return -EINVAL;
7806 }
7807
7808 i8xx_compute_dpll(crtc, crtc_state, NULL);
7809
7810 return 0;
7811}
7812
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007813static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
7814 struct intel_crtc_state *crtc_state)
7815{
7816 struct drm_device *dev = crtc->base.dev;
7817 struct drm_i915_private *dev_priv = dev->dev_private;
7818 const intel_limit_t *limit;
7819 int refclk = 96000;
7820
7821 memset(&crtc_state->dpll_hw_state, 0,
7822 sizeof(crtc_state->dpll_hw_state));
7823
7824 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7825 if (intel_panel_use_ssc(dev_priv)) {
7826 refclk = dev_priv->vbt.lvds_ssc_freq;
7827 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7828 }
7829
7830 if (intel_is_dual_link_lvds(dev))
7831 limit = &intel_limits_g4x_dual_channel_lvds;
7832 else
7833 limit = &intel_limits_g4x_single_channel_lvds;
7834 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
7835 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
7836 limit = &intel_limits_g4x_hdmi;
7837 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
7838 limit = &intel_limits_g4x_sdvo;
7839 } else {
7840 /* The option is for other outputs */
7841 limit = &intel_limits_i9xx_sdvo;
7842 }
7843
7844 if (!crtc_state->clock_set &&
7845 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7846 refclk, NULL, &crtc_state->dpll)) {
7847 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7848 return -EINVAL;
7849 }
7850
7851 i9xx_compute_dpll(crtc, crtc_state, NULL);
7852
7853 return 0;
7854}
7855
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007856static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
7857 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08007858{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007859 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007860 struct drm_i915_private *dev_priv = dev->dev_private;
Ma Lingd4906092009-03-18 20:13:27 +08007861 const intel_limit_t *limit;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007862 int refclk = 96000;
Jesse Barnes79e53942008-11-07 14:24:08 -08007863
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03007864 memset(&crtc_state->dpll_hw_state, 0,
7865 sizeof(crtc_state->dpll_hw_state));
7866
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007867 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7868 if (intel_panel_use_ssc(dev_priv)) {
7869 refclk = dev_priv->vbt.lvds_ssc_freq;
7870 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7871 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007872
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007873 limit = &intel_limits_pineview_lvds;
7874 } else {
7875 limit = &intel_limits_pineview_sdvo;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007876 }
Jani Nikulaf2335332013-09-13 11:03:09 +03007877
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007878 if (!crtc_state->clock_set &&
7879 !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7880 refclk, NULL, &crtc_state->dpll)) {
7881 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7882 return -EINVAL;
7883 }
7884
7885 i9xx_compute_dpll(crtc, crtc_state, NULL);
7886
7887 return 0;
7888}
7889
7890static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7891 struct intel_crtc_state *crtc_state)
7892{
7893 struct drm_device *dev = crtc->base.dev;
7894 struct drm_i915_private *dev_priv = dev->dev_private;
7895 const intel_limit_t *limit;
7896 int refclk = 96000;
7897
7898 memset(&crtc_state->dpll_hw_state, 0,
7899 sizeof(crtc_state->dpll_hw_state));
7900
7901 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7902 if (intel_panel_use_ssc(dev_priv)) {
7903 refclk = dev_priv->vbt.lvds_ssc_freq;
7904 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007905 }
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007906
7907 limit = &intel_limits_i9xx_lvds;
7908 } else {
7909 limit = &intel_limits_i9xx_sdvo;
7910 }
7911
7912 if (!crtc_state->clock_set &&
7913 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7914 refclk, NULL, &crtc_state->dpll)) {
7915 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7916 return -EINVAL;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007917 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007918
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007919 i9xx_compute_dpll(crtc, crtc_state, NULL);
Eric Anholtf564048e2011-03-30 13:01:02 -07007920
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007921 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07007922}
7923
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02007924static int chv_crtc_compute_clock(struct intel_crtc *crtc,
7925 struct intel_crtc_state *crtc_state)
7926{
7927 int refclk = 100000;
7928 const intel_limit_t *limit = &intel_limits_chv;
7929
7930 memset(&crtc_state->dpll_hw_state, 0,
7931 sizeof(crtc_state->dpll_hw_state));
7932
7933 if (crtc_state->has_dsi_encoder)
7934 return 0;
7935
7936 if (!crtc_state->clock_set &&
7937 !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7938 refclk, NULL, &crtc_state->dpll)) {
7939 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7940 return -EINVAL;
7941 }
7942
7943 chv_compute_dpll(crtc, crtc_state);
7944
7945 return 0;
7946}
7947
7948static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
7949 struct intel_crtc_state *crtc_state)
7950{
7951 int refclk = 100000;
7952 const intel_limit_t *limit = &intel_limits_vlv;
7953
7954 memset(&crtc_state->dpll_hw_state, 0,
7955 sizeof(crtc_state->dpll_hw_state));
7956
7957 if (crtc_state->has_dsi_encoder)
7958 return 0;
7959
7960 if (!crtc_state->clock_set &&
7961 !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7962 refclk, NULL, &crtc_state->dpll)) {
7963 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7964 return -EINVAL;
7965 }
7966
7967 vlv_compute_dpll(crtc, crtc_state);
7968
7969 return 0;
7970}
7971
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007972static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007973 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007974{
7975 struct drm_device *dev = crtc->base.dev;
7976 struct drm_i915_private *dev_priv = dev->dev_private;
7977 uint32_t tmp;
7978
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02007979 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7980 return;
7981
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007982 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02007983 if (!(tmp & PFIT_ENABLE))
7984 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007985
Daniel Vetter06922822013-07-11 13:35:40 +02007986 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007987 if (INTEL_INFO(dev)->gen < 4) {
7988 if (crtc->pipe != PIPE_B)
7989 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007990 } else {
7991 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7992 return;
7993 }
7994
Daniel Vetter06922822013-07-11 13:35:40 +02007995 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007996 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
7997 if (INTEL_INFO(dev)->gen < 5)
7998 pipe_config->gmch_pfit.lvds_border_bits =
7999 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
8000}
8001
Jesse Barnesacbec812013-09-20 11:29:32 -07008002static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008003 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07008004{
8005 struct drm_device *dev = crtc->base.dev;
8006 struct drm_i915_private *dev_priv = dev->dev_private;
8007 int pipe = pipe_config->cpu_transcoder;
8008 intel_clock_t clock;
8009 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07008010 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07008011
Shobhit Kumarf573de52014-07-30 20:32:37 +05308012 /* In case of MIPI DPLL will not even be used */
8013 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
8014 return;
8015
Ville Syrjäläa5805162015-05-26 20:42:30 +03008016 mutex_lock(&dev_priv->sb_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08008017 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008018 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesacbec812013-09-20 11:29:32 -07008019
8020 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
8021 clock.m2 = mdiv & DPIO_M2DIV_MASK;
8022 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
8023 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
8024 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
8025
Imre Deakdccbea32015-06-22 23:35:51 +03008026 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07008027}
8028
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008029static void
8030i9xx_get_initial_plane_config(struct intel_crtc *crtc,
8031 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008032{
8033 struct drm_device *dev = crtc->base.dev;
8034 struct drm_i915_private *dev_priv = dev->dev_private;
8035 u32 val, base, offset;
8036 int pipe = crtc->pipe, plane = crtc->plane;
8037 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008038 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008039 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008040 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008041
Damien Lespiau42a7b082015-02-05 19:35:13 +00008042 val = I915_READ(DSPCNTR(plane));
8043 if (!(val & DISPLAY_PLANE_ENABLE))
8044 return;
8045
Damien Lespiaud9806c92015-01-21 14:07:19 +00008046 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008047 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008048 DRM_DEBUG_KMS("failed to alloc fb\n");
8049 return;
8050 }
8051
Damien Lespiau1b842c82015-01-21 13:50:54 +00008052 fb = &intel_fb->base;
8053
Daniel Vetter18c52472015-02-10 17:16:09 +00008054 if (INTEL_INFO(dev)->gen >= 4) {
8055 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008056 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00008057 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8058 }
8059 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008060
8061 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00008062 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008063 fb->pixel_format = fourcc;
8064 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008065
8066 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008067 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008068 offset = I915_READ(DSPTILEOFF(plane));
8069 else
8070 offset = I915_READ(DSPLINOFF(plane));
8071 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8072 } else {
8073 base = I915_READ(DSPADDR(plane));
8074 }
8075 plane_config->base = base;
8076
8077 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008078 fb->width = ((val >> 16) & 0xfff) + 1;
8079 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008080
8081 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008082 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008083
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008084 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008085 fb->pixel_format,
8086 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008087
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008088 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008089
Damien Lespiau2844a922015-01-20 12:51:48 +00008090 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8091 pipe_name(pipe), plane, fb->width, fb->height,
8092 fb->bits_per_pixel, base, fb->pitches[0],
8093 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008094
Damien Lespiau2d140302015-02-05 17:22:18 +00008095 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008096}
8097
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008098static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008099 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008100{
8101 struct drm_device *dev = crtc->base.dev;
8102 struct drm_i915_private *dev_priv = dev->dev_private;
8103 int pipe = pipe_config->cpu_transcoder;
8104 enum dpio_channel port = vlv_pipe_to_channel(pipe);
8105 intel_clock_t clock;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008106 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008107 int refclk = 100000;
8108
Ville Syrjäläa5805162015-05-26 20:42:30 +03008109 mutex_lock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008110 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8111 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8112 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8113 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
Imre Deak0d7b6b12015-07-02 14:29:58 +03008114 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008115 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008116
8117 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008118 clock.m2 = (pll_dw0 & 0xff) << 22;
8119 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
8120 clock.m2 |= pll_dw2 & 0x3fffff;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008121 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8122 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8123 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8124
Imre Deakdccbea32015-06-22 23:35:51 +03008125 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008126}
8127
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008128static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008129 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008130{
8131 struct drm_device *dev = crtc->base.dev;
8132 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak17290502016-02-12 18:55:11 +02008133 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008134 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02008135 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008136
Imre Deak17290502016-02-12 18:55:11 +02008137 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8138 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +02008139 return false;
8140
Daniel Vettere143a212013-07-04 12:01:15 +02008141 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008142 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02008143
Imre Deak17290502016-02-12 18:55:11 +02008144 ret = false;
8145
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008146 tmp = I915_READ(PIPECONF(crtc->pipe));
8147 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02008148 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008149
Wayne Boyer666a4532015-12-09 12:29:35 -08008150 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008151 switch (tmp & PIPECONF_BPC_MASK) {
8152 case PIPECONF_6BPC:
8153 pipe_config->pipe_bpp = 18;
8154 break;
8155 case PIPECONF_8BPC:
8156 pipe_config->pipe_bpp = 24;
8157 break;
8158 case PIPECONF_10BPC:
8159 pipe_config->pipe_bpp = 30;
8160 break;
8161 default:
8162 break;
8163 }
8164 }
8165
Wayne Boyer666a4532015-12-09 12:29:35 -08008166 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
8167 (tmp & PIPECONF_COLOR_RANGE_SELECT))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008168 pipe_config->limited_color_range = true;
8169
Ville Syrjälä282740f2013-09-04 18:30:03 +03008170 if (INTEL_INFO(dev)->gen < 4)
8171 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8172
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008173 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +02008174 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008175
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008176 i9xx_get_pfit_config(crtc, pipe_config);
8177
Daniel Vetter6c49f242013-06-06 12:45:25 +02008178 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjäläc2317752016-03-15 16:39:56 +02008179 /* No way to read it out on pipes B and C */
8180 if (IS_CHERRYVIEW(dev) && crtc->pipe != PIPE_A)
8181 tmp = dev_priv->chv_dpll_md[crtc->pipe];
8182 else
8183 tmp = I915_READ(DPLL_MD(crtc->pipe));
Daniel Vetter6c49f242013-06-06 12:45:25 +02008184 pipe_config->pixel_multiplier =
8185 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8186 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008187 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02008188 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8189 tmp = I915_READ(DPLL(crtc->pipe));
8190 pipe_config->pixel_multiplier =
8191 ((tmp & SDVO_MULTIPLIER_MASK)
8192 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8193 } else {
8194 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8195 * port and will be fixed up in the encoder->get_config
8196 * function. */
8197 pipe_config->pixel_multiplier = 1;
8198 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008199 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
Wayne Boyer666a4532015-12-09 12:29:35 -08008200 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03008201 /*
8202 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8203 * on 830. Filter it out here so that we don't
8204 * report errors due to that.
8205 */
8206 if (IS_I830(dev))
8207 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8208
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008209 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8210 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03008211 } else {
8212 /* Mask out read-only status bits. */
8213 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8214 DPLL_PORTC_READY_MASK |
8215 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008216 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02008217
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008218 if (IS_CHERRYVIEW(dev))
8219 chv_crtc_clock_get(crtc, pipe_config);
8220 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07008221 vlv_crtc_clock_get(crtc, pipe_config);
8222 else
8223 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03008224
Ville Syrjälä0f646142015-08-26 19:39:18 +03008225 /*
8226 * Normally the dotclock is filled in by the encoder .get_config()
8227 * but in case the pipe is enabled w/o any ports we need a sane
8228 * default.
8229 */
8230 pipe_config->base.adjusted_mode.crtc_clock =
8231 pipe_config->port_clock / pipe_config->pixel_multiplier;
8232
Imre Deak17290502016-02-12 18:55:11 +02008233 ret = true;
8234
8235out:
8236 intel_display_power_put(dev_priv, power_domain);
8237
8238 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008239}
8240
Paulo Zanonidde86e22012-12-01 12:04:25 -02008241static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07008242{
8243 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008244 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008245 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008246 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008247 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008248 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07008249 bool has_ck505 = false;
8250 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008251
8252 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01008253 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07008254 switch (encoder->type) {
8255 case INTEL_OUTPUT_LVDS:
8256 has_panel = true;
8257 has_lvds = true;
8258 break;
8259 case INTEL_OUTPUT_EDP:
8260 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03008261 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07008262 has_cpu_edp = true;
8263 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008264 default:
8265 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008266 }
8267 }
8268
Keith Packard99eb6a02011-09-26 14:29:12 -07008269 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008270 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07008271 can_ssc = has_ck505;
8272 } else {
8273 has_ck505 = false;
8274 can_ssc = true;
8275 }
8276
Imre Deak2de69052013-05-08 13:14:04 +03008277 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
8278 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008279
8280 /* Ironlake: try to setup display ref clock before DPLL
8281 * enabling. This is only under driver's control after
8282 * PCH B stepping, previous chipset stepping should be
8283 * ignoring this setting.
8284 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008285 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008286
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008287 /* As we must carefully and slowly disable/enable each source in turn,
8288 * compute the final state we want first and check if we need to
8289 * make any changes at all.
8290 */
8291 final = val;
8292 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07008293 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008294 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07008295 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008296 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8297
8298 final &= ~DREF_SSC_SOURCE_MASK;
8299 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8300 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008301
Keith Packard199e5d72011-09-22 12:01:57 -07008302 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008303 final |= DREF_SSC_SOURCE_ENABLE;
8304
8305 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8306 final |= DREF_SSC1_ENABLE;
8307
8308 if (has_cpu_edp) {
8309 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8310 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8311 else
8312 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8313 } else
8314 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8315 } else {
8316 final |= DREF_SSC_SOURCE_DISABLE;
8317 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8318 }
8319
8320 if (final == val)
8321 return;
8322
8323 /* Always enable nonspread source */
8324 val &= ~DREF_NONSPREAD_SOURCE_MASK;
8325
8326 if (has_ck505)
8327 val |= DREF_NONSPREAD_CK505_ENABLE;
8328 else
8329 val |= DREF_NONSPREAD_SOURCE_ENABLE;
8330
8331 if (has_panel) {
8332 val &= ~DREF_SSC_SOURCE_MASK;
8333 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008334
Keith Packard199e5d72011-09-22 12:01:57 -07008335 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07008336 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008337 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008338 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02008339 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008340 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008341
8342 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008343 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008344 POSTING_READ(PCH_DREF_CONTROL);
8345 udelay(200);
8346
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008347 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008348
8349 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07008350 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07008351 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008352 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008353 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02008354 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008355 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07008356 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008357 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008358
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008359 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008360 POSTING_READ(PCH_DREF_CONTROL);
8361 udelay(200);
8362 } else {
8363 DRM_DEBUG_KMS("Disabling SSC entirely\n");
8364
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008365 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07008366
8367 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008368 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008369
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008370 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008371 POSTING_READ(PCH_DREF_CONTROL);
8372 udelay(200);
8373
8374 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008375 val &= ~DREF_SSC_SOURCE_MASK;
8376 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008377
8378 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008379 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008380
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008381 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008382 POSTING_READ(PCH_DREF_CONTROL);
8383 udelay(200);
8384 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008385
8386 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008387}
8388
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008389static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02008390{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008391 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008392
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008393 tmp = I915_READ(SOUTH_CHICKEN2);
8394 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8395 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008396
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008397 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
8398 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
8399 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02008400
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008401 tmp = I915_READ(SOUTH_CHICKEN2);
8402 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8403 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008404
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008405 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
8406 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
8407 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008408}
8409
8410/* WaMPhyProgramming:hsw */
8411static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8412{
8413 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008414
8415 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8416 tmp &= ~(0xFF << 24);
8417 tmp |= (0x12 << 24);
8418 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8419
Paulo Zanonidde86e22012-12-01 12:04:25 -02008420 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8421 tmp |= (1 << 11);
8422 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8423
8424 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8425 tmp |= (1 << 11);
8426 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8427
Paulo Zanonidde86e22012-12-01 12:04:25 -02008428 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8429 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8430 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8431
8432 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8433 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8434 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8435
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008436 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8437 tmp &= ~(7 << 13);
8438 tmp |= (5 << 13);
8439 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008440
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008441 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8442 tmp &= ~(7 << 13);
8443 tmp |= (5 << 13);
8444 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008445
8446 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8447 tmp &= ~0xFF;
8448 tmp |= 0x1C;
8449 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8450
8451 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8452 tmp &= ~0xFF;
8453 tmp |= 0x1C;
8454 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8455
8456 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8457 tmp &= ~(0xFF << 16);
8458 tmp |= (0x1C << 16);
8459 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8460
8461 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8462 tmp &= ~(0xFF << 16);
8463 tmp |= (0x1C << 16);
8464 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8465
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008466 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8467 tmp |= (1 << 27);
8468 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008469
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008470 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8471 tmp |= (1 << 27);
8472 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008473
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008474 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8475 tmp &= ~(0xF << 28);
8476 tmp |= (4 << 28);
8477 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008478
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008479 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8480 tmp &= ~(0xF << 28);
8481 tmp |= (4 << 28);
8482 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008483}
8484
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008485/* Implements 3 different sequences from BSpec chapter "Display iCLK
8486 * Programming" based on the parameters passed:
8487 * - Sequence to enable CLKOUT_DP
8488 * - Sequence to enable CLKOUT_DP without spread
8489 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8490 */
8491static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8492 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008493{
8494 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008495 uint32_t reg, tmp;
8496
8497 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8498 with_spread = true;
Ville Syrjäläc2699522015-08-27 23:55:59 +03008499 if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008500 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008501
Ville Syrjäläa5805162015-05-26 20:42:30 +03008502 mutex_lock(&dev_priv->sb_lock);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008503
8504 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8505 tmp &= ~SBI_SSCCTL_DISABLE;
8506 tmp |= SBI_SSCCTL_PATHALT;
8507 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8508
8509 udelay(24);
8510
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008511 if (with_spread) {
8512 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8513 tmp &= ~SBI_SSCCTL_PATHALT;
8514 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008515
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008516 if (with_fdi) {
8517 lpt_reset_fdi_mphy(dev_priv);
8518 lpt_program_fdi_mphy(dev_priv);
8519 }
8520 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02008521
Ville Syrjäläc2699522015-08-27 23:55:59 +03008522 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008523 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8524 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8525 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01008526
Ville Syrjäläa5805162015-05-26 20:42:30 +03008527 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008528}
8529
Paulo Zanoni47701c32013-07-23 11:19:25 -03008530/* Sequence to disable CLKOUT_DP */
8531static void lpt_disable_clkout_dp(struct drm_device *dev)
8532{
8533 struct drm_i915_private *dev_priv = dev->dev_private;
8534 uint32_t reg, tmp;
8535
Ville Syrjäläa5805162015-05-26 20:42:30 +03008536 mutex_lock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008537
Ville Syrjäläc2699522015-08-27 23:55:59 +03008538 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni47701c32013-07-23 11:19:25 -03008539 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8540 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8541 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8542
8543 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8544 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8545 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8546 tmp |= SBI_SSCCTL_PATHALT;
8547 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8548 udelay(32);
8549 }
8550 tmp |= SBI_SSCCTL_DISABLE;
8551 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8552 }
8553
Ville Syrjäläa5805162015-05-26 20:42:30 +03008554 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008555}
8556
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008557#define BEND_IDX(steps) ((50 + (steps)) / 5)
8558
8559static const uint16_t sscdivintphase[] = {
8560 [BEND_IDX( 50)] = 0x3B23,
8561 [BEND_IDX( 45)] = 0x3B23,
8562 [BEND_IDX( 40)] = 0x3C23,
8563 [BEND_IDX( 35)] = 0x3C23,
8564 [BEND_IDX( 30)] = 0x3D23,
8565 [BEND_IDX( 25)] = 0x3D23,
8566 [BEND_IDX( 20)] = 0x3E23,
8567 [BEND_IDX( 15)] = 0x3E23,
8568 [BEND_IDX( 10)] = 0x3F23,
8569 [BEND_IDX( 5)] = 0x3F23,
8570 [BEND_IDX( 0)] = 0x0025,
8571 [BEND_IDX( -5)] = 0x0025,
8572 [BEND_IDX(-10)] = 0x0125,
8573 [BEND_IDX(-15)] = 0x0125,
8574 [BEND_IDX(-20)] = 0x0225,
8575 [BEND_IDX(-25)] = 0x0225,
8576 [BEND_IDX(-30)] = 0x0325,
8577 [BEND_IDX(-35)] = 0x0325,
8578 [BEND_IDX(-40)] = 0x0425,
8579 [BEND_IDX(-45)] = 0x0425,
8580 [BEND_IDX(-50)] = 0x0525,
8581};
8582
8583/*
8584 * Bend CLKOUT_DP
8585 * steps -50 to 50 inclusive, in steps of 5
8586 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
8587 * change in clock period = -(steps / 10) * 5.787 ps
8588 */
8589static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
8590{
8591 uint32_t tmp;
8592 int idx = BEND_IDX(steps);
8593
8594 if (WARN_ON(steps % 5 != 0))
8595 return;
8596
8597 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
8598 return;
8599
8600 mutex_lock(&dev_priv->sb_lock);
8601
8602 if (steps % 10 != 0)
8603 tmp = 0xAAAAAAAB;
8604 else
8605 tmp = 0x00000000;
8606 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
8607
8608 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
8609 tmp &= 0xffff0000;
8610 tmp |= sscdivintphase[idx];
8611 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
8612
8613 mutex_unlock(&dev_priv->sb_lock);
8614}
8615
8616#undef BEND_IDX
8617
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008618static void lpt_init_pch_refclk(struct drm_device *dev)
8619{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008620 struct intel_encoder *encoder;
8621 bool has_vga = false;
8622
Damien Lespiaub2784e12014-08-05 11:29:37 +01008623 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008624 switch (encoder->type) {
8625 case INTEL_OUTPUT_ANALOG:
8626 has_vga = true;
8627 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008628 default:
8629 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008630 }
8631 }
8632
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008633 if (has_vga) {
8634 lpt_bend_clkout_dp(to_i915(dev), 0);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008635 lpt_enable_clkout_dp(dev, true, true);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008636 } else {
Paulo Zanoni47701c32013-07-23 11:19:25 -03008637 lpt_disable_clkout_dp(dev);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008638 }
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008639}
8640
Paulo Zanonidde86e22012-12-01 12:04:25 -02008641/*
8642 * Initialize reference clocks when the driver loads
8643 */
8644void intel_init_pch_refclk(struct drm_device *dev)
8645{
8646 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8647 ironlake_init_pch_refclk(dev);
8648 else if (HAS_PCH_LPT(dev))
8649 lpt_init_pch_refclk(dev);
8650}
8651
Daniel Vetter6ff93602013-04-19 11:24:36 +02008652static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03008653{
8654 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8655 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8656 int pipe = intel_crtc->pipe;
8657 uint32_t val;
8658
Daniel Vetter78114072013-06-13 00:54:57 +02008659 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03008660
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008661 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03008662 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008663 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008664 break;
8665 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008666 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008667 break;
8668 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008669 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008670 break;
8671 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008672 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008673 break;
8674 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03008675 /* Case prevented by intel_choose_pipe_bpp_dither. */
8676 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03008677 }
8678
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008679 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03008680 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8681
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008682 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03008683 val |= PIPECONF_INTERLACED_ILK;
8684 else
8685 val |= PIPECONF_PROGRESSIVE;
8686
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008687 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008688 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008689
Paulo Zanonic8203562012-09-12 10:06:29 -03008690 I915_WRITE(PIPECONF(pipe), val);
8691 POSTING_READ(PIPECONF(pipe));
8692}
8693
Daniel Vetter6ff93602013-04-19 11:24:36 +02008694static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008695{
Jani Nikula391bf042016-03-18 17:05:40 +02008696 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008697 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008698 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jani Nikula391bf042016-03-18 17:05:40 +02008699 u32 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008700
Jani Nikula391bf042016-03-18 17:05:40 +02008701 if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008702 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8703
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008704 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008705 val |= PIPECONF_INTERLACED_ILK;
8706 else
8707 val |= PIPECONF_PROGRESSIVE;
8708
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008709 I915_WRITE(PIPECONF(cpu_transcoder), val);
8710 POSTING_READ(PIPECONF(cpu_transcoder));
Jani Nikula391bf042016-03-18 17:05:40 +02008711}
8712
Jani Nikula391bf042016-03-18 17:05:40 +02008713static void haswell_set_pipemisc(struct drm_crtc *crtc)
8714{
8715 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8716 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8717
8718 if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
8719 u32 val = 0;
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008720
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008721 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008722 case 18:
8723 val |= PIPEMISC_DITHER_6_BPC;
8724 break;
8725 case 24:
8726 val |= PIPEMISC_DITHER_8_BPC;
8727 break;
8728 case 30:
8729 val |= PIPEMISC_DITHER_10_BPC;
8730 break;
8731 case 36:
8732 val |= PIPEMISC_DITHER_12_BPC;
8733 break;
8734 default:
8735 /* Case prevented by pipe_config_set_bpp. */
8736 BUG();
8737 }
8738
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008739 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008740 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8741
Jani Nikula391bf042016-03-18 17:05:40 +02008742 I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008743 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008744}
8745
Paulo Zanonid4b19312012-11-29 11:29:32 -02008746int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8747{
8748 /*
8749 * Account for spread spectrum to avoid
8750 * oversubscribing the link. Max center spread
8751 * is 2.5%; use 5% for safety's sake.
8752 */
8753 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02008754 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02008755}
8756
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008757static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02008758{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008759 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03008760}
8761
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008762static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
8763 struct intel_crtc_state *crtc_state,
8764 intel_clock_t *reduced_clock)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008765{
8766 struct drm_crtc *crtc = &intel_crtc->base;
8767 struct drm_device *dev = crtc->dev;
8768 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008769 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008770 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008771 struct drm_connector_state *connector_state;
8772 struct intel_encoder *encoder;
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008773 u32 dpll, fp, fp2;
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02008774 int factor, i;
Daniel Vetter09ede542013-04-30 14:01:45 +02008775 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008776
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008777 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008778 if (connector_state->crtc != crtc_state->base.crtc)
8779 continue;
8780
8781 encoder = to_intel_encoder(connector_state->best_encoder);
8782
8783 switch (encoder->type) {
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008784 case INTEL_OUTPUT_LVDS:
8785 is_lvds = true;
8786 break;
8787 case INTEL_OUTPUT_SDVO:
8788 case INTEL_OUTPUT_HDMI:
8789 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008790 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008791 default:
8792 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008793 }
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008794 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008795
Chris Wilsonc1858122010-12-03 21:35:48 +00008796 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07008797 factor = 21;
8798 if (is_lvds) {
8799 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008800 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02008801 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07008802 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008803 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07008804 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00008805
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008806 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Chris Wilsonc1858122010-12-03 21:35:48 +00008807
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008808 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
8809 fp |= FP_CB_TUNE;
8810
8811 if (reduced_clock) {
8812 fp2 = i9xx_dpll_compute_fp(reduced_clock);
8813
8814 if (reduced_clock->m < factor * reduced_clock->n)
8815 fp2 |= FP_CB_TUNE;
8816 } else {
8817 fp2 = fp;
8818 }
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008819
Chris Wilson5eddb702010-09-11 13:48:45 +01008820 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08008821
Eric Anholta07d6782011-03-30 13:01:08 -07008822 if (is_lvds)
8823 dpll |= DPLLB_MODE_LVDS;
8824 else
8825 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008826
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008827 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02008828 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008829
8830 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008831 dpll |= DPLL_SDVO_HIGH_SPEED;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008832 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008833 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08008834
Eric Anholta07d6782011-03-30 13:01:08 -07008835 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008836 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008837 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008838 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008839
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008840 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07008841 case 5:
8842 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8843 break;
8844 case 7:
8845 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8846 break;
8847 case 10:
8848 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8849 break;
8850 case 14:
8851 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8852 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08008853 }
8854
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02008855 if (is_lvds && intel_panel_use_ssc(dev_priv))
Kristian Høgsberg43565a02009-02-13 20:56:52 -05008856 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08008857 else
8858 dpll |= PLL_REF_INPUT_DREFCLK;
8859
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008860 dpll |= DPLL_VCO_ENABLE;
8861
8862 crtc_state->dpll_hw_state.dpll = dpll;
8863 crtc_state->dpll_hw_state.fp0 = fp;
8864 crtc_state->dpll_hw_state.fp1 = fp2;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008865}
8866
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008867static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8868 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008869{
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008870 struct drm_device *dev = crtc->base.dev;
8871 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02008872 intel_clock_t reduced_clock;
Ander Conselvan de Oliveira7ed9f892016-03-21 18:00:07 +02008873 bool has_reduced_clock = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008874 struct intel_shared_dpll *pll;
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008875 const intel_limit_t *limit;
8876 int refclk = 120000;
Jesse Barnes79e53942008-11-07 14:24:08 -08008877
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03008878 memset(&crtc_state->dpll_hw_state, 0,
8879 sizeof(crtc_state->dpll_hw_state));
8880
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02008881 crtc->lowfreq_avail = false;
8882
8883 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
8884 if (!crtc_state->has_pch_encoder)
8885 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008886
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008887 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
8888 if (intel_panel_use_ssc(dev_priv)) {
8889 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
8890 dev_priv->vbt.lvds_ssc_freq);
8891 refclk = dev_priv->vbt.lvds_ssc_freq;
8892 }
8893
8894 if (intel_is_dual_link_lvds(dev)) {
8895 if (refclk == 100000)
8896 limit = &intel_limits_ironlake_dual_lvds_100m;
8897 else
8898 limit = &intel_limits_ironlake_dual_lvds;
8899 } else {
8900 if (refclk == 100000)
8901 limit = &intel_limits_ironlake_single_lvds_100m;
8902 else
8903 limit = &intel_limits_ironlake_single_lvds;
8904 }
8905 } else {
8906 limit = &intel_limits_ironlake_dac;
8907 }
8908
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02008909 if (!crtc_state->clock_set &&
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008910 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8911 refclk, NULL, &crtc_state->dpll)) {
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02008912 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8913 return -EINVAL;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008914 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008915
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008916 ironlake_compute_dpll(crtc, crtc_state,
8917 has_reduced_clock ? &reduced_clock : NULL);
Daniel Vetter66e985c2013-06-05 13:34:20 +02008918
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02008919 pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
8920 if (pll == NULL) {
8921 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
8922 pipe_name(crtc->pipe));
8923 return -EINVAL;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02008924 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008925
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02008926 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
8927 has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008928 crtc->lowfreq_avail = true;
Daniel Vettere2b78262013-06-07 23:10:03 +02008929
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008930 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008931}
8932
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008933static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8934 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02008935{
8936 struct drm_device *dev = crtc->base.dev;
8937 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008938 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02008939
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008940 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8941 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8942 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8943 & ~TU_SIZE_MASK;
8944 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8945 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8946 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8947}
8948
8949static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8950 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008951 struct intel_link_m_n *m_n,
8952 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008953{
8954 struct drm_device *dev = crtc->base.dev;
8955 struct drm_i915_private *dev_priv = dev->dev_private;
8956 enum pipe pipe = crtc->pipe;
8957
8958 if (INTEL_INFO(dev)->gen >= 5) {
8959 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8960 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8961 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8962 & ~TU_SIZE_MASK;
8963 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8964 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8965 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008966 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8967 * gen < 8) and if DRRS is supported (to make sure the
8968 * registers are not unnecessarily read).
8969 */
8970 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008971 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008972 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8973 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8974 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8975 & ~TU_SIZE_MASK;
8976 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8977 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8978 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8979 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008980 } else {
8981 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8982 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8983 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8984 & ~TU_SIZE_MASK;
8985 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8986 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8987 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8988 }
8989}
8990
8991void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008992 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008993{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02008994 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008995 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
8996 else
8997 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008998 &pipe_config->dp_m_n,
8999 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009000}
9001
Daniel Vetter72419202013-04-04 13:28:53 +02009002static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009003 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02009004{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009005 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009006 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02009007}
9008
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009009static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009010 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009011{
9012 struct drm_device *dev = crtc->base.dev;
9013 struct drm_i915_private *dev_priv = dev->dev_private;
Chandra Kondurua1b22782015-04-07 15:28:45 -07009014 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
9015 uint32_t ps_ctrl = 0;
9016 int id = -1;
9017 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009018
Chandra Kondurua1b22782015-04-07 15:28:45 -07009019 /* find scaler attached to this pipe */
9020 for (i = 0; i < crtc->num_scalers; i++) {
9021 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
9022 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
9023 id = i;
9024 pipe_config->pch_pfit.enabled = true;
9025 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9026 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9027 break;
9028 }
9029 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009030
Chandra Kondurua1b22782015-04-07 15:28:45 -07009031 scaler_state->scaler_id = id;
9032 if (id >= 0) {
9033 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9034 } else {
9035 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009036 }
9037}
9038
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009039static void
9040skylake_get_initial_plane_config(struct intel_crtc *crtc,
9041 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009042{
9043 struct drm_device *dev = crtc->base.dev;
9044 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau40f46282015-02-27 11:15:21 +00009045 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009046 int pipe = crtc->pipe;
9047 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009048 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009049 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009050 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009051
Damien Lespiaud9806c92015-01-21 14:07:19 +00009052 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009053 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009054 DRM_DEBUG_KMS("failed to alloc fb\n");
9055 return;
9056 }
9057
Damien Lespiau1b842c82015-01-21 13:50:54 +00009058 fb = &intel_fb->base;
9059
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009060 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00009061 if (!(val & PLANE_CTL_ENABLE))
9062 goto error;
9063
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009064 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9065 fourcc = skl_format_to_fourcc(pixel_format,
9066 val & PLANE_CTL_ORDER_RGBX,
9067 val & PLANE_CTL_ALPHA_MASK);
9068 fb->pixel_format = fourcc;
9069 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9070
Damien Lespiau40f46282015-02-27 11:15:21 +00009071 tiling = val & PLANE_CTL_TILED_MASK;
9072 switch (tiling) {
9073 case PLANE_CTL_TILED_LINEAR:
9074 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9075 break;
9076 case PLANE_CTL_TILED_X:
9077 plane_config->tiling = I915_TILING_X;
9078 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9079 break;
9080 case PLANE_CTL_TILED_Y:
9081 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9082 break;
9083 case PLANE_CTL_TILED_YF:
9084 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9085 break;
9086 default:
9087 MISSING_CASE(tiling);
9088 goto error;
9089 }
9090
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009091 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9092 plane_config->base = base;
9093
9094 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9095
9096 val = I915_READ(PLANE_SIZE(pipe, 0));
9097 fb->height = ((val >> 16) & 0xfff) + 1;
9098 fb->width = ((val >> 0) & 0x1fff) + 1;
9099
9100 val = I915_READ(PLANE_STRIDE(pipe, 0));
Ville Syrjälä7b49f942016-01-12 21:08:32 +02009101 stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
Damien Lespiau40f46282015-02-27 11:15:21 +00009102 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009103 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9104
9105 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009106 fb->pixel_format,
9107 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009108
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009109 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009110
9111 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9112 pipe_name(pipe), fb->width, fb->height,
9113 fb->bits_per_pixel, base, fb->pitches[0],
9114 plane_config->size);
9115
Damien Lespiau2d140302015-02-05 17:22:18 +00009116 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009117 return;
9118
9119error:
9120 kfree(fb);
9121}
9122
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009123static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009124 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009125{
9126 struct drm_device *dev = crtc->base.dev;
9127 struct drm_i915_private *dev_priv = dev->dev_private;
9128 uint32_t tmp;
9129
9130 tmp = I915_READ(PF_CTL(crtc->pipe));
9131
9132 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009133 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009134 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9135 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02009136
9137 /* We currently do not free assignements of panel fitters on
9138 * ivb/hsw (since we don't use the higher upscaling modes which
9139 * differentiates them) so just WARN about this case for now. */
9140 if (IS_GEN7(dev)) {
9141 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9142 PF_PIPE_SEL_IVB(crtc->pipe));
9143 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009144 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009145}
9146
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009147static void
9148ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9149 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009150{
9151 struct drm_device *dev = crtc->base.dev;
9152 struct drm_i915_private *dev_priv = dev->dev_private;
9153 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009154 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009155 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009156 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009157 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009158 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009159
Damien Lespiau42a7b082015-02-05 19:35:13 +00009160 val = I915_READ(DSPCNTR(pipe));
9161 if (!(val & DISPLAY_PLANE_ENABLE))
9162 return;
9163
Damien Lespiaud9806c92015-01-21 14:07:19 +00009164 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009165 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009166 DRM_DEBUG_KMS("failed to alloc fb\n");
9167 return;
9168 }
9169
Damien Lespiau1b842c82015-01-21 13:50:54 +00009170 fb = &intel_fb->base;
9171
Daniel Vetter18c52472015-02-10 17:16:09 +00009172 if (INTEL_INFO(dev)->gen >= 4) {
9173 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00009174 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00009175 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9176 }
9177 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009178
9179 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00009180 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009181 fb->pixel_format = fourcc;
9182 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009183
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009184 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009185 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009186 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009187 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00009188 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009189 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009190 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009191 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009192 }
9193 plane_config->base = base;
9194
9195 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009196 fb->width = ((val >> 16) & 0xfff) + 1;
9197 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009198
9199 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009200 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009201
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009202 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009203 fb->pixel_format,
9204 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009205
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009206 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009207
Damien Lespiau2844a922015-01-20 12:51:48 +00009208 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9209 pipe_name(pipe), fb->width, fb->height,
9210 fb->bits_per_pixel, base, fb->pitches[0],
9211 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009212
Damien Lespiau2d140302015-02-05 17:22:18 +00009213 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009214}
9215
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009216static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009217 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009218{
9219 struct drm_device *dev = crtc->base.dev;
9220 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak17290502016-02-12 18:55:11 +02009221 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009222 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02009223 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009224
Imre Deak17290502016-02-12 18:55:11 +02009225 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9226 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03009227 return false;
9228
Daniel Vettere143a212013-07-04 12:01:15 +02009229 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009230 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02009231
Imre Deak17290502016-02-12 18:55:11 +02009232 ret = false;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009233 tmp = I915_READ(PIPECONF(crtc->pipe));
9234 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02009235 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009236
Ville Syrjälä42571ae2013-09-06 23:29:00 +03009237 switch (tmp & PIPECONF_BPC_MASK) {
9238 case PIPECONF_6BPC:
9239 pipe_config->pipe_bpp = 18;
9240 break;
9241 case PIPECONF_8BPC:
9242 pipe_config->pipe_bpp = 24;
9243 break;
9244 case PIPECONF_10BPC:
9245 pipe_config->pipe_bpp = 30;
9246 break;
9247 case PIPECONF_12BPC:
9248 pipe_config->pipe_bpp = 36;
9249 break;
9250 default:
9251 break;
9252 }
9253
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02009254 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9255 pipe_config->limited_color_range = true;
9256
Daniel Vetterab9412b2013-05-03 11:49:46 +02009257 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02009258 struct intel_shared_dpll *pll;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009259 enum intel_dpll_id pll_id;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009260
Daniel Vetter88adfff2013-03-28 10:42:01 +01009261 pipe_config->has_pch_encoder = true;
9262
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009263 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9264 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9265 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02009266
9267 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009268
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009269 if (HAS_PCH_IBX(dev_priv)) {
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009270 pll_id = (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009271 } else {
9272 tmp = I915_READ(PCH_DPLL_SEL);
9273 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009274 pll_id = DPLL_ID_PCH_PLL_B;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009275 else
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009276 pll_id= DPLL_ID_PCH_PLL_A;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009277 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02009278
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009279 pipe_config->shared_dpll =
9280 intel_get_shared_dpll_by_id(dev_priv, pll_id);
9281 pll = pipe_config->shared_dpll;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009282
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +02009283 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9284 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02009285
9286 tmp = pipe_config->dpll_hw_state.dpll;
9287 pipe_config->pixel_multiplier =
9288 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9289 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03009290
9291 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009292 } else {
9293 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009294 }
9295
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009296 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +02009297 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009298
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009299 ironlake_get_pfit_config(crtc, pipe_config);
9300
Imre Deak17290502016-02-12 18:55:11 +02009301 ret = true;
9302
9303out:
9304 intel_display_power_put(dev_priv, power_domain);
9305
9306 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009307}
9308
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009309static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9310{
9311 struct drm_device *dev = dev_priv->dev;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009312 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009313
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009314 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05009315 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009316 pipe_name(crtc->pipe));
9317
Rob Clarke2c719b2014-12-15 13:56:32 -05009318 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9319 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
Ville Syrjälä01403de2015-09-18 20:03:33 +03009320 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9321 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009322 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9323 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009324 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03009325 if (IS_HASWELL(dev))
Rob Clarke2c719b2014-12-15 13:56:32 -05009326 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03009327 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009328 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009329 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009330 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009331 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009332 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009333
Paulo Zanoni9926ada2014-04-01 19:39:47 -03009334 /*
9335 * In theory we can still leave IRQs enabled, as long as only the HPD
9336 * interrupts remain enabled. We used to check for that, but since it's
9337 * gen-specific and since we only disable LCPLL after we fully disable
9338 * the interrupts, the check below should be enough.
9339 */
Rob Clarke2c719b2014-12-15 13:56:32 -05009340 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009341}
9342
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009343static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9344{
9345 struct drm_device *dev = dev_priv->dev;
9346
9347 if (IS_HASWELL(dev))
9348 return I915_READ(D_COMP_HSW);
9349 else
9350 return I915_READ(D_COMP_BDW);
9351}
9352
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009353static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9354{
9355 struct drm_device *dev = dev_priv->dev;
9356
9357 if (IS_HASWELL(dev)) {
9358 mutex_lock(&dev_priv->rps.hw_lock);
9359 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9360 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03009361 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009362 mutex_unlock(&dev_priv->rps.hw_lock);
9363 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009364 I915_WRITE(D_COMP_BDW, val);
9365 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009366 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009367}
9368
9369/*
9370 * This function implements pieces of two sequences from BSpec:
9371 * - Sequence for display software to disable LCPLL
9372 * - Sequence for display software to allow package C8+
9373 * The steps implemented here are just the steps that actually touch the LCPLL
9374 * register. Callers should take care of disabling all the display engine
9375 * functions, doing the mode unset, fixing interrupts, etc.
9376 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009377static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9378 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009379{
9380 uint32_t val;
9381
9382 assert_can_disable_lcpll(dev_priv);
9383
9384 val = I915_READ(LCPLL_CTL);
9385
9386 if (switch_to_fclk) {
9387 val |= LCPLL_CD_SOURCE_FCLK;
9388 I915_WRITE(LCPLL_CTL, val);
9389
9390 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9391 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9392 DRM_ERROR("Switching to FCLK failed\n");
9393
9394 val = I915_READ(LCPLL_CTL);
9395 }
9396
9397 val |= LCPLL_PLL_DISABLE;
9398 I915_WRITE(LCPLL_CTL, val);
9399 POSTING_READ(LCPLL_CTL);
9400
9401 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9402 DRM_ERROR("LCPLL still locked\n");
9403
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009404 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009405 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009406 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009407 ndelay(100);
9408
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009409 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9410 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009411 DRM_ERROR("D_COMP RCOMP still in progress\n");
9412
9413 if (allow_power_down) {
9414 val = I915_READ(LCPLL_CTL);
9415 val |= LCPLL_POWER_DOWN_ALLOW;
9416 I915_WRITE(LCPLL_CTL, val);
9417 POSTING_READ(LCPLL_CTL);
9418 }
9419}
9420
9421/*
9422 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9423 * source.
9424 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009425static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009426{
9427 uint32_t val;
9428
9429 val = I915_READ(LCPLL_CTL);
9430
9431 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9432 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9433 return;
9434
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009435 /*
9436 * Make sure we're not on PC8 state before disabling PC8, otherwise
9437 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009438 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02009439 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03009440
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009441 if (val & LCPLL_POWER_DOWN_ALLOW) {
9442 val &= ~LCPLL_POWER_DOWN_ALLOW;
9443 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02009444 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009445 }
9446
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009447 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009448 val |= D_COMP_COMP_FORCE;
9449 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009450 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009451
9452 val = I915_READ(LCPLL_CTL);
9453 val &= ~LCPLL_PLL_DISABLE;
9454 I915_WRITE(LCPLL_CTL, val);
9455
9456 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9457 DRM_ERROR("LCPLL not locked yet\n");
9458
9459 if (val & LCPLL_CD_SOURCE_FCLK) {
9460 val = I915_READ(LCPLL_CTL);
9461 val &= ~LCPLL_CD_SOURCE_FCLK;
9462 I915_WRITE(LCPLL_CTL, val);
9463
9464 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9465 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9466 DRM_ERROR("Switching back to LCPLL failed\n");
9467 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03009468
Mika Kuoppala59bad942015-01-16 11:34:40 +02009469 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ville Syrjäläb6283052015-06-03 15:45:07 +03009470 intel_update_cdclk(dev_priv->dev);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009471}
9472
Paulo Zanoni765dab672014-03-07 20:08:18 -03009473/*
9474 * Package states C8 and deeper are really deep PC states that can only be
9475 * reached when all the devices on the system allow it, so even if the graphics
9476 * device allows PC8+, it doesn't mean the system will actually get to these
9477 * states. Our driver only allows PC8+ when going into runtime PM.
9478 *
9479 * The requirements for PC8+ are that all the outputs are disabled, the power
9480 * well is disabled and most interrupts are disabled, and these are also
9481 * requirements for runtime PM. When these conditions are met, we manually do
9482 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9483 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9484 * hang the machine.
9485 *
9486 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9487 * the state of some registers, so when we come back from PC8+ we need to
9488 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9489 * need to take care of the registers kept by RC6. Notice that this happens even
9490 * if we don't put the device in PCI D3 state (which is what currently happens
9491 * because of the runtime PM support).
9492 *
9493 * For more, read "Display Sequences for Package C8" on the hardware
9494 * documentation.
9495 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009496void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009497{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009498 struct drm_device *dev = dev_priv->dev;
9499 uint32_t val;
9500
Paulo Zanonic67a4702013-08-19 13:18:09 -03009501 DRM_DEBUG_KMS("Enabling package C8+\n");
9502
Ville Syrjäläc2699522015-08-27 23:55:59 +03009503 if (HAS_PCH_LPT_LP(dev)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009504 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9505 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9506 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9507 }
9508
9509 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009510 hsw_disable_lcpll(dev_priv, true, true);
9511}
9512
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009513void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009514{
9515 struct drm_device *dev = dev_priv->dev;
9516 uint32_t val;
9517
Paulo Zanonic67a4702013-08-19 13:18:09 -03009518 DRM_DEBUG_KMS("Disabling package C8+\n");
9519
9520 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009521 lpt_init_pch_refclk(dev);
9522
Ville Syrjäläc2699522015-08-27 23:55:59 +03009523 if (HAS_PCH_LPT_LP(dev)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009524 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9525 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9526 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9527 }
Paulo Zanonic67a4702013-08-19 13:18:09 -03009528}
9529
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009530static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309531{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009532 struct drm_device *dev = old_state->dev;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009533 struct intel_atomic_state *old_intel_state =
9534 to_intel_atomic_state(old_state);
9535 unsigned int req_cdclk = old_intel_state->dev_cdclk;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309536
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009537 broxton_set_cdclk(dev, req_cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309538}
9539
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009540/* compute the max rate for new configuration */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009541static int ilk_max_pixel_rate(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009542{
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009543 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
9544 struct drm_i915_private *dev_priv = state->dev->dev_private;
9545 struct drm_crtc *crtc;
9546 struct drm_crtc_state *cstate;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009547 struct intel_crtc_state *crtc_state;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009548 unsigned max_pixel_rate = 0, i;
9549 enum pipe pipe;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009550
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009551 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
9552 sizeof(intel_state->min_pixclk));
9553
9554 for_each_crtc_in_state(state, crtc, cstate, i) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009555 int pixel_rate;
9556
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009557 crtc_state = to_intel_crtc_state(cstate);
9558 if (!crtc_state->base.enable) {
9559 intel_state->min_pixclk[i] = 0;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009560 continue;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009561 }
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009562
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009563 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009564
9565 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009566 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009567 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9568
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009569 intel_state->min_pixclk[i] = pixel_rate;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009570 }
9571
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009572 for_each_pipe(dev_priv, pipe)
9573 max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
9574
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009575 return max_pixel_rate;
9576}
9577
9578static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9579{
9580 struct drm_i915_private *dev_priv = dev->dev_private;
9581 uint32_t val, data;
9582 int ret;
9583
9584 if (WARN((I915_READ(LCPLL_CTL) &
9585 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9586 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9587 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9588 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9589 "trying to change cdclk frequency with cdclk not enabled\n"))
9590 return;
9591
9592 mutex_lock(&dev_priv->rps.hw_lock);
9593 ret = sandybridge_pcode_write(dev_priv,
9594 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9595 mutex_unlock(&dev_priv->rps.hw_lock);
9596 if (ret) {
9597 DRM_ERROR("failed to inform pcode about cdclk change\n");
9598 return;
9599 }
9600
9601 val = I915_READ(LCPLL_CTL);
9602 val |= LCPLL_CD_SOURCE_FCLK;
9603 I915_WRITE(LCPLL_CTL, val);
9604
Tvrtko Ursulin5ba00172016-03-03 14:36:45 +00009605 if (wait_for_us(I915_READ(LCPLL_CTL) &
9606 LCPLL_CD_SOURCE_FCLK_DONE, 1))
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009607 DRM_ERROR("Switching to FCLK failed\n");
9608
9609 val = I915_READ(LCPLL_CTL);
9610 val &= ~LCPLL_CLK_FREQ_MASK;
9611
9612 switch (cdclk) {
9613 case 450000:
9614 val |= LCPLL_CLK_FREQ_450;
9615 data = 0;
9616 break;
9617 case 540000:
9618 val |= LCPLL_CLK_FREQ_54O_BDW;
9619 data = 1;
9620 break;
9621 case 337500:
9622 val |= LCPLL_CLK_FREQ_337_5_BDW;
9623 data = 2;
9624 break;
9625 case 675000:
9626 val |= LCPLL_CLK_FREQ_675_BDW;
9627 data = 3;
9628 break;
9629 default:
9630 WARN(1, "invalid cdclk frequency\n");
9631 return;
9632 }
9633
9634 I915_WRITE(LCPLL_CTL, val);
9635
9636 val = I915_READ(LCPLL_CTL);
9637 val &= ~LCPLL_CD_SOURCE_FCLK;
9638 I915_WRITE(LCPLL_CTL, val);
9639
Tvrtko Ursulin5ba00172016-03-03 14:36:45 +00009640 if (wait_for_us((I915_READ(LCPLL_CTL) &
9641 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009642 DRM_ERROR("Switching back to LCPLL failed\n");
9643
9644 mutex_lock(&dev_priv->rps.hw_lock);
9645 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9646 mutex_unlock(&dev_priv->rps.hw_lock);
9647
9648 intel_update_cdclk(dev);
9649
9650 WARN(cdclk != dev_priv->cdclk_freq,
9651 "cdclk requested %d kHz but got %d kHz\n",
9652 cdclk, dev_priv->cdclk_freq);
9653}
9654
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009655static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009656{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009657 struct drm_i915_private *dev_priv = to_i915(state->dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009658 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009659 int max_pixclk = ilk_max_pixel_rate(state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009660 int cdclk;
9661
9662 /*
9663 * FIXME should also account for plane ratio
9664 * once 64bpp pixel formats are supported.
9665 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009666 if (max_pixclk > 540000)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009667 cdclk = 675000;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009668 else if (max_pixclk > 450000)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009669 cdclk = 540000;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009670 else if (max_pixclk > 337500)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009671 cdclk = 450000;
9672 else
9673 cdclk = 337500;
9674
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009675 if (cdclk > dev_priv->max_cdclk_freq) {
Maarten Lankhorst63ba5342015-11-24 11:29:03 +01009676 DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9677 cdclk, dev_priv->max_cdclk_freq);
9678 return -EINVAL;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009679 }
9680
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009681 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
9682 if (!intel_state->active_crtcs)
9683 intel_state->dev_cdclk = 337500;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009684
9685 return 0;
9686}
9687
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009688static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009689{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009690 struct drm_device *dev = old_state->dev;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009691 struct intel_atomic_state *old_intel_state =
9692 to_intel_atomic_state(old_state);
9693 unsigned req_cdclk = old_intel_state->dev_cdclk;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009694
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009695 broadwell_set_cdclk(dev, req_cdclk);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009696}
9697
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009698static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9699 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009700{
Mika Kaholaaf3997b2016-02-05 13:29:28 +02009701 struct intel_encoder *intel_encoder =
9702 intel_ddi_get_crtc_new_encoder(crtc_state);
9703
9704 if (intel_encoder->type != INTEL_OUTPUT_DSI) {
9705 if (!intel_ddi_pll_select(crtc, crtc_state))
9706 return -EINVAL;
9707 }
Daniel Vetter716c2e52014-06-25 22:02:02 +03009708
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009709 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02009710
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009711 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009712}
9713
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309714static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9715 enum port port,
9716 struct intel_crtc_state *pipe_config)
9717{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009718 enum intel_dpll_id id;
9719
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309720 switch (port) {
9721 case PORT_A:
9722 pipe_config->ddi_pll_sel = SKL_DPLL0;
Imre Deak08250c42016-03-14 19:55:34 +02009723 id = DPLL_ID_SKL_DPLL0;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309724 break;
9725 case PORT_B:
9726 pipe_config->ddi_pll_sel = SKL_DPLL1;
Imre Deak08250c42016-03-14 19:55:34 +02009727 id = DPLL_ID_SKL_DPLL1;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309728 break;
9729 case PORT_C:
9730 pipe_config->ddi_pll_sel = SKL_DPLL2;
Imre Deak08250c42016-03-14 19:55:34 +02009731 id = DPLL_ID_SKL_DPLL2;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309732 break;
9733 default:
9734 DRM_ERROR("Incorrect port type\n");
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009735 return;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309736 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009737
9738 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309739}
9740
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009741static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9742 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009743 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009744{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009745 enum intel_dpll_id id;
Ander Conselvan de Oliveiraa3c988e2016-03-08 17:46:27 +02009746 u32 temp;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009747
9748 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9749 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9750
9751 switch (pipe_config->ddi_pll_sel) {
Damien Lespiau3148ade2014-11-21 16:14:56 +00009752 case SKL_DPLL0:
Ander Conselvan de Oliveiraa3c988e2016-03-08 17:46:27 +02009753 id = DPLL_ID_SKL_DPLL0;
9754 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009755 case SKL_DPLL1:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009756 id = DPLL_ID_SKL_DPLL1;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009757 break;
9758 case SKL_DPLL2:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009759 id = DPLL_ID_SKL_DPLL2;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009760 break;
9761 case SKL_DPLL3:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009762 id = DPLL_ID_SKL_DPLL3;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009763 break;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009764 default:
9765 MISSING_CASE(pipe_config->ddi_pll_sel);
9766 return;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009767 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009768
9769 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009770}
9771
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009772static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9773 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009774 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009775{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009776 enum intel_dpll_id id;
9777
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009778 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9779
9780 switch (pipe_config->ddi_pll_sel) {
9781 case PORT_CLK_SEL_WRPLL1:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009782 id = DPLL_ID_WRPLL1;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009783 break;
9784 case PORT_CLK_SEL_WRPLL2:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009785 id = DPLL_ID_WRPLL2;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009786 break;
Maarten Lankhorst00490c22015-11-16 14:42:12 +01009787 case PORT_CLK_SEL_SPLL:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009788 id = DPLL_ID_SPLL;
Ville Syrjälä79bd23d2015-12-01 23:32:07 +02009789 break;
Ander Conselvan de Oliveira9d16da62016-03-08 17:46:26 +02009790 case PORT_CLK_SEL_LCPLL_810:
9791 id = DPLL_ID_LCPLL_810;
9792 break;
9793 case PORT_CLK_SEL_LCPLL_1350:
9794 id = DPLL_ID_LCPLL_1350;
9795 break;
9796 case PORT_CLK_SEL_LCPLL_2700:
9797 id = DPLL_ID_LCPLL_2700;
9798 break;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009799 default:
9800 MISSING_CASE(pipe_config->ddi_pll_sel);
9801 /* fall through */
9802 case PORT_CLK_SEL_NONE:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009803 return;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009804 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009805
9806 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009807}
9808
Jani Nikulacf304292016-03-18 17:05:41 +02009809static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
9810 struct intel_crtc_state *pipe_config,
9811 unsigned long *power_domain_mask)
9812{
9813 struct drm_device *dev = crtc->base.dev;
9814 struct drm_i915_private *dev_priv = dev->dev_private;
9815 enum intel_display_power_domain power_domain;
9816 u32 tmp;
9817
9818 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
9819
9820 /*
9821 * XXX: Do intel_display_power_get_if_enabled before reading this (for
9822 * consistency and less surprising code; it's in always on power).
9823 */
9824 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9825 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9826 enum pipe trans_edp_pipe;
9827 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9828 default:
9829 WARN(1, "unknown pipe linked to edp transcoder\n");
9830 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9831 case TRANS_DDI_EDP_INPUT_A_ON:
9832 trans_edp_pipe = PIPE_A;
9833 break;
9834 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9835 trans_edp_pipe = PIPE_B;
9836 break;
9837 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9838 trans_edp_pipe = PIPE_C;
9839 break;
9840 }
9841
9842 if (trans_edp_pipe == crtc->pipe)
9843 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9844 }
9845
9846 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
9847 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9848 return false;
9849 *power_domain_mask |= BIT(power_domain);
9850
9851 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
9852
9853 return tmp & PIPECONF_ENABLE;
9854}
9855
Jani Nikula4d1de972016-03-18 17:05:42 +02009856static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
9857 struct intel_crtc_state *pipe_config,
9858 unsigned long *power_domain_mask)
9859{
9860 struct drm_device *dev = crtc->base.dev;
9861 struct drm_i915_private *dev_priv = dev->dev_private;
9862 enum intel_display_power_domain power_domain;
9863 enum port port;
9864 enum transcoder cpu_transcoder;
9865 u32 tmp;
9866
9867 pipe_config->has_dsi_encoder = false;
9868
9869 for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
9870 if (port == PORT_A)
9871 cpu_transcoder = TRANSCODER_DSI_A;
9872 else
9873 cpu_transcoder = TRANSCODER_DSI_C;
9874
9875 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
9876 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9877 continue;
9878 *power_domain_mask |= BIT(power_domain);
9879
Imre Deakdb18b6a2016-03-24 12:41:40 +02009880 /*
9881 * The PLL needs to be enabled with a valid divider
9882 * configuration, otherwise accessing DSI registers will hang
9883 * the machine. See BSpec North Display Engine
9884 * registers/MIPI[BXT]. We can break out here early, since we
9885 * need the same DSI PLL to be enabled for both DSI ports.
9886 */
9887 if (!intel_dsi_pll_is_enabled(dev_priv))
9888 break;
9889
Jani Nikula4d1de972016-03-18 17:05:42 +02009890 /* XXX: this works for video mode only */
9891 tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
9892 if (!(tmp & DPI_ENABLE))
9893 continue;
9894
9895 tmp = I915_READ(MIPI_CTRL(port));
9896 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
9897 continue;
9898
9899 pipe_config->cpu_transcoder = cpu_transcoder;
9900 pipe_config->has_dsi_encoder = true;
9901 break;
9902 }
9903
9904 return pipe_config->has_dsi_encoder;
9905}
9906
Daniel Vetter26804af2014-06-25 22:01:55 +03009907static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009908 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +03009909{
9910 struct drm_device *dev = crtc->base.dev;
9911 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009912 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +03009913 enum port port;
9914 uint32_t tmp;
9915
9916 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9917
9918 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9919
Rodrigo Vivief11bdb2015-10-28 04:16:45 -07009920 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009921 skylake_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309922 else if (IS_BROXTON(dev))
9923 bxt_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009924 else
9925 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +03009926
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009927 pll = pipe_config->shared_dpll;
9928 if (pll) {
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +02009929 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9930 &pipe_config->dpll_hw_state));
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009931 }
9932
Daniel Vetter26804af2014-06-25 22:01:55 +03009933 /*
9934 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9935 * DDI E. So just check whether this pipe is wired to DDI E and whether
9936 * the PCH transcoder is on.
9937 */
Damien Lespiauca370452013-12-03 13:56:24 +00009938 if (INTEL_INFO(dev)->gen < 9 &&
9939 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +03009940 pipe_config->has_pch_encoder = true;
9941
9942 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9943 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9944 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9945
9946 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9947 }
9948}
9949
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009950static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009951 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009952{
9953 struct drm_device *dev = crtc->base.dev;
9954 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak17290502016-02-12 18:55:11 +02009955 enum intel_display_power_domain power_domain;
9956 unsigned long power_domain_mask;
Jani Nikulacf304292016-03-18 17:05:41 +02009957 bool active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009958
Imre Deak17290502016-02-12 18:55:11 +02009959 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9960 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +02009961 return false;
Imre Deak17290502016-02-12 18:55:11 +02009962 power_domain_mask = BIT(power_domain);
9963
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009964 pipe_config->shared_dpll = NULL;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009965
Jani Nikulacf304292016-03-18 17:05:41 +02009966 active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
Daniel Vettereccb1402013-05-22 00:50:22 +02009967
Jani Nikula4d1de972016-03-18 17:05:42 +02009968 if (IS_BROXTON(dev_priv)) {
9969 bxt_get_dsi_transcoder_state(crtc, pipe_config,
9970 &power_domain_mask);
9971 WARN_ON(active && pipe_config->has_dsi_encoder);
9972 if (pipe_config->has_dsi_encoder)
9973 active = true;
9974 }
9975
Jani Nikulacf304292016-03-18 17:05:41 +02009976 if (!active)
Imre Deak17290502016-02-12 18:55:11 +02009977 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009978
Jani Nikula4d1de972016-03-18 17:05:42 +02009979 if (!pipe_config->has_dsi_encoder) {
9980 haswell_get_ddi_port_state(crtc, pipe_config);
9981 intel_get_pipe_timings(crtc, pipe_config);
9982 }
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009983
Jani Nikulabc58be62016-03-18 17:05:39 +02009984 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009985
Lionel Landwerlin05dc6982016-03-16 10:57:15 +00009986 pipe_config->gamma_mode =
9987 I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
9988
Chandra Kondurua1b22782015-04-07 15:28:45 -07009989 if (INTEL_INFO(dev)->gen >= 9) {
9990 skl_init_scalers(dev, crtc, pipe_config);
9991 }
9992
Chandra Konduruaf99ceda2015-05-11 14:35:47 -07009993 if (INTEL_INFO(dev)->gen >= 9) {
9994 pipe_config->scaler_state.scaler_id = -1;
9995 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
9996 }
9997
Imre Deak17290502016-02-12 18:55:11 +02009998 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
9999 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
10000 power_domain_mask |= BIT(power_domain);
Rodrigo Vivi1c132b42015-09-02 15:19:26 -070010001 if (INTEL_INFO(dev)->gen >= 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +000010002 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -080010003 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -070010004 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesbd2e2442014-11-13 17:51:47 +000010005 }
Daniel Vetter88adfff2013-03-28 10:42:01 +010010006
Jesse Barnese59150d2014-01-07 13:30:45 -080010007 if (IS_HASWELL(dev))
10008 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
10009 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030010010
Jani Nikula4d1de972016-03-18 17:05:42 +020010011 if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
10012 !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
Clint Taylorebb69c92014-09-30 10:30:22 -070010013 pipe_config->pixel_multiplier =
10014 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
10015 } else {
10016 pipe_config->pixel_multiplier = 1;
10017 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020010018
Imre Deak17290502016-02-12 18:55:11 +020010019out:
10020 for_each_power_domain(power_domain, power_domain_mask)
10021 intel_display_power_put(dev_priv, power_domain);
10022
Jani Nikulacf304292016-03-18 17:05:41 +020010023 return active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010024}
10025
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010026static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
10027 const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +010010028{
10029 struct drm_device *dev = crtc->dev;
10030 struct drm_i915_private *dev_priv = dev->dev_private;
10031 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +030010032 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +010010033
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010034 if (plane_state && plane_state->visible) {
10035 unsigned int width = plane_state->base.crtc_w;
10036 unsigned int height = plane_state->base.crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +030010037 unsigned int stride = roundup_pow_of_two(width) * 4;
10038
10039 switch (stride) {
10040 default:
10041 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
10042 width, stride);
10043 stride = 256;
10044 /* fallthrough */
10045 case 256:
10046 case 512:
10047 case 1024:
10048 case 2048:
10049 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010050 }
10051
Ville Syrjälädc41c152014-08-13 11:57:05 +030010052 cntl |= CURSOR_ENABLE |
10053 CURSOR_GAMMA_ENABLE |
10054 CURSOR_FORMAT_ARGB |
10055 CURSOR_STRIDE(stride);
10056
10057 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010058 }
Chris Wilson560b85b2010-08-07 11:01:38 +010010059
Ville Syrjälädc41c152014-08-13 11:57:05 +030010060 if (intel_crtc->cursor_cntl != 0 &&
10061 (intel_crtc->cursor_base != base ||
10062 intel_crtc->cursor_size != size ||
10063 intel_crtc->cursor_cntl != cntl)) {
10064 /* On these chipsets we can only modify the base/size/stride
10065 * whilst the cursor is disabled.
10066 */
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010067 I915_WRITE(CURCNTR(PIPE_A), 0);
10068 POSTING_READ(CURCNTR(PIPE_A));
Ville Syrjälädc41c152014-08-13 11:57:05 +030010069 intel_crtc->cursor_cntl = 0;
10070 }
10071
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010072 if (intel_crtc->cursor_base != base) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010073 I915_WRITE(CURBASE(PIPE_A), base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010074 intel_crtc->cursor_base = base;
10075 }
Ville Syrjälädc41c152014-08-13 11:57:05 +030010076
10077 if (intel_crtc->cursor_size != size) {
10078 I915_WRITE(CURSIZE, size);
10079 intel_crtc->cursor_size = size;
10080 }
10081
Chris Wilson4b0e3332014-05-30 16:35:26 +030010082 if (intel_crtc->cursor_cntl != cntl) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010083 I915_WRITE(CURCNTR(PIPE_A), cntl);
10084 POSTING_READ(CURCNTR(PIPE_A));
Chris Wilson4b0e3332014-05-30 16:35:26 +030010085 intel_crtc->cursor_cntl = cntl;
10086 }
Chris Wilson560b85b2010-08-07 11:01:38 +010010087}
10088
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010089static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
10090 const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +010010091{
10092 struct drm_device *dev = crtc->dev;
10093 struct drm_i915_private *dev_priv = dev->dev_private;
10094 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10095 int pipe = intel_crtc->pipe;
Ville Syrjälä663f3122015-12-14 13:16:48 +020010096 uint32_t cntl = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +010010097
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010098 if (plane_state && plane_state->visible) {
Chris Wilson4b0e3332014-05-30 16:35:26 +030010099 cntl = MCURSOR_GAMMA_ENABLE;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010100 switch (plane_state->base.crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010101 case 64:
10102 cntl |= CURSOR_MODE_64_ARGB_AX;
10103 break;
10104 case 128:
10105 cntl |= CURSOR_MODE_128_ARGB_AX;
10106 break;
10107 case 256:
10108 cntl |= CURSOR_MODE_256_ARGB_AX;
10109 break;
10110 default:
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010111 MISSING_CASE(plane_state->base.crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010112 return;
Chris Wilson560b85b2010-08-07 11:01:38 +010010113 }
Chris Wilson4b0e3332014-05-30 16:35:26 +030010114 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010115
Bob Paauwefc6f93b2015-08-31 14:03:30 -070010116 if (HAS_DDI(dev))
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010117 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010118
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010119 if (plane_state->base.rotation == BIT(DRM_ROTATE_180))
10120 cntl |= CURSOR_ROTATE_180;
10121 }
Ville Syrjälä4398ad42014-10-23 07:41:34 -070010122
Chris Wilson4b0e3332014-05-30 16:35:26 +030010123 if (intel_crtc->cursor_cntl != cntl) {
10124 I915_WRITE(CURCNTR(pipe), cntl);
10125 POSTING_READ(CURCNTR(pipe));
10126 intel_crtc->cursor_cntl = cntl;
10127 }
10128
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010129 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010130 I915_WRITE(CURBASE(pipe), base);
10131 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010132
10133 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010134}
10135
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010136/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +010010137static void intel_crtc_update_cursor(struct drm_crtc *crtc,
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010138 const struct intel_plane_state *plane_state)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010139{
10140 struct drm_device *dev = crtc->dev;
10141 struct drm_i915_private *dev_priv = dev->dev_private;
10142 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10143 int pipe = intel_crtc->pipe;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010144 u32 base = intel_crtc->cursor_addr;
10145 u32 pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010146
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010147 if (plane_state) {
10148 int x = plane_state->base.crtc_x;
10149 int y = plane_state->base.crtc_y;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010150
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010151 if (x < 0) {
10152 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10153 x = -x;
10154 }
10155 pos |= x << CURSOR_X_SHIFT;
10156
10157 if (y < 0) {
10158 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10159 y = -y;
10160 }
10161 pos |= y << CURSOR_Y_SHIFT;
10162
10163 /* ILK+ do this automagically */
10164 if (HAS_GMCH_DISPLAY(dev) &&
10165 plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
10166 base += (plane_state->base.crtc_h *
10167 plane_state->base.crtc_w - 1) * 4;
10168 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010169 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010170
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010171 I915_WRITE(CURPOS(pipe), pos);
10172
Ville Syrjälä8ac54662014-08-12 19:39:54 +030010173 if (IS_845G(dev) || IS_I865G(dev))
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010174 i845_update_cursor(crtc, base, plane_state);
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010175 else
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010176 i9xx_update_cursor(crtc, base, plane_state);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010177}
10178
Ville Syrjälädc41c152014-08-13 11:57:05 +030010179static bool cursor_size_ok(struct drm_device *dev,
10180 uint32_t width, uint32_t height)
10181{
10182 if (width == 0 || height == 0)
10183 return false;
10184
10185 /*
10186 * 845g/865g are special in that they are only limited by
10187 * the width of their cursors, the height is arbitrary up to
10188 * the precision of the register. Everything else requires
10189 * square cursors, limited to a few power-of-two sizes.
10190 */
10191 if (IS_845G(dev) || IS_I865G(dev)) {
10192 if ((width & 63) != 0)
10193 return false;
10194
10195 if (width > (IS_845G(dev) ? 64 : 512))
10196 return false;
10197
10198 if (height > 1023)
10199 return false;
10200 } else {
10201 switch (width | height) {
10202 case 256:
10203 case 128:
10204 if (IS_GEN2(dev))
10205 return false;
10206 case 64:
10207 break;
10208 default:
10209 return false;
10210 }
10211 }
10212
10213 return true;
10214}
10215
Jesse Barnes79e53942008-11-07 14:24:08 -080010216/* VESA 640x480x72Hz mode to set on the pipe */
10217static struct drm_display_mode load_detect_mode = {
10218 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10219 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10220};
10221
Daniel Vettera8bb6812014-02-10 18:00:39 +010010222struct drm_framebuffer *
10223__intel_framebuffer_create(struct drm_device *dev,
10224 struct drm_mode_fb_cmd2 *mode_cmd,
10225 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +010010226{
10227 struct intel_framebuffer *intel_fb;
10228 int ret;
10229
10230 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010231 if (!intel_fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010232 return ERR_PTR(-ENOMEM);
Chris Wilsond2dff872011-04-19 08:36:26 +010010233
10234 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010235 if (ret)
10236 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +010010237
10238 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010239
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010240err:
10241 kfree(intel_fb);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010242 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +010010243}
10244
Daniel Vetterb5ea6422014-03-02 21:18:00 +010010245static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +010010246intel_framebuffer_create(struct drm_device *dev,
10247 struct drm_mode_fb_cmd2 *mode_cmd,
10248 struct drm_i915_gem_object *obj)
10249{
10250 struct drm_framebuffer *fb;
10251 int ret;
10252
10253 ret = i915_mutex_lock_interruptible(dev);
10254 if (ret)
10255 return ERR_PTR(ret);
10256 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10257 mutex_unlock(&dev->struct_mutex);
10258
10259 return fb;
10260}
10261
Chris Wilsond2dff872011-04-19 08:36:26 +010010262static u32
10263intel_framebuffer_pitch_for_width(int width, int bpp)
10264{
10265 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10266 return ALIGN(pitch, 64);
10267}
10268
10269static u32
10270intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10271{
10272 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +020010273 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +010010274}
10275
10276static struct drm_framebuffer *
10277intel_framebuffer_create_for_mode(struct drm_device *dev,
10278 struct drm_display_mode *mode,
10279 int depth, int bpp)
10280{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010281 struct drm_framebuffer *fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010282 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +000010283 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +010010284
10285 obj = i915_gem_alloc_object(dev,
10286 intel_framebuffer_size_for_mode(mode, bpp));
10287 if (obj == NULL)
10288 return ERR_PTR(-ENOMEM);
10289
10290 mode_cmd.width = mode->hdisplay;
10291 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010292 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10293 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +000010294 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +010010295
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010296 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
10297 if (IS_ERR(fb))
10298 drm_gem_object_unreference_unlocked(&obj->base);
10299
10300 return fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010301}
10302
10303static struct drm_framebuffer *
10304mode_fits_in_fbdev(struct drm_device *dev,
10305 struct drm_display_mode *mode)
10306{
Daniel Vetter06957262015-08-10 13:34:08 +020010307#ifdef CONFIG_DRM_FBDEV_EMULATION
Chris Wilsond2dff872011-04-19 08:36:26 +010010308 struct drm_i915_private *dev_priv = dev->dev_private;
10309 struct drm_i915_gem_object *obj;
10310 struct drm_framebuffer *fb;
10311
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010312 if (!dev_priv->fbdev)
10313 return NULL;
10314
10315 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010316 return NULL;
10317
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010318 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010319 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +010010320
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010321 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010322 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10323 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +010010324 return NULL;
10325
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010326 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +010010327 return NULL;
10328
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010329 drm_framebuffer_reference(fb);
Chris Wilsond2dff872011-04-19 08:36:26 +010010330 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +020010331#else
10332 return NULL;
10333#endif
Chris Wilsond2dff872011-04-19 08:36:26 +010010334}
10335
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010336static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10337 struct drm_crtc *crtc,
10338 struct drm_display_mode *mode,
10339 struct drm_framebuffer *fb,
10340 int x, int y)
10341{
10342 struct drm_plane_state *plane_state;
10343 int hdisplay, vdisplay;
10344 int ret;
10345
10346 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10347 if (IS_ERR(plane_state))
10348 return PTR_ERR(plane_state);
10349
10350 if (mode)
10351 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10352 else
10353 hdisplay = vdisplay = 0;
10354
10355 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10356 if (ret)
10357 return ret;
10358 drm_atomic_set_fb_for_plane(plane_state, fb);
10359 plane_state->crtc_x = 0;
10360 plane_state->crtc_y = 0;
10361 plane_state->crtc_w = hdisplay;
10362 plane_state->crtc_h = vdisplay;
10363 plane_state->src_x = x << 16;
10364 plane_state->src_y = y << 16;
10365 plane_state->src_w = hdisplay << 16;
10366 plane_state->src_h = vdisplay << 16;
10367
10368 return 0;
10369}
10370
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010371bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +010010372 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -050010373 struct intel_load_detect_pipe *old,
10374 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010375{
10376 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010377 struct intel_encoder *intel_encoder =
10378 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -080010379 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +010010380 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -080010381 struct drm_crtc *crtc = NULL;
10382 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +020010383 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -050010384 struct drm_mode_config *config = &dev->mode_config;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010385 struct drm_atomic_state *state = NULL, *restore_state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010386 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010387 struct intel_crtc_state *crtc_state;
Rob Clark51fd3712013-11-19 12:10:12 -050010388 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010389
Chris Wilsond2dff872011-04-19 08:36:26 +010010390 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010391 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010392 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010393
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010394 old->restore_state = NULL;
10395
Rob Clark51fd3712013-11-19 12:10:12 -050010396retry:
10397 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10398 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010399 goto fail;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020010400
Jesse Barnes79e53942008-11-07 14:24:08 -080010401 /*
10402 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +010010403 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010404 * - if the connector already has an assigned crtc, use it (but make
10405 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +010010406 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010407 * - try to find the first unused crtc that can drive this connector,
10408 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -080010409 */
10410
10411 /* See if we already have a CRTC for this connector */
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010412 if (connector->state->crtc) {
10413 crtc = connector->state->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +010010414
Rob Clark51fd3712013-11-19 12:10:12 -050010415 ret = drm_modeset_lock(&crtc->mutex, ctx);
10416 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010417 goto fail;
Chris Wilson8261b192011-04-19 23:18:09 +010010418
10419 /* Make sure the crtc and connector are running */
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010420 goto found;
Jesse Barnes79e53942008-11-07 14:24:08 -080010421 }
10422
10423 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010424 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010425 i++;
10426 if (!(encoder->possible_crtcs & (1 << i)))
10427 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010428
10429 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
10430 if (ret)
10431 goto fail;
10432
10433 if (possible_crtc->state->enable) {
10434 drm_modeset_unlock(&possible_crtc->mutex);
Ville Syrjäläa4592492014-08-11 13:15:36 +030010435 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010436 }
Ville Syrjäläa4592492014-08-11 13:15:36 +030010437
10438 crtc = possible_crtc;
10439 break;
Jesse Barnes79e53942008-11-07 14:24:08 -080010440 }
10441
10442 /*
10443 * If we didn't find an unused CRTC, don't use any.
10444 */
10445 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +010010446 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010447 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010448 }
10449
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010450found:
10451 intel_crtc = to_intel_crtc(crtc);
10452
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010453 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10454 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010455 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010456
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010457 state = drm_atomic_state_alloc(dev);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010458 restore_state = drm_atomic_state_alloc(dev);
10459 if (!state || !restore_state) {
10460 ret = -ENOMEM;
10461 goto fail;
10462 }
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010463
10464 state->acquire_ctx = ctx;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010465 restore_state->acquire_ctx = ctx;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010466
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010467 connector_state = drm_atomic_get_connector_state(state, connector);
10468 if (IS_ERR(connector_state)) {
10469 ret = PTR_ERR(connector_state);
10470 goto fail;
10471 }
10472
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010473 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
10474 if (ret)
10475 goto fail;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010476
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010477 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10478 if (IS_ERR(crtc_state)) {
10479 ret = PTR_ERR(crtc_state);
10480 goto fail;
10481 }
10482
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010483 crtc_state->base.active = crtc_state->base.enable = true;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010484
Chris Wilson64927112011-04-20 07:25:26 +010010485 if (!mode)
10486 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -080010487
Chris Wilsond2dff872011-04-19 08:36:26 +010010488 /* We need a framebuffer large enough to accommodate all accesses
10489 * that the plane may generate whilst we perform load detection.
10490 * We can not rely on the fbcon either being present (we get called
10491 * during its initialisation to detect all boot displays, or it may
10492 * not even exist) or that it is large enough to satisfy the
10493 * requested mode.
10494 */
Daniel Vetter94352cf2012-07-05 22:51:56 +020010495 fb = mode_fits_in_fbdev(dev, mode);
10496 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010497 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010498 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
Chris Wilsond2dff872011-04-19 08:36:26 +010010499 } else
10500 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010501 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010502 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010503 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010504 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010505
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010506 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10507 if (ret)
10508 goto fail;
10509
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010510 drm_framebuffer_unreference(fb);
10511
10512 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
10513 if (ret)
10514 goto fail;
10515
10516 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
10517 if (!ret)
10518 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
10519 if (!ret)
10520 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
10521 if (ret) {
10522 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
10523 goto fail;
10524 }
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030010525
Maarten Lankhorst3ba86072016-02-29 09:18:57 +010010526 ret = drm_atomic_commit(state);
10527 if (ret) {
Chris Wilson64927112011-04-20 07:25:26 +010010528 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010529 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010530 }
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010531
10532 old->restore_state = restore_state;
Chris Wilson71731882011-04-19 23:10:58 +010010533
Jesse Barnes79e53942008-11-07 14:24:08 -080010534 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010535 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +010010536 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010537
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010538fail:
Ander Conselvan de Oliveirae5d958e2015-04-21 17:12:57 +030010539 drm_atomic_state_free(state);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010540 drm_atomic_state_free(restore_state);
10541 restore_state = state = NULL;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010542
Rob Clark51fd3712013-11-19 12:10:12 -050010543 if (ret == -EDEADLK) {
10544 drm_modeset_backoff(ctx);
10545 goto retry;
10546 }
10547
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010548 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -080010549}
10550
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010551void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020010552 struct intel_load_detect_pipe *old,
10553 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010554{
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010555 struct intel_encoder *intel_encoder =
10556 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +010010557 struct drm_encoder *encoder = &intel_encoder->base;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010558 struct drm_atomic_state *state = old->restore_state;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010559 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080010560
Chris Wilsond2dff872011-04-19 08:36:26 +010010561 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010562 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010563 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010564
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010565 if (!state)
Chris Wilson0622a532011-04-21 09:32:11 +010010566 return;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010567
10568 ret = drm_atomic_commit(state);
10569 if (ret) {
10570 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
10571 drm_atomic_state_free(state);
Jesse Barnes79e53942008-11-07 14:24:08 -080010572 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010573}
10574
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010575static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010576 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010577{
10578 struct drm_i915_private *dev_priv = dev->dev_private;
10579 u32 dpll = pipe_config->dpll_hw_state.dpll;
10580
10581 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +020010582 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010583 else if (HAS_PCH_SPLIT(dev))
10584 return 120000;
10585 else if (!IS_GEN2(dev))
10586 return 96000;
10587 else
10588 return 48000;
10589}
10590
Jesse Barnes79e53942008-11-07 14:24:08 -080010591/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010592static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010593 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -080010594{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010595 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080010596 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010597 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010598 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -080010599 u32 fp;
10600 intel_clock_t clock;
Imre Deakdccbea32015-06-22 23:35:51 +030010601 int port_clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010602 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -080010603
10604 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +030010605 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010606 else
Ville Syrjälä293623f2013-09-13 16:18:46 +030010607 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010608
10609 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010610 if (IS_PINEVIEW(dev)) {
10611 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10612 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +080010613 } else {
10614 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10615 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10616 }
10617
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010618 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010619 if (IS_PINEVIEW(dev))
10620 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10621 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +080010622 else
10623 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -080010624 DPLL_FPA01_P1_POST_DIV_SHIFT);
10625
10626 switch (dpll & DPLL_MODE_MASK) {
10627 case DPLLB_MODE_DAC_SERIAL:
10628 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10629 5 : 10;
10630 break;
10631 case DPLLB_MODE_LVDS:
10632 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10633 7 : 14;
10634 break;
10635 default:
Zhao Yakui28c97732009-10-09 11:39:41 +080010636 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -080010637 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010638 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010639 }
10640
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010641 if (IS_PINEVIEW(dev))
Imre Deakdccbea32015-06-22 23:35:51 +030010642 port_clock = pnv_calc_dpll_params(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010643 else
Imre Deakdccbea32015-06-22 23:35:51 +030010644 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010645 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +020010646 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010647 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -080010648
10649 if (is_lvds) {
10650 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10651 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010652
10653 if (lvds & LVDS_CLKB_POWER_UP)
10654 clock.p2 = 7;
10655 else
10656 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -080010657 } else {
10658 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10659 clock.p1 = 2;
10660 else {
10661 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10662 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10663 }
10664 if (dpll & PLL_P2_DIVIDE_BY_4)
10665 clock.p2 = 4;
10666 else
10667 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -080010668 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010669
Imre Deakdccbea32015-06-22 23:35:51 +030010670 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010671 }
10672
Ville Syrjälä18442d02013-09-13 16:00:08 +030010673 /*
10674 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +010010675 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +030010676 * encoder's get_config() function.
10677 */
Imre Deakdccbea32015-06-22 23:35:51 +030010678 pipe_config->port_clock = port_clock;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010679}
10680
Ville Syrjälä6878da02013-09-13 15:59:11 +030010681int intel_dotclock_calculate(int link_freq,
10682 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010683{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010684 /*
10685 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010686 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010687 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010688 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010689 *
10690 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010691 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -080010692 */
10693
Ville Syrjälä6878da02013-09-13 15:59:11 +030010694 if (!m_n->link_n)
10695 return 0;
10696
10697 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10698}
10699
Ville Syrjälä18442d02013-09-13 16:00:08 +030010700static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010701 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +030010702{
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020010703 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä18442d02013-09-13 16:00:08 +030010704
10705 /* read out port_clock from the DPLL */
10706 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +030010707
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010708 /*
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020010709 * In case there is an active pipe without active ports,
10710 * we may need some idea for the dotclock anyway.
10711 * Calculate one based on the FDI configuration.
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010712 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010713 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä21a727b2016-02-17 21:41:10 +020010714 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjälä18442d02013-09-13 16:00:08 +030010715 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -080010716}
10717
10718/** Returns the currently programmed mode of the given pipe. */
10719struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10720 struct drm_crtc *crtc)
10721{
Jesse Barnes548f2452011-02-17 10:40:53 -080010722 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080010723 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010724 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010725 struct drm_display_mode *mode;
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010726 struct intel_crtc_state *pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -020010727 int htot = I915_READ(HTOTAL(cpu_transcoder));
10728 int hsync = I915_READ(HSYNC(cpu_transcoder));
10729 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10730 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +030010731 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -080010732
10733 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10734 if (!mode)
10735 return NULL;
10736
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010737 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
10738 if (!pipe_config) {
10739 kfree(mode);
10740 return NULL;
10741 }
10742
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010743 /*
10744 * Construct a pipe_config sufficient for getting the clock info
10745 * back out of crtc_clock_get.
10746 *
10747 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10748 * to use a real value here instead.
10749 */
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010750 pipe_config->cpu_transcoder = (enum transcoder) pipe;
10751 pipe_config->pixel_multiplier = 1;
10752 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10753 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10754 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
10755 i9xx_crtc_clock_get(intel_crtc, pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010756
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010757 mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -080010758 mode->hdisplay = (htot & 0xffff) + 1;
10759 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10760 mode->hsync_start = (hsync & 0xffff) + 1;
10761 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10762 mode->vdisplay = (vtot & 0xffff) + 1;
10763 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10764 mode->vsync_start = (vsync & 0xffff) + 1;
10765 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10766
10767 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -080010768
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010769 kfree(pipe_config);
10770
Jesse Barnes79e53942008-11-07 14:24:08 -080010771 return mode;
10772}
10773
Chris Wilsonf047e392012-07-21 12:31:41 +010010774void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -070010775{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010776 struct drm_i915_private *dev_priv = dev->dev_private;
10777
Chris Wilsonf62a0072014-02-21 17:55:39 +000010778 if (dev_priv->mm.busy)
10779 return;
10780
Paulo Zanoni43694d62014-03-07 20:08:08 -030010781 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -030010782 i915_update_gfx_val(dev_priv);
Chris Wilson43cf3bf2015-03-18 09:48:22 +000010783 if (INTEL_INFO(dev)->gen >= 6)
10784 gen6_rps_busy(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +000010785 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +010010786}
10787
10788void intel_mark_idle(struct drm_device *dev)
10789{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010790 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +000010791
Chris Wilsonf62a0072014-02-21 17:55:39 +000010792 if (!dev_priv->mm.busy)
10793 return;
10794
10795 dev_priv->mm.busy = false;
10796
Damien Lespiau3d13ef22014-02-07 19:12:47 +000010797 if (INTEL_INFO(dev)->gen >= 6)
Chris Wilsonb29c19b2013-09-25 17:34:56 +010010798 gen6_rps_idle(dev->dev_private);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -030010799
Paulo Zanoni43694d62014-03-07 20:08:08 -030010800 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +010010801}
10802
Jesse Barnes79e53942008-11-07 14:24:08 -080010803static void intel_crtc_destroy(struct drm_crtc *crtc)
10804{
10805 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010806 struct drm_device *dev = crtc->dev;
10807 struct intel_unpin_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +020010808
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010809 spin_lock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010810 work = intel_crtc->unpin_work;
10811 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010812 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010813
10814 if (work) {
10815 cancel_work_sync(&work->work);
10816 kfree(work);
10817 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010818
10819 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010820
Jesse Barnes79e53942008-11-07 14:24:08 -080010821 kfree(intel_crtc);
10822}
10823
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010824static void intel_unpin_work_fn(struct work_struct *__work)
10825{
10826 struct intel_unpin_work *work =
10827 container_of(__work, struct intel_unpin_work, work);
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010828 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
10829 struct drm_device *dev = crtc->base.dev;
10830 struct drm_plane *primary = crtc->base.primary;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010831
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010832 mutex_lock(&dev->struct_mutex);
Ville Syrjälä3465c582016-02-15 22:54:43 +020010833 intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
Chris Wilson05394f32010-11-08 19:18:58 +000010834 drm_gem_object_unreference(&work->pending_flip_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +000010835
John Harrisonf06cc1b2014-11-24 18:49:37 +000010836 if (work->flip_queued_req)
John Harrison146d84f2014-12-05 13:49:33 +000010837 i915_gem_request_assign(&work->flip_queued_req, NULL);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010838 mutex_unlock(&dev->struct_mutex);
10839
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010840 intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
Paulo Zanoni1eb52232016-01-19 11:35:44 -020010841 intel_fbc_post_update(crtc);
Chris Wilson89ed88b2015-02-16 14:31:49 +000010842 drm_framebuffer_unreference(work->old_fb);
Daniel Vetterf99d7062014-06-19 16:01:59 +020010843
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010844 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
10845 atomic_dec(&crtc->unpin_work_count);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010846
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010847 kfree(work);
10848}
10849
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010850static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +010010851 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010852{
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010853 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10854 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010855 unsigned long flags;
10856
10857 /* Ignore early vblank irqs */
10858 if (intel_crtc == NULL)
10859 return;
10860
Daniel Vetterf3260382014-09-15 14:55:23 +020010861 /*
10862 * This is called both by irq handlers and the reset code (to complete
10863 * lost pageflips) so needs the full irqsave spinlocks.
10864 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010865 spin_lock_irqsave(&dev->event_lock, flags);
10866 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +000010867
10868 /* Ensure we don't miss a work->pending update ... */
10869 smp_rmb();
10870
10871 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010872 spin_unlock_irqrestore(&dev->event_lock, flags);
10873 return;
10874 }
10875
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010876 page_flip_completed(intel_crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +010010877
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010878 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010879}
10880
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010881void intel_finish_page_flip(struct drm_device *dev, int pipe)
10882{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010883 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010884 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10885
Mario Kleiner49b14a52010-12-09 07:00:07 +010010886 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010887}
10888
10889void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
10890{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010891 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010892 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
10893
Mario Kleiner49b14a52010-12-09 07:00:07 +010010894 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010895}
10896
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010897/* Is 'a' after or equal to 'b'? */
10898static bool g4x_flip_count_after_eq(u32 a, u32 b)
10899{
10900 return !((a - b) & 0x80000000);
10901}
10902
10903static bool page_flip_finished(struct intel_crtc *crtc)
10904{
10905 struct drm_device *dev = crtc->base.dev;
10906 struct drm_i915_private *dev_priv = dev->dev_private;
10907
Ville Syrjäläbdfa7542014-05-27 21:33:09 +030010908 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
10909 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
10910 return true;
10911
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010912 /*
10913 * The relevant registers doen't exist on pre-ctg.
10914 * As the flip done interrupt doesn't trigger for mmio
10915 * flips on gmch platforms, a flip count check isn't
10916 * really needed there. But since ctg has the registers,
10917 * include it in the check anyway.
10918 */
10919 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10920 return true;
10921
10922 /*
Maarten Lankhorste8861672016-02-24 11:24:26 +010010923 * BDW signals flip done immediately if the plane
10924 * is disabled, even if the plane enable is already
10925 * armed to occur at the next vblank :(
10926 */
10927
10928 /*
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010929 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10930 * used the same base address. In that case the mmio flip might
10931 * have completed, but the CS hasn't even executed the flip yet.
10932 *
10933 * A flip count check isn't enough as the CS might have updated
10934 * the base address just after start of vblank, but before we
10935 * managed to process the interrupt. This means we'd complete the
10936 * CS flip too soon.
10937 *
10938 * Combining both checks should get us a good enough result. It may
10939 * still happen that the CS flip has been executed, but has not
10940 * yet actually completed. But in case the base address is the same
10941 * anyway, we don't really care.
10942 */
10943 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
10944 crtc->unpin_work->gtt_offset &&
Ville Syrjäläfd8f5072015-09-18 20:03:42 +030010945 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010946 crtc->unpin_work->flip_count);
10947}
10948
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010949void intel_prepare_page_flip(struct drm_device *dev, int plane)
10950{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010951 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010952 struct intel_crtc *intel_crtc =
10953 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
10954 unsigned long flags;
10955
Daniel Vetterf3260382014-09-15 14:55:23 +020010956
10957 /*
10958 * This is called both by irq handlers and the reset code (to complete
10959 * lost pageflips) so needs the full irqsave spinlocks.
10960 *
10961 * NB: An MMIO update of the plane base pointer will also
Chris Wilsone7d841c2012-12-03 11:36:30 +000010962 * generate a page-flip completion irq, i.e. every modeset
10963 * is also accompanied by a spurious intel_prepare_page_flip().
10964 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010965 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010966 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
Chris Wilsone7d841c2012-12-03 11:36:30 +000010967 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010968 spin_unlock_irqrestore(&dev->event_lock, flags);
10969}
10970
Chris Wilson60426392015-10-10 10:44:32 +010010971static inline void intel_mark_page_flip_active(struct intel_unpin_work *work)
Chris Wilsone7d841c2012-12-03 11:36:30 +000010972{
10973 /* Ensure that the work item is consistent when activating it ... */
10974 smp_wmb();
Chris Wilson60426392015-10-10 10:44:32 +010010975 atomic_set(&work->pending, INTEL_FLIP_PENDING);
Chris Wilsone7d841c2012-12-03 11:36:30 +000010976 /* and that it is marked active as soon as the irq could fire. */
10977 smp_wmb();
10978}
10979
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010980static int intel_gen2_queue_flip(struct drm_device *dev,
10981 struct drm_crtc *crtc,
10982 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010983 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010010984 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070010985 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010986{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000010987 struct intel_engine_cs *engine = req->engine;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010988 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010989 u32 flip_mask;
10990 int ret;
10991
John Harrison5fb9de12015-05-29 17:44:07 +010010992 ret = intel_ring_begin(req, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010993 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010994 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010995
10996 /* Can't queue multiple flips, so wait for the previous
10997 * one to finish before executing the next.
10998 */
10999 if (intel_crtc->plane)
11000 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11001 else
11002 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011003 intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
11004 intel_ring_emit(engine, MI_NOOP);
11005 intel_ring_emit(engine, MI_DISPLAY_FLIP |
Daniel Vetter6d90c952012-04-26 23:28:05 +020011006 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011007 intel_ring_emit(engine, fb->pitches[0]);
11008 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
11009 intel_ring_emit(engine, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +000011010
Chris Wilson60426392015-10-10 10:44:32 +010011011 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011012 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011013}
11014
11015static int intel_gen3_queue_flip(struct drm_device *dev,
11016 struct drm_crtc *crtc,
11017 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011018 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011019 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011020 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011021{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011022 struct intel_engine_cs *engine = req->engine;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011023 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011024 u32 flip_mask;
11025 int ret;
11026
John Harrison5fb9de12015-05-29 17:44:07 +010011027 ret = intel_ring_begin(req, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011028 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011029 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011030
11031 if (intel_crtc->plane)
11032 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11033 else
11034 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011035 intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
11036 intel_ring_emit(engine, MI_NOOP);
11037 intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 |
Daniel Vetter6d90c952012-04-26 23:28:05 +020011038 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011039 intel_ring_emit(engine, fb->pitches[0]);
11040 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
11041 intel_ring_emit(engine, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011042
Chris Wilson60426392015-10-10 10:44:32 +010011043 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011044 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011045}
11046
11047static int intel_gen4_queue_flip(struct drm_device *dev,
11048 struct drm_crtc *crtc,
11049 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011050 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011051 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011052 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011053{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011054 struct intel_engine_cs *engine = req->engine;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011055 struct drm_i915_private *dev_priv = dev->dev_private;
11056 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11057 uint32_t pf, pipesrc;
11058 int ret;
11059
John Harrison5fb9de12015-05-29 17:44:07 +010011060 ret = intel_ring_begin(req, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011061 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011062 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011063
11064 /* i965+ uses the linear or tiled offsets from the
11065 * Display Registers (which do not change across a page-flip)
11066 * so we need only reprogram the base address.
11067 */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011068 intel_ring_emit(engine, MI_DISPLAY_FLIP |
Daniel Vetter6d90c952012-04-26 23:28:05 +020011069 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011070 intel_ring_emit(engine, fb->pitches[0]);
11071 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset |
Daniel Vetterc2c75132012-07-05 12:17:30 +020011072 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011073
11074 /* XXX Enabling the panel-fitter across page-flip is so far
11075 * untested on non-native modes, so ignore it for now.
11076 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
11077 */
11078 pf = 0;
11079 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011080 intel_ring_emit(engine, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000011081
Chris Wilson60426392015-10-10 10:44:32 +010011082 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011083 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011084}
11085
11086static int intel_gen6_queue_flip(struct drm_device *dev,
11087 struct drm_crtc *crtc,
11088 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011089 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011090 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011091 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011092{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011093 struct intel_engine_cs *engine = req->engine;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011094 struct drm_i915_private *dev_priv = dev->dev_private;
11095 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11096 uint32_t pf, pipesrc;
11097 int ret;
11098
John Harrison5fb9de12015-05-29 17:44:07 +010011099 ret = intel_ring_begin(req, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011100 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011101 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011102
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011103 intel_ring_emit(engine, MI_DISPLAY_FLIP |
Daniel Vetter6d90c952012-04-26 23:28:05 +020011104 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011105 intel_ring_emit(engine, fb->pitches[0] | obj->tiling_mode);
11106 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011107
Chris Wilson99d9acd2012-04-17 20:37:00 +010011108 /* Contrary to the suggestions in the documentation,
11109 * "Enable Panel Fitter" does not seem to be required when page
11110 * flipping with a non-native mode, and worse causes a normal
11111 * modeset to fail.
11112 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11113 */
11114 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011115 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011116 intel_ring_emit(engine, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000011117
Chris Wilson60426392015-10-10 10:44:32 +010011118 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011119 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011120}
11121
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011122static int intel_gen7_queue_flip(struct drm_device *dev,
11123 struct drm_crtc *crtc,
11124 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011125 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011126 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011127 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011128{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011129 struct intel_engine_cs *engine = req->engine;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011130 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011131 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +010011132 int len, ret;
11133
Robin Schroereba905b2014-05-18 02:24:50 +020011134 switch (intel_crtc->plane) {
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011135 case PLANE_A:
11136 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11137 break;
11138 case PLANE_B:
11139 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11140 break;
11141 case PLANE_C:
11142 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11143 break;
11144 default:
11145 WARN_ONCE(1, "unknown plane in flip command\n");
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011146 return -ENODEV;
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011147 }
11148
Chris Wilsonffe74d72013-08-26 20:58:12 +010011149 len = 4;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011150 if (engine->id == RCS) {
Chris Wilsonffe74d72013-08-26 20:58:12 +010011151 len += 6;
Damien Lespiauf4768282014-04-07 20:24:34 +010011152 /*
11153 * On Gen 8, SRM is now taking an extra dword to accommodate
11154 * 48bits addresses, and we need a NOOP for the batch size to
11155 * stay even.
11156 */
11157 if (IS_GEN8(dev))
11158 len += 2;
11159 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010011160
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011161 /*
11162 * BSpec MI_DISPLAY_FLIP for IVB:
11163 * "The full packet must be contained within the same cache line."
11164 *
11165 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11166 * cacheline, if we ever start emitting more commands before
11167 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11168 * then do the cacheline alignment, and finally emit the
11169 * MI_DISPLAY_FLIP.
11170 */
John Harrisonbba09b12015-05-29 17:44:06 +010011171 ret = intel_ring_cacheline_align(req);
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011172 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011173 return ret;
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011174
John Harrison5fb9de12015-05-29 17:44:07 +010011175 ret = intel_ring_begin(req, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011176 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011177 return ret;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011178
Chris Wilsonffe74d72013-08-26 20:58:12 +010011179 /* Unmask the flip-done completion message. Note that the bspec says that
11180 * we should do this for both the BCS and RCS, and that we must not unmask
11181 * more than one flip event at any time (or ensure that one flip message
11182 * can be sent by waiting for flip-done prior to queueing new flips).
11183 * Experimentation says that BCS works despite DERRMR masking all
11184 * flip-done completion events and that unmasking all planes at once
11185 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11186 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11187 */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011188 if (engine->id == RCS) {
11189 intel_ring_emit(engine, MI_LOAD_REGISTER_IMM(1));
11190 intel_ring_emit_reg(engine, DERRMR);
11191 intel_ring_emit(engine, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
11192 DERRMR_PIPEB_PRI_FLIP_DONE |
11193 DERRMR_PIPEC_PRI_FLIP_DONE));
Damien Lespiauf4768282014-04-07 20:24:34 +010011194 if (IS_GEN8(dev))
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011195 intel_ring_emit(engine, MI_STORE_REGISTER_MEM_GEN8 |
Damien Lespiauf4768282014-04-07 20:24:34 +010011196 MI_SRM_LRM_GLOBAL_GTT);
11197 else
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011198 intel_ring_emit(engine, MI_STORE_REGISTER_MEM |
Damien Lespiauf4768282014-04-07 20:24:34 +010011199 MI_SRM_LRM_GLOBAL_GTT);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011200 intel_ring_emit_reg(engine, DERRMR);
11201 intel_ring_emit(engine, engine->scratch.gtt_offset + 256);
Damien Lespiauf4768282014-04-07 20:24:34 +010011202 if (IS_GEN8(dev)) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011203 intel_ring_emit(engine, 0);
11204 intel_ring_emit(engine, MI_NOOP);
Damien Lespiauf4768282014-04-07 20:24:34 +010011205 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010011206 }
11207
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011208 intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 | plane_bit);
11209 intel_ring_emit(engine, (fb->pitches[0] | obj->tiling_mode));
11210 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
11211 intel_ring_emit(engine, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +000011212
Chris Wilson60426392015-10-10 10:44:32 +010011213 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011214 return 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011215}
11216
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +000011217static bool use_mmio_flip(struct intel_engine_cs *engine,
Sourab Gupta84c33a62014-06-02 16:47:17 +053011218 struct drm_i915_gem_object *obj)
11219{
11220 /*
11221 * This is not being used for older platforms, because
11222 * non-availability of flip done interrupt forces us to use
11223 * CS flips. Older platforms derive flip done using some clever
11224 * tricks involving the flip_pending status bits and vblank irqs.
11225 * So using MMIO flips there would disrupt this mechanism.
11226 */
11227
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +000011228 if (engine == NULL)
Chris Wilson8e09bf82014-07-08 10:40:30 +010011229 return true;
11230
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +000011231 if (INTEL_INFO(engine->dev)->gen < 5)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011232 return false;
11233
11234 if (i915.use_mmio_flip < 0)
11235 return false;
11236 else if (i915.use_mmio_flip > 0)
11237 return true;
Oscar Mateo14bf9932014-07-24 17:04:34 +010011238 else if (i915.enable_execlists)
11239 return true;
Alex Goinsfd8e0582015-11-25 18:43:38 -080011240 else if (obj->base.dma_buf &&
11241 !reservation_object_test_signaled_rcu(obj->base.dma_buf->resv,
11242 false))
11243 return true;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011244 else
Tvrtko Ursulin666796d2016-03-16 11:00:39 +000011245 return engine != i915_gem_request_get_engine(obj->last_write_req);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011246}
11247
Chris Wilson60426392015-10-10 10:44:32 +010011248static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011249 unsigned int rotation,
Chris Wilson60426392015-10-10 10:44:32 +010011250 struct intel_unpin_work *work)
Damien Lespiauff944562014-11-20 14:58:16 +000011251{
11252 struct drm_device *dev = intel_crtc->base.dev;
11253 struct drm_i915_private *dev_priv = dev->dev_private;
11254 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
Damien Lespiauff944562014-11-20 14:58:16 +000011255 const enum pipe pipe = intel_crtc->pipe;
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011256 u32 ctl, stride, tile_height;
Damien Lespiauff944562014-11-20 14:58:16 +000011257
11258 ctl = I915_READ(PLANE_CTL(pipe, 0));
11259 ctl &= ~PLANE_CTL_TILED_MASK;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011260 switch (fb->modifier[0]) {
11261 case DRM_FORMAT_MOD_NONE:
11262 break;
11263 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauff944562014-11-20 14:58:16 +000011264 ctl |= PLANE_CTL_TILED_X;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011265 break;
11266 case I915_FORMAT_MOD_Y_TILED:
11267 ctl |= PLANE_CTL_TILED_Y;
11268 break;
11269 case I915_FORMAT_MOD_Yf_TILED:
11270 ctl |= PLANE_CTL_TILED_YF;
11271 break;
11272 default:
11273 MISSING_CASE(fb->modifier[0]);
11274 }
Damien Lespiauff944562014-11-20 14:58:16 +000011275
11276 /*
11277 * The stride is either expressed as a multiple of 64 bytes chunks for
11278 * linear buffers or in number of tiles for tiled buffers.
11279 */
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011280 if (intel_rotation_90_or_270(rotation)) {
11281 /* stride = Surface height in tiles */
Ville Syrjälä832be822016-01-12 21:08:33 +020011282 tile_height = intel_tile_height(dev_priv, fb->modifier[0], 0);
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011283 stride = DIV_ROUND_UP(fb->height, tile_height);
11284 } else {
11285 stride = fb->pitches[0] /
Ville Syrjälä7b49f942016-01-12 21:08:32 +020011286 intel_fb_stride_alignment(dev_priv, fb->modifier[0],
11287 fb->pixel_format);
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011288 }
Damien Lespiauff944562014-11-20 14:58:16 +000011289
11290 /*
11291 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11292 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11293 */
11294 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11295 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11296
Chris Wilson60426392015-10-10 10:44:32 +010011297 I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
Damien Lespiauff944562014-11-20 14:58:16 +000011298 POSTING_READ(PLANE_SURF(pipe, 0));
11299}
11300
Chris Wilson60426392015-10-10 10:44:32 +010011301static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
11302 struct intel_unpin_work *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011303{
11304 struct drm_device *dev = intel_crtc->base.dev;
11305 struct drm_i915_private *dev_priv = dev->dev_private;
11306 struct intel_framebuffer *intel_fb =
11307 to_intel_framebuffer(intel_crtc->base.primary->fb);
11308 struct drm_i915_gem_object *obj = intel_fb->obj;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020011309 i915_reg_t reg = DSPCNTR(intel_crtc->plane);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011310 u32 dspcntr;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011311
Sourab Gupta84c33a62014-06-02 16:47:17 +053011312 dspcntr = I915_READ(reg);
11313
Damien Lespiauc5d97472014-10-25 00:11:11 +010011314 if (obj->tiling_mode != I915_TILING_NONE)
11315 dspcntr |= DISPPLANE_TILED;
11316 else
11317 dspcntr &= ~DISPPLANE_TILED;
11318
Sourab Gupta84c33a62014-06-02 16:47:17 +053011319 I915_WRITE(reg, dspcntr);
11320
Chris Wilson60426392015-10-10 10:44:32 +010011321 I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011322 POSTING_READ(DSPSURF(intel_crtc->plane));
Damien Lespiauff944562014-11-20 14:58:16 +000011323}
11324
11325/*
11326 * XXX: This is the temporary way to update the plane registers until we get
11327 * around to using the usual plane update functions for MMIO flips
11328 */
Chris Wilson60426392015-10-10 10:44:32 +010011329static void intel_do_mmio_flip(struct intel_mmio_flip *mmio_flip)
Damien Lespiauff944562014-11-20 14:58:16 +000011330{
Chris Wilson60426392015-10-10 10:44:32 +010011331 struct intel_crtc *crtc = mmio_flip->crtc;
11332 struct intel_unpin_work *work;
Damien Lespiauff944562014-11-20 14:58:16 +000011333
Chris Wilson60426392015-10-10 10:44:32 +010011334 spin_lock_irq(&crtc->base.dev->event_lock);
11335 work = crtc->unpin_work;
11336 spin_unlock_irq(&crtc->base.dev->event_lock);
11337 if (work == NULL)
11338 return;
Damien Lespiauff944562014-11-20 14:58:16 +000011339
Chris Wilson60426392015-10-10 10:44:32 +010011340 intel_mark_page_flip_active(work);
Damien Lespiauff944562014-11-20 14:58:16 +000011341
Chris Wilson60426392015-10-10 10:44:32 +010011342 intel_pipe_update_start(crtc);
11343
11344 if (INTEL_INFO(mmio_flip->i915)->gen >= 9)
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011345 skl_do_mmio_flip(crtc, mmio_flip->rotation, work);
Damien Lespiauff944562014-11-20 14:58:16 +000011346 else
11347 /* use_mmio_flip() retricts MMIO flips to ilk+ */
Chris Wilson60426392015-10-10 10:44:32 +010011348 ilk_do_mmio_flip(crtc, work);
Damien Lespiauff944562014-11-20 14:58:16 +000011349
Chris Wilson60426392015-10-10 10:44:32 +010011350 intel_pipe_update_end(crtc);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011351}
11352
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020011353static void intel_mmio_flip_work_func(struct work_struct *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011354{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011355 struct intel_mmio_flip *mmio_flip =
11356 container_of(work, struct intel_mmio_flip, work);
Alex Goinsfd8e0582015-11-25 18:43:38 -080011357 struct intel_framebuffer *intel_fb =
11358 to_intel_framebuffer(mmio_flip->crtc->base.primary->fb);
11359 struct drm_i915_gem_object *obj = intel_fb->obj;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011360
Chris Wilson60426392015-10-10 10:44:32 +010011361 if (mmio_flip->req) {
Daniel Vettereed29a52015-05-21 14:21:25 +020011362 WARN_ON(__i915_wait_request(mmio_flip->req,
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011363 mmio_flip->crtc->reset_counter,
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011364 false, NULL,
11365 &mmio_flip->i915->rps.mmioflips));
Chris Wilson60426392015-10-10 10:44:32 +010011366 i915_gem_request_unreference__unlocked(mmio_flip->req);
11367 }
Sourab Gupta84c33a62014-06-02 16:47:17 +053011368
Alex Goinsfd8e0582015-11-25 18:43:38 -080011369 /* For framebuffer backed by dmabuf, wait for fence */
11370 if (obj->base.dma_buf)
11371 WARN_ON(reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
11372 false, false,
11373 MAX_SCHEDULE_TIMEOUT) < 0);
11374
Chris Wilson60426392015-10-10 10:44:32 +010011375 intel_do_mmio_flip(mmio_flip);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011376 kfree(mmio_flip);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011377}
11378
11379static int intel_queue_mmio_flip(struct drm_device *dev,
11380 struct drm_crtc *crtc,
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011381 struct drm_i915_gem_object *obj)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011382{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011383 struct intel_mmio_flip *mmio_flip;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011384
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011385 mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
11386 if (mmio_flip == NULL)
11387 return -ENOMEM;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011388
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011389 mmio_flip->i915 = to_i915(dev);
Daniel Vettereed29a52015-05-21 14:21:25 +020011390 mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011391 mmio_flip->crtc = to_intel_crtc(crtc);
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011392 mmio_flip->rotation = crtc->primary->state->rotation;
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011393
11394 INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
11395 schedule_work(&mmio_flip->work);
Ander Conselvan de Oliveira536f5b52014-11-06 11:03:40 +020011396
Sourab Gupta84c33a62014-06-02 16:47:17 +053011397 return 0;
11398}
11399
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011400static int intel_default_queue_flip(struct drm_device *dev,
11401 struct drm_crtc *crtc,
11402 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011403 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011404 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011405 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011406{
11407 return -ENODEV;
11408}
11409
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011410static bool __intel_pageflip_stall_check(struct drm_device *dev,
11411 struct drm_crtc *crtc)
11412{
11413 struct drm_i915_private *dev_priv = dev->dev_private;
11414 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11415 struct intel_unpin_work *work = intel_crtc->unpin_work;
11416 u32 addr;
11417
11418 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
11419 return true;
11420
Chris Wilson908565c2015-08-12 13:08:22 +010011421 if (atomic_read(&work->pending) < INTEL_FLIP_PENDING)
11422 return false;
11423
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011424 if (!work->enable_stall_check)
11425 return false;
11426
11427 if (work->flip_ready_vblank == 0) {
Daniel Vetter3a8a9462014-11-26 14:39:48 +010011428 if (work->flip_queued_req &&
11429 !i915_gem_request_completed(work->flip_queued_req, true))
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011430 return false;
11431
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011432 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011433 }
11434
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011435 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011436 return false;
11437
11438 /* Potential stall - if we see that the flip has happened,
11439 * assume a missed interrupt. */
11440 if (INTEL_INFO(dev)->gen >= 4)
11441 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11442 else
11443 addr = I915_READ(DSPADDR(intel_crtc->plane));
11444
11445 /* There is a potential issue here with a false positive after a flip
11446 * to the same address. We could address this by checking for a
11447 * non-incrementing frame counter.
11448 */
11449 return addr == work->gtt_offset;
11450}
11451
11452void intel_check_page_flip(struct drm_device *dev, int pipe)
11453{
11454 struct drm_i915_private *dev_priv = dev->dev_private;
11455 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11456 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011457 struct intel_unpin_work *work;
Daniel Vetterf3260382014-09-15 14:55:23 +020011458
Dave Gordon6c51d462015-03-06 15:34:26 +000011459 WARN_ON(!in_interrupt());
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011460
11461 if (crtc == NULL)
11462 return;
11463
Daniel Vetterf3260382014-09-15 14:55:23 +020011464 spin_lock(&dev->event_lock);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011465 work = intel_crtc->unpin_work;
11466 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011467 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
Chris Wilson6ad790c2015-04-07 16:20:31 +010011468 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011469 page_flip_completed(intel_crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011470 work = NULL;
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011471 }
Chris Wilson6ad790c2015-04-07 16:20:31 +010011472 if (work != NULL &&
11473 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
11474 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
Daniel Vetterf3260382014-09-15 14:55:23 +020011475 spin_unlock(&dev->event_lock);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011476}
11477
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011478static int intel_crtc_page_flip(struct drm_crtc *crtc,
11479 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011480 struct drm_pending_vblank_event *event,
11481 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011482{
11483 struct drm_device *dev = crtc->dev;
11484 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -070011485 struct drm_framebuffer *old_fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -070011486 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011487 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gustavo Padovan455a6802014-12-01 15:40:11 -080011488 struct drm_plane *primary = crtc->primary;
Daniel Vettera071fa02014-06-18 23:28:09 +020011489 enum pipe pipe = intel_crtc->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011490 struct intel_unpin_work *work;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011491 struct intel_engine_cs *engine;
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011492 bool mmio_flip;
John Harrison91af1272015-06-18 13:14:56 +010011493 struct drm_i915_gem_request *request = NULL;
Chris Wilson52e68632010-08-08 10:15:59 +010011494 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011495
Matt Roper2ff8fde2014-07-08 07:50:07 -070011496 /*
11497 * drm_mode_page_flip_ioctl() should already catch this, but double
11498 * check to be safe. In the future we may enable pageflipping from
11499 * a disabled primary plane.
11500 */
11501 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11502 return -EBUSY;
11503
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011504 /* Can't change pixel format via MI display flips. */
Matt Roperf4510a22014-04-01 15:22:40 -070011505 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011506 return -EINVAL;
11507
11508 /*
11509 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11510 * Note that pitch changes could also affect these register.
11511 */
11512 if (INTEL_INFO(dev)->gen > 3 &&
Matt Roperf4510a22014-04-01 15:22:40 -070011513 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11514 fb->pitches[0] != crtc->primary->fb->pitches[0]))
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011515 return -EINVAL;
11516
Chris Wilsonf900db42014-02-20 09:26:13 +000011517 if (i915_terminally_wedged(&dev_priv->gpu_error))
11518 goto out_hang;
11519
Daniel Vetterb14c5672013-09-19 12:18:32 +020011520 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011521 if (work == NULL)
11522 return -ENOMEM;
11523
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011524 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011525 work->crtc = crtc;
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011526 work->old_fb = old_fb;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011527 INIT_WORK(&work->work, intel_unpin_work_fn);
11528
Daniel Vetter87b6b102014-05-15 15:33:46 +020011529 ret = drm_crtc_vblank_get(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011530 if (ret)
11531 goto free_work;
11532
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011533 /* We borrow the event spin lock for protecting unpin_work */
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011534 spin_lock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011535 if (intel_crtc->unpin_work) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011536 /* Before declaring the flip queue wedged, check if
11537 * the hardware completed the operation behind our backs.
11538 */
11539 if (__intel_pageflip_stall_check(dev, crtc)) {
11540 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11541 page_flip_completed(intel_crtc);
11542 } else {
11543 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011544 spin_unlock_irq(&dev->event_lock);
Chris Wilson468f0b42010-05-27 13:18:13 +010011545
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011546 drm_crtc_vblank_put(crtc);
11547 kfree(work);
11548 return -EBUSY;
11549 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011550 }
11551 intel_crtc->unpin_work = work;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011552 spin_unlock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011553
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011554 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11555 flush_workqueue(dev_priv->wq);
11556
Jesse Barnes75dfca82010-02-10 15:09:44 -080011557 /* Reference the objects for the scheduled work. */
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011558 drm_framebuffer_reference(work->old_fb);
Chris Wilson05394f32010-11-08 19:18:58 +000011559 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011560
Matt Roperf4510a22014-04-01 15:22:40 -070011561 crtc->primary->fb = fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011562 update_state_fb(crtc->primary);
Paulo Zanonie8216e52016-01-19 11:35:56 -020011563 intel_fbc_pre_update(intel_crtc);
Matt Roper1ed1f962015-01-30 16:22:36 -080011564
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011565 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011566
Chris Wilson89ed88b2015-02-16 14:31:49 +000011567 ret = i915_mutex_lock_interruptible(dev);
11568 if (ret)
11569 goto cleanup;
11570
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011571 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +020011572 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011573
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011574 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
Ville Syrjäläfd8f5072015-09-18 20:03:42 +030011575 work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011576
Wayne Boyer666a4532015-12-09 12:29:35 -080011577 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011578 engine = &dev_priv->engine[BCS];
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011579 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
Chris Wilson8e09bf82014-07-08 10:40:30 +010011580 /* vlv: DISPLAY_FLIP fails to change tiling */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011581 engine = NULL;
Chris Wilson48bf5b22014-12-27 09:48:28 +000011582 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011583 engine = &dev_priv->engine[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011584 } else if (INTEL_INFO(dev)->gen >= 7) {
Tvrtko Ursulin666796d2016-03-16 11:00:39 +000011585 engine = i915_gem_request_get_engine(obj->last_write_req);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011586 if (engine == NULL || engine->id != RCS)
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011587 engine = &dev_priv->engine[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011588 } else {
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011589 engine = &dev_priv->engine[RCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011590 }
11591
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011592 mmio_flip = use_mmio_flip(engine, obj);
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011593
11594 /* When using CS flips, we want to emit semaphores between rings.
11595 * However, when using mmio flips we will create a task to do the
11596 * synchronisation, so all we want here is to pin the framebuffer
11597 * into the display plane and skip any waits.
11598 */
Maarten Lankhorst7580d772015-08-18 13:40:06 +020011599 if (!mmio_flip) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011600 ret = i915_gem_object_sync(obj, engine, &request);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020011601 if (ret)
11602 goto cleanup_pending;
11603 }
11604
Ville Syrjälä3465c582016-02-15 22:54:43 +020011605 ret = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011606 if (ret)
11607 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011608
Tvrtko Ursulindedf2782015-09-21 10:45:35 +010011609 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary),
11610 obj, 0);
11611 work->gtt_offset += intel_crtc->dspaddr_offset;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011612
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011613 if (mmio_flip) {
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011614 ret = intel_queue_mmio_flip(dev, crtc, obj);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011615 if (ret)
11616 goto cleanup_unpin;
11617
John Harrisonf06cc1b2014-11-24 18:49:37 +000011618 i915_gem_request_assign(&work->flip_queued_req,
11619 obj->last_write_req);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011620 } else {
John Harrison6258fbe2015-05-29 17:43:48 +010011621 if (!request) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011622 request = i915_gem_request_alloc(engine, NULL);
Dave Gordon26827082016-01-19 19:02:53 +000011623 if (IS_ERR(request)) {
11624 ret = PTR_ERR(request);
John Harrison6258fbe2015-05-29 17:43:48 +010011625 goto cleanup_unpin;
Dave Gordon26827082016-01-19 19:02:53 +000011626 }
John Harrison6258fbe2015-05-29 17:43:48 +010011627 }
11628
11629 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011630 page_flip_flags);
11631 if (ret)
11632 goto cleanup_unpin;
11633
John Harrison6258fbe2015-05-29 17:43:48 +010011634 i915_gem_request_assign(&work->flip_queued_req, request);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011635 }
11636
John Harrison91af1272015-06-18 13:14:56 +010011637 if (request)
John Harrison75289872015-05-29 17:43:49 +010011638 i915_add_request_no_flush(request);
John Harrison91af1272015-06-18 13:14:56 +010011639
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011640 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011641 work->enable_stall_check = true;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011642
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011643 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011644 to_intel_plane(primary)->frontbuffer_bit);
Paulo Zanonic80ac852015-07-02 19:25:13 -030011645 mutex_unlock(&dev->struct_mutex);
Daniel Vettera071fa02014-06-18 23:28:09 +020011646
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011647 intel_frontbuffer_flip_prepare(dev,
11648 to_intel_plane(primary)->frontbuffer_bit);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011649
Jesse Barnese5510fa2010-07-01 16:48:37 -070011650 trace_i915_flip_request(intel_crtc->plane, obj);
11651
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011652 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +010011653
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011654cleanup_unpin:
Ville Syrjälä3465c582016-02-15 22:54:43 +020011655 intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011656cleanup_pending:
Dave Gordon0aa498d2016-01-28 10:48:09 +000011657 if (!IS_ERR_OR_NULL(request))
John Harrison91af1272015-06-18 13:14:56 +010011658 i915_gem_request_cancel(request);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011659 atomic_dec(&intel_crtc->unpin_work_count);
Chris Wilson89ed88b2015-02-16 14:31:49 +000011660 mutex_unlock(&dev->struct_mutex);
11661cleanup:
Matt Roperf4510a22014-04-01 15:22:40 -070011662 crtc->primary->fb = old_fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011663 update_state_fb(crtc->primary);
Chris Wilson96b099f2010-06-07 14:03:04 +010011664
Chris Wilson89ed88b2015-02-16 14:31:49 +000011665 drm_gem_object_unreference_unlocked(&obj->base);
11666 drm_framebuffer_unreference(work->old_fb);
11667
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011668 spin_lock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011669 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011670 spin_unlock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011671
Daniel Vetter87b6b102014-05-15 15:33:46 +020011672 drm_crtc_vblank_put(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011673free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +010011674 kfree(work);
11675
Chris Wilsonf900db42014-02-20 09:26:13 +000011676 if (ret == -EIO) {
Maarten Lankhorst02e0efb2015-06-12 11:15:40 +020011677 struct drm_atomic_state *state;
11678 struct drm_plane_state *plane_state;
11679
Chris Wilsonf900db42014-02-20 09:26:13 +000011680out_hang:
Maarten Lankhorst02e0efb2015-06-12 11:15:40 +020011681 state = drm_atomic_state_alloc(dev);
11682 if (!state)
11683 return -ENOMEM;
11684 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
11685
11686retry:
11687 plane_state = drm_atomic_get_plane_state(state, primary);
11688 ret = PTR_ERR_OR_ZERO(plane_state);
11689 if (!ret) {
11690 drm_atomic_set_fb_for_plane(plane_state, fb);
11691
11692 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
11693 if (!ret)
11694 ret = drm_atomic_commit(state);
11695 }
11696
11697 if (ret == -EDEADLK) {
11698 drm_modeset_backoff(state->acquire_ctx);
11699 drm_atomic_state_clear(state);
11700 goto retry;
11701 }
11702
11703 if (ret)
11704 drm_atomic_state_free(state);
11705
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011706 if (ret == 0 && event) {
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011707 spin_lock_irq(&dev->event_lock);
Daniel Vettera071fa02014-06-18 23:28:09 +020011708 drm_send_vblank_event(dev, pipe, event);
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011709 spin_unlock_irq(&dev->event_lock);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011710 }
Chris Wilsonf900db42014-02-20 09:26:13 +000011711 }
Chris Wilson96b099f2010-06-07 14:03:04 +010011712 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011713}
11714
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011715
11716/**
11717 * intel_wm_need_update - Check whether watermarks need updating
11718 * @plane: drm plane
11719 * @state: new plane state
11720 *
11721 * Check current plane state versus the new one to determine whether
11722 * watermarks need to be recalculated.
11723 *
11724 * Returns true or false.
11725 */
11726static bool intel_wm_need_update(struct drm_plane *plane,
11727 struct drm_plane_state *state)
11728{
Matt Roperd21fbe82015-09-24 15:53:12 -070011729 struct intel_plane_state *new = to_intel_plane_state(state);
11730 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
11731
11732 /* Update watermarks on tiling or size changes. */
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010011733 if (new->visible != cur->visible)
11734 return true;
11735
11736 if (!cur->base.fb || !new->base.fb)
11737 return false;
11738
11739 if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
11740 cur->base.rotation != new->base.rotation ||
Matt Roperd21fbe82015-09-24 15:53:12 -070011741 drm_rect_width(&new->src) != drm_rect_width(&cur->src) ||
11742 drm_rect_height(&new->src) != drm_rect_height(&cur->src) ||
11743 drm_rect_width(&new->dst) != drm_rect_width(&cur->dst) ||
11744 drm_rect_height(&new->dst) != drm_rect_height(&cur->dst))
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011745 return true;
11746
11747 return false;
11748}
11749
Matt Roperd21fbe82015-09-24 15:53:12 -070011750static bool needs_scaling(struct intel_plane_state *state)
11751{
11752 int src_w = drm_rect_width(&state->src) >> 16;
11753 int src_h = drm_rect_height(&state->src) >> 16;
11754 int dst_w = drm_rect_width(&state->dst);
11755 int dst_h = drm_rect_height(&state->dst);
11756
11757 return (src_w != dst_w || src_h != dst_h);
11758}
11759
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011760int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
11761 struct drm_plane_state *plane_state)
11762{
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010011763 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011764 struct drm_crtc *crtc = crtc_state->crtc;
11765 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11766 struct drm_plane *plane = plane_state->plane;
11767 struct drm_device *dev = crtc->dev;
Matt Ropered4a6a72016-02-23 17:20:13 -080011768 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011769 struct intel_plane_state *old_plane_state =
11770 to_intel_plane_state(plane->state);
11771 int idx = intel_crtc->base.base.id, ret;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011772 bool mode_changed = needs_modeset(crtc_state);
11773 bool was_crtc_enabled = crtc->state->active;
11774 bool is_crtc_enabled = crtc_state->active;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011775 bool turn_off, turn_on, visible, was_visible;
11776 struct drm_framebuffer *fb = plane_state->fb;
11777
11778 if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
11779 plane->type != DRM_PLANE_TYPE_CURSOR) {
11780 ret = skl_update_scaler_plane(
11781 to_intel_crtc_state(crtc_state),
11782 to_intel_plane_state(plane_state));
11783 if (ret)
11784 return ret;
11785 }
11786
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011787 was_visible = old_plane_state->visible;
11788 visible = to_intel_plane_state(plane_state)->visible;
11789
11790 if (!was_crtc_enabled && WARN_ON(was_visible))
11791 was_visible = false;
11792
Maarten Lankhorst35c08f42015-12-03 14:31:07 +010011793 /*
11794 * Visibility is calculated as if the crtc was on, but
11795 * after scaler setup everything depends on it being off
11796 * when the crtc isn't active.
11797 */
11798 if (!is_crtc_enabled)
11799 to_intel_plane_state(plane_state)->visible = visible = false;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011800
11801 if (!was_visible && !visible)
11802 return 0;
11803
Maarten Lankhorste8861672016-02-24 11:24:26 +010011804 if (fb != old_plane_state->base.fb)
11805 pipe_config->fb_changed = true;
11806
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011807 turn_off = was_visible && (!visible || mode_changed);
11808 turn_on = visible && (!was_visible || mode_changed);
11809
11810 DRM_DEBUG_ATOMIC("[CRTC:%i] has [PLANE:%i] with fb %i\n", idx,
11811 plane->base.id, fb ? fb->base.id : -1);
11812
11813 DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
11814 plane->base.id, was_visible, visible,
11815 turn_off, turn_on, mode_changed);
11816
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011817 if (turn_on) {
11818 pipe_config->update_wm_pre = true;
11819
11820 /* must disable cxsr around plane enable/disable */
11821 if (plane->type != DRM_PLANE_TYPE_CURSOR)
11822 pipe_config->disable_cxsr = true;
11823 } else if (turn_off) {
11824 pipe_config->update_wm_post = true;
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010011825
Ville Syrjälä852eb002015-06-24 22:00:07 +030011826 /* must disable cxsr around plane enable/disable */
Maarten Lankhorste8861672016-02-24 11:24:26 +010011827 if (plane->type != DRM_PLANE_TYPE_CURSOR)
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010011828 pipe_config->disable_cxsr = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011829 } else if (intel_wm_need_update(plane, plane_state)) {
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011830 /* FIXME bollocks */
11831 pipe_config->update_wm_pre = true;
11832 pipe_config->update_wm_post = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011833 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011834
Matt Ropered4a6a72016-02-23 17:20:13 -080011835 /* Pre-gen9 platforms need two-step watermark updates */
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011836 if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
11837 INTEL_INFO(dev)->gen < 9 && dev_priv->display.optimize_watermarks)
Matt Ropered4a6a72016-02-23 17:20:13 -080011838 to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
11839
Rodrigo Vivi8be6ca82015-08-24 16:38:23 -070011840 if (visible || was_visible)
Maarten Lankhorstcd202f62016-03-09 10:35:44 +010011841 pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011842
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +010011843 /*
11844 * WaCxSRDisabledForSpriteScaling:ivb
11845 *
11846 * cstate->update_wm was already set above, so this flag will
11847 * take effect when we commit and program watermarks.
11848 */
11849 if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev) &&
11850 needs_scaling(to_intel_plane_state(plane_state)) &&
11851 !needs_scaling(old_plane_state))
11852 pipe_config->disable_lp_wm = true;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011853
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011854 return 0;
11855}
11856
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011857static bool encoders_cloneable(const struct intel_encoder *a,
11858 const struct intel_encoder *b)
11859{
11860 /* masks could be asymmetric, so check both ways */
11861 return a == b || (a->cloneable & (1 << b->type) &&
11862 b->cloneable & (1 << a->type));
11863}
11864
11865static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11866 struct intel_crtc *crtc,
11867 struct intel_encoder *encoder)
11868{
11869 struct intel_encoder *source_encoder;
11870 struct drm_connector *connector;
11871 struct drm_connector_state *connector_state;
11872 int i;
11873
11874 for_each_connector_in_state(state, connector, connector_state, i) {
11875 if (connector_state->crtc != &crtc->base)
11876 continue;
11877
11878 source_encoder =
11879 to_intel_encoder(connector_state->best_encoder);
11880 if (!encoders_cloneable(encoder, source_encoder))
11881 return false;
11882 }
11883
11884 return true;
11885}
11886
11887static bool check_encoder_cloning(struct drm_atomic_state *state,
11888 struct intel_crtc *crtc)
11889{
11890 struct intel_encoder *encoder;
11891 struct drm_connector *connector;
11892 struct drm_connector_state *connector_state;
11893 int i;
11894
11895 for_each_connector_in_state(state, connector, connector_state, i) {
11896 if (connector_state->crtc != &crtc->base)
11897 continue;
11898
11899 encoder = to_intel_encoder(connector_state->best_encoder);
11900 if (!check_single_encoder_cloning(state, crtc, encoder))
11901 return false;
11902 }
11903
11904 return true;
11905}
11906
11907static int intel_crtc_atomic_check(struct drm_crtc *crtc,
11908 struct drm_crtc_state *crtc_state)
11909{
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020011910 struct drm_device *dev = crtc->dev;
Maarten Lankhorstad421372015-06-15 12:33:42 +020011911 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011912 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020011913 struct intel_crtc_state *pipe_config =
11914 to_intel_crtc_state(crtc_state);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011915 struct drm_atomic_state *state = crtc_state->state;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020011916 int ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011917 bool mode_changed = needs_modeset(crtc_state);
11918
11919 if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
11920 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
11921 return -EINVAL;
11922 }
11923
Ville Syrjälä852eb002015-06-24 22:00:07 +030011924 if (mode_changed && !crtc_state->active)
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011925 pipe_config->update_wm_post = true;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020011926
Maarten Lankhorstad421372015-06-15 12:33:42 +020011927 if (mode_changed && crtc_state->enable &&
11928 dev_priv->display.crtc_compute_clock &&
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020011929 !WARN_ON(pipe_config->shared_dpll)) {
Maarten Lankhorstad421372015-06-15 12:33:42 +020011930 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
11931 pipe_config);
11932 if (ret)
11933 return ret;
11934 }
11935
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000011936 if (crtc_state->color_mgmt_changed) {
11937 ret = intel_color_check(crtc, crtc_state);
11938 if (ret)
11939 return ret;
11940 }
11941
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020011942 ret = 0;
Matt Roper86c8bbb2015-09-24 15:53:16 -070011943 if (dev_priv->display.compute_pipe_wm) {
Maarten Lankhorste3bddde2016-03-01 11:07:22 +010011944 ret = dev_priv->display.compute_pipe_wm(pipe_config);
Matt Ropered4a6a72016-02-23 17:20:13 -080011945 if (ret) {
11946 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
Matt Roper86c8bbb2015-09-24 15:53:16 -070011947 return ret;
Matt Ropered4a6a72016-02-23 17:20:13 -080011948 }
11949 }
11950
11951 if (dev_priv->display.compute_intermediate_wm &&
11952 !to_intel_atomic_state(state)->skip_intermediate_wm) {
11953 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
11954 return 0;
11955
11956 /*
11957 * Calculate 'intermediate' watermarks that satisfy both the
11958 * old state and the new state. We can program these
11959 * immediately.
11960 */
11961 ret = dev_priv->display.compute_intermediate_wm(crtc->dev,
11962 intel_crtc,
11963 pipe_config);
11964 if (ret) {
11965 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
11966 return ret;
11967 }
Matt Roper86c8bbb2015-09-24 15:53:16 -070011968 }
11969
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020011970 if (INTEL_INFO(dev)->gen >= 9) {
11971 if (mode_changed)
11972 ret = skl_update_scaler_crtc(pipe_config);
11973
11974 if (!ret)
11975 ret = intel_atomic_setup_scalers(dev, intel_crtc,
11976 pipe_config);
11977 }
11978
11979 return ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011980}
11981
Jani Nikula65b38e02015-04-13 11:26:56 +030011982static const struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011983 .mode_set_base_atomic = intel_pipe_set_base_atomic,
Matt Roperea2c67b2014-12-23 10:41:52 -080011984 .atomic_begin = intel_begin_crtc_commit,
11985 .atomic_flush = intel_finish_crtc_commit,
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011986 .atomic_check = intel_crtc_atomic_check,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011987};
11988
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020011989static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
11990{
11991 struct intel_connector *connector;
11992
11993 for_each_intel_connector(dev, connector) {
11994 if (connector->base.encoder) {
11995 connector->base.state->best_encoder =
11996 connector->base.encoder;
11997 connector->base.state->crtc =
11998 connector->base.encoder->crtc;
11999 } else {
12000 connector->base.state->best_encoder = NULL;
12001 connector->base.state->crtc = NULL;
12002 }
12003 }
12004}
12005
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012006static void
Robin Schroereba905b2014-05-18 02:24:50 +020012007connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012008 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012009{
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012010 int bpp = pipe_config->pipe_bpp;
12011
12012 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
12013 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030012014 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012015
12016 /* Don't use an invalid EDID bpc value */
12017 if (connector->base.display_info.bpc &&
12018 connector->base.display_info.bpc * 3 < bpp) {
12019 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
12020 bpp, connector->base.display_info.bpc*3);
12021 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
12022 }
12023
Jani Nikula013dd9e2016-01-13 16:35:20 +020012024 /* Clamp bpp to default limit on screens without EDID 1.4 */
12025 if (connector->base.display_info.bpc == 0) {
12026 int type = connector->base.connector_type;
12027 int clamp_bpp = 24;
12028
12029 /* Fall back to 18 bpp when DP sink capability is unknown. */
12030 if (type == DRM_MODE_CONNECTOR_DisplayPort ||
12031 type == DRM_MODE_CONNECTOR_eDP)
12032 clamp_bpp = 18;
12033
12034 if (bpp > clamp_bpp) {
12035 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of %d\n",
12036 bpp, clamp_bpp);
12037 pipe_config->pipe_bpp = clamp_bpp;
12038 }
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012039 }
12040}
12041
12042static int
12043compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012044 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012045{
12046 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012047 struct drm_atomic_state *state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012048 struct drm_connector *connector;
12049 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012050 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012051
Wayne Boyer666a4532015-12-09 12:29:35 -080012052 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012053 bpp = 10*3;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012054 else if (INTEL_INFO(dev)->gen >= 5)
12055 bpp = 12*3;
12056 else
12057 bpp = 8*3;
12058
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012059
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012060 pipe_config->pipe_bpp = bpp;
12061
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012062 state = pipe_config->base.state;
12063
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012064 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012065 for_each_connector_in_state(state, connector, connector_state, i) {
12066 if (connector_state->crtc != &crtc->base)
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012067 continue;
12068
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012069 connected_sink_compute_bpp(to_intel_connector(connector),
12070 pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012071 }
12072
12073 return bpp;
12074}
12075
Daniel Vetter644db712013-09-19 14:53:58 +020012076static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
12077{
12078 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
12079 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010012080 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020012081 mode->crtc_hdisplay, mode->crtc_hsync_start,
12082 mode->crtc_hsync_end, mode->crtc_htotal,
12083 mode->crtc_vdisplay, mode->crtc_vsync_start,
12084 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
12085}
12086
Daniel Vetterc0b03412013-05-28 12:05:54 +020012087static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012088 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012089 const char *context)
12090{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012091 struct drm_device *dev = crtc->base.dev;
12092 struct drm_plane *plane;
12093 struct intel_plane *intel_plane;
12094 struct intel_plane_state *state;
12095 struct drm_framebuffer *fb;
12096
12097 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
12098 context, pipe_config, pipe_name(crtc->pipe));
Daniel Vetterc0b03412013-05-28 12:05:54 +020012099
Jani Nikulada205632016-03-15 21:51:10 +020012100 DRM_DEBUG_KMS("cpu_transcoder: %s\n", transcoder_name(pipe_config->cpu_transcoder));
Daniel Vetterc0b03412013-05-28 12:05:54 +020012101 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
12102 pipe_config->pipe_bpp, pipe_config->dither);
12103 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
12104 pipe_config->has_pch_encoder,
12105 pipe_config->fdi_lanes,
12106 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
12107 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
12108 pipe_config->fdi_m_n.tu);
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012109 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 +030012110 pipe_config->has_dp_encoder,
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012111 pipe_config->lane_count,
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012112 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
12113 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
12114 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012115
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012116 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 -070012117 pipe_config->has_dp_encoder,
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012118 pipe_config->lane_count,
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012119 pipe_config->dp_m2_n2.gmch_m,
12120 pipe_config->dp_m2_n2.gmch_n,
12121 pipe_config->dp_m2_n2.link_m,
12122 pipe_config->dp_m2_n2.link_n,
12123 pipe_config->dp_m2_n2.tu);
12124
Daniel Vetter55072d12014-11-20 16:10:28 +010012125 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
12126 pipe_config->has_audio,
12127 pipe_config->has_infoframe);
12128
Daniel Vetterc0b03412013-05-28 12:05:54 +020012129 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012130 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012131 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012132 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
12133 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030012134 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030012135 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
12136 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Tvrtko Ursulin0ec463d2015-05-13 16:51:08 +010012137 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
12138 crtc->num_scalers,
12139 pipe_config->scaler_state.scaler_users,
12140 pipe_config->scaler_state.scaler_id);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012141 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
12142 pipe_config->gmch_pfit.control,
12143 pipe_config->gmch_pfit.pgm_ratios,
12144 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012145 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020012146 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012147 pipe_config->pch_pfit.size,
12148 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012149 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030012150 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012151
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012152 if (IS_BROXTON(dev)) {
Imre Deak05712c12015-06-18 17:25:54 +030012153 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012154 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
Imre Deakc8453332015-06-18 17:25:55 +030012155 "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012156 pipe_config->ddi_pll_sel,
12157 pipe_config->dpll_hw_state.ebb0,
Imre Deak05712c12015-06-18 17:25:54 +030012158 pipe_config->dpll_hw_state.ebb4,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012159 pipe_config->dpll_hw_state.pll0,
12160 pipe_config->dpll_hw_state.pll1,
12161 pipe_config->dpll_hw_state.pll2,
12162 pipe_config->dpll_hw_state.pll3,
12163 pipe_config->dpll_hw_state.pll6,
12164 pipe_config->dpll_hw_state.pll8,
Imre Deak05712c12015-06-18 17:25:54 +030012165 pipe_config->dpll_hw_state.pll9,
Imre Deakc8453332015-06-18 17:25:55 +030012166 pipe_config->dpll_hw_state.pll10,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012167 pipe_config->dpll_hw_state.pcsdw12);
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070012168 } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012169 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
12170 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
12171 pipe_config->ddi_pll_sel,
12172 pipe_config->dpll_hw_state.ctrl1,
12173 pipe_config->dpll_hw_state.cfgcr1,
12174 pipe_config->dpll_hw_state.cfgcr2);
12175 } else if (HAS_DDI(dev)) {
Ville Syrjälä1260f072016-02-17 21:41:08 +020012176 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 +010012177 pipe_config->ddi_pll_sel,
Maarten Lankhorst00490c22015-11-16 14:42:12 +010012178 pipe_config->dpll_hw_state.wrpll,
12179 pipe_config->dpll_hw_state.spll);
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012180 } else {
12181 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
12182 "fp0: 0x%x, fp1: 0x%x\n",
12183 pipe_config->dpll_hw_state.dpll,
12184 pipe_config->dpll_hw_state.dpll_md,
12185 pipe_config->dpll_hw_state.fp0,
12186 pipe_config->dpll_hw_state.fp1);
12187 }
12188
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012189 DRM_DEBUG_KMS("planes on this crtc\n");
12190 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
12191 intel_plane = to_intel_plane(plane);
12192 if (intel_plane->pipe != crtc->pipe)
12193 continue;
12194
12195 state = to_intel_plane_state(plane->state);
12196 fb = state->base.fb;
12197 if (!fb) {
12198 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
12199 "disabled, scaler_id = %d\n",
12200 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12201 plane->base.id, intel_plane->pipe,
12202 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
12203 drm_plane_index(plane), state->scaler_id);
12204 continue;
12205 }
12206
12207 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
12208 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12209 plane->base.id, intel_plane->pipe,
12210 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
12211 drm_plane_index(plane));
12212 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
12213 fb->base.id, fb->width, fb->height, fb->pixel_format);
12214 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
12215 state->scaler_id,
12216 state->src.x1 >> 16, state->src.y1 >> 16,
12217 drm_rect_width(&state->src) >> 16,
12218 drm_rect_height(&state->src) >> 16,
12219 state->dst.x1, state->dst.y1,
12220 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
12221 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020012222}
12223
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012224static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012225{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012226 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012227 struct drm_connector *connector;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012228 unsigned int used_ports = 0;
12229
12230 /*
12231 * Walk the connector list instead of the encoder
12232 * list to detect the problem on ddi platforms
12233 * where there's just one encoder per digital port.
12234 */
Ville Syrjälä0bff4852015-12-10 18:22:31 +020012235 drm_for_each_connector(connector, dev) {
12236 struct drm_connector_state *connector_state;
12237 struct intel_encoder *encoder;
12238
12239 connector_state = drm_atomic_get_existing_connector_state(state, connector);
12240 if (!connector_state)
12241 connector_state = connector->state;
12242
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012243 if (!connector_state->best_encoder)
12244 continue;
12245
12246 encoder = to_intel_encoder(connector_state->best_encoder);
12247
12248 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012249
12250 switch (encoder->type) {
12251 unsigned int port_mask;
12252 case INTEL_OUTPUT_UNKNOWN:
12253 if (WARN_ON(!HAS_DDI(dev)))
12254 break;
12255 case INTEL_OUTPUT_DISPLAYPORT:
12256 case INTEL_OUTPUT_HDMI:
12257 case INTEL_OUTPUT_EDP:
12258 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12259
12260 /* the same port mustn't appear more than once */
12261 if (used_ports & port_mask)
12262 return false;
12263
12264 used_ports |= port_mask;
12265 default:
12266 break;
12267 }
12268 }
12269
12270 return true;
12271}
12272
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012273static void
12274clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12275{
12276 struct drm_crtc_state tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012277 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012278 struct intel_dpll_hw_state dpll_hw_state;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012279 struct intel_shared_dpll *shared_dpll;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012280 uint32_t ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012281 bool force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012282
Ander Conselvan de Oliveira7546a382015-05-20 09:03:27 +030012283 /* FIXME: before the switch to atomic started, a new pipe_config was
12284 * kzalloc'd. Code that depends on any field being zero should be
12285 * fixed, so that the crtc_state can be safely duplicated. For now,
12286 * only fields that are know to not cause problems are preserved. */
12287
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012288 tmp_state = crtc_state->base;
Chandra Konduru663a3642015-04-07 15:28:41 -070012289 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012290 shared_dpll = crtc_state->shared_dpll;
12291 dpll_hw_state = crtc_state->dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012292 ddi_pll_sel = crtc_state->ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012293 force_thru = crtc_state->pch_pfit.force_thru;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012294
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012295 memset(crtc_state, 0, sizeof *crtc_state);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012296
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012297 crtc_state->base = tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012298 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012299 crtc_state->shared_dpll = shared_dpll;
12300 crtc_state->dpll_hw_state = dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012301 crtc_state->ddi_pll_sel = ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012302 crtc_state->pch_pfit.force_thru = force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012303}
12304
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012305static int
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012306intel_modeset_pipe_config(struct drm_crtc *crtc,
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012307 struct intel_crtc_state *pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +020012308{
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012309 struct drm_atomic_state *state = pipe_config->base.state;
Daniel Vetter7758a112012-07-08 19:40:39 +020012310 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012311 struct drm_connector *connector;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012312 struct drm_connector_state *connector_state;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012313 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012314 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010012315 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020012316
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012317 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020012318
Daniel Vettere143a212013-07-04 12:01:15 +020012319 pipe_config->cpu_transcoder =
12320 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012321
Imre Deak2960bc92013-07-30 13:36:32 +030012322 /*
12323 * Sanitize sync polarity flags based on requested ones. If neither
12324 * positive or negative polarity is requested, treat this as meaning
12325 * negative polarity.
12326 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012327 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012328 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012329 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012330
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012331 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012332 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012333 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012334
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012335 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12336 pipe_config);
12337 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012338 goto fail;
12339
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012340 /*
12341 * Determine the real pipe dimensions. Note that stereo modes can
12342 * increase the actual pipe size due to the frame doubling and
12343 * insertion of additional space for blanks between the frame. This
12344 * is stored in the crtc timings. We use the requested mode to do this
12345 * computation to clearly distinguish it from the adjusted mode, which
12346 * can be changed by the connectors in the below retry loop.
12347 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012348 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080012349 &pipe_config->pipe_src_w,
12350 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012351
Daniel Vettere29c22c2013-02-21 00:00:16 +010012352encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020012353 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020012354 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020012355 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012356
Daniel Vetter135c81b2013-07-21 21:37:09 +020012357 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012358 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12359 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020012360
Daniel Vetter7758a112012-07-08 19:40:39 +020012361 /* Pass our mode to the connectors and the CRTC to give them a chance to
12362 * adjust it according to limitations or connector properties, and also
12363 * a chance to reject the mode entirely.
12364 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012365 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012366 if (connector_state->crtc != crtc)
12367 continue;
12368
12369 encoder = to_intel_encoder(connector_state->best_encoder);
12370
Daniel Vetterefea6e82013-07-21 21:36:59 +020012371 if (!(encoder->compute_config(encoder, pipe_config))) {
12372 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020012373 goto fail;
12374 }
12375 }
12376
Daniel Vetterff9a6752013-06-01 17:16:21 +020012377 /* Set default port clock if not overwritten by the encoder. Needs to be
12378 * done afterwards in case the encoder adjusts the mode. */
12379 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012380 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010012381 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012382
Daniel Vettera43f6e02013-06-07 23:10:32 +020012383 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010012384 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020012385 DRM_DEBUG_KMS("CRTC fixup failed\n");
12386 goto fail;
12387 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010012388
12389 if (ret == RETRY) {
12390 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12391 ret = -EINVAL;
12392 goto fail;
12393 }
12394
12395 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12396 retry = false;
12397 goto encoder_retry;
12398 }
12399
Daniel Vettere8fa4272015-08-12 11:43:34 +020012400 /* Dithering seems to not pass-through bits correctly when it should, so
12401 * only enable it on 6bpc panels. */
12402 pipe_config->dither = pipe_config->pipe_bpp == 6*3;
Daniel Vetter62f0ace2015-08-26 18:57:26 +020012403 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012404 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012405
Daniel Vetter7758a112012-07-08 19:40:39 +020012406fail:
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012407 return ret;
Daniel Vetter7758a112012-07-08 19:40:39 +020012408}
12409
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012410static void
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020012411intel_modeset_update_crtc_state(struct drm_atomic_state *state)
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012412{
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012413 struct drm_crtc *crtc;
12414 struct drm_crtc_state *crtc_state;
Maarten Lankhorst8a75d157c2015-07-13 16:30:14 +020012415 int i;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012416
Ville Syrjälä76688512014-01-10 11:28:06 +020012417 /* Double check state. */
Maarten Lankhorst8a75d157c2015-07-13 16:30:14 +020012418 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst3cb480b2015-06-01 12:49:49 +020012419 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
Maarten Lankhorstfc467a222015-06-01 12:50:07 +020012420
12421 /* Update hwmode for vblank functions */
12422 if (crtc->state->active)
12423 crtc->hwmode = crtc->state->adjusted_mode;
12424 else
12425 crtc->hwmode.crtc_clock = 0;
Maarten Lankhorst61067a52015-09-23 16:29:36 +020012426
12427 /*
12428 * Update legacy state to satisfy fbc code. This can
12429 * be removed when fbc uses the atomic state.
12430 */
12431 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
12432 struct drm_plane_state *plane_state = crtc->primary->state;
12433
12434 crtc->primary->fb = plane_state->fb;
12435 crtc->x = plane_state->src_x >> 16;
12436 crtc->y = plane_state->src_y >> 16;
12437 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012438 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012439}
12440
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012441static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012442{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012443 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012444
12445 if (clock1 == clock2)
12446 return true;
12447
12448 if (!clock1 || !clock2)
12449 return false;
12450
12451 diff = abs(clock1 - clock2);
12452
12453 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12454 return true;
12455
12456 return false;
12457}
12458
Daniel Vetter25c5b262012-07-08 22:08:04 +020012459#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12460 list_for_each_entry((intel_crtc), \
12461 &(dev)->mode_config.crtc_list, \
12462 base.head) \
Jani Nikula95150bd2015-11-24 21:21:56 +020012463 for_each_if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020012464
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012465static bool
12466intel_compare_m_n(unsigned int m, unsigned int n,
12467 unsigned int m2, unsigned int n2,
12468 bool exact)
12469{
12470 if (m == m2 && n == n2)
12471 return true;
12472
12473 if (exact || !m || !n || !m2 || !n2)
12474 return false;
12475
12476 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
12477
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012478 if (n > n2) {
12479 while (n > n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012480 m2 <<= 1;
12481 n2 <<= 1;
12482 }
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012483 } else if (n < n2) {
12484 while (n < n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012485 m <<= 1;
12486 n <<= 1;
12487 }
12488 }
12489
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012490 if (n != n2)
12491 return false;
12492
12493 return intel_fuzzy_clock_check(m, m2);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012494}
12495
12496static bool
12497intel_compare_link_m_n(const struct intel_link_m_n *m_n,
12498 struct intel_link_m_n *m2_n2,
12499 bool adjust)
12500{
12501 if (m_n->tu == m2_n2->tu &&
12502 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
12503 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
12504 intel_compare_m_n(m_n->link_m, m_n->link_n,
12505 m2_n2->link_m, m2_n2->link_n, !adjust)) {
12506 if (adjust)
12507 *m2_n2 = *m_n;
12508
12509 return true;
12510 }
12511
12512 return false;
12513}
12514
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012515static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012516intel_pipe_config_compare(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012517 struct intel_crtc_state *current_config,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012518 struct intel_crtc_state *pipe_config,
12519 bool adjust)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012520{
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012521 bool ret = true;
12522
12523#define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
12524 do { \
12525 if (!adjust) \
12526 DRM_ERROR(fmt, ##__VA_ARGS__); \
12527 else \
12528 DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
12529 } while (0)
12530
Daniel Vetter66e985c2013-06-05 13:34:20 +020012531#define PIPE_CONF_CHECK_X(name) \
12532 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012533 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012534 "(expected 0x%08x, found 0x%08x)\n", \
12535 current_config->name, \
12536 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012537 ret = false; \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012538 }
12539
Daniel Vetter08a24032013-04-19 11:25:34 +020012540#define PIPE_CONF_CHECK_I(name) \
12541 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012542 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter08a24032013-04-19 11:25:34 +020012543 "(expected %i, found %i)\n", \
12544 current_config->name, \
12545 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012546 ret = false; \
12547 }
12548
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012549#define PIPE_CONF_CHECK_P(name) \
12550 if (current_config->name != pipe_config->name) { \
12551 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12552 "(expected %p, found %p)\n", \
12553 current_config->name, \
12554 pipe_config->name); \
12555 ret = false; \
12556 }
12557
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012558#define PIPE_CONF_CHECK_M_N(name) \
12559 if (!intel_compare_link_m_n(&current_config->name, \
12560 &pipe_config->name,\
12561 adjust)) { \
12562 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12563 "(expected tu %i gmch %i/%i link %i/%i, " \
12564 "found tu %i, gmch %i/%i link %i/%i)\n", \
12565 current_config->name.tu, \
12566 current_config->name.gmch_m, \
12567 current_config->name.gmch_n, \
12568 current_config->name.link_m, \
12569 current_config->name.link_n, \
12570 pipe_config->name.tu, \
12571 pipe_config->name.gmch_m, \
12572 pipe_config->name.gmch_n, \
12573 pipe_config->name.link_m, \
12574 pipe_config->name.link_n); \
12575 ret = false; \
12576 }
12577
Daniel Vetter55c561a2016-03-30 11:34:36 +020012578/* This is required for BDW+ where there is only one set of registers for
12579 * switching between high and low RR.
12580 * This macro can be used whenever a comparison has to be made between one
12581 * hw state and multiple sw state variables.
12582 */
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012583#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
12584 if (!intel_compare_link_m_n(&current_config->name, \
12585 &pipe_config->name, adjust) && \
12586 !intel_compare_link_m_n(&current_config->alt_name, \
12587 &pipe_config->name, adjust)) { \
12588 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12589 "(expected tu %i gmch %i/%i link %i/%i, " \
12590 "or tu %i gmch %i/%i link %i/%i, " \
12591 "found tu %i, gmch %i/%i link %i/%i)\n", \
12592 current_config->name.tu, \
12593 current_config->name.gmch_m, \
12594 current_config->name.gmch_n, \
12595 current_config->name.link_m, \
12596 current_config->name.link_n, \
12597 current_config->alt_name.tu, \
12598 current_config->alt_name.gmch_m, \
12599 current_config->alt_name.gmch_n, \
12600 current_config->alt_name.link_m, \
12601 current_config->alt_name.link_n, \
12602 pipe_config->name.tu, \
12603 pipe_config->name.gmch_m, \
12604 pipe_config->name.gmch_n, \
12605 pipe_config->name.link_m, \
12606 pipe_config->name.link_n); \
12607 ret = false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010012608 }
12609
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012610#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12611 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012612 INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012613 "(expected %i, found %i)\n", \
12614 current_config->name & (mask), \
12615 pipe_config->name & (mask)); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012616 ret = false; \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012617 }
12618
Ville Syrjälä5e550652013-09-06 23:29:07 +030012619#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12620 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012621 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012622 "(expected %i, found %i)\n", \
12623 current_config->name, \
12624 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012625 ret = false; \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012626 }
12627
Daniel Vetterbb760062013-06-06 14:55:52 +020012628#define PIPE_CONF_QUIRK(quirk) \
12629 ((current_config->quirks | pipe_config->quirks) & (quirk))
12630
Daniel Vettereccb1402013-05-22 00:50:22 +020012631 PIPE_CONF_CHECK_I(cpu_transcoder);
12632
Daniel Vetter08a24032013-04-19 11:25:34 +020012633 PIPE_CONF_CHECK_I(has_pch_encoder);
12634 PIPE_CONF_CHECK_I(fdi_lanes);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012635 PIPE_CONF_CHECK_M_N(fdi_m_n);
Daniel Vetter08a24032013-04-19 11:25:34 +020012636
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012637 PIPE_CONF_CHECK_I(has_dp_encoder);
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012638 PIPE_CONF_CHECK_I(lane_count);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012639
12640 if (INTEL_INFO(dev)->gen < 8) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012641 PIPE_CONF_CHECK_M_N(dp_m_n);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012642
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012643 if (current_config->has_drrs)
12644 PIPE_CONF_CHECK_M_N(dp_m2_n2);
12645 } else
12646 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012647
Jani Nikulaa65347b2015-11-27 12:21:46 +020012648 PIPE_CONF_CHECK_I(has_dsi_encoder);
12649
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012650 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12651 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12652 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12653 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12654 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12655 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012656
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012657 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12658 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12659 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12660 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12661 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12662 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012663
Daniel Vetterc93f54c2013-06-27 19:47:19 +020012664 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b52014-04-24 23:54:47 +020012665 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012666 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
Wayne Boyer666a4532015-12-09 12:29:35 -080012667 IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012668 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080012669 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020012670
Daniel Vetter9ed109a2014-04-24 23:54:52 +020012671 PIPE_CONF_CHECK_I(has_audio);
12672
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012673 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012674 DRM_MODE_FLAG_INTERLACE);
12675
Daniel Vetterbb760062013-06-06 14:55:52 +020012676 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012677 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012678 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012679 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012680 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012681 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012682 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012683 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012684 DRM_MODE_FLAG_NVSYNC);
12685 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012686
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030012687 PIPE_CONF_CHECK_X(gmch_pfit.control);
Daniel Vettere2ff2d42015-07-15 14:15:50 +020012688 /* pfit ratios are autocomputed by the hw on gen4+ */
12689 if (INTEL_INFO(dev)->gen < 4)
Ville Syrjälä7f7d8dd2016-03-15 16:40:07 +020012690 PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030012691 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
Daniel Vetter99535992014-04-13 12:00:33 +020012692
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020012693 if (!adjust) {
12694 PIPE_CONF_CHECK_I(pipe_src_w);
12695 PIPE_CONF_CHECK_I(pipe_src_h);
12696
12697 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12698 if (current_config->pch_pfit.enabled) {
12699 PIPE_CONF_CHECK_X(pch_pfit.pos);
12700 PIPE_CONF_CHECK_X(pch_pfit.size);
12701 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012702
Maarten Lankhorst7aefe2b2015-09-14 11:30:10 +020012703 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12704 }
Chandra Kondurua1b22782015-04-07 15:28:45 -070012705
Jesse Barnese59150d2014-01-07 13:30:45 -080012706 /* BDW+ don't expose a synchronous way to read the state */
12707 if (IS_HASWELL(dev))
12708 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012709
Ville Syrjälä282740f2013-09-04 18:30:03 +030012710 PIPE_CONF_CHECK_I(double_wide);
12711
Daniel Vetter26804af2014-06-25 22:01:55 +030012712 PIPE_CONF_CHECK_X(ddi_pll_sel);
12713
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012714 PIPE_CONF_CHECK_P(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012715 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020012716 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012717 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12718 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030012719 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Maarten Lankhorst00490c22015-11-16 14:42:12 +010012720 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000012721 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12722 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12723 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012724
Ville Syrjälä42571ae2013-09-06 23:29:00 +030012725 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12726 PIPE_CONF_CHECK_I(pipe_bpp);
12727
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012728 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080012729 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030012730
Daniel Vetter66e985c2013-06-05 13:34:20 +020012731#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020012732#undef PIPE_CONF_CHECK_I
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012733#undef PIPE_CONF_CHECK_P
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012734#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030012735#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020012736#undef PIPE_CONF_QUIRK
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012737#undef INTEL_ERR_OR_DBG_KMS
Daniel Vetter627eb5a2013-04-29 19:33:42 +020012738
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012739 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012740}
12741
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020012742static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
12743 const struct intel_crtc_state *pipe_config)
12744{
12745 if (pipe_config->has_pch_encoder) {
Ville Syrjälä21a727b2016-02-17 21:41:10 +020012746 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020012747 &pipe_config->fdi_m_n);
12748 int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
12749
12750 /*
12751 * FDI already provided one idea for the dotclock.
12752 * Yell if the encoder disagrees.
12753 */
12754 WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
12755 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
12756 fdi_dotclock, dotclock);
12757 }
12758}
12759
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012760static void verify_wm_state(struct drm_crtc *crtc,
12761 struct drm_crtc_state *new_state)
Damien Lespiau08db6652014-11-04 17:06:52 +000012762{
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012763 struct drm_device *dev = crtc->dev;
Damien Lespiau08db6652014-11-04 17:06:52 +000012764 struct drm_i915_private *dev_priv = dev->dev_private;
12765 struct skl_ddb_allocation hw_ddb, *sw_ddb;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012766 struct skl_ddb_entry *hw_entry, *sw_entry;
12767 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12768 const enum pipe pipe = intel_crtc->pipe;
Damien Lespiau08db6652014-11-04 17:06:52 +000012769 int plane;
12770
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012771 if (INTEL_INFO(dev)->gen < 9 || !new_state->active)
Damien Lespiau08db6652014-11-04 17:06:52 +000012772 return;
12773
12774 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12775 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12776
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012777 /* planes */
12778 for_each_plane(dev_priv, pipe, plane) {
12779 hw_entry = &hw_ddb.plane[pipe][plane];
12780 sw_entry = &sw_ddb->plane[pipe][plane];
Damien Lespiau08db6652014-11-04 17:06:52 +000012781
12782 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12783 continue;
12784
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012785 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12786 "(expected (%u,%u), found (%u,%u))\n",
12787 pipe_name(pipe), plane + 1,
12788 sw_entry->start, sw_entry->end,
12789 hw_entry->start, hw_entry->end);
12790 }
12791
12792 /* cursor */
12793 hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
12794 sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
12795
12796 if (!skl_ddb_entry_equal(hw_entry, sw_entry)) {
Damien Lespiau08db6652014-11-04 17:06:52 +000012797 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12798 "(expected (%u,%u), found (%u,%u))\n",
12799 pipe_name(pipe),
12800 sw_entry->start, sw_entry->end,
12801 hw_entry->start, hw_entry->end);
12802 }
12803}
12804
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012805static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012806verify_connector_state(struct drm_device *dev, struct drm_crtc *crtc)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012807{
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012808 struct drm_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012809
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012810 drm_for_each_connector(connector, dev) {
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012811 struct drm_encoder *encoder = connector->encoder;
12812 struct drm_connector_state *state = connector->state;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012813
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012814 if (state->crtc != crtc)
12815 continue;
12816
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012817 intel_connector_verify_state(to_intel_connector(connector));
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012818
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012819 I915_STATE_WARN(state->best_encoder != encoder,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012820 "connector's atomic encoder doesn't match legacy encoder\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012821 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012822}
12823
12824static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012825verify_encoder_state(struct drm_device *dev)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012826{
12827 struct intel_encoder *encoder;
12828 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012829
Damien Lespiaub2784e12014-08-05 11:29:37 +010012830 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012831 bool enabled = false;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012832 enum pipe pipe;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012833
12834 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
12835 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030012836 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012837
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012838 for_each_intel_connector(dev, connector) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012839 if (connector->base.state->best_encoder != &encoder->base)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012840 continue;
12841 enabled = true;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012842
12843 I915_STATE_WARN(connector->base.state->crtc !=
12844 encoder->base.crtc,
12845 "connector's crtc doesn't match encoder crtc\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012846 }
Dave Airlie0e32b392014-05-02 14:02:48 +100012847
Rob Clarke2c719b2014-12-15 13:56:32 -050012848 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012849 "encoder's enabled state mismatch "
12850 "(expected %i, found %i)\n",
12851 !!encoder->base.crtc, enabled);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012852
12853 if (!encoder->base.crtc) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012854 bool active;
12855
12856 active = encoder->get_hw_state(encoder, &pipe);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012857 I915_STATE_WARN(active,
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012858 "encoder detached but still enabled on pipe %c.\n",
12859 pipe_name(pipe));
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012860 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012861 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012862}
12863
12864static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012865verify_crtc_state(struct drm_crtc *crtc,
12866 struct drm_crtc_state *old_crtc_state,
12867 struct drm_crtc_state *new_crtc_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012868{
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012869 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030012870 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012871 struct intel_encoder *encoder;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012872 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12873 struct intel_crtc_state *pipe_config, *sw_config;
12874 struct drm_atomic_state *old_state;
12875 bool active;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012876
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012877 old_state = old_crtc_state->state;
12878 __drm_atomic_helper_crtc_destroy_state(crtc, old_crtc_state);
12879 pipe_config = to_intel_crtc_state(old_crtc_state);
12880 memset(pipe_config, 0, sizeof(*pipe_config));
12881 pipe_config->base.crtc = crtc;
12882 pipe_config->base.state = old_state;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012883
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012884 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012885
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012886 active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012887
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012888 /* hw state is inconsistent with the pipe quirk */
12889 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12890 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
12891 active = new_crtc_state->active;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012892
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012893 I915_STATE_WARN(new_crtc_state->active != active,
12894 "crtc active state doesn't match with hw state "
12895 "(expected %i, found %i)\n", new_crtc_state->active, active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012896
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012897 I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
12898 "transitional active state does not match atomic hw state "
12899 "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012900
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012901 for_each_encoder_on_crtc(dev, crtc, encoder) {
12902 enum pipe pipe;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012903
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012904 active = encoder->get_hw_state(encoder, &pipe);
12905 I915_STATE_WARN(active != new_crtc_state->active,
12906 "[ENCODER:%i] active %i with crtc active %i\n",
12907 encoder->base.base.id, active, new_crtc_state->active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012908
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012909 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
12910 "Encoder connected to wrong pipe %c\n",
12911 pipe_name(pipe));
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012912
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012913 if (active)
12914 encoder->get_config(encoder, pipe_config);
12915 }
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012916
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012917 if (!new_crtc_state->active)
12918 return;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012919
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012920 intel_pipe_config_sanity_check(dev_priv, pipe_config);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012921
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012922 sw_config = to_intel_crtc_state(crtc->state);
12923 if (!intel_pipe_config_compare(dev, sw_config,
12924 pipe_config, false)) {
12925 I915_STATE_WARN(1, "pipe state doesn't match!\n");
12926 intel_dump_pipe_config(intel_crtc, pipe_config,
12927 "[hw state]");
12928 intel_dump_pipe_config(intel_crtc, sw_config,
12929 "[sw state]");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012930 }
12931}
12932
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012933static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012934verify_single_dpll_state(struct drm_i915_private *dev_priv,
12935 struct intel_shared_dpll *pll,
12936 struct drm_crtc *crtc,
12937 struct drm_crtc_state *new_state)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012938{
12939 struct intel_dpll_hw_state dpll_hw_state;
12940 unsigned crtc_mask;
12941 bool active;
12942
12943 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
12944
12945 DRM_DEBUG_KMS("%s\n", pll->name);
12946
12947 active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
12948
12949 if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
12950 I915_STATE_WARN(!pll->on && pll->active_mask,
12951 "pll in active use but not on in sw tracking\n");
12952 I915_STATE_WARN(pll->on && !pll->active_mask,
12953 "pll is on but not used by any active crtc\n");
12954 I915_STATE_WARN(pll->on != active,
12955 "pll on state mismatch (expected %i, found %i)\n",
12956 pll->on, active);
12957 }
12958
12959 if (!crtc) {
12960 I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
12961 "more active pll users than references: %x vs %x\n",
12962 pll->active_mask, pll->config.crtc_mask);
12963
12964 return;
12965 }
12966
12967 crtc_mask = 1 << drm_crtc_index(crtc);
12968
12969 if (new_state->active)
12970 I915_STATE_WARN(!(pll->active_mask & crtc_mask),
12971 "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
12972 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
12973 else
12974 I915_STATE_WARN(pll->active_mask & crtc_mask,
12975 "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
12976 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
12977
12978 I915_STATE_WARN(!(pll->config.crtc_mask & crtc_mask),
12979 "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
12980 crtc_mask, pll->config.crtc_mask);
12981
12982 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state,
12983 &dpll_hw_state,
12984 sizeof(dpll_hw_state)),
12985 "pll hw state mismatch\n");
12986}
12987
12988static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012989verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
12990 struct drm_crtc_state *old_crtc_state,
12991 struct drm_crtc_state *new_crtc_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012992{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012993 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012994 struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
12995 struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
12996
12997 if (new_state->shared_dpll)
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012998 verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012999
13000 if (old_state->shared_dpll &&
13001 old_state->shared_dpll != new_state->shared_dpll) {
13002 unsigned crtc_mask = 1 << drm_crtc_index(crtc);
13003 struct intel_shared_dpll *pll = old_state->shared_dpll;
13004
13005 I915_STATE_WARN(pll->active_mask & crtc_mask,
13006 "pll active mismatch (didn't expect pipe %c in active mask)\n",
13007 pipe_name(drm_crtc_index(crtc)));
13008 I915_STATE_WARN(pll->config.crtc_mask & crtc_mask,
13009 "pll enabled crtcs mismatch (found %x in enabled mask)\n",
13010 pipe_name(drm_crtc_index(crtc)));
13011 }
13012}
13013
13014static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013015intel_modeset_verify_crtc(struct drm_crtc *crtc,
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013016 struct drm_crtc_state *old_state,
13017 struct drm_crtc_state *new_state)
13018{
13019 if (!needs_modeset(new_state) &&
13020 !to_intel_crtc_state(new_state)->update_pipe)
13021 return;
13022
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013023 verify_wm_state(crtc, new_state);
13024 verify_connector_state(crtc->dev, crtc);
13025 verify_crtc_state(crtc, old_state, new_state);
13026 verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013027}
13028
13029static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013030verify_disabled_dpll_state(struct drm_device *dev)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013031{
13032 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013033 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020013034
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013035 for (i = 0; i < dev_priv->num_shared_dpll; i++)
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013036 verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013037}
Daniel Vetter53589012013-06-05 13:34:16 +020013038
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013039static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013040intel_modeset_verify_disabled(struct drm_device *dev)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013041{
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013042 verify_encoder_state(dev);
13043 verify_connector_state(dev, NULL);
13044 verify_disabled_dpll_state(dev);
Daniel Vettere2e1ed42012-07-08 21:14:38 +020013045}
13046
Ville Syrjälä80715b22014-05-15 20:23:23 +030013047static void update_scanline_offset(struct intel_crtc *crtc)
13048{
13049 struct drm_device *dev = crtc->base.dev;
13050
13051 /*
13052 * The scanline counter increments at the leading edge of hsync.
13053 *
13054 * On most platforms it starts counting from vtotal-1 on the
13055 * first active line. That means the scanline counter value is
13056 * always one less than what we would expect. Ie. just after
13057 * start of vblank, which also occurs at start of hsync (on the
13058 * last active line), the scanline counter will read vblank_start-1.
13059 *
13060 * On gen2 the scanline counter starts counting from 1 instead
13061 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
13062 * to keep the value positive), instead of adding one.
13063 *
13064 * On HSW+ the behaviour of the scanline counter depends on the output
13065 * type. For DP ports it behaves like most other platforms, but on HDMI
13066 * there's an extra 1 line difference. So we need to add two instead of
13067 * one to the value.
13068 */
13069 if (IS_GEN2(dev)) {
Ville Syrjälä124abe02015-09-08 13:40:45 +030013070 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030013071 int vtotal;
13072
Ville Syrjälä124abe02015-09-08 13:40:45 +030013073 vtotal = adjusted_mode->crtc_vtotal;
13074 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
Ville Syrjälä80715b22014-05-15 20:23:23 +030013075 vtotal /= 2;
13076
13077 crtc->scanline_offset = vtotal - 1;
13078 } else if (HAS_DDI(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +030013079 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030013080 crtc->scanline_offset = 2;
13081 } else
13082 crtc->scanline_offset = 1;
13083}
13084
Maarten Lankhorstad421372015-06-15 12:33:42 +020013085static void intel_modeset_clear_plls(struct drm_atomic_state *state)
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013086{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030013087 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013088 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstad421372015-06-15 12:33:42 +020013089 struct intel_shared_dpll_config *shared_dpll = NULL;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013090 struct drm_crtc *crtc;
13091 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013092 int i;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013093
13094 if (!dev_priv->display.crtc_compute_clock)
Maarten Lankhorstad421372015-06-15 12:33:42 +020013095 return;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013096
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013097 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013098 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013099 struct intel_shared_dpll *old_dpll =
13100 to_intel_crtc_state(crtc->state)->shared_dpll;
Maarten Lankhorstad421372015-06-15 12:33:42 +020013101
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013102 if (!needs_modeset(crtc_state))
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030013103 continue;
13104
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013105 to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013106
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013107 if (!old_dpll)
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013108 continue;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013109
Maarten Lankhorstad421372015-06-15 12:33:42 +020013110 if (!shared_dpll)
13111 shared_dpll = intel_atomic_get_shared_dpll_state(state);
13112
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013113 intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013114 }
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013115}
13116
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013117/*
13118 * This implements the workaround described in the "notes" section of the mode
13119 * set sequence documentation. When going from no pipes or single pipe to
13120 * multiple pipes, and planes are enabled after the pipe, we need to wait at
13121 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
13122 */
13123static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
13124{
13125 struct drm_crtc_state *crtc_state;
13126 struct intel_crtc *intel_crtc;
13127 struct drm_crtc *crtc;
13128 struct intel_crtc_state *first_crtc_state = NULL;
13129 struct intel_crtc_state *other_crtc_state = NULL;
13130 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
13131 int i;
13132
13133 /* look at all crtc's that are going to be enabled in during modeset */
13134 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13135 intel_crtc = to_intel_crtc(crtc);
13136
13137 if (!crtc_state->active || !needs_modeset(crtc_state))
13138 continue;
13139
13140 if (first_crtc_state) {
13141 other_crtc_state = to_intel_crtc_state(crtc_state);
13142 break;
13143 } else {
13144 first_crtc_state = to_intel_crtc_state(crtc_state);
13145 first_pipe = intel_crtc->pipe;
13146 }
13147 }
13148
13149 /* No workaround needed? */
13150 if (!first_crtc_state)
13151 return 0;
13152
13153 /* w/a possibly needed, check how many crtc's are already enabled. */
13154 for_each_intel_crtc(state->dev, intel_crtc) {
13155 struct intel_crtc_state *pipe_config;
13156
13157 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
13158 if (IS_ERR(pipe_config))
13159 return PTR_ERR(pipe_config);
13160
13161 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
13162
13163 if (!pipe_config->base.active ||
13164 needs_modeset(&pipe_config->base))
13165 continue;
13166
13167 /* 2 or more enabled crtcs means no need for w/a */
13168 if (enabled_pipe != INVALID_PIPE)
13169 return 0;
13170
13171 enabled_pipe = intel_crtc->pipe;
13172 }
13173
13174 if (enabled_pipe != INVALID_PIPE)
13175 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
13176 else if (other_crtc_state)
13177 other_crtc_state->hsw_workaround_pipe = first_pipe;
13178
13179 return 0;
13180}
13181
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013182static int intel_modeset_all_pipes(struct drm_atomic_state *state)
13183{
13184 struct drm_crtc *crtc;
13185 struct drm_crtc_state *crtc_state;
13186 int ret = 0;
13187
13188 /* add all active pipes to the state */
13189 for_each_crtc(state->dev, crtc) {
13190 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13191 if (IS_ERR(crtc_state))
13192 return PTR_ERR(crtc_state);
13193
13194 if (!crtc_state->active || needs_modeset(crtc_state))
13195 continue;
13196
13197 crtc_state->mode_changed = true;
13198
13199 ret = drm_atomic_add_affected_connectors(state, crtc);
13200 if (ret)
13201 break;
13202
13203 ret = drm_atomic_add_affected_planes(state, crtc);
13204 if (ret)
13205 break;
13206 }
13207
13208 return ret;
13209}
13210
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013211static int intel_modeset_checks(struct drm_atomic_state *state)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013212{
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013213 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
13214 struct drm_i915_private *dev_priv = state->dev->dev_private;
13215 struct drm_crtc *crtc;
13216 struct drm_crtc_state *crtc_state;
13217 int ret = 0, i;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013218
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013219 if (!check_digital_port_conflicts(state)) {
13220 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
13221 return -EINVAL;
13222 }
13223
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013224 intel_state->modeset = true;
13225 intel_state->active_crtcs = dev_priv->active_crtcs;
13226
13227 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13228 if (crtc_state->active)
13229 intel_state->active_crtcs |= 1 << i;
13230 else
13231 intel_state->active_crtcs &= ~(1 << i);
13232 }
13233
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013234 /*
13235 * See if the config requires any additional preparation, e.g.
13236 * to adjust global state with pipes off. We need to do this
13237 * here so we can get the modeset_pipe updated config for the new
13238 * mode set on this crtc. For other crtcs we need to use the
13239 * adjusted_mode bits in the crtc directly.
13240 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013241 if (dev_priv->display.modeset_calc_cdclk) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013242 ret = dev_priv->display.modeset_calc_cdclk(state);
13243
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013244 if (!ret && intel_state->dev_cdclk != dev_priv->cdclk_freq)
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013245 ret = intel_modeset_all_pipes(state);
13246
13247 if (ret < 0)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013248 return ret;
Maarten Lankhorste8788cb2016-02-16 10:25:11 +010013249
13250 DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
13251 intel_state->cdclk, intel_state->dev_cdclk);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013252 } else
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013253 to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013254
Maarten Lankhorstad421372015-06-15 12:33:42 +020013255 intel_modeset_clear_plls(state);
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013256
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013257 if (IS_HASWELL(dev_priv))
Maarten Lankhorstad421372015-06-15 12:33:42 +020013258 return haswell_mode_set_planes_workaround(state);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013259
Maarten Lankhorstad421372015-06-15 12:33:42 +020013260 return 0;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013261}
13262
Matt Roperaa363132015-09-24 15:53:18 -070013263/*
13264 * Handle calculation of various watermark data at the end of the atomic check
13265 * phase. The code here should be run after the per-crtc and per-plane 'check'
13266 * handlers to ensure that all derived state has been updated.
13267 */
13268static void calc_watermark_data(struct drm_atomic_state *state)
13269{
13270 struct drm_device *dev = state->dev;
13271 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
13272 struct drm_crtc *crtc;
13273 struct drm_crtc_state *cstate;
13274 struct drm_plane *plane;
13275 struct drm_plane_state *pstate;
13276
13277 /*
13278 * Calculate watermark configuration details now that derived
13279 * plane/crtc state is all properly updated.
13280 */
13281 drm_for_each_crtc(crtc, dev) {
13282 cstate = drm_atomic_get_existing_crtc_state(state, crtc) ?:
13283 crtc->state;
13284
13285 if (cstate->active)
13286 intel_state->wm_config.num_pipes_active++;
13287 }
13288 drm_for_each_legacy_plane(plane, dev) {
13289 pstate = drm_atomic_get_existing_plane_state(state, plane) ?:
13290 plane->state;
13291
13292 if (!to_intel_plane_state(pstate)->visible)
13293 continue;
13294
13295 intel_state->wm_config.sprites_enabled = true;
13296 if (pstate->crtc_w != pstate->src_w >> 16 ||
13297 pstate->crtc_h != pstate->src_h >> 16)
13298 intel_state->wm_config.sprites_scaled = true;
13299 }
13300}
13301
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013302/**
13303 * intel_atomic_check - validate state object
13304 * @dev: drm device
13305 * @state: state to validate
13306 */
13307static int intel_atomic_check(struct drm_device *dev,
13308 struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020013309{
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013310 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roperaa363132015-09-24 15:53:18 -070013311 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013312 struct drm_crtc *crtc;
13313 struct drm_crtc_state *crtc_state;
13314 int ret, i;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013315 bool any_ms = false;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013316
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013317 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020013318 if (ret)
13319 return ret;
13320
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013321 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013322 struct intel_crtc_state *pipe_config =
13323 to_intel_crtc_state(crtc_state);
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013324
13325 /* Catch I915_MODE_FLAG_INHERITED */
13326 if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
13327 crtc_state->mode_changed = true;
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013328
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013329 if (!crtc_state->enable) {
13330 if (needs_modeset(crtc_state))
13331 any_ms = true;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013332 continue;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013333 }
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013334
Daniel Vetter26495482015-07-15 14:15:52 +020013335 if (!needs_modeset(crtc_state))
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013336 continue;
13337
Daniel Vetter26495482015-07-15 14:15:52 +020013338 /* FIXME: For only active_changed we shouldn't need to do any
13339 * state recomputation at all. */
13340
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013341 ret = drm_atomic_add_affected_connectors(state, crtc);
13342 if (ret)
13343 return ret;
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013344
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013345 ret = intel_modeset_pipe_config(crtc, pipe_config);
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013346 if (ret)
13347 return ret;
13348
Jani Nikula73831232015-11-19 10:26:30 +020013349 if (i915.fastboot &&
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013350 intel_pipe_config_compare(dev,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013351 to_intel_crtc_state(crtc->state),
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013352 pipe_config, true)) {
Daniel Vetter26495482015-07-15 14:15:52 +020013353 crtc_state->mode_changed = false;
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013354 to_intel_crtc_state(crtc_state)->update_pipe = true;
Daniel Vetter26495482015-07-15 14:15:52 +020013355 }
13356
13357 if (needs_modeset(crtc_state)) {
13358 any_ms = true;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013359
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013360 ret = drm_atomic_add_affected_planes(state, crtc);
13361 if (ret)
13362 return ret;
13363 }
13364
Daniel Vetter26495482015-07-15 14:15:52 +020013365 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
13366 needs_modeset(crtc_state) ?
13367 "[modeset]" : "[fastset]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013368 }
13369
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013370 if (any_ms) {
13371 ret = intel_modeset_checks(state);
13372
13373 if (ret)
13374 return ret;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013375 } else
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013376 intel_state->cdclk = dev_priv->cdclk_freq;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013377
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013378 ret = drm_atomic_helper_check_planes(dev, state);
Matt Roperaa363132015-09-24 15:53:18 -070013379 if (ret)
13380 return ret;
13381
Paulo Zanonif51be2e2016-01-19 11:35:50 -020013382 intel_fbc_choose_crtc(dev_priv, state);
Matt Roperaa363132015-09-24 15:53:18 -070013383 calc_watermark_data(state);
13384
13385 return 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020013386}
13387
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013388static int intel_atomic_prepare_commit(struct drm_device *dev,
13389 struct drm_atomic_state *state,
13390 bool async)
13391{
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013392 struct drm_i915_private *dev_priv = dev->dev_private;
13393 struct drm_plane_state *plane_state;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013394 struct drm_crtc_state *crtc_state;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013395 struct drm_plane *plane;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013396 struct drm_crtc *crtc;
13397 int i, ret;
13398
13399 if (async) {
13400 DRM_DEBUG_KMS("i915 does not yet support async commit\n");
13401 return -EINVAL;
13402 }
13403
13404 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13405 ret = intel_crtc_wait_for_pending_flips(crtc);
13406 if (ret)
13407 return ret;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013408
13409 if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
13410 flush_workqueue(dev_priv->wq);
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013411 }
13412
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013413 ret = mutex_lock_interruptible(&dev->struct_mutex);
13414 if (ret)
13415 return ret;
13416
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013417 ret = drm_atomic_helper_prepare_planes(dev, state);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013418 if (!ret && !async && !i915_reset_in_progress(&dev_priv->gpu_error)) {
13419 u32 reset_counter;
13420
13421 reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
13422 mutex_unlock(&dev->struct_mutex);
13423
13424 for_each_plane_in_state(state, plane, plane_state, i) {
13425 struct intel_plane_state *intel_plane_state =
13426 to_intel_plane_state(plane_state);
13427
13428 if (!intel_plane_state->wait_req)
13429 continue;
13430
13431 ret = __i915_wait_request(intel_plane_state->wait_req,
13432 reset_counter, true,
13433 NULL, NULL);
13434
13435 /* Swallow -EIO errors to allow updates during hw lockup. */
13436 if (ret == -EIO)
13437 ret = 0;
13438
13439 if (ret)
13440 break;
13441 }
13442
13443 if (!ret)
13444 return 0;
13445
13446 mutex_lock(&dev->struct_mutex);
13447 drm_atomic_helper_cleanup_planes(dev, state);
13448 }
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013449
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013450 mutex_unlock(&dev->struct_mutex);
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013451 return ret;
13452}
13453
Maarten Lankhorste8861672016-02-24 11:24:26 +010013454static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
13455 struct drm_i915_private *dev_priv,
13456 unsigned crtc_mask)
13457{
13458 unsigned last_vblank_count[I915_MAX_PIPES];
13459 enum pipe pipe;
13460 int ret;
13461
13462 if (!crtc_mask)
13463 return;
13464
13465 for_each_pipe(dev_priv, pipe) {
13466 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13467
13468 if (!((1 << pipe) & crtc_mask))
13469 continue;
13470
13471 ret = drm_crtc_vblank_get(crtc);
13472 if (WARN_ON(ret != 0)) {
13473 crtc_mask &= ~(1 << pipe);
13474 continue;
13475 }
13476
13477 last_vblank_count[pipe] = drm_crtc_vblank_count(crtc);
13478 }
13479
13480 for_each_pipe(dev_priv, pipe) {
13481 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13482 long lret;
13483
13484 if (!((1 << pipe) & crtc_mask))
13485 continue;
13486
13487 lret = wait_event_timeout(dev->vblank[pipe].queue,
13488 last_vblank_count[pipe] !=
13489 drm_crtc_vblank_count(crtc),
13490 msecs_to_jiffies(50));
13491
13492 WARN_ON(!lret);
13493
13494 drm_crtc_vblank_put(crtc);
13495 }
13496}
13497
13498static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
13499{
13500 /* fb updated, need to unpin old fb */
13501 if (crtc_state->fb_changed)
13502 return true;
13503
13504 /* wm changes, need vblank before final wm's */
Ville Syrjäläcaed3612016-03-09 19:07:25 +020013505 if (crtc_state->update_wm_post)
Maarten Lankhorste8861672016-02-24 11:24:26 +010013506 return true;
13507
13508 /*
13509 * cxsr is re-enabled after vblank.
Ville Syrjäläcaed3612016-03-09 19:07:25 +020013510 * This is already handled by crtc_state->update_wm_post,
Maarten Lankhorste8861672016-02-24 11:24:26 +010013511 * but added for clarity.
13512 */
13513 if (crtc_state->disable_cxsr)
13514 return true;
13515
13516 return false;
13517}
13518
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013519/**
13520 * intel_atomic_commit - commit validated state object
13521 * @dev: DRM device
13522 * @state: the top-level driver state object
13523 * @async: asynchronous commit
13524 *
13525 * This function commits a top-level state object that has been validated
13526 * with drm_atomic_helper_check().
13527 *
13528 * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
13529 * we can only handle plane-related operations and do not yet support
13530 * asynchronous commit.
13531 *
13532 * RETURNS
13533 * Zero for success or -errno.
13534 */
13535static int intel_atomic_commit(struct drm_device *dev,
13536 struct drm_atomic_state *state,
13537 bool async)
Daniel Vettera6778b32012-07-02 09:56:42 +020013538{
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013539 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Jani Nikulafbee40d2014-03-31 14:27:18 +030013540 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013541 struct drm_crtc_state *old_crtc_state;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013542 struct drm_crtc *crtc;
Matt Ropered4a6a72016-02-23 17:20:13 -080013543 struct intel_crtc_state *intel_cstate;
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013544 int ret = 0, i;
13545 bool hw_check = intel_state->modeset;
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013546 unsigned long put_domains[I915_MAX_PIPES] = {};
Maarten Lankhorste8861672016-02-24 11:24:26 +010013547 unsigned crtc_vblank_mask = 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020013548
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013549 ret = intel_atomic_prepare_commit(dev, state, async);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013550 if (ret) {
13551 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013552 return ret;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013553 }
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013554
Maarten Lankhorst1c5e19f2015-06-01 12:50:06 +020013555 drm_atomic_helper_swap_state(dev, state);
Maarten Lankhorsta1475e72016-03-14 09:27:53 +010013556 dev_priv->wm.config = intel_state->wm_config;
13557 intel_shared_dpll_commit(state);
Maarten Lankhorst1c5e19f2015-06-01 12:50:06 +020013558
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013559 if (intel_state->modeset) {
13560 memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
13561 sizeof(intel_state->min_pixclk));
13562 dev_priv->active_crtcs = intel_state->active_crtcs;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013563 dev_priv->atomic_cdclk_freq = intel_state->cdclk;
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013564
13565 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013566 }
13567
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013568 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013569 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13570
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013571 if (needs_modeset(crtc->state) ||
13572 to_intel_crtc_state(crtc->state)->update_pipe) {
13573 hw_check = true;
13574
13575 put_domains[to_intel_crtc(crtc)->pipe] =
13576 modeset_get_crtc_power_domains(crtc,
13577 to_intel_crtc_state(crtc->state));
13578 }
13579
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013580 if (!needs_modeset(crtc->state))
13581 continue;
13582
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013583 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
Daniel Vetter460da9162013-03-27 00:44:51 +010013584
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013585 if (old_crtc_state->active) {
13586 intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013587 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013588 intel_crtc->active = false;
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -020013589 intel_fbc_disable(intel_crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013590 intel_disable_shared_dpll(intel_crtc);
Ville Syrjälä9bbc8258a2015-11-20 22:09:20 +020013591
13592 /*
13593 * Underruns don't always raise
13594 * interrupts, so check manually.
13595 */
13596 intel_check_cpu_fifo_underruns(dev_priv);
13597 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorstb9001112015-11-19 16:07:16 +010013598
13599 if (!crtc->state->active)
13600 intel_update_watermarks(crtc);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013601 }
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010013602 }
Daniel Vetter7758a112012-07-08 19:40:39 +020013603
Daniel Vetterea9d7582012-07-10 10:42:52 +020013604 /* Only after disabling all output pipelines that will be changed can we
13605 * update the the output configuration. */
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013606 intel_modeset_update_crtc_state(state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020013607
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013608 if (intel_state->modeset) {
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013609 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013610
13611 if (dev_priv->display.modeset_commit_cdclk &&
13612 intel_state->dev_cdclk != dev_priv->cdclk_freq)
13613 dev_priv->display.modeset_commit_cdclk(state);
Maarten Lankhorstf6d19732016-03-23 14:58:07 +010013614
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013615 intel_modeset_verify_disabled(dev);
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013616 }
Daniel Vetter47fab732012-10-26 10:58:18 +020013617
Daniel Vettera6778b32012-07-02 09:56:42 +020013618 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013619 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013620 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13621 bool modeset = needs_modeset(crtc->state);
Maarten Lankhorste8861672016-02-24 11:24:26 +010013622 struct intel_crtc_state *pipe_config =
13623 to_intel_crtc_state(crtc->state);
13624 bool update_pipe = !modeset && pipe_config->update_pipe;
Patrik Jakobsson9f836f92015-11-16 16:20:01 +010013625
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013626 if (modeset && crtc->state->active) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013627 update_scanline_offset(to_intel_crtc(crtc));
13628 dev_priv->display.crtc_enable(crtc);
13629 }
13630
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013631 if (!modeset)
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013632 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013633
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +010013634 if (crtc->state->active &&
13635 drm_atomic_get_existing_plane_state(state, crtc->primary))
Paulo Zanoni49227c42016-01-19 11:35:52 -020013636 intel_fbc_enable(intel_crtc);
13637
Maarten Lankhorst6173ee22015-09-23 16:29:39 +020013638 if (crtc->state->active &&
13639 (crtc->state->planes_changed || update_pipe))
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013640 drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013641
Maarten Lankhorste8861672016-02-24 11:24:26 +010013642 if (pipe_config->base.active && needs_vblank_wait(pipe_config))
13643 crtc_vblank_mask |= 1 << i;
Ville Syrjälä80715b22014-05-15 20:23:23 +030013644 }
Daniel Vettera6778b32012-07-02 09:56:42 +020013645
Daniel Vettera6778b32012-07-02 09:56:42 +020013646 /* FIXME: add subpixel order */
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013647
Maarten Lankhorste8861672016-02-24 11:24:26 +010013648 if (!state->legacy_cursor_update)
13649 intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013650
Matt Ropered4a6a72016-02-23 17:20:13 -080013651 /*
13652 * Now that the vblank has passed, we can go ahead and program the
13653 * optimal watermarks on platforms that need two-step watermark
13654 * programming.
13655 *
13656 * TODO: Move this (and other cleanup) to an async worker eventually.
13657 */
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013658 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Matt Ropered4a6a72016-02-23 17:20:13 -080013659 intel_cstate = to_intel_crtc_state(crtc->state);
13660
13661 if (dev_priv->display.optimize_watermarks)
13662 dev_priv->display.optimize_watermarks(intel_cstate);
13663 }
13664
Matt Roper177246a2016-03-04 15:59:39 -080013665 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
13666 intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
13667
13668 if (put_domains[i])
13669 modeset_put_power_domains(dev_priv, put_domains[i]);
Maarten Lankhorstf6d19732016-03-23 14:58:07 +010013670
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013671 intel_modeset_verify_crtc(crtc, old_crtc_state, crtc->state);
Matt Roper177246a2016-03-04 15:59:39 -080013672 }
13673
13674 if (intel_state->modeset)
13675 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
13676
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013677 mutex_lock(&dev->struct_mutex);
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013678 drm_atomic_helper_cleanup_planes(dev, state);
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013679 mutex_unlock(&dev->struct_mutex);
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013680
Maarten Lankhorstee165b12015-08-05 12:37:00 +020013681 drm_atomic_state_free(state);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013682
Mika Kuoppala75714942015-12-16 09:26:48 +020013683 /* As one of the primary mmio accessors, KMS has a high likelihood
13684 * of triggering bugs in unclaimed access. After we finish
13685 * modesetting, see if an error has been flagged, and if so
13686 * enable debugging for the next modeset - and hope we catch
13687 * the culprit.
13688 *
13689 * XXX note that we assume display power is on at this point.
13690 * This might hold true now but we need to add pm helper to check
13691 * unclaimed only when the hardware is on, as atomic commits
13692 * can happen also when the device is completely off.
13693 */
13694 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
13695
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013696 return 0;
Daniel Vetterf30da182013-04-11 20:22:50 +020013697}
13698
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013699void intel_crtc_restore_mode(struct drm_crtc *crtc)
13700{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013701 struct drm_device *dev = crtc->dev;
13702 struct drm_atomic_state *state;
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013703 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013704 int ret;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013705
13706 state = drm_atomic_state_alloc(dev);
13707 if (!state) {
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013708 DRM_DEBUG_KMS("[CRTC:%d] crtc restore failed, out of memory",
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013709 crtc->base.id);
13710 return;
13711 }
13712
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013713 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013714
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013715retry:
13716 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13717 ret = PTR_ERR_OR_ZERO(crtc_state);
13718 if (!ret) {
13719 if (!crtc_state->active)
13720 goto out;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013721
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013722 crtc_state->mode_changed = true;
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013723 ret = drm_atomic_commit(state);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013724 }
13725
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013726 if (ret == -EDEADLK) {
13727 drm_atomic_state_clear(state);
13728 drm_modeset_backoff(state->acquire_ctx);
13729 goto retry;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013730 }
13731
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013732 if (ret)
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013733out:
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013734 drm_atomic_state_free(state);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013735}
13736
Daniel Vetter25c5b262012-07-08 22:08:04 +020013737#undef for_each_intel_crtc_masked
13738
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013739static const struct drm_crtc_funcs intel_crtc_funcs = {
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000013740 .gamma_set = drm_atomic_helper_legacy_gamma_set,
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013741 .set_config = drm_atomic_helper_set_config,
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000013742 .set_property = drm_atomic_helper_crtc_set_property,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013743 .destroy = intel_crtc_destroy,
13744 .page_flip = intel_crtc_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080013745 .atomic_duplicate_state = intel_crtc_duplicate_state,
13746 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013747};
13748
Matt Roper6beb8c232014-12-01 15:40:14 -080013749/**
13750 * intel_prepare_plane_fb - Prepare fb for usage on plane
13751 * @plane: drm plane to prepare for
13752 * @fb: framebuffer to prepare for presentation
13753 *
13754 * Prepares a framebuffer for usage on a display plane. Generally this
13755 * involves pinning the underlying object and updating the frontbuffer tracking
13756 * bits. Some older platforms need special physical address handling for
13757 * cursor planes.
13758 *
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013759 * Must be called with struct_mutex held.
13760 *
Matt Roper6beb8c232014-12-01 15:40:14 -080013761 * Returns 0 on success, negative error code on failure.
13762 */
13763int
13764intel_prepare_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013765 const struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070013766{
13767 struct drm_device *dev = plane->dev;
Maarten Lankhorst844f9112015-09-02 10:42:40 +020013768 struct drm_framebuffer *fb = new_state->fb;
Matt Roper6beb8c232014-12-01 15:40:14 -080013769 struct intel_plane *intel_plane = to_intel_plane(plane);
Matt Roper6beb8c232014-12-01 15:40:14 -080013770 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013771 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
Matt Roper6beb8c232014-12-01 15:40:14 -080013772 int ret = 0;
Matt Roper465c1202014-05-29 08:06:54 -070013773
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013774 if (!obj && !old_obj)
Matt Roper465c1202014-05-29 08:06:54 -070013775 return 0;
13776
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013777 if (old_obj) {
13778 struct drm_crtc_state *crtc_state =
13779 drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
13780
13781 /* Big Hammer, we also need to ensure that any pending
13782 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
13783 * current scanout is retired before unpinning the old
13784 * framebuffer. Note that we rely on userspace rendering
13785 * into the buffer attached to the pipe they are waiting
13786 * on. If not, userspace generates a GPU hang with IPEHR
13787 * point to the MI_WAIT_FOR_EVENT.
13788 *
13789 * This should only fail upon a hung GPU, in which case we
13790 * can safely continue.
13791 */
13792 if (needs_modeset(crtc_state))
13793 ret = i915_gem_object_wait_rendering(old_obj, true);
13794
13795 /* Swallow -EIO errors to allow updates during hw lockup. */
13796 if (ret && ret != -EIO)
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013797 return ret;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013798 }
13799
Alex Goins3c28ff22015-11-25 18:43:39 -080013800 /* For framebuffer backed by dmabuf, wait for fence */
13801 if (obj && obj->base.dma_buf) {
Maarten Lankhorstbcf8be22015-12-08 15:52:56 +010013802 long lret;
Alex Goins3c28ff22015-11-25 18:43:39 -080013803
Maarten Lankhorstbcf8be22015-12-08 15:52:56 +010013804 lret = reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
13805 false, true,
13806 MAX_SCHEDULE_TIMEOUT);
13807 if (lret == -ERESTARTSYS)
13808 return lret;
13809
13810 WARN(lret < 0, "waiting returns %li\n", lret);
Alex Goins3c28ff22015-11-25 18:43:39 -080013811 }
13812
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013813 if (!obj) {
13814 ret = 0;
13815 } else if (plane->type == DRM_PLANE_TYPE_CURSOR &&
Matt Roper6beb8c232014-12-01 15:40:14 -080013816 INTEL_INFO(dev)->cursor_needs_physical) {
13817 int align = IS_I830(dev) ? 16 * 1024 : 256;
13818 ret = i915_gem_object_attach_phys(obj, align);
13819 if (ret)
13820 DRM_DEBUG_KMS("failed to attach phys object\n");
13821 } else {
Ville Syrjälä3465c582016-02-15 22:54:43 +020013822 ret = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
Matt Roper6beb8c232014-12-01 15:40:14 -080013823 }
13824
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013825 if (ret == 0) {
13826 if (obj) {
13827 struct intel_plane_state *plane_state =
13828 to_intel_plane_state(new_state);
13829
13830 i915_gem_request_assign(&plane_state->wait_req,
13831 obj->last_write_req);
13832 }
13833
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030013834 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013835 }
Matt Roper6beb8c232014-12-01 15:40:14 -080013836
Matt Roper6beb8c232014-12-01 15:40:14 -080013837 return ret;
13838}
13839
Matt Roper38f3ce32014-12-02 07:45:25 -080013840/**
13841 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13842 * @plane: drm plane to clean up for
13843 * @fb: old framebuffer that was on plane
13844 *
13845 * Cleans up a framebuffer that has just been removed from a plane.
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013846 *
13847 * Must be called with struct_mutex held.
Matt Roper38f3ce32014-12-02 07:45:25 -080013848 */
13849void
13850intel_cleanup_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013851 const struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080013852{
13853 struct drm_device *dev = plane->dev;
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013854 struct intel_plane *intel_plane = to_intel_plane(plane);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013855 struct intel_plane_state *old_intel_state;
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013856 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
13857 struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
Matt Roper38f3ce32014-12-02 07:45:25 -080013858
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013859 old_intel_state = to_intel_plane_state(old_state);
13860
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013861 if (!obj && !old_obj)
Matt Roper38f3ce32014-12-02 07:45:25 -080013862 return;
13863
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013864 if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
13865 !INTEL_INFO(dev)->cursor_needs_physical))
Ville Syrjälä3465c582016-02-15 22:54:43 +020013866 intel_unpin_fb_obj(old_state->fb, old_state->rotation);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013867
13868 /* prepare_fb aborted? */
13869 if ((old_obj && (old_obj->frontbuffer_bits & intel_plane->frontbuffer_bit)) ||
13870 (obj && !(obj->frontbuffer_bits & intel_plane->frontbuffer_bit)))
13871 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013872
13873 i915_gem_request_assign(&old_intel_state->wait_req, NULL);
Matt Roper465c1202014-05-29 08:06:54 -070013874}
13875
Chandra Konduru6156a452015-04-27 13:48:39 -070013876int
13877skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13878{
13879 int max_scale;
13880 struct drm_device *dev;
13881 struct drm_i915_private *dev_priv;
13882 int crtc_clock, cdclk;
13883
Maarten Lankhorstbf8a0af2015-11-24 11:29:02 +010013884 if (!intel_crtc || !crtc_state->base.enable)
Chandra Konduru6156a452015-04-27 13:48:39 -070013885 return DRM_PLANE_HELPER_NO_SCALING;
13886
13887 dev = intel_crtc->base.dev;
13888 dev_priv = dev->dev_private;
13889 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013890 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
Chandra Konduru6156a452015-04-27 13:48:39 -070013891
Tvrtko Ursulin54bf1ce2015-10-20 17:17:07 +010013892 if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
Chandra Konduru6156a452015-04-27 13:48:39 -070013893 return DRM_PLANE_HELPER_NO_SCALING;
13894
13895 /*
13896 * skl max scale is lower of:
13897 * close to 3 but not 3, -1 is for that purpose
13898 * or
13899 * cdclk/crtc_clock
13900 */
13901 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
13902
13903 return max_scale;
13904}
13905
Matt Roper465c1202014-05-29 08:06:54 -070013906static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013907intel_check_primary_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013908 struct intel_crtc_state *crtc_state,
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013909 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070013910{
Matt Roper2b875c22014-12-01 15:40:13 -080013911 struct drm_crtc *crtc = state->base.crtc;
13912 struct drm_framebuffer *fb = state->base.fb;
Chandra Konduru6156a452015-04-27 13:48:39 -070013913 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013914 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13915 bool can_position = false;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013916
Ville Syrjälä693bdc22016-01-15 20:46:53 +020013917 if (INTEL_INFO(plane->dev)->gen >= 9) {
13918 /* use scaler when colorkey is not required */
13919 if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
13920 min_scale = 1;
13921 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
13922 }
Sonika Jindald8106362015-04-10 14:37:28 +053013923 can_position = true;
Chandra Konduru6156a452015-04-27 13:48:39 -070013924 }
Sonika Jindald8106362015-04-10 14:37:28 +053013925
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013926 return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
13927 &state->dst, &state->clip,
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020013928 min_scale, max_scale,
13929 can_position, true,
13930 &state->visible);
Matt Roper465c1202014-05-29 08:06:54 -070013931}
13932
Maarten Lankhorst613d2b22015-07-21 13:28:58 +020013933static void intel_begin_crtc_commit(struct drm_crtc *crtc,
13934 struct drm_crtc_state *old_crtc_state)
Matt Roper32b7eee2014-12-24 07:59:06 -080013935{
13936 struct drm_device *dev = crtc->dev;
Matt Roper32b7eee2014-12-24 07:59:06 -080013937 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013938 struct intel_crtc_state *old_intel_state =
13939 to_intel_crtc_state(old_crtc_state);
13940 bool modeset = needs_modeset(crtc->state);
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013941
Matt Roperc34c9ee2014-12-23 10:41:50 -080013942 /* Perform vblank evasion around commit operation */
Maarten Lankhorst62852622015-09-23 16:29:38 +020013943 intel_pipe_update_start(intel_crtc);
Maarten Lankhorst05832362015-06-15 12:33:48 +020013944
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013945 if (modeset)
13946 return;
13947
Maarten Lankhorst20a34e72016-03-30 17:16:36 +020013948 if (crtc->state->color_mgmt_changed || to_intel_crtc_state(crtc->state)->update_pipe) {
13949 intel_color_set_csc(crtc->state);
13950 intel_color_load_luts(crtc->state);
13951 }
13952
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013953 if (to_intel_crtc_state(crtc->state)->update_pipe)
13954 intel_update_pipe_config(intel_crtc, old_intel_state);
13955 else if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorst05832362015-06-15 12:33:48 +020013956 skl_detach_scalers(intel_crtc);
Matt Roper32b7eee2014-12-24 07:59:06 -080013957}
13958
Maarten Lankhorst613d2b22015-07-21 13:28:58 +020013959static void intel_finish_crtc_commit(struct drm_crtc *crtc,
13960 struct drm_crtc_state *old_crtc_state)
Matt Roper32b7eee2014-12-24 07:59:06 -080013961{
Matt Roper32b7eee2014-12-24 07:59:06 -080013962 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roper32b7eee2014-12-24 07:59:06 -080013963
Maarten Lankhorst62852622015-09-23 16:29:38 +020013964 intel_pipe_update_end(intel_crtc);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013965}
13966
Matt Ropercf4c7c12014-12-04 10:27:42 -080013967/**
Matt Roper4a3b8762014-12-23 10:41:51 -080013968 * intel_plane_destroy - destroy a plane
13969 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080013970 *
Matt Roper4a3b8762014-12-23 10:41:51 -080013971 * Common destruction function for all types of planes (primary, cursor,
13972 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080013973 */
Matt Roper4a3b8762014-12-23 10:41:51 -080013974void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070013975{
13976 struct intel_plane *intel_plane = to_intel_plane(plane);
13977 drm_plane_cleanup(plane);
13978 kfree(intel_plane);
13979}
13980
Matt Roper65a3fea2015-01-21 16:35:42 -080013981const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070013982 .update_plane = drm_atomic_helper_update_plane,
13983 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070013984 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080013985 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080013986 .atomic_get_property = intel_plane_atomic_get_property,
13987 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080013988 .atomic_duplicate_state = intel_plane_duplicate_state,
13989 .atomic_destroy_state = intel_plane_destroy_state,
13990
Matt Roper465c1202014-05-29 08:06:54 -070013991};
13992
13993static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
13994 int pipe)
13995{
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013996 struct intel_plane *primary = NULL;
13997 struct intel_plane_state *state = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070013998 const uint32_t *intel_primary_formats;
Thierry Reding45e37432015-08-12 16:54:28 +020013999 unsigned int num_formats;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014000 int ret;
Matt Roper465c1202014-05-29 08:06:54 -070014001
14002 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014003 if (!primary)
14004 goto fail;
Matt Roper465c1202014-05-29 08:06:54 -070014005
Matt Roper8e7d6882015-01-21 16:35:41 -080014006 state = intel_create_plane_state(&primary->base);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014007 if (!state)
14008 goto fail;
Matt Roper8e7d6882015-01-21 16:35:41 -080014009 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080014010
Matt Roper465c1202014-05-29 08:06:54 -070014011 primary->can_scale = false;
14012 primary->max_downscale = 1;
Chandra Konduru6156a452015-04-27 13:48:39 -070014013 if (INTEL_INFO(dev)->gen >= 9) {
14014 primary->can_scale = true;
Chandra Konduruaf99ceda2015-05-11 14:35:47 -070014015 state->scaler_id = -1;
Chandra Konduru6156a452015-04-27 13:48:39 -070014016 }
Matt Roper465c1202014-05-29 08:06:54 -070014017 primary->pipe = pipe;
14018 primary->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030014019 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080014020 primary->check_plane = intel_check_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070014021 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
14022 primary->plane = !pipe;
14023
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014024 if (INTEL_INFO(dev)->gen >= 9) {
14025 intel_primary_formats = skl_primary_formats;
14026 num_formats = ARRAY_SIZE(skl_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010014027
14028 primary->update_plane = skylake_update_primary_plane;
14029 primary->disable_plane = skylake_disable_primary_plane;
14030 } else if (HAS_PCH_SPLIT(dev)) {
14031 intel_primary_formats = i965_primary_formats;
14032 num_formats = ARRAY_SIZE(i965_primary_formats);
14033
14034 primary->update_plane = ironlake_update_primary_plane;
14035 primary->disable_plane = i9xx_disable_primary_plane;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014036 } else if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau568db4f2015-05-12 16:13:18 +010014037 intel_primary_formats = i965_primary_formats;
14038 num_formats = ARRAY_SIZE(i965_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010014039
14040 primary->update_plane = i9xx_update_primary_plane;
14041 primary->disable_plane = i9xx_disable_primary_plane;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014042 } else {
14043 intel_primary_formats = i8xx_primary_formats;
14044 num_formats = ARRAY_SIZE(i8xx_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010014045
14046 primary->update_plane = i9xx_update_primary_plane;
14047 primary->disable_plane = i9xx_disable_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070014048 }
14049
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014050 ret = drm_universal_plane_init(dev, &primary->base, 0,
14051 &intel_plane_funcs,
14052 intel_primary_formats, num_formats,
14053 DRM_PLANE_TYPE_PRIMARY, NULL);
14054 if (ret)
14055 goto fail;
Sonika Jindal48404c12014-08-22 14:06:04 +053014056
Sonika Jindal3b7a5112015-04-10 14:37:29 +053014057 if (INTEL_INFO(dev)->gen >= 4)
14058 intel_create_rotation_property(dev, primary);
Sonika Jindal48404c12014-08-22 14:06:04 +053014059
Matt Roperea2c67b2014-12-23 10:41:52 -080014060 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
14061
Matt Roper465c1202014-05-29 08:06:54 -070014062 return &primary->base;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014063
14064fail:
14065 kfree(state);
14066 kfree(primary);
14067
14068 return NULL;
Matt Roper465c1202014-05-29 08:06:54 -070014069}
14070
Sonika Jindal3b7a5112015-04-10 14:37:29 +053014071void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
14072{
14073 if (!dev->mode_config.rotation_property) {
14074 unsigned long flags = BIT(DRM_ROTATE_0) |
14075 BIT(DRM_ROTATE_180);
14076
14077 if (INTEL_INFO(dev)->gen >= 9)
14078 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
14079
14080 dev->mode_config.rotation_property =
14081 drm_mode_create_rotation_property(dev, flags);
14082 }
14083 if (dev->mode_config.rotation_property)
14084 drm_object_attach_property(&plane->base.base,
14085 dev->mode_config.rotation_property,
14086 plane->base.state->rotation);
14087}
14088
Matt Roper3d7d6512014-06-10 08:28:13 -070014089static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030014090intel_check_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014091 struct intel_crtc_state *crtc_state,
Gustavo Padovan852e7872014-09-05 17:22:31 -030014092 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070014093{
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014094 struct drm_crtc *crtc = crtc_state->base.crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080014095 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014096 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Ville Syrjäläb29ec922015-12-18 19:24:39 +020014097 enum pipe pipe = to_intel_plane(plane)->pipe;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014098 unsigned stride;
14099 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030014100
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014101 ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
14102 &state->dst, &state->clip,
Gustavo Padovan852e7872014-09-05 17:22:31 -030014103 DRM_PLANE_HELPER_NO_SCALING,
14104 DRM_PLANE_HELPER_NO_SCALING,
14105 true, true, &state->visible);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014106 if (ret)
14107 return ret;
14108
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014109 /* if we want to turn off the cursor ignore width and height */
14110 if (!obj)
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014111 return 0;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014112
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014113 /* Check for which cursor types we support */
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014114 if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
Matt Roperea2c67b2014-12-23 10:41:52 -080014115 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
14116 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014117 return -EINVAL;
14118 }
14119
Matt Roperea2c67b2014-12-23 10:41:52 -080014120 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
14121 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014122 DRM_DEBUG_KMS("buffer is too small\n");
14123 return -ENOMEM;
14124 }
14125
Ville Syrjälä3a656b52015-03-09 21:08:37 +020014126 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014127 DRM_DEBUG_KMS("cursor cannot be tiled\n");
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014128 return -EINVAL;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014129 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014130
Ville Syrjäläb29ec922015-12-18 19:24:39 +020014131 /*
14132 * There's something wrong with the cursor on CHV pipe C.
14133 * If it straddles the left edge of the screen then
14134 * moving it away from the edge or disabling it often
14135 * results in a pipe underrun, and often that can lead to
14136 * dead pipe (constant underrun reported, and it scans
14137 * out just a solid color). To recover from that, the
14138 * display power well must be turned off and on again.
14139 * Refuse the put the cursor into that compromised position.
14140 */
14141 if (IS_CHERRYVIEW(plane->dev) && pipe == PIPE_C &&
14142 state->visible && state->base.crtc_x < 0) {
14143 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
14144 return -EINVAL;
14145 }
14146
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014147 return 0;
Gustavo Padovan852e7872014-09-05 17:22:31 -030014148}
14149
Matt Roperf4a2cf22014-12-01 15:40:12 -080014150static void
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014151intel_disable_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst7fabf5e2015-06-15 12:33:47 +020014152 struct drm_crtc *crtc)
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014153{
Maarten Lankhorstf2858022016-01-07 11:54:09 +010014154 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14155
14156 intel_crtc->cursor_addr = 0;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014157 intel_crtc_update_cursor(crtc, NULL);
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014158}
14159
14160static void
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014161intel_update_cursor_plane(struct drm_plane *plane,
14162 const struct intel_crtc_state *crtc_state,
14163 const struct intel_plane_state *state)
Gustavo Padovan852e7872014-09-05 17:22:31 -030014164{
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014165 struct drm_crtc *crtc = crtc_state->base.crtc;
14166 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roperea2c67b2014-12-23 10:41:52 -080014167 struct drm_device *dev = plane->dev;
Matt Roper2b875c22014-12-01 15:40:13 -080014168 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080014169 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070014170
Matt Roperf4a2cf22014-12-01 15:40:12 -080014171 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080014172 addr = 0;
Matt Roperf4a2cf22014-12-01 15:40:12 -080014173 else if (!INTEL_INFO(dev)->cursor_needs_physical)
Gustavo Padovana912f122014-12-01 15:40:10 -080014174 addr = i915_gem_obj_ggtt_offset(obj);
Matt Roperf4a2cf22014-12-01 15:40:12 -080014175 else
Gustavo Padovana912f122014-12-01 15:40:10 -080014176 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080014177
Gustavo Padovana912f122014-12-01 15:40:10 -080014178 intel_crtc->cursor_addr = addr;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014179 intel_crtc_update_cursor(crtc, state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014180}
Gustavo Padovan852e7872014-09-05 17:22:31 -030014181
Matt Roper3d7d6512014-06-10 08:28:13 -070014182static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
14183 int pipe)
14184{
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014185 struct intel_plane *cursor = NULL;
14186 struct intel_plane_state *state = NULL;
14187 int ret;
Matt Roper3d7d6512014-06-10 08:28:13 -070014188
14189 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014190 if (!cursor)
14191 goto fail;
Matt Roper3d7d6512014-06-10 08:28:13 -070014192
Matt Roper8e7d6882015-01-21 16:35:41 -080014193 state = intel_create_plane_state(&cursor->base);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014194 if (!state)
14195 goto fail;
Matt Roper8e7d6882015-01-21 16:35:41 -080014196 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080014197
Matt Roper3d7d6512014-06-10 08:28:13 -070014198 cursor->can_scale = false;
14199 cursor->max_downscale = 1;
14200 cursor->pipe = pipe;
14201 cursor->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030014202 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080014203 cursor->check_plane = intel_check_cursor_plane;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014204 cursor->update_plane = intel_update_cursor_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014205 cursor->disable_plane = intel_disable_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070014206
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014207 ret = drm_universal_plane_init(dev, &cursor->base, 0,
14208 &intel_plane_funcs,
14209 intel_cursor_formats,
14210 ARRAY_SIZE(intel_cursor_formats),
14211 DRM_PLANE_TYPE_CURSOR, NULL);
14212 if (ret)
14213 goto fail;
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014214
14215 if (INTEL_INFO(dev)->gen >= 4) {
14216 if (!dev->mode_config.rotation_property)
14217 dev->mode_config.rotation_property =
14218 drm_mode_create_rotation_property(dev,
14219 BIT(DRM_ROTATE_0) |
14220 BIT(DRM_ROTATE_180));
14221 if (dev->mode_config.rotation_property)
14222 drm_object_attach_property(&cursor->base.base,
14223 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080014224 state->base.rotation);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014225 }
14226
Chandra Konduruaf99ceda2015-05-11 14:35:47 -070014227 if (INTEL_INFO(dev)->gen >=9)
14228 state->scaler_id = -1;
14229
Matt Roperea2c67b2014-12-23 10:41:52 -080014230 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
14231
Matt Roper3d7d6512014-06-10 08:28:13 -070014232 return &cursor->base;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014233
14234fail:
14235 kfree(state);
14236 kfree(cursor);
14237
14238 return NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070014239}
14240
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014241static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
14242 struct intel_crtc_state *crtc_state)
14243{
14244 int i;
14245 struct intel_scaler *intel_scaler;
14246 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
14247
14248 for (i = 0; i < intel_crtc->num_scalers; i++) {
14249 intel_scaler = &scaler_state->scalers[i];
14250 intel_scaler->in_use = 0;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014251 intel_scaler->mode = PS_SCALER_MODE_DYN;
14252 }
14253
14254 scaler_state->scaler_id = -1;
14255}
14256
Hannes Ederb358d0a2008-12-18 21:18:47 +010014257static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080014258{
Jani Nikulafbee40d2014-03-31 14:27:18 +030014259 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080014260 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014261 struct intel_crtc_state *crtc_state = NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070014262 struct drm_plane *primary = NULL;
14263 struct drm_plane *cursor = NULL;
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +000014264 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080014265
Daniel Vetter955382f2013-09-19 14:05:45 +020014266 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080014267 if (intel_crtc == NULL)
14268 return;
14269
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014270 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
14271 if (!crtc_state)
14272 goto fail;
Ander Conselvan de Oliveira550acef2015-04-21 17:13:24 +030014273 intel_crtc->config = crtc_state;
14274 intel_crtc->base.state = &crtc_state->base;
Matt Roper07878242015-02-25 11:43:26 -080014275 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014276
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014277 /* initialize shared scalers */
14278 if (INTEL_INFO(dev)->gen >= 9) {
14279 if (pipe == PIPE_C)
14280 intel_crtc->num_scalers = 1;
14281 else
14282 intel_crtc->num_scalers = SKL_NUM_SCALERS;
14283
14284 skl_init_scalers(dev, intel_crtc, crtc_state);
14285 }
14286
Matt Roper465c1202014-05-29 08:06:54 -070014287 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014288 if (!primary)
14289 goto fail;
14290
14291 cursor = intel_cursor_plane_create(dev, pipe);
14292 if (!cursor)
14293 goto fail;
14294
Matt Roper465c1202014-05-29 08:06:54 -070014295 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Ville Syrjäläf9882872015-12-09 16:19:31 +020014296 cursor, &intel_crtc_funcs, NULL);
Matt Roper3d7d6512014-06-10 08:28:13 -070014297 if (ret)
14298 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080014299
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014300 /*
14301 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020014302 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014303 */
Jesse Barnes80824002009-09-10 15:28:06 -070014304 intel_crtc->pipe = pipe;
14305 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010014306 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080014307 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010014308 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070014309 }
14310
Chris Wilson4b0e3332014-05-30 16:35:26 +030014311 intel_crtc->cursor_base = ~0;
14312 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030014313 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030014314
Ville Syrjälä852eb002015-06-24 22:00:07 +030014315 intel_crtc->wm.cxsr_allowed = true;
14316
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080014317 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
14318 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
14319 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
14320 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
14321
Jesse Barnes79e53942008-11-07 14:24:08 -080014322 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020014323
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +000014324 intel_color_init(&intel_crtc->base);
14325
Daniel Vetter87b6b102014-05-15 15:33:46 +020014326 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014327 return;
14328
14329fail:
14330 if (primary)
14331 drm_plane_cleanup(primary);
14332 if (cursor)
14333 drm_plane_cleanup(cursor);
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014334 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014335 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080014336}
14337
Jesse Barnes752aa882013-10-31 18:55:49 +020014338enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14339{
14340 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014341 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020014342
Rob Clark51fd3712013-11-19 12:10:12 -050014343 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020014344
Ville Syrjäläd3babd32014-11-07 11:16:01 +020014345 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020014346 return INVALID_PIPE;
14347
14348 return to_intel_crtc(encoder->crtc)->pipe;
14349}
14350
Carl Worth08d7b3d2009-04-29 14:43:54 -070014351int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000014352 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070014353{
Carl Worth08d7b3d2009-04-29 14:43:54 -070014354 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040014355 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020014356 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014357
Rob Clark7707e652014-07-17 23:30:04 -040014358 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Carl Worth08d7b3d2009-04-29 14:43:54 -070014359
Rob Clark7707e652014-07-17 23:30:04 -040014360 if (!drmmode_crtc) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070014361 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030014362 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014363 }
14364
Rob Clark7707e652014-07-17 23:30:04 -040014365 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020014366 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014367
Daniel Vetterc05422d2009-08-11 16:05:30 +020014368 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014369}
14370
Daniel Vetter66a92782012-07-12 20:08:18 +020014371static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080014372{
Daniel Vetter66a92782012-07-12 20:08:18 +020014373 struct drm_device *dev = encoder->base.dev;
14374 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080014375 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080014376 int entry = 0;
14377
Damien Lespiaub2784e12014-08-05 11:29:37 +010014378 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020014379 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020014380 index_mask |= (1 << entry);
14381
Jesse Barnes79e53942008-11-07 14:24:08 -080014382 entry++;
14383 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010014384
Jesse Barnes79e53942008-11-07 14:24:08 -080014385 return index_mask;
14386}
14387
Chris Wilson4d302442010-12-14 19:21:29 +000014388static bool has_edp_a(struct drm_device *dev)
14389{
14390 struct drm_i915_private *dev_priv = dev->dev_private;
14391
14392 if (!IS_MOBILE(dev))
14393 return false;
14394
14395 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14396 return false;
14397
Damien Lespiaue3589902014-02-07 19:12:50 +000014398 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000014399 return false;
14400
14401 return true;
14402}
14403
Jesse Barnes84b4e042014-06-25 08:24:29 -070014404static bool intel_crt_present(struct drm_device *dev)
14405{
14406 struct drm_i915_private *dev_priv = dev->dev_private;
14407
Damien Lespiau884497e2013-12-03 13:56:23 +000014408 if (INTEL_INFO(dev)->gen >= 9)
14409 return false;
14410
Damien Lespiaucf404ce2014-10-01 20:04:15 +010014411 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
Jesse Barnes84b4e042014-06-25 08:24:29 -070014412 return false;
14413
14414 if (IS_CHERRYVIEW(dev))
14415 return false;
14416
Ville Syrjälä65e472e2015-12-01 23:28:55 +020014417 if (HAS_PCH_LPT_H(dev) && I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
14418 return false;
14419
Ville Syrjälä70ac54d2015-12-01 23:29:56 +020014420 /* DDI E can't be used if DDI A requires 4 lanes */
14421 if (HAS_DDI(dev) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
14422 return false;
14423
Ville Syrjäläe4abb732015-12-01 23:31:33 +020014424 if (!dev_priv->vbt.int_crt_support)
Jesse Barnes84b4e042014-06-25 08:24:29 -070014425 return false;
14426
14427 return true;
14428}
14429
Jesse Barnes79e53942008-11-07 14:24:08 -080014430static void intel_setup_outputs(struct drm_device *dev)
14431{
Eric Anholt725e30a2009-01-22 13:01:02 -080014432 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010014433 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014434 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080014435
Daniel Vetterc9093352013-06-06 22:22:47 +020014436 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014437
Jesse Barnes84b4e042014-06-25 08:24:29 -070014438 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020014439 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014440
Vandana Kannanc776eb22014-08-19 12:05:01 +053014441 if (IS_BROXTON(dev)) {
14442 /*
14443 * FIXME: Broxton doesn't support port detection via the
14444 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14445 * detect the ports.
14446 */
14447 intel_ddi_init(dev, PORT_A);
14448 intel_ddi_init(dev, PORT_B);
14449 intel_ddi_init(dev, PORT_C);
Shashank Sharmac6c794a2016-03-22 12:01:50 +020014450
14451 intel_dsi_init(dev);
Vandana Kannanc776eb22014-08-19 12:05:01 +053014452 } else if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014453 int found;
14454
Jesse Barnesde31fac2015-03-06 15:53:32 -080014455 /*
14456 * Haswell uses DDI functions to detect digital outputs.
14457 * On SKL pre-D0 the strap isn't connected, so we assume
14458 * it's there.
14459 */
Ville Syrjälä77179402015-09-18 20:03:35 +030014460 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080014461 /* WaIgnoreDDIAStrap: skl */
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070014462 if (found || IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014463 intel_ddi_init(dev, PORT_A);
14464
14465 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14466 * register */
14467 found = I915_READ(SFUSE_STRAP);
14468
14469 if (found & SFUSE_STRAP_DDIB_DETECTED)
14470 intel_ddi_init(dev, PORT_B);
14471 if (found & SFUSE_STRAP_DDIC_DETECTED)
14472 intel_ddi_init(dev, PORT_C);
14473 if (found & SFUSE_STRAP_DDID_DETECTED)
14474 intel_ddi_init(dev, PORT_D);
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014475 /*
14476 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
14477 */
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070014478 if ((IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) &&
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014479 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
14480 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
14481 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
14482 intel_ddi_init(dev, PORT_E);
14483
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014484 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014485 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020014486 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020014487
14488 if (has_edp_a(dev))
14489 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014490
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014491 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080014492 /* PCH SDVOB multiplex with HDMIB */
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014493 found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014494 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014495 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014496 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014497 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014498 }
14499
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014500 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014501 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014502
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014503 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014504 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014505
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014506 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014507 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014508
Daniel Vetter270b3042012-10-27 15:52:05 +020014509 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014510 intel_dp_init(dev, PCH_DP_D, PORT_D);
Wayne Boyer666a4532015-12-09 12:29:35 -080014511 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014512 /*
14513 * The DP_DETECTED bit is the latched state of the DDC
14514 * SDA pin at boot. However since eDP doesn't require DDC
14515 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14516 * eDP ports may have been muxed to an alternate function.
14517 * Thus we can't rely on the DP_DETECTED bit alone to detect
14518 * eDP ports. Consult the VBT as well as DP_DETECTED to
14519 * detect eDP ports.
14520 */
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014521 if (I915_READ(VLV_HDMIB) & SDVO_DETECTED &&
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014522 !intel_dp_is_edp(dev, PORT_B))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014523 intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
14524 if (I915_READ(VLV_DP_B) & DP_DETECTED ||
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014525 intel_dp_is_edp(dev, PORT_B))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014526 intel_dp_init(dev, VLV_DP_B, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014527
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014528 if (I915_READ(VLV_HDMIC) & SDVO_DETECTED &&
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014529 !intel_dp_is_edp(dev, PORT_C))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014530 intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
14531 if (I915_READ(VLV_DP_C) & DP_DETECTED ||
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014532 intel_dp_is_edp(dev, PORT_C))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014533 intel_dp_init(dev, VLV_DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053014534
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014535 if (IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014536 /* eDP not supported on port D, so don't check VBT */
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014537 if (I915_READ(CHV_HDMID) & SDVO_DETECTED)
14538 intel_hdmi_init(dev, CHV_HDMID, PORT_D);
14539 if (I915_READ(CHV_DP_D) & DP_DETECTED)
14540 intel_dp_init(dev, CHV_DP_D, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014541 }
14542
Jani Nikula3cfca972013-08-27 15:12:26 +030014543 intel_dsi_init(dev);
Daniel Vetter09da55d2015-07-07 11:44:32 +020014544 } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014545 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080014546
Paulo Zanonie2debe92013-02-18 19:00:27 -030014547 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014548 DRM_DEBUG_KMS("probing SDVOB\n");
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014549 found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014550 if (!found && IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014551 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014552 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014553 }
Ma Ling27185ae2009-08-24 13:50:23 +080014554
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014555 if (!found && IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014556 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080014557 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014558
14559 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014560
Paulo Zanonie2debe92013-02-18 19:00:27 -030014561 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014562 DRM_DEBUG_KMS("probing SDVOC\n");
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014563 found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014564 }
Ma Ling27185ae2009-08-24 13:50:23 +080014565
Paulo Zanonie2debe92013-02-18 19:00:27 -030014566 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014567
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014568 if (IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014569 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014570 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014571 }
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014572 if (IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014573 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080014574 }
Ma Ling27185ae2009-08-24 13:50:23 +080014575
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014576 if (IS_G4X(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030014577 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014578 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070014579 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014580 intel_dvo_init(dev);
14581
Zhenyu Wang103a1962009-11-27 11:44:36 +080014582 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014583 intel_tv_init(dev);
14584
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -080014585 intel_psr_init(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070014586
Damien Lespiaub2784e12014-08-05 11:29:37 +010014587 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010014588 encoder->base.possible_crtcs = encoder->crtc_mask;
14589 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020014590 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080014591 }
Chris Wilson47356eb2011-01-11 17:06:04 +000014592
Paulo Zanonidde86e22012-12-01 12:04:25 -020014593 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020014594
14595 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014596}
14597
14598static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14599{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014600 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080014601 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080014602
Daniel Vetteref2d6332014-02-10 18:00:38 +010014603 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014604 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010014605 WARN_ON(!intel_fb->obj->framebuffer_references--);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014606 drm_gem_object_unreference(&intel_fb->obj->base);
14607 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080014608 kfree(intel_fb);
14609}
14610
14611static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000014612 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080014613 unsigned int *handle)
14614{
14615 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000014616 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014617
Chris Wilsoncc917ab2015-10-13 14:22:26 +010014618 if (obj->userptr.mm) {
14619 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
14620 return -EINVAL;
14621 }
14622
Chris Wilson05394f32010-11-08 19:18:58 +000014623 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080014624}
14625
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014626static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
14627 struct drm_file *file,
14628 unsigned flags, unsigned color,
14629 struct drm_clip_rect *clips,
14630 unsigned num_clips)
14631{
14632 struct drm_device *dev = fb->dev;
14633 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
14634 struct drm_i915_gem_object *obj = intel_fb->obj;
14635
14636 mutex_lock(&dev->struct_mutex);
Paulo Zanoni74b4ea12015-07-14 16:29:14 -030014637 intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014638 mutex_unlock(&dev->struct_mutex);
14639
14640 return 0;
14641}
14642
Jesse Barnes79e53942008-11-07 14:24:08 -080014643static const struct drm_framebuffer_funcs intel_fb_funcs = {
14644 .destroy = intel_user_framebuffer_destroy,
14645 .create_handle = intel_user_framebuffer_create_handle,
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014646 .dirty = intel_user_framebuffer_dirty,
Jesse Barnes79e53942008-11-07 14:24:08 -080014647};
14648
Damien Lespiaub3218032015-02-27 11:15:18 +000014649static
14650u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14651 uint32_t pixel_format)
14652{
14653 u32 gen = INTEL_INFO(dev)->gen;
14654
14655 if (gen >= 9) {
Ville Syrjäläac484962016-01-20 21:05:26 +020014656 int cpp = drm_format_plane_cpp(pixel_format, 0);
14657
Damien Lespiaub3218032015-02-27 11:15:18 +000014658 /* "The stride in bytes must not exceed the of the size of 8K
14659 * pixels and 32K bytes."
14660 */
Ville Syrjäläac484962016-01-20 21:05:26 +020014661 return min(8192 * cpp, 32768);
Wayne Boyer666a4532015-12-09 12:29:35 -080014662 } else if (gen >= 5 && !IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014663 return 32*1024;
14664 } else if (gen >= 4) {
14665 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14666 return 16*1024;
14667 else
14668 return 32*1024;
14669 } else if (gen >= 3) {
14670 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14671 return 8*1024;
14672 else
14673 return 16*1024;
14674 } else {
14675 /* XXX DSPC is limited to 4k tiled */
14676 return 8*1024;
14677 }
14678}
14679
Daniel Vetterb5ea6422014-03-02 21:18:00 +010014680static int intel_framebuffer_init(struct drm_device *dev,
14681 struct intel_framebuffer *intel_fb,
14682 struct drm_mode_fb_cmd2 *mode_cmd,
14683 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080014684{
Ville Syrjälä7b49f942016-01-12 21:08:32 +020014685 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +000014686 unsigned int aligned_height;
Jesse Barnes79e53942008-11-07 14:24:08 -080014687 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000014688 u32 pitch_limit, stride_alignment;
Jesse Barnes79e53942008-11-07 14:24:08 -080014689
Daniel Vetterdd4916c2013-10-09 21:23:51 +020014690 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14691
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014692 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14693 /* Enforce that fb modifier and tiling mode match, but only for
14694 * X-tiled. This is needed for FBC. */
14695 if (!!(obj->tiling_mode == I915_TILING_X) !=
14696 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14697 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14698 return -EINVAL;
14699 }
14700 } else {
14701 if (obj->tiling_mode == I915_TILING_X)
14702 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14703 else if (obj->tiling_mode == I915_TILING_Y) {
14704 DRM_DEBUG("No Y tiling for legacy addfb\n");
14705 return -EINVAL;
14706 }
14707 }
14708
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014709 /* Passed in modifier sanity checking. */
14710 switch (mode_cmd->modifier[0]) {
14711 case I915_FORMAT_MOD_Y_TILED:
14712 case I915_FORMAT_MOD_Yf_TILED:
14713 if (INTEL_INFO(dev)->gen < 9) {
14714 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14715 mode_cmd->modifier[0]);
14716 return -EINVAL;
14717 }
14718 case DRM_FORMAT_MOD_NONE:
14719 case I915_FORMAT_MOD_X_TILED:
14720 break;
14721 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070014722 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14723 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010014724 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014725 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014726
Ville Syrjälä7b49f942016-01-12 21:08:32 +020014727 stride_alignment = intel_fb_stride_alignment(dev_priv,
14728 mode_cmd->modifier[0],
Damien Lespiaub3218032015-02-27 11:15:18 +000014729 mode_cmd->pixel_format);
14730 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14731 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14732 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010014733 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014734 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014735
Damien Lespiaub3218032015-02-27 11:15:18 +000014736 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14737 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014738 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014739 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14740 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014741 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014742 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014743 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014744 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014745
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014746 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014747 mode_cmd->pitches[0] != obj->stride) {
14748 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14749 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014750 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014751 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014752
Ville Syrjälä57779d02012-10-31 17:50:14 +020014753 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014754 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020014755 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014756 case DRM_FORMAT_RGB565:
14757 case DRM_FORMAT_XRGB8888:
14758 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014759 break;
14760 case DRM_FORMAT_XRGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014761 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014762 DRM_DEBUG("unsupported pixel format: %s\n",
14763 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014764 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014765 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020014766 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +020014767 case DRM_FORMAT_ABGR8888:
Wayne Boyer666a4532015-12-09 12:29:35 -080014768 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
14769 INTEL_INFO(dev)->gen < 9) {
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014770 DRM_DEBUG("unsupported pixel format: %s\n",
14771 drm_get_format_name(mode_cmd->pixel_format));
14772 return -EINVAL;
14773 }
14774 break;
14775 case DRM_FORMAT_XBGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014776 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014777 case DRM_FORMAT_XBGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014778 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014779 DRM_DEBUG("unsupported pixel format: %s\n",
14780 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014781 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014782 }
Jesse Barnesb5626742011-06-24 12:19:27 -070014783 break;
Damien Lespiau75312082015-05-15 19:06:01 +010014784 case DRM_FORMAT_ABGR2101010:
Wayne Boyer666a4532015-12-09 12:29:35 -080014785 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Damien Lespiau75312082015-05-15 19:06:01 +010014786 DRM_DEBUG("unsupported pixel format: %s\n",
14787 drm_get_format_name(mode_cmd->pixel_format));
14788 return -EINVAL;
14789 }
14790 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020014791 case DRM_FORMAT_YUYV:
14792 case DRM_FORMAT_UYVY:
14793 case DRM_FORMAT_YVYU:
14794 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014795 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014796 DRM_DEBUG("unsupported pixel format: %s\n",
14797 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014798 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014799 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014800 break;
14801 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014802 DRM_DEBUG("unsupported pixel format: %s\n",
14803 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010014804 return -EINVAL;
14805 }
14806
Ville Syrjälä90f9a332012-10-31 17:50:19 +020014807 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14808 if (mode_cmd->offsets[0] != 0)
14809 return -EINVAL;
14810
Damien Lespiauec2c9812015-01-20 12:51:45 +000014811 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +000014812 mode_cmd->pixel_format,
14813 mode_cmd->modifier[0]);
Daniel Vetter53155c02013-10-09 21:55:33 +020014814 /* FIXME drm helper for size checks (especially planar formats)? */
14815 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14816 return -EINVAL;
14817
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014818 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14819 intel_fb->obj = obj;
14820
Ville Syrjälä2d7a2152016-02-15 22:54:47 +020014821 intel_fill_fb_info(dev_priv, &intel_fb->base);
14822
Jesse Barnes79e53942008-11-07 14:24:08 -080014823 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14824 if (ret) {
14825 DRM_ERROR("framebuffer init failed %d\n", ret);
14826 return ret;
14827 }
14828
Ville Syrjälä0b05e1e2016-01-14 15:22:09 +020014829 intel_fb->obj->framebuffer_references++;
14830
Jesse Barnes79e53942008-11-07 14:24:08 -080014831 return 0;
14832}
14833
Jesse Barnes79e53942008-11-07 14:24:08 -080014834static struct drm_framebuffer *
14835intel_user_framebuffer_create(struct drm_device *dev,
14836 struct drm_file *filp,
Ville Syrjälä1eb83452015-11-11 19:11:29 +020014837 const struct drm_mode_fb_cmd2 *user_mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080014838{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020014839 struct drm_framebuffer *fb;
Chris Wilson05394f32010-11-08 19:18:58 +000014840 struct drm_i915_gem_object *obj;
Ville Syrjälä76dc3762015-11-11 19:11:28 +020014841 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
Jesse Barnes79e53942008-11-07 14:24:08 -080014842
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014843 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
Ville Syrjälä76dc3762015-11-11 19:11:28 +020014844 mode_cmd.handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000014845 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010014846 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080014847
Daniel Vetter92907cb2015-11-23 09:04:05 +010014848 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020014849 if (IS_ERR(fb))
14850 drm_gem_object_unreference_unlocked(&obj->base);
14851
14852 return fb;
Jesse Barnes79e53942008-11-07 14:24:08 -080014853}
14854
Daniel Vetter06957262015-08-10 13:34:08 +020014855#ifndef CONFIG_DRM_FBDEV_EMULATION
Daniel Vetter0632fef2013-10-08 17:44:49 +020014856static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020014857{
14858}
14859#endif
14860
Jesse Barnes79e53942008-11-07 14:24:08 -080014861static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080014862 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020014863 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080014864 .atomic_check = intel_atomic_check,
14865 .atomic_commit = intel_atomic_commit,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +020014866 .atomic_state_alloc = intel_atomic_state_alloc,
14867 .atomic_state_clear = intel_atomic_state_clear,
Jesse Barnes79e53942008-11-07 14:24:08 -080014868};
14869
Imre Deak88212942016-03-16 13:38:53 +020014870/**
14871 * intel_init_display_hooks - initialize the display modesetting hooks
14872 * @dev_priv: device private
14873 */
14874void intel_init_display_hooks(struct drm_i915_private *dev_priv)
Jesse Barnese70236a2009-09-21 10:42:27 -070014875{
Imre Deak88212942016-03-16 13:38:53 +020014876 if (INTEL_INFO(dev_priv)->gen >= 9) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014877 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014878 dev_priv->display.get_initial_plane_config =
14879 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014880 dev_priv->display.crtc_compute_clock =
14881 haswell_crtc_compute_clock;
14882 dev_priv->display.crtc_enable = haswell_crtc_enable;
14883 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020014884 } else if (HAS_DDI(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014885 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014886 dev_priv->display.get_initial_plane_config =
14887 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020014888 dev_priv->display.crtc_compute_clock =
14889 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020014890 dev_priv->display.crtc_enable = haswell_crtc_enable;
14891 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020014892 } else if (HAS_PCH_SPLIT(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014893 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014894 dev_priv->display.get_initial_plane_config =
14895 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020014896 dev_priv->display.crtc_compute_clock =
14897 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014898 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14899 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +020014900 } else if (IS_CHERRYVIEW(dev_priv)) {
Jesse Barnes89b667f2013-04-18 14:51:36 -070014901 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014902 dev_priv->display.get_initial_plane_config =
14903 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +020014904 dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
14905 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14906 dev_priv->display.crtc_disable = i9xx_crtc_disable;
14907 } else if (IS_VALLEYVIEW(dev_priv)) {
14908 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14909 dev_priv->display.get_initial_plane_config =
14910 i9xx_get_initial_plane_config;
14911 dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014912 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14913 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +020014914 } else if (IS_G4X(dev_priv)) {
14915 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14916 dev_priv->display.get_initial_plane_config =
14917 i9xx_get_initial_plane_config;
14918 dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
14919 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14920 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +020014921 } else if (IS_PINEVIEW(dev_priv)) {
14922 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14923 dev_priv->display.get_initial_plane_config =
14924 i9xx_get_initial_plane_config;
14925 dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
14926 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14927 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +020014928 } else if (!IS_GEN2(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014929 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014930 dev_priv->display.get_initial_plane_config =
14931 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014932 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014933 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14934 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +020014935 } else {
14936 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14937 dev_priv->display.get_initial_plane_config =
14938 i9xx_get_initial_plane_config;
14939 dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
14940 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14941 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Eric Anholtf564048e2011-03-30 13:01:02 -070014942 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014943
Jesse Barnese70236a2009-09-21 10:42:27 -070014944 /* Returns the core display clock speed */
Imre Deak88212942016-03-16 13:38:53 +020014945 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030014946 dev_priv->display.get_display_clock_speed =
14947 skylake_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014948 else if (IS_BROXTON(dev_priv))
Bob Paauweacd3f3d2015-06-23 14:14:26 -070014949 dev_priv->display.get_display_clock_speed =
14950 broxton_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014951 else if (IS_BROADWELL(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030014952 dev_priv->display.get_display_clock_speed =
14953 broadwell_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014954 else if (IS_HASWELL(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030014955 dev_priv->display.get_display_clock_speed =
14956 haswell_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014957 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070014958 dev_priv->display.get_display_clock_speed =
14959 valleyview_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014960 else if (IS_GEN5(dev_priv))
Ville Syrjäläb37a6432015-03-31 14:11:54 +030014961 dev_priv->display.get_display_clock_speed =
14962 ilk_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014963 else if (IS_I945G(dev_priv) || IS_BROADWATER(dev_priv) ||
14964 IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014965 dev_priv->display.get_display_clock_speed =
14966 i945_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014967 else if (IS_GM45(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030014968 dev_priv->display.get_display_clock_speed =
14969 gm45_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014970 else if (IS_CRESTLINE(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030014971 dev_priv->display.get_display_clock_speed =
14972 i965gm_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014973 else if (IS_PINEVIEW(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030014974 dev_priv->display.get_display_clock_speed =
14975 pnv_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014976 else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030014977 dev_priv->display.get_display_clock_speed =
14978 g33_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014979 else if (IS_I915G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014980 dev_priv->display.get_display_clock_speed =
14981 i915_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014982 else if (IS_I945GM(dev_priv) || IS_845G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014983 dev_priv->display.get_display_clock_speed =
14984 i9xx_misc_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014985 else if (IS_I915GM(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014986 dev_priv->display.get_display_clock_speed =
14987 i915gm_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014988 else if (IS_I865G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014989 dev_priv->display.get_display_clock_speed =
14990 i865_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014991 else if (IS_I85X(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014992 dev_priv->display.get_display_clock_speed =
Ville Syrjälä1b1d2712015-05-22 11:22:31 +030014993 i85x_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030014994 else { /* 830 */
Imre Deak88212942016-03-16 13:38:53 +020014995 WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
Jesse Barnese70236a2009-09-21 10:42:27 -070014996 dev_priv->display.get_display_clock_speed =
14997 i830_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030014998 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014999
Imre Deak88212942016-03-16 13:38:53 +020015000 if (IS_GEN5(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015001 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020015002 } else if (IS_GEN6(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015003 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020015004 } else if (IS_IVYBRIDGE(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015005 /* FIXME: detect B0+ stepping and use auto training */
15006 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020015007 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015008 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020015009 if (IS_BROADWELL(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020015010 dev_priv->display.modeset_commit_cdclk =
15011 broadwell_modeset_commit_cdclk;
15012 dev_priv->display.modeset_calc_cdclk =
15013 broadwell_modeset_calc_cdclk;
15014 }
Imre Deak88212942016-03-16 13:38:53 +020015015 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020015016 dev_priv->display.modeset_commit_cdclk =
15017 valleyview_modeset_commit_cdclk;
15018 dev_priv->display.modeset_calc_cdclk =
15019 valleyview_modeset_calc_cdclk;
Imre Deak88212942016-03-16 13:38:53 +020015020 } else if (IS_BROXTON(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020015021 dev_priv->display.modeset_commit_cdclk =
15022 broxton_modeset_commit_cdclk;
15023 dev_priv->display.modeset_calc_cdclk =
15024 broxton_modeset_calc_cdclk;
Jesse Barnese70236a2009-09-21 10:42:27 -070015025 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070015026
Imre Deak88212942016-03-16 13:38:53 +020015027 switch (INTEL_INFO(dev_priv)->gen) {
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070015028 case 2:
15029 dev_priv->display.queue_flip = intel_gen2_queue_flip;
15030 break;
15031
15032 case 3:
15033 dev_priv->display.queue_flip = intel_gen3_queue_flip;
15034 break;
15035
15036 case 4:
15037 case 5:
15038 dev_priv->display.queue_flip = intel_gen4_queue_flip;
15039 break;
15040
15041 case 6:
15042 dev_priv->display.queue_flip = intel_gen6_queue_flip;
15043 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070015044 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070015045 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070015046 dev_priv->display.queue_flip = intel_gen7_queue_flip;
15047 break;
Damien Lespiau830c81d2014-11-13 17:51:46 +000015048 case 9:
Tvrtko Ursulinba343e02015-02-10 17:16:12 +000015049 /* Drop through - unsupported since execlist only. */
15050 default:
15051 /* Default just returns -ENODEV to indicate unsupported */
15052 dev_priv->display.queue_flip = intel_default_queue_flip;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070015053 }
Jesse Barnese70236a2009-09-21 10:42:27 -070015054}
15055
Jesse Barnesb690e962010-07-19 13:53:12 -070015056/*
15057 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
15058 * resume, or other times. This quirk makes sure that's the case for
15059 * affected systems.
15060 */
Akshay Joshi0206e352011-08-16 15:34:10 -040015061static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070015062{
15063 struct drm_i915_private *dev_priv = dev->dev_private;
15064
15065 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015066 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070015067}
15068
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030015069static void quirk_pipeb_force(struct drm_device *dev)
15070{
15071 struct drm_i915_private *dev_priv = dev->dev_private;
15072
15073 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
15074 DRM_INFO("applying pipe b force quirk\n");
15075}
15076
Keith Packard435793d2011-07-12 14:56:22 -070015077/*
15078 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
15079 */
15080static void quirk_ssc_force_disable(struct drm_device *dev)
15081{
15082 struct drm_i915_private *dev_priv = dev->dev_private;
15083 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015084 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070015085}
15086
Carsten Emde4dca20e2012-03-15 15:56:26 +010015087/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010015088 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
15089 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010015090 */
15091static void quirk_invert_brightness(struct drm_device *dev)
15092{
15093 struct drm_i915_private *dev_priv = dev->dev_private;
15094 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015095 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070015096}
15097
Scot Doyle9c72cc62014-07-03 23:27:50 +000015098/* Some VBT's incorrectly indicate no backlight is present */
15099static void quirk_backlight_present(struct drm_device *dev)
15100{
15101 struct drm_i915_private *dev_priv = dev->dev_private;
15102 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
15103 DRM_INFO("applying backlight present quirk\n");
15104}
15105
Jesse Barnesb690e962010-07-19 13:53:12 -070015106struct intel_quirk {
15107 int device;
15108 int subsystem_vendor;
15109 int subsystem_device;
15110 void (*hook)(struct drm_device *dev);
15111};
15112
Egbert Eich5f85f172012-10-14 15:46:38 +020015113/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
15114struct intel_dmi_quirk {
15115 void (*hook)(struct drm_device *dev);
15116 const struct dmi_system_id (*dmi_id_list)[];
15117};
15118
15119static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
15120{
15121 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
15122 return 1;
15123}
15124
15125static const struct intel_dmi_quirk intel_dmi_quirks[] = {
15126 {
15127 .dmi_id_list = &(const struct dmi_system_id[]) {
15128 {
15129 .callback = intel_dmi_reverse_brightness,
15130 .ident = "NCR Corporation",
15131 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
15132 DMI_MATCH(DMI_PRODUCT_NAME, ""),
15133 },
15134 },
15135 { } /* terminating entry */
15136 },
15137 .hook = quirk_invert_brightness,
15138 },
15139};
15140
Ben Widawskyc43b5632012-04-16 14:07:40 -070015141static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070015142 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
15143 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
15144
Jesse Barnesb690e962010-07-19 13:53:12 -070015145 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
15146 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
15147
Ville Syrjälä5f080c02014-08-15 01:22:06 +030015148 /* 830 needs to leave pipe A & dpll A up */
15149 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
15150
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030015151 /* 830 needs to leave pipe B & dpll B up */
15152 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
15153
Keith Packard435793d2011-07-12 14:56:22 -070015154 /* Lenovo U160 cannot use SSC on LVDS */
15155 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020015156
15157 /* Sony Vaio Y cannot use SSC on LVDS */
15158 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010015159
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010015160 /* Acer Aspire 5734Z must invert backlight brightness */
15161 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
15162
15163 /* Acer/eMachines G725 */
15164 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
15165
15166 /* Acer/eMachines e725 */
15167 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
15168
15169 /* Acer/Packard Bell NCL20 */
15170 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
15171
15172 /* Acer Aspire 4736Z */
15173 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020015174
15175 /* Acer Aspire 5336 */
15176 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000015177
15178 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
15179 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000015180
Scot Doyledfb3d47b2014-08-21 16:08:02 +000015181 /* Acer C720 Chromebook (Core i3 4005U) */
15182 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
15183
jens steinb2a96012014-10-28 20:25:53 +010015184 /* Apple Macbook 2,1 (Core 2 T7400) */
15185 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
15186
Jani Nikula1b9448b2015-11-05 11:49:59 +020015187 /* Apple Macbook 4,1 */
15188 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
15189
Scot Doyled4967d82014-07-03 23:27:52 +000015190 /* Toshiba CB35 Chromebook (Celeron 2955U) */
15191 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000015192
15193 /* HP Chromebook 14 (Celeron 2955U) */
15194 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020015195
15196 /* Dell Chromebook 11 */
15197 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jani Nikula9be64ee2015-10-30 14:50:24 +020015198
15199 /* Dell Chromebook 11 (2015 version) */
15200 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070015201};
15202
15203static void intel_init_quirks(struct drm_device *dev)
15204{
15205 struct pci_dev *d = dev->pdev;
15206 int i;
15207
15208 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
15209 struct intel_quirk *q = &intel_quirks[i];
15210
15211 if (d->device == q->device &&
15212 (d->subsystem_vendor == q->subsystem_vendor ||
15213 q->subsystem_vendor == PCI_ANY_ID) &&
15214 (d->subsystem_device == q->subsystem_device ||
15215 q->subsystem_device == PCI_ANY_ID))
15216 q->hook(dev);
15217 }
Egbert Eich5f85f172012-10-14 15:46:38 +020015218 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
15219 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
15220 intel_dmi_quirks[i].hook(dev);
15221 }
Jesse Barnesb690e962010-07-19 13:53:12 -070015222}
15223
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015224/* Disable the VGA plane that we never use */
15225static void i915_disable_vga(struct drm_device *dev)
15226{
15227 struct drm_i915_private *dev_priv = dev->dev_private;
15228 u8 sr1;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020015229 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015230
Ville Syrjälä2b37c612014-01-22 21:32:38 +020015231 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015232 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070015233 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015234 sr1 = inb(VGA_SR_DATA);
15235 outb(sr1 | 1<<5, VGA_SR_DATA);
15236 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
15237 udelay(300);
15238
Ville Syrjälä01f5a622014-12-16 18:38:37 +020015239 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015240 POSTING_READ(vga_reg);
15241}
15242
Daniel Vetterf8175862012-04-10 15:50:11 +020015243void intel_modeset_init_hw(struct drm_device *dev)
15244{
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010015245 struct drm_i915_private *dev_priv = dev->dev_private;
15246
Ville Syrjäläb6283052015-06-03 15:45:07 +030015247 intel_update_cdclk(dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010015248
15249 dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
15250
Daniel Vetterf8175862012-04-10 15:50:11 +020015251 intel_init_clock_gating(dev);
Daniel Vetter8090c6b2012-06-24 16:42:32 +020015252 intel_enable_gt_powersave(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020015253}
15254
Matt Roperd93c0372015-12-03 11:37:41 -080015255/*
15256 * Calculate what we think the watermarks should be for the state we've read
15257 * out of the hardware and then immediately program those watermarks so that
15258 * we ensure the hardware settings match our internal state.
15259 *
15260 * We can calculate what we think WM's should be by creating a duplicate of the
15261 * current state (which was constructed during hardware readout) and running it
15262 * through the atomic check code to calculate new watermark values in the
15263 * state object.
15264 */
15265static void sanitize_watermarks(struct drm_device *dev)
15266{
15267 struct drm_i915_private *dev_priv = to_i915(dev);
15268 struct drm_atomic_state *state;
15269 struct drm_crtc *crtc;
15270 struct drm_crtc_state *cstate;
15271 struct drm_modeset_acquire_ctx ctx;
15272 int ret;
15273 int i;
15274
15275 /* Only supported on platforms that use atomic watermark design */
Matt Ropered4a6a72016-02-23 17:20:13 -080015276 if (!dev_priv->display.optimize_watermarks)
Matt Roperd93c0372015-12-03 11:37:41 -080015277 return;
15278
15279 /*
15280 * We need to hold connection_mutex before calling duplicate_state so
15281 * that the connector loop is protected.
15282 */
15283 drm_modeset_acquire_init(&ctx, 0);
15284retry:
Matt Roper0cd12622016-01-12 07:13:37 -080015285 ret = drm_modeset_lock_all_ctx(dev, &ctx);
Matt Roperd93c0372015-12-03 11:37:41 -080015286 if (ret == -EDEADLK) {
15287 drm_modeset_backoff(&ctx);
15288 goto retry;
15289 } else if (WARN_ON(ret)) {
Matt Roper0cd12622016-01-12 07:13:37 -080015290 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015291 }
15292
15293 state = drm_atomic_helper_duplicate_state(dev, &ctx);
15294 if (WARN_ON(IS_ERR(state)))
Matt Roper0cd12622016-01-12 07:13:37 -080015295 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015296
Matt Ropered4a6a72016-02-23 17:20:13 -080015297 /*
15298 * Hardware readout is the only time we don't want to calculate
15299 * intermediate watermarks (since we don't trust the current
15300 * watermarks).
15301 */
15302 to_intel_atomic_state(state)->skip_intermediate_wm = true;
15303
Matt Roperd93c0372015-12-03 11:37:41 -080015304 ret = intel_atomic_check(dev, state);
15305 if (ret) {
15306 /*
15307 * If we fail here, it means that the hardware appears to be
15308 * programmed in a way that shouldn't be possible, given our
15309 * understanding of watermark requirements. This might mean a
15310 * mistake in the hardware readout code or a mistake in the
15311 * watermark calculations for a given platform. Raise a WARN
15312 * so that this is noticeable.
15313 *
15314 * If this actually happens, we'll have to just leave the
15315 * BIOS-programmed watermarks untouched and hope for the best.
15316 */
15317 WARN(true, "Could not determine valid watermarks for inherited state\n");
Matt Roper0cd12622016-01-12 07:13:37 -080015318 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015319 }
15320
15321 /* Write calculated watermark values back */
15322 to_i915(dev)->wm.config = to_intel_atomic_state(state)->wm_config;
15323 for_each_crtc_in_state(state, crtc, cstate, i) {
15324 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
15325
Matt Ropered4a6a72016-02-23 17:20:13 -080015326 cs->wm.need_postvbl_update = true;
15327 dev_priv->display.optimize_watermarks(cs);
Matt Roperd93c0372015-12-03 11:37:41 -080015328 }
15329
15330 drm_atomic_state_free(state);
Matt Roper0cd12622016-01-12 07:13:37 -080015331fail:
Matt Roperd93c0372015-12-03 11:37:41 -080015332 drm_modeset_drop_locks(&ctx);
15333 drm_modeset_acquire_fini(&ctx);
15334}
15335
Jesse Barnes79e53942008-11-07 14:24:08 -080015336void intel_modeset_init(struct drm_device *dev)
15337{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +030015338 struct drm_i915_private *dev_priv = to_i915(dev);
15339 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Damien Lespiau1fe47782014-03-03 17:31:47 +000015340 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015341 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080015342 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080015343
15344 drm_mode_config_init(dev);
15345
15346 dev->mode_config.min_width = 0;
15347 dev->mode_config.min_height = 0;
15348
Dave Airlie019d96c2011-09-29 16:20:42 +010015349 dev->mode_config.preferred_depth = 24;
15350 dev->mode_config.prefer_shadow = 1;
15351
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000015352 dev->mode_config.allow_fb_modifiers = true;
15353
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020015354 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080015355
Jesse Barnesb690e962010-07-19 13:53:12 -070015356 intel_init_quirks(dev);
15357
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030015358 intel_init_pm(dev);
15359
Ben Widawskye3c74752013-04-05 13:12:39 -070015360 if (INTEL_INFO(dev)->num_pipes == 0)
15361 return;
15362
Lukas Wunner69f92f62015-07-15 13:57:35 +020015363 /*
15364 * There may be no VBT; and if the BIOS enabled SSC we can
15365 * just keep using it to avoid unnecessary flicker. Whereas if the
15366 * BIOS isn't using it, don't assume it will work even if the VBT
15367 * indicates as much.
15368 */
15369 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
15370 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15371 DREF_SSC1_ENABLE);
15372
15373 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
15374 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
15375 bios_lvds_use_ssc ? "en" : "dis",
15376 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
15377 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
15378 }
15379 }
15380
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015381 if (IS_GEN2(dev)) {
15382 dev->mode_config.max_width = 2048;
15383 dev->mode_config.max_height = 2048;
15384 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070015385 dev->mode_config.max_width = 4096;
15386 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080015387 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015388 dev->mode_config.max_width = 8192;
15389 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080015390 }
Damien Lespiau068be562014-03-28 14:17:49 +000015391
Ville Syrjälädc41c152014-08-13 11:57:05 +030015392 if (IS_845G(dev) || IS_I865G(dev)) {
15393 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
15394 dev->mode_config.cursor_height = 1023;
15395 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000015396 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
15397 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
15398 } else {
15399 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
15400 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
15401 }
15402
Joonas Lahtinen72e96d62016-03-30 16:57:10 +030015403 dev->mode_config.fb_base = ggtt->mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080015404
Zhao Yakui28c97732009-10-09 11:39:41 +080015405 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015406 INTEL_INFO(dev)->num_pipes,
15407 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080015408
Damien Lespiau055e3932014-08-18 13:49:10 +010015409 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015410 intel_crtc_init(dev, pipe);
Damien Lespiau3bdcfc02015-02-28 14:54:09 +000015411 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +000015412 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015413 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030015414 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000015415 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015416 }
Jesse Barnes79e53942008-11-07 14:24:08 -080015417 }
15418
Ville Syrjäläbfa7df02015-09-24 23:29:18 +030015419 intel_update_czclk(dev_priv);
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +020015420 intel_update_rawclk(dev_priv);
Ville Syrjäläbfa7df02015-09-24 23:29:18 +030015421 intel_update_cdclk(dev);
15422
Daniel Vettere72f9fb2013-06-05 13:34:06 +020015423 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010015424
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015425 /* Just disable it once at startup */
15426 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015427 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000015428
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015429 drm_modeset_lock_all(dev);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015430 intel_modeset_setup_hw_state(dev);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015431 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015432
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015433 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015434 struct intel_initial_plane_config plane_config = {};
15435
Jesse Barnes46f297f2014-03-07 08:57:48 -080015436 if (!crtc->active)
15437 continue;
15438
Jesse Barnes46f297f2014-03-07 08:57:48 -080015439 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080015440 * Note that reserving the BIOS fb up front prevents us
15441 * from stuffing other stolen allocations like the ring
15442 * on top. This prevents some ugliness at boot time, and
15443 * can even allow for smooth boot transitions if the BIOS
15444 * fb is large enough for the active pipe configuration.
15445 */
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015446 dev_priv->display.get_initial_plane_config(crtc,
15447 &plane_config);
15448
15449 /*
15450 * If the fb is shared between multiple heads, we'll
15451 * just get the first one.
15452 */
15453 intel_find_initial_plane_obj(crtc, &plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015454 }
Matt Roperd93c0372015-12-03 11:37:41 -080015455
15456 /*
15457 * Make sure hardware watermarks really match the state we read out.
15458 * Note that we need to do this after reconstructing the BIOS fb's
15459 * since the watermark calculation done here will use pstate->fb.
15460 */
15461 sanitize_watermarks(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010015462}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080015463
Daniel Vetter7fad7982012-07-04 17:51:47 +020015464static void intel_enable_pipe_a(struct drm_device *dev)
15465{
15466 struct intel_connector *connector;
15467 struct drm_connector *crt = NULL;
15468 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015469 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020015470
15471 /* We can't just switch on the pipe A, we need to set things up with a
15472 * proper mode and output configuration. As a gross hack, enable pipe A
15473 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015474 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020015475 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15476 crt = &connector->base;
15477 break;
15478 }
15479 }
15480
15481 if (!crt)
15482 return;
15483
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015484 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020015485 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020015486}
15487
Daniel Vetterfa555832012-10-10 23:14:00 +020015488static bool
15489intel_check_plane_mapping(struct intel_crtc *crtc)
15490{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015491 struct drm_device *dev = crtc->base.dev;
15492 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä649636e2015-09-22 19:50:01 +030015493 u32 val;
Daniel Vetterfa555832012-10-10 23:14:00 +020015494
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015495 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020015496 return true;
15497
Ville Syrjälä649636e2015-09-22 19:50:01 +030015498 val = I915_READ(DSPCNTR(!crtc->plane));
Daniel Vetterfa555832012-10-10 23:14:00 +020015499
15500 if ((val & DISPLAY_PLANE_ENABLE) &&
15501 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15502 return false;
15503
15504 return true;
15505}
15506
Ville Syrjälä02e93c32015-08-26 19:39:19 +030015507static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
15508{
15509 struct drm_device *dev = crtc->base.dev;
15510 struct intel_encoder *encoder;
15511
15512 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15513 return true;
15514
15515 return false;
15516}
15517
Ville Syrjälädd756192016-02-17 21:28:45 +020015518static bool intel_encoder_has_connectors(struct intel_encoder *encoder)
15519{
15520 struct drm_device *dev = encoder->base.dev;
15521 struct intel_connector *connector;
15522
15523 for_each_connector_on_encoder(dev, &encoder->base, connector)
15524 return true;
15525
15526 return false;
15527}
15528
Daniel Vetter24929352012-07-02 20:28:59 +020015529static void intel_sanitize_crtc(struct intel_crtc *crtc)
15530{
15531 struct drm_device *dev = crtc->base.dev;
15532 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula4d1de972016-03-18 17:05:42 +020015533 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter24929352012-07-02 20:28:59 +020015534
Daniel Vetter24929352012-07-02 20:28:59 +020015535 /* Clear any frame start delays used for debugging left by the BIOS */
Jani Nikula4d1de972016-03-18 17:05:42 +020015536 if (!transcoder_is_dsi(cpu_transcoder)) {
15537 i915_reg_t reg = PIPECONF(cpu_transcoder);
15538
15539 I915_WRITE(reg,
15540 I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15541 }
Daniel Vetter24929352012-07-02 20:28:59 +020015542
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015543 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010015544 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030015545 if (crtc->active) {
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015546 struct intel_plane *plane;
15547
Daniel Vetter96256042015-02-13 21:03:42 +010015548 drm_crtc_vblank_on(&crtc->base);
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015549
15550 /* Disable everything but the primary plane */
15551 for_each_intel_plane_on_crtc(dev, crtc, plane) {
15552 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
15553 continue;
15554
15555 plane->disable_plane(&plane->base, &crtc->base);
15556 }
Daniel Vetter96256042015-02-13 21:03:42 +010015557 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015558
Daniel Vetter24929352012-07-02 20:28:59 +020015559 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020015560 * disable the crtc (and hence change the state) if it is wrong. Note
15561 * that gen4+ has a fixed plane -> pipe mapping. */
15562 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020015563 bool plane;
15564
Daniel Vetter24929352012-07-02 20:28:59 +020015565 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
15566 crtc->base.base.id);
15567
15568 /* Pipe has the wrong plane attached and the plane is active.
15569 * Temporarily change the plane mapping and disable everything
15570 * ... */
15571 plane = crtc->plane;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015572 to_intel_plane_state(crtc->base.primary->state)->visible = true;
Daniel Vetter24929352012-07-02 20:28:59 +020015573 crtc->plane = !plane;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015574 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015575 crtc->plane = plane;
Daniel Vetter24929352012-07-02 20:28:59 +020015576 }
Daniel Vetter24929352012-07-02 20:28:59 +020015577
Daniel Vetter7fad7982012-07-04 17:51:47 +020015578 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15579 crtc->pipe == PIPE_A && !crtc->active) {
15580 /* BIOS forgot to enable pipe A, this mostly happens after
15581 * resume. Force-enable the pipe to fix this, the update_dpms
15582 * call below we restore the pipe to the right state, but leave
15583 * the required bits on. */
15584 intel_enable_pipe_a(dev);
15585 }
15586
Daniel Vetter24929352012-07-02 20:28:59 +020015587 /* Adjust the state of the output pipe according to whether we
15588 * have active connectors/encoders. */
Maarten Lankhorst842e0302016-03-02 15:48:01 +010015589 if (crtc->active && !intel_crtc_has_encoders(crtc))
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015590 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015591
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030015592 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010015593 /*
15594 * We start out with underrun reporting disabled to avoid races.
15595 * For correct bookkeeping mark this on active crtcs.
15596 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020015597 * Also on gmch platforms we dont have any hardware bits to
15598 * disable the underrun reporting. Which means we need to start
15599 * out with underrun reporting disabled also on inactive pipes,
15600 * since otherwise we'll complain about the garbage we read when
15601 * e.g. coming up after runtime pm.
15602 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010015603 * No protection against concurrent access is required - at
15604 * worst a fifo underrun happens which also sets this to false.
15605 */
15606 crtc->cpu_fifo_underrun_disabled = true;
15607 crtc->pch_fifo_underrun_disabled = true;
15608 }
Daniel Vetter24929352012-07-02 20:28:59 +020015609}
15610
15611static void intel_sanitize_encoder(struct intel_encoder *encoder)
15612{
15613 struct intel_connector *connector;
15614 struct drm_device *dev = encoder->base.dev;
15615
15616 /* We need to check both for a crtc link (meaning that the
15617 * encoder is active and trying to read from a pipe) and the
15618 * pipe itself being active. */
15619 bool has_active_crtc = encoder->base.crtc &&
15620 to_intel_crtc(encoder->base.crtc)->active;
15621
Ville Syrjälädd756192016-02-17 21:28:45 +020015622 if (intel_encoder_has_connectors(encoder) && !has_active_crtc) {
Daniel Vetter24929352012-07-02 20:28:59 +020015623 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15624 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015625 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015626
15627 /* Connector is active, but has no active pipe. This is
15628 * fallout from our resume register restoring. Disable
15629 * the encoder manually again. */
15630 if (encoder->base.crtc) {
15631 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15632 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015633 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015634 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030015635 if (encoder->post_disable)
15636 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020015637 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020015638 encoder->base.crtc = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015639
15640 /* Inconsistent output/port/pipe state happens presumably due to
15641 * a bug in one of the get_hw_state functions. Or someplace else
15642 * in our code, like the register restore mess on resume. Clamp
15643 * things to off as a safer default. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015644 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015645 if (connector->encoder != encoder)
15646 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020015647 connector->base.dpms = DRM_MODE_DPMS_OFF;
15648 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015649 }
15650 }
15651 /* Enabled encoders without active connectors will be fixed in
15652 * the crtc fixup. */
15653}
15654
Imre Deak04098752014-02-18 00:02:16 +020015655void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015656{
15657 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020015658 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015659
Imre Deak04098752014-02-18 00:02:16 +020015660 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15661 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15662 i915_disable_vga(dev);
15663 }
15664}
15665
15666void i915_redisable_vga(struct drm_device *dev)
15667{
15668 struct drm_i915_private *dev_priv = dev->dev_private;
15669
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015670 /* This function can be called both from intel_modeset_setup_hw_state or
15671 * at a very early point in our resume sequence, where the power well
15672 * structures are not yet restored. Since this function is at a very
15673 * paranoid "someone might have enabled VGA while we were not looking"
15674 * level, just check if the power well is enabled instead of trying to
15675 * follow the "don't touch the power well if we don't need it" policy
15676 * the rest of the driver uses. */
Imre Deak6392f842016-02-12 18:55:13 +020015677 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015678 return;
15679
Imre Deak04098752014-02-18 00:02:16 +020015680 i915_redisable_vga_power_on(dev);
Imre Deak6392f842016-02-12 18:55:13 +020015681
15682 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015683}
15684
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015685static bool primary_get_hw_state(struct intel_plane *plane)
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015686{
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015687 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015688
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015689 return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015690}
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015691
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015692/* FIXME read out full plane state for all planes */
15693static void readout_plane_state(struct intel_crtc *crtc)
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015694{
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015695 struct drm_plane *primary = crtc->base.primary;
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015696 struct intel_plane_state *plane_state =
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015697 to_intel_plane_state(primary->state);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015698
Matt Roper19b8d382015-09-24 15:53:17 -070015699 plane_state->visible = crtc->active &&
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015700 primary_get_hw_state(to_intel_plane(primary));
15701
15702 if (plane_state->visible)
15703 crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015704}
15705
Daniel Vetter30e984d2013-06-05 13:34:17 +020015706static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020015707{
15708 struct drm_i915_private *dev_priv = dev->dev_private;
15709 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020015710 struct intel_crtc *crtc;
15711 struct intel_encoder *encoder;
15712 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020015713 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020015714
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015715 dev_priv->active_crtcs = 0;
15716
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015717 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015718 struct intel_crtc_state *crtc_state = crtc->config;
15719 int pixclk = 0;
Daniel Vetter3b117c82013-04-17 20:15:07 +020015720
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015721 __drm_atomic_helper_crtc_destroy_state(&crtc->base, &crtc_state->base);
15722 memset(crtc_state, 0, sizeof(*crtc_state));
15723 crtc_state->base.crtc = &crtc->base;
Daniel Vetter24929352012-07-02 20:28:59 +020015724
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015725 crtc_state->base.active = crtc_state->base.enable =
15726 dev_priv->display.get_pipe_config(crtc, crtc_state);
15727
15728 crtc->base.enabled = crtc_state->base.enable;
15729 crtc->active = crtc_state->base.active;
15730
15731 if (crtc_state->base.active) {
15732 dev_priv->active_crtcs |= 1 << crtc->pipe;
15733
15734 if (IS_BROADWELL(dev_priv)) {
15735 pixclk = ilk_pipe_pixel_rate(crtc_state);
15736
15737 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
15738 if (crtc_state->ips_enabled)
15739 pixclk = DIV_ROUND_UP(pixclk * 100, 95);
15740 } else if (IS_VALLEYVIEW(dev_priv) ||
15741 IS_CHERRYVIEW(dev_priv) ||
15742 IS_BROXTON(dev_priv))
15743 pixclk = crtc_state->base.adjusted_mode.crtc_clock;
15744 else
15745 WARN_ON(dev_priv->display.modeset_calc_cdclk);
15746 }
15747
15748 dev_priv->min_pixclk[crtc->pipe] = pixclk;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015749
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015750 readout_plane_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020015751
15752 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
15753 crtc->base.base.id,
15754 crtc->active ? "enabled" : "disabled");
15755 }
15756
Daniel Vetter53589012013-06-05 13:34:16 +020015757 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15758 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15759
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020015760 pll->on = pll->funcs.get_hw_state(dev_priv, pll,
15761 &pll->config.hw_state);
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015762 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015763 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010015764 if (crtc->active && crtc->config->shared_dpll == pll)
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015765 pll->config.crtc_mask |= 1 << crtc->pipe;
Daniel Vetter53589012013-06-05 13:34:16 +020015766 }
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010015767 pll->active_mask = pll->config.crtc_mask;
Daniel Vetter53589012013-06-05 13:34:16 +020015768
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015769 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015770 pll->name, pll->config.crtc_mask, pll->on);
Daniel Vetter53589012013-06-05 13:34:16 +020015771 }
15772
Damien Lespiaub2784e12014-08-05 11:29:37 +010015773 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015774 pipe = 0;
15775
15776 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070015777 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15778 encoder->base.crtc = &crtc->base;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015779 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020015780 } else {
15781 encoder->base.crtc = NULL;
15782 }
15783
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015784 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020015785 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015786 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015787 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015788 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020015789 }
15790
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015791 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015792 if (connector->get_hw_state(connector)) {
15793 connector->base.dpms = DRM_MODE_DPMS_ON;
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010015794
15795 encoder = connector->encoder;
15796 connector->base.encoder = &encoder->base;
15797
15798 if (encoder->base.crtc &&
15799 encoder->base.crtc->state->active) {
15800 /*
15801 * This has to be done during hardware readout
15802 * because anything calling .crtc_disable may
15803 * rely on the connector_mask being accurate.
15804 */
15805 encoder->base.crtc->state->connector_mask |=
15806 1 << drm_connector_index(&connector->base);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +010015807 encoder->base.crtc->state->encoder_mask |=
15808 1 << drm_encoder_index(&encoder->base);
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010015809 }
15810
Daniel Vetter24929352012-07-02 20:28:59 +020015811 } else {
15812 connector->base.dpms = DRM_MODE_DPMS_OFF;
15813 connector->base.encoder = NULL;
15814 }
15815 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
15816 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030015817 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015818 connector->base.encoder ? "enabled" : "disabled");
15819 }
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015820
15821 for_each_intel_crtc(dev, crtc) {
15822 crtc->base.hwmode = crtc->config->base.adjusted_mode;
15823
15824 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
15825 if (crtc->base.state->active) {
15826 intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
15827 intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
15828 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
15829
15830 /*
15831 * The initial mode needs to be set in order to keep
15832 * the atomic core happy. It wants a valid mode if the
15833 * crtc's enabled, so we do the above call.
15834 *
15835 * At this point some state updated by the connectors
15836 * in their ->detect() callback has not run yet, so
15837 * no recalculation can be done yet.
15838 *
15839 * Even if we could do a recalculation and modeset
15840 * right now it would cause a double modeset if
15841 * fbdev or userspace chooses a different initial mode.
15842 *
15843 * If that happens, someone indicated they wanted a
15844 * mode change, which means it's safe to do a full
15845 * recalculation.
15846 */
15847 crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
Ville Syrjälä9eca68322015-09-10 18:59:10 +030015848
15849 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
15850 update_scanline_offset(crtc);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015851 }
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020015852
15853 intel_pipe_config_sanity_check(dev_priv, crtc->config);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015854 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020015855}
15856
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015857/* Scan out the current hw modeset state,
15858 * and sanitizes it to the current state
15859 */
15860static void
15861intel_modeset_setup_hw_state(struct drm_device *dev)
Daniel Vetter30e984d2013-06-05 13:34:17 +020015862{
15863 struct drm_i915_private *dev_priv = dev->dev_private;
15864 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015865 struct intel_crtc *crtc;
15866 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020015867 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015868
15869 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020015870
15871 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010015872 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015873 intel_sanitize_encoder(encoder);
15874 }
15875
Damien Lespiau055e3932014-08-18 13:49:10 +010015876 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020015877 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15878 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015879 intel_dump_pipe_config(crtc, crtc->config,
15880 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020015881 }
Daniel Vetter9a935852012-07-05 22:34:27 +020015882
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020015883 intel_modeset_update_connector_atomic_state(dev);
15884
Daniel Vetter35c95372013-07-17 06:55:04 +020015885 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15886 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15887
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010015888 if (!pll->on || pll->active_mask)
Daniel Vetter35c95372013-07-17 06:55:04 +020015889 continue;
15890
15891 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15892
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020015893 pll->funcs.disable(dev_priv, pll);
Daniel Vetter35c95372013-07-17 06:55:04 +020015894 pll->on = false;
15895 }
15896
Wayne Boyer666a4532015-12-09 12:29:35 -080015897 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Ville Syrjälä6eb1a682015-06-24 22:00:03 +030015898 vlv_wm_get_hw_state(dev);
15899 else if (IS_GEN9(dev))
Pradeep Bhat30789992014-11-04 17:06:45 +000015900 skl_wm_get_hw_state(dev);
15901 else if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030015902 ilk_wm_get_hw_state(dev);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020015903
15904 for_each_intel_crtc(dev, crtc) {
15905 unsigned long put_domains;
15906
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +010015907 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020015908 if (WARN_ON(put_domains))
15909 modeset_put_power_domains(dev_priv, put_domains);
15910 }
15911 intel_display_set_init_power(dev_priv, false);
Paulo Zanoni010cf732016-01-19 11:35:48 -020015912
15913 intel_fbc_init_pipe_state(dev_priv);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015914}
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030015915
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015916void intel_display_resume(struct drm_device *dev)
15917{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015918 struct drm_i915_private *dev_priv = to_i915(dev);
15919 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
15920 struct drm_modeset_acquire_ctx ctx;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015921 int ret;
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015922 bool setup = false;
Daniel Vetterf30da182013-04-11 20:22:50 +020015923
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015924 dev_priv->modeset_restore_state = NULL;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015925
Maarten Lankhorstea49c9a2016-02-16 15:27:42 +010015926 /*
15927 * This is a cludge because with real atomic modeset mode_config.mutex
15928 * won't be taken. Unfortunately some probed state like
15929 * audio_codec_enable is still protected by mode_config.mutex, so lock
15930 * it here for now.
15931 */
15932 mutex_lock(&dev->mode_config.mutex);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015933 drm_modeset_acquire_init(&ctx, 0);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015934
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015935retry:
15936 ret = drm_modeset_lock_all_ctx(dev, &ctx);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015937
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015938 if (ret == 0 && !setup) {
15939 setup = true;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015940
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015941 intel_modeset_setup_hw_state(dev);
15942 i915_redisable_vga(dev);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010015943 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020015944
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015945 if (ret == 0 && state) {
15946 struct drm_crtc_state *crtc_state;
15947 struct drm_crtc *crtc;
15948 int i;
15949
15950 state->acquire_ctx = &ctx;
15951
15952 for_each_crtc_in_state(state, crtc, crtc_state, i) {
15953 /*
15954 * Force recalculation even if we restore
15955 * current state. With fast modeset this may not result
15956 * in a modeset when the state is compatible.
15957 */
15958 crtc_state->mode_changed = true;
15959 }
15960
15961 ret = drm_atomic_commit(state);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015962 }
15963
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015964 if (ret == -EDEADLK) {
15965 drm_modeset_backoff(&ctx);
15966 goto retry;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015967 }
15968
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015969 drm_modeset_drop_locks(&ctx);
15970 drm_modeset_acquire_fini(&ctx);
Maarten Lankhorstea49c9a2016-02-16 15:27:42 +010015971 mutex_unlock(&dev->mode_config.mutex);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015972
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015973 if (ret) {
15974 DRM_ERROR("Restoring old state failed with %i\n", ret);
15975 drm_atomic_state_free(state);
15976 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010015977}
15978
15979void intel_modeset_gem_init(struct drm_device *dev)
15980{
Jesse Barnes484b41d2014-03-07 08:57:55 -080015981 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070015982 struct drm_i915_gem_object *obj;
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015983 int ret;
Jesse Barnes484b41d2014-03-07 08:57:55 -080015984
Imre Deakae484342014-03-31 15:10:44 +030015985 intel_init_gt_powersave(dev);
Imre Deakae484342014-03-31 15:10:44 +030015986
Chris Wilson1833b132012-05-09 11:56:28 +010015987 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020015988
15989 intel_setup_overlay(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080015990
15991 /*
15992 * Make sure any fbs we allocated at startup are properly
15993 * pinned & fenced. When we do the allocation it's too early
15994 * for this.
15995 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010015996 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070015997 obj = intel_fb_obj(c->primary->fb);
15998 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080015999 continue;
16000
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010016001 mutex_lock(&dev->struct_mutex);
Ville Syrjälä3465c582016-02-15 22:54:43 +020016002 ret = intel_pin_and_fence_fb_obj(c->primary->fb,
16003 c->primary->state->rotation);
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010016004 mutex_unlock(&dev->struct_mutex);
16005 if (ret) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080016006 DRM_ERROR("failed to pin boot fb on pipe %d\n",
16007 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100016008 drm_framebuffer_unreference(c->primary->fb);
16009 c->primary->fb = NULL;
Maarten Lankhorst36750f22015-06-01 12:49:54 +020016010 c->primary->crtc = c->primary->state->crtc = NULL;
Matt Roperafd65eb2015-02-03 13:10:04 -080016011 update_state_fb(c->primary);
Maarten Lankhorst36750f22015-06-01 12:49:54 +020016012 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
Jesse Barnes484b41d2014-03-07 08:57:55 -080016013 }
16014 }
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020016015
16016 intel_backlight_register(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080016017}
16018
Imre Deak4932e2c2014-02-11 17:12:48 +020016019void intel_connector_unregister(struct intel_connector *intel_connector)
16020{
16021 struct drm_connector *connector = &intel_connector->base;
16022
16023 intel_panel_destroy_backlight(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +010016024 drm_connector_unregister(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020016025}
16026
Jesse Barnes79e53942008-11-07 14:24:08 -080016027void intel_modeset_cleanup(struct drm_device *dev)
16028{
Jesse Barnes652c3932009-08-17 13:31:43 -070016029 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula19c80542015-12-16 12:48:16 +020016030 struct intel_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070016031
Imre Deak2eb52522014-11-19 15:30:05 +020016032 intel_disable_gt_powersave(dev);
16033
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020016034 intel_backlight_unregister(dev);
16035
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016036 /*
16037 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020016038 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016039 * experience fancy races otherwise.
16040 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020016041 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070016042
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016043 /*
16044 * Due to the hpd irq storm handling the hotplug work can re-arm the
16045 * poll handlers. Hence disable polling after hpd handling is shut down.
16046 */
Keith Packardf87ea762010-10-03 19:36:26 -070016047 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016048
Jesse Barnes723bfd72010-10-07 16:01:13 -070016049 intel_unregister_dsm_handler();
16050
Paulo Zanonic937ab3e52016-01-19 11:35:46 -020016051 intel_fbc_global_disable(dev_priv);
Kristian Høgsberg69341a52009-11-11 12:19:17 -050016052
Chris Wilson1630fe72011-07-08 12:22:42 +010016053 /* flush any delayed tasks or pending work */
16054 flush_scheduled_work();
16055
Jani Nikuladb31af1d2013-11-08 16:48:53 +020016056 /* destroy the backlight and sysfs files before encoders/connectors */
Jani Nikula19c80542015-12-16 12:48:16 +020016057 for_each_intel_connector(dev, connector)
16058 connector->unregister(connector);
Paulo Zanonid9255d52013-09-26 20:05:59 -030016059
Jesse Barnes79e53942008-11-07 14:24:08 -080016060 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010016061
16062 intel_cleanup_overlay(dev);
Imre Deakae484342014-03-31 15:10:44 +030016063
Imre Deakae484342014-03-31 15:10:44 +030016064 intel_cleanup_gt_powersave(dev);
Daniel Vetterf5949142016-01-13 11:55:28 +010016065
16066 intel_teardown_gmbus(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080016067}
16068
Dave Airlie28d52042009-09-21 14:33:58 +100016069/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080016070 * Return which encoder is currently attached for connector.
16071 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010016072struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080016073{
Chris Wilsondf0e9242010-09-09 16:20:55 +010016074 return &intel_attached_encoder(connector)->base;
16075}
Jesse Barnes79e53942008-11-07 14:24:08 -080016076
Chris Wilsondf0e9242010-09-09 16:20:55 +010016077void intel_connector_attach_encoder(struct intel_connector *connector,
16078 struct intel_encoder *encoder)
16079{
16080 connector->encoder = encoder;
16081 drm_mode_connector_attach_encoder(&connector->base,
16082 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080016083}
Dave Airlie28d52042009-09-21 14:33:58 +100016084
16085/*
16086 * set vga decode state - true == enable VGA decode
16087 */
16088int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
16089{
16090 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000016091 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100016092 u16 gmch_ctrl;
16093
Chris Wilson75fa0412014-02-07 18:37:02 -020016094 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
16095 DRM_ERROR("failed to read control word\n");
16096 return -EIO;
16097 }
16098
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020016099 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
16100 return 0;
16101
Dave Airlie28d52042009-09-21 14:33:58 +100016102 if (state)
16103 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
16104 else
16105 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020016106
16107 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
16108 DRM_ERROR("failed to write control word\n");
16109 return -EIO;
16110 }
16111
Dave Airlie28d52042009-09-21 14:33:58 +100016112 return 0;
16113}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016114
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016115struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016116
16117 u32 power_well_driver;
16118
Chris Wilson63b66e52013-08-08 15:12:06 +020016119 int num_transcoders;
16120
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016121 struct intel_cursor_error_state {
16122 u32 control;
16123 u32 position;
16124 u32 base;
16125 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010016126 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016127
16128 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020016129 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016130 u32 source;
Imre Deakf301b1e2014-04-18 15:55:04 +030016131 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010016132 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016133
16134 struct intel_plane_error_state {
16135 u32 control;
16136 u32 stride;
16137 u32 size;
16138 u32 pos;
16139 u32 addr;
16140 u32 surface;
16141 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010016142 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020016143
16144 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020016145 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020016146 enum transcoder cpu_transcoder;
16147
16148 u32 conf;
16149
16150 u32 htotal;
16151 u32 hblank;
16152 u32 hsync;
16153 u32 vtotal;
16154 u32 vblank;
16155 u32 vsync;
16156 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016157};
16158
16159struct intel_display_error_state *
16160intel_display_capture_error_state(struct drm_device *dev)
16161{
Jani Nikulafbee40d2014-03-31 14:27:18 +030016162 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016163 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020016164 int transcoders[] = {
16165 TRANSCODER_A,
16166 TRANSCODER_B,
16167 TRANSCODER_C,
16168 TRANSCODER_EDP,
16169 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016170 int i;
16171
Chris Wilson63b66e52013-08-08 15:12:06 +020016172 if (INTEL_INFO(dev)->num_pipes == 0)
16173 return NULL;
16174
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016175 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016176 if (error == NULL)
16177 return NULL;
16178
Imre Deak190be112013-11-25 17:15:31 +020016179 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016180 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
16181
Damien Lespiau055e3932014-08-18 13:49:10 +010016182 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020016183 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020016184 __intel_display_power_is_enabled(dev_priv,
16185 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020016186 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016187 continue;
16188
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030016189 error->cursor[i].control = I915_READ(CURCNTR(i));
16190 error->cursor[i].position = I915_READ(CURPOS(i));
16191 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016192
16193 error->plane[i].control = I915_READ(DSPCNTR(i));
16194 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016195 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030016196 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016197 error->plane[i].pos = I915_READ(DSPPOS(i));
16198 }
Paulo Zanonica291362013-03-06 20:03:14 -030016199 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
16200 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016201 if (INTEL_INFO(dev)->gen >= 4) {
16202 error->plane[i].surface = I915_READ(DSPSURF(i));
16203 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
16204 }
16205
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016206 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e2014-04-18 15:55:04 +030016207
Sonika Jindal3abfce72014-07-21 15:23:43 +053016208 if (HAS_GMCH_DISPLAY(dev))
Imre Deakf301b1e2014-04-18 15:55:04 +030016209 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020016210 }
16211
Jani Nikula4d1de972016-03-18 17:05:42 +020016212 /* Note: this does not include DSI transcoders. */
Chris Wilson63b66e52013-08-08 15:12:06 +020016213 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +030016214 if (HAS_DDI(dev_priv))
Chris Wilson63b66e52013-08-08 15:12:06 +020016215 error->num_transcoders++; /* Account for eDP. */
16216
16217 for (i = 0; i < error->num_transcoders; i++) {
16218 enum transcoder cpu_transcoder = transcoders[i];
16219
Imre Deakddf9c532013-11-27 22:02:02 +020016220 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020016221 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020016222 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020016223 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016224 continue;
16225
Chris Wilson63b66e52013-08-08 15:12:06 +020016226 error->transcoder[i].cpu_transcoder = cpu_transcoder;
16227
16228 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
16229 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
16230 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
16231 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
16232 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
16233 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
16234 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016235 }
16236
16237 return error;
16238}
16239
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016240#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
16241
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016242void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016243intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016244 struct drm_device *dev,
16245 struct intel_display_error_state *error)
16246{
Damien Lespiau055e3932014-08-18 13:49:10 +010016247 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016248 int i;
16249
Chris Wilson63b66e52013-08-08 15:12:06 +020016250 if (!error)
16251 return;
16252
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016253 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020016254 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016255 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016256 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010016257 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016258 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020016259 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020016260 onoff(error->pipe[i].power_domain_on));
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016261 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e2014-04-18 15:55:04 +030016262 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016263
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016264 err_printf(m, "Plane [%d]:\n", i);
16265 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
16266 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016267 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016268 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
16269 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016270 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030016271 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016272 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016273 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016274 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
16275 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016276 }
16277
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016278 err_printf(m, "Cursor [%d]:\n", i);
16279 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
16280 err_printf(m, " POS: %08x\n", error->cursor[i].position);
16281 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016282 }
Chris Wilson63b66e52013-08-08 15:12:06 +020016283
16284 for (i = 0; i < error->num_transcoders; i++) {
Jani Nikulada205632016-03-15 21:51:10 +020016285 err_printf(m, "CPU transcoder: %s\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020016286 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020016287 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020016288 onoff(error->transcoder[i].power_domain_on));
Chris Wilson63b66e52013-08-08 15:12:06 +020016289 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
16290 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
16291 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
16292 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
16293 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
16294 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
16295 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
16296 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016297}