blob: a297e1ffafb58aa71f58e305c3e725f352a01243 [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äcd2d34d2016-04-12 22:14:34 +03001533static void _vlv_enable_pll(struct intel_crtc *crtc,
1534 const struct intel_crtc_state *pipe_config)
1535{
1536 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1537 enum pipe pipe = crtc->pipe;
1538
1539 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
1540 POSTING_READ(DPLL(pipe));
1541 udelay(150);
1542
1543 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1544 DRM_ERROR("DPLL %d failed to lock\n", pipe);
1545}
1546
Ville Syrjäläd288f652014-10-28 13:20:22 +02001547static void vlv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001548 const struct intel_crtc_state *pipe_config)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001549{
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001550 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001551 enum pipe pipe = crtc->pipe;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001552
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001553 assert_pipe_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001554
Daniel Vetter87442f72013-06-06 00:52:17 +02001555 /* PLL is protected by panel, make sure we can write it */
Ville Syrjälä7d1a83c2016-03-15 16:39:58 +02001556 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001557
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001558 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1559 _vlv_enable_pll(crtc, pipe_config);
Daniel Vetter426115c2013-07-11 22:13:42 +02001560
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001561 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1562 POSTING_READ(DPLL_MD(pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001563}
1564
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001565
1566static void _chv_enable_pll(struct intel_crtc *crtc,
1567 const struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001568{
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001569 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001570 enum pipe pipe = crtc->pipe;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001571 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001572 u32 tmp;
1573
Ville Syrjäläa5805162015-05-26 20:42:30 +03001574 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001575
1576 /* Enable back the 10bit clock to display controller */
1577 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1578 tmp |= DPIO_DCLKP_EN;
1579 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1580
Ville Syrjälä54433e92015-05-26 20:42:31 +03001581 mutex_unlock(&dev_priv->sb_lock);
1582
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001583 /*
1584 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1585 */
1586 udelay(1);
1587
1588 /* Enable PLL */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001589 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001590
1591 /* Check PLL is locked */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001592 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001593 DRM_ERROR("PLL %d failed to lock\n", pipe);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001594}
1595
1596static void chv_enable_pll(struct intel_crtc *crtc,
1597 const struct intel_crtc_state *pipe_config)
1598{
1599 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1600 enum pipe pipe = crtc->pipe;
1601
1602 assert_pipe_disabled(dev_priv, pipe);
1603
1604 /* PLL is protected by panel, make sure we can write it */
1605 assert_panel_unlocked(dev_priv, pipe);
1606
1607 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1608 _chv_enable_pll(crtc, pipe_config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001609
Ville Syrjäläc2317752016-03-15 16:39:56 +02001610 if (pipe != PIPE_A) {
1611 /*
1612 * WaPixelRepeatModeFixForC0:chv
1613 *
1614 * DPLLCMD is AWOL. Use chicken bits to propagate
1615 * the value from DPLLBMD to either pipe B or C.
1616 */
1617 I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
1618 I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
1619 I915_WRITE(CBR4_VLV, 0);
1620 dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
1621
1622 /*
1623 * DPLLB VGA mode also seems to cause problems.
1624 * We should always have it disabled.
1625 */
1626 WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
1627 } else {
1628 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1629 POSTING_READ(DPLL_MD(pipe));
1630 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001631}
1632
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001633static int intel_num_dvo_pipes(struct drm_device *dev)
1634{
1635 struct intel_crtc *crtc;
1636 int count = 0;
1637
1638 for_each_intel_crtc(dev, crtc)
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001639 count += crtc->base.state->active &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001640 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001641
1642 return count;
1643}
1644
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001645static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001646{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001647 struct drm_device *dev = crtc->base.dev;
1648 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001649 i915_reg_t reg = DPLL(crtc->pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001650 u32 dpll = crtc->config->dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001651
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001652 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001653
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001654 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001655 if (IS_MOBILE(dev) && !IS_I830(dev))
1656 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001657
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001658 /* Enable DVO 2x clock on both PLLs if necessary */
1659 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1660 /*
1661 * It appears to be important that we don't enable this
1662 * for the current pipe before otherwise configuring the
1663 * PLL. No idea how this should be handled if multiple
1664 * DVO outputs are enabled simultaneosly.
1665 */
1666 dpll |= DPLL_DVO_2X_MODE;
1667 I915_WRITE(DPLL(!crtc->pipe),
1668 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1669 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001670
Ville Syrjäläc2b63372015-10-07 22:08:25 +03001671 /*
1672 * Apparently we need to have VGA mode enabled prior to changing
1673 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1674 * dividers, even though the register value does change.
1675 */
1676 I915_WRITE(reg, 0);
1677
Ville Syrjälä8e7a65a2015-10-07 22:08:24 +03001678 I915_WRITE(reg, dpll);
1679
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001680 /* Wait for the clocks to stabilize. */
1681 POSTING_READ(reg);
1682 udelay(150);
1683
1684 if (INTEL_INFO(dev)->gen >= 4) {
1685 I915_WRITE(DPLL_MD(crtc->pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001686 crtc->config->dpll_hw_state.dpll_md);
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001687 } else {
1688 /* The pixel multiplier can only be updated once the
1689 * DPLL is enabled and the clocks are stable.
1690 *
1691 * So write it again.
1692 */
1693 I915_WRITE(reg, dpll);
1694 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001695
1696 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001697 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001698 POSTING_READ(reg);
1699 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001700 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001701 POSTING_READ(reg);
1702 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001703 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001704 POSTING_READ(reg);
1705 udelay(150); /* wait for warmup */
1706}
1707
1708/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001709 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001710 * @dev_priv: i915 private structure
1711 * @pipe: pipe PLL to disable
1712 *
1713 * Disable the PLL for @pipe, making sure the pipe is off first.
1714 *
1715 * Note! This is for pre-ILK only.
1716 */
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001717static void i9xx_disable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001718{
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001719 struct drm_device *dev = crtc->base.dev;
1720 struct drm_i915_private *dev_priv = dev->dev_private;
1721 enum pipe pipe = crtc->pipe;
1722
1723 /* Disable DVO 2x clock on both PLLs if necessary */
1724 if (IS_I830(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001725 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001726 !intel_num_dvo_pipes(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001727 I915_WRITE(DPLL(PIPE_B),
1728 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1729 I915_WRITE(DPLL(PIPE_A),
1730 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1731 }
1732
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001733 /* Don't disable pipe or pipe PLLs if needed */
1734 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1735 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001736 return;
1737
1738 /* Make sure the pipe isn't still relying on us */
1739 assert_pipe_disabled(dev_priv, pipe);
1740
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001741 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
Daniel Vetter50b44a42013-06-05 13:34:33 +02001742 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001743}
1744
Jesse Barnesf6071162013-10-01 10:41:38 -07001745static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1746{
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001747 u32 val;
Jesse Barnesf6071162013-10-01 10:41:38 -07001748
1749 /* Make sure the pipe isn't still relying on us */
1750 assert_pipe_disabled(dev_priv, pipe);
1751
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02001752 val = DPLL_INTEGRATED_REF_CLK_VLV |
1753 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1754 if (pipe != PIPE_A)
1755 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1756
Jesse Barnesf6071162013-10-01 10:41:38 -07001757 I915_WRITE(DPLL(pipe), val);
1758 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001759}
1760
1761static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1762{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001763 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001764 u32 val;
1765
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001766 /* Make sure the pipe isn't still relying on us */
1767 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001768
Ville Syrjälä60bfe442015-06-29 15:25:49 +03001769 val = DPLL_SSC_REF_CLK_CHV |
1770 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001771 if (pipe != PIPE_A)
1772 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02001773
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001774 I915_WRITE(DPLL(pipe), val);
1775 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001776
Ville Syrjäläa5805162015-05-26 20:42:30 +03001777 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd7520482014-04-09 13:28:59 +03001778
1779 /* Disable 10bit clock to display controller */
1780 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1781 val &= ~DPIO_DCLKP_EN;
1782 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1783
Ville Syrjäläa5805162015-05-26 20:42:30 +03001784 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001785}
1786
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001787void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001788 struct intel_digital_port *dport,
1789 unsigned int expected_mask)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001790{
1791 u32 port_mask;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001792 i915_reg_t dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001793
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001794 switch (dport->port) {
1795 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001796 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001797 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001798 break;
1799 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001800 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001801 dpll_reg = DPLL(0);
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001802 expected_mask <<= 4;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001803 break;
1804 case PORT_D:
1805 port_mask = DPLL_PORTD_READY_MASK;
1806 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001807 break;
1808 default:
1809 BUG();
1810 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001811
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001812 if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
1813 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1814 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
Jesse Barnes89b667f2013-04-18 14:51:36 -07001815}
1816
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001817static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1818 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001819{
Daniel Vetter23670b322012-11-01 09:15:30 +01001820 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001821 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001822 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001823 i915_reg_t reg;
1824 uint32_t val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001825
Jesse Barnes040484a2011-01-03 12:14:26 -08001826 /* Make sure PCH DPLL is enabled */
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02001827 assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
Jesse Barnes040484a2011-01-03 12:14:26 -08001828
1829 /* FDI must be feeding us bits for PCH ports */
1830 assert_fdi_tx_enabled(dev_priv, pipe);
1831 assert_fdi_rx_enabled(dev_priv, pipe);
1832
Daniel Vetter23670b322012-11-01 09:15:30 +01001833 if (HAS_PCH_CPT(dev)) {
1834 /* Workaround: Set the timing override bit before enabling the
1835 * pch transcoder. */
1836 reg = TRANS_CHICKEN2(pipe);
1837 val = I915_READ(reg);
1838 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1839 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001840 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001841
Daniel Vetterab9412b2013-05-03 11:49:46 +02001842 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001843 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001844 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001845
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001846 if (HAS_PCH_IBX(dev_priv)) {
Jesse Barnese9bcff52011-06-24 12:19:20 -07001847 /*
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001848 * Make the BPC in transcoder be consistent with
1849 * that in pipeconf reg. For HDMI we must use 8bpc
1850 * here for both 8bpc and 12bpc.
Jesse Barnese9bcff52011-06-24 12:19:20 -07001851 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001852 val &= ~PIPECONF_BPC_MASK;
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001853 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
1854 val |= PIPECONF_8BPC;
1855 else
1856 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001857 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001858
1859 val &= ~TRANS_INTERLACE_MASK;
1860 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001861 if (HAS_PCH_IBX(dev_priv) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001862 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001863 val |= TRANS_LEGACY_INTERLACED_ILK;
1864 else
1865 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001866 else
1867 val |= TRANS_PROGRESSIVE;
1868
Jesse Barnes040484a2011-01-03 12:14:26 -08001869 I915_WRITE(reg, val | TRANS_ENABLE);
1870 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001871 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08001872}
1873
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001874static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001875 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001876{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001877 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001878
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001879 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001880 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001881 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001882
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001883 /* Workaround: set timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001884 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01001885 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001886 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001887
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001888 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001889 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001890
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001891 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1892 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001893 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001894 else
1895 val |= TRANS_PROGRESSIVE;
1896
Daniel Vetterab9412b2013-05-03 11:49:46 +02001897 I915_WRITE(LPT_TRANSCONF, val);
1898 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02001899 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001900}
1901
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001902static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1903 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001904{
Daniel Vetter23670b322012-11-01 09:15:30 +01001905 struct drm_device *dev = dev_priv->dev;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001906 i915_reg_t reg;
1907 uint32_t val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001908
1909 /* FDI relies on the transcoder */
1910 assert_fdi_tx_disabled(dev_priv, pipe);
1911 assert_fdi_rx_disabled(dev_priv, pipe);
1912
Jesse Barnes291906f2011-02-02 12:28:03 -08001913 /* Ports must be off as well */
1914 assert_pch_ports_disabled(dev_priv, pipe);
1915
Daniel Vetterab9412b2013-05-03 11:49:46 +02001916 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001917 val = I915_READ(reg);
1918 val &= ~TRANS_ENABLE;
1919 I915_WRITE(reg, val);
1920 /* wait for PCH transcoder off, transcoder state */
1921 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001922 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01001923
Ville Syrjäläc4656132015-10-29 21:25:56 +02001924 if (HAS_PCH_CPT(dev)) {
Daniel Vetter23670b322012-11-01 09:15:30 +01001925 /* Workaround: Clear the timing override chicken bit again. */
1926 reg = TRANS_CHICKEN2(pipe);
1927 val = I915_READ(reg);
1928 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1929 I915_WRITE(reg, val);
1930 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001931}
1932
Paulo Zanoniab4d9662012-10-31 18:12:55 -02001933static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001934{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001935 u32 val;
1936
Daniel Vetterab9412b2013-05-03 11:49:46 +02001937 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001938 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02001939 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001940 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02001941 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001942 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001943
1944 /* Workaround: clear timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001945 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01001946 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001947 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Jesse Barnes92f25842011-01-04 15:09:34 -08001948}
1949
1950/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001951 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02001952 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08001953 *
Paulo Zanoni03722642014-01-17 13:51:09 -02001954 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001955 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08001956 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02001957static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001958{
Paulo Zanoni03722642014-01-17 13:51:09 -02001959 struct drm_device *dev = crtc->base.dev;
1960 struct drm_i915_private *dev_priv = dev->dev_private;
1961 enum pipe pipe = crtc->pipe;
Ville Syrjälä1a70a7282015-10-29 21:25:50 +02001962 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter1a240d42012-11-29 22:18:51 +01001963 enum pipe pch_transcoder;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001964 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001965 u32 val;
1966
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03001967 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
1968
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001969 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001970 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001971 assert_sprites_disabled(dev_priv, pipe);
1972
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001973 if (HAS_PCH_LPT(dev_priv))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001974 pch_transcoder = TRANSCODER_A;
1975 else
1976 pch_transcoder = pipe;
1977
Jesse Barnesb24e7172011-01-04 15:09:30 -08001978 /*
1979 * A pipe without a PLL won't actually be able to drive bits from
1980 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1981 * need the check.
1982 */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001983 if (HAS_GMCH_DISPLAY(dev_priv))
Jani Nikulaa65347b2015-11-27 12:21:46 +02001984 if (crtc->config->has_dsi_encoder)
Jani Nikula23538ef2013-08-27 15:12:22 +03001985 assert_dsi_pll_enabled(dev_priv);
1986 else
1987 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001988 else {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001989 if (crtc->config->has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08001990 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001991 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001992 assert_fdi_tx_pll_enabled(dev_priv,
1993 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08001994 }
1995 /* FIXME: assert CPU port conditions for SNB+ */
1996 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08001997
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001998 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001999 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002000 if (val & PIPECONF_ENABLE) {
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002001 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2002 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
Chris Wilson00d70b12011-03-17 07:18:29 +00002003 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002004 }
Chris Wilson00d70b12011-03-17 07:18:29 +00002005
2006 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02002007 POSTING_READ(reg);
Ville Syrjäläb7792d82015-12-14 18:23:43 +02002008
2009 /*
2010 * Until the pipe starts DSL will read as 0, which would cause
2011 * an apparent vblank timestamp jump, which messes up also the
2012 * frame count when it's derived from the timestamps. So let's
2013 * wait for the pipe to start properly before we call
2014 * drm_crtc_vblank_on()
2015 */
2016 if (dev->max_vblank_count == 0 &&
2017 wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
2018 DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08002019}
2020
2021/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002022 * intel_disable_pipe - disable a pipe, asserting requirements
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002023 * @crtc: crtc whose pipes is to be disabled
Jesse Barnesb24e7172011-01-04 15:09:30 -08002024 *
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002025 * Disable the pipe of @crtc, making sure that various hardware
2026 * specific requirements are met, if applicable, e.g. plane
2027 * disabled, panel fitter off, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002028 *
2029 * Will wait until the pipe has shut down before returning.
2030 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002031static void intel_disable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002032{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002033 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002034 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002035 enum pipe pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002036 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002037 u32 val;
2038
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03002039 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
2040
Jesse Barnesb24e7172011-01-04 15:09:30 -08002041 /*
2042 * Make sure planes won't keep trying to pump pixels to us,
2043 * or we might hang the display.
2044 */
2045 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002046 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07002047 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002048
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002049 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002050 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00002051 if ((val & PIPECONF_ENABLE) == 0)
2052 return;
2053
Ville Syrjälä67adc642014-08-15 01:21:57 +03002054 /*
2055 * Double wide has implications for planes
2056 * so best keep it disabled when not needed.
2057 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002058 if (crtc->config->double_wide)
Ville Syrjälä67adc642014-08-15 01:21:57 +03002059 val &= ~PIPECONF_DOUBLE_WIDE;
2060
2061 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002062 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2063 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Ville Syrjälä67adc642014-08-15 01:21:57 +03002064 val &= ~PIPECONF_ENABLE;
2065
2066 I915_WRITE(reg, val);
2067 if ((val & PIPECONF_ENABLE) == 0)
2068 intel_wait_for_pipe_off(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002069}
2070
Chris Wilson693db182013-03-05 14:52:39 +00002071static bool need_vtd_wa(struct drm_device *dev)
2072{
2073#ifdef CONFIG_INTEL_IOMMU
2074 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2075 return true;
2076#endif
2077 return false;
2078}
2079
Ville Syrjälä832be822016-01-12 21:08:33 +02002080static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
2081{
2082 return IS_GEN2(dev_priv) ? 2048 : 4096;
2083}
2084
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002085static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
2086 uint64_t fb_modifier, unsigned int cpp)
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002087{
2088 switch (fb_modifier) {
2089 case DRM_FORMAT_MOD_NONE:
2090 return cpp;
2091 case I915_FORMAT_MOD_X_TILED:
2092 if (IS_GEN2(dev_priv))
2093 return 128;
2094 else
2095 return 512;
2096 case I915_FORMAT_MOD_Y_TILED:
2097 if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
2098 return 128;
2099 else
2100 return 512;
2101 case I915_FORMAT_MOD_Yf_TILED:
2102 switch (cpp) {
2103 case 1:
2104 return 64;
2105 case 2:
2106 case 4:
2107 return 128;
2108 case 8:
2109 case 16:
2110 return 256;
2111 default:
2112 MISSING_CASE(cpp);
2113 return cpp;
2114 }
2115 break;
2116 default:
2117 MISSING_CASE(fb_modifier);
2118 return cpp;
2119 }
2120}
2121
Ville Syrjälä832be822016-01-12 21:08:33 +02002122unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
2123 uint64_t fb_modifier, unsigned int cpp)
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002124{
Ville Syrjälä832be822016-01-12 21:08:33 +02002125 if (fb_modifier == DRM_FORMAT_MOD_NONE)
2126 return 1;
2127 else
2128 return intel_tile_size(dev_priv) /
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002129 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002130}
2131
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002132/* Return the tile dimensions in pixel units */
2133static void intel_tile_dims(const struct drm_i915_private *dev_priv,
2134 unsigned int *tile_width,
2135 unsigned int *tile_height,
2136 uint64_t fb_modifier,
2137 unsigned int cpp)
2138{
2139 unsigned int tile_width_bytes =
2140 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
2141
2142 *tile_width = tile_width_bytes / cpp;
2143 *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
2144}
2145
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002146unsigned int
2147intel_fb_align_height(struct drm_device *dev, unsigned int height,
Ville Syrjälä832be822016-01-12 21:08:33 +02002148 uint32_t pixel_format, uint64_t fb_modifier)
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002149{
Ville Syrjälä832be822016-01-12 21:08:33 +02002150 unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
2151 unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
2152
2153 return ALIGN(height, tile_height);
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002154}
2155
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002156unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
2157{
2158 unsigned int size = 0;
2159 int i;
2160
2161 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
2162 size += rot_info->plane[i].width * rot_info->plane[i].height;
2163
2164 return size;
2165}
2166
Daniel Vetter75c82a52015-10-14 16:51:04 +02002167static void
Ville Syrjälä3465c582016-02-15 22:54:43 +02002168intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
2169 const struct drm_framebuffer *fb,
2170 unsigned int rotation)
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002171{
Ville Syrjälä2d7a2152016-02-15 22:54:47 +02002172 if (intel_rotation_90_or_270(rotation)) {
2173 *view = i915_ggtt_view_rotated;
2174 view->params.rotated = to_intel_framebuffer(fb)->rot_info;
2175 } else {
2176 *view = i915_ggtt_view_normal;
2177 }
2178}
2179
2180static void
2181intel_fill_fb_info(struct drm_i915_private *dev_priv,
2182 struct drm_framebuffer *fb)
2183{
2184 struct intel_rotation_info *info = &to_intel_framebuffer(fb)->rot_info;
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002185 unsigned int tile_size, tile_width, tile_height, cpp;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002186
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002187 tile_size = intel_tile_size(dev_priv);
2188
2189 cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002190 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2191 fb->modifier[0], cpp);
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002192
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002193 info->plane[0].width = DIV_ROUND_UP(fb->pitches[0], tile_width * cpp);
2194 info->plane[0].height = DIV_ROUND_UP(fb->height, tile_height);
Tvrtko Ursulin84fe03f2015-06-23 14:26:46 +01002195
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01002196 if (info->pixel_format == DRM_FORMAT_NV12) {
Ville Syrjälä832be822016-01-12 21:08:33 +02002197 cpp = drm_format_plane_cpp(fb->pixel_format, 1);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002198 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2199 fb->modifier[1], cpp);
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002200
Ville Syrjälä2d7a2152016-02-15 22:54:47 +02002201 info->uv_offset = fb->offsets[1];
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002202 info->plane[1].width = DIV_ROUND_UP(fb->pitches[1], tile_width * cpp);
2203 info->plane[1].height = DIV_ROUND_UP(fb->height / 2, tile_height);
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01002204 }
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002205}
2206
Ville Syrjälä603525d2016-01-12 21:08:37 +02002207static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002208{
2209 if (INTEL_INFO(dev_priv)->gen >= 9)
2210 return 256 * 1024;
Ville Syrjälä985b8bb2015-06-11 16:31:15 +03002211 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
Wayne Boyer666a4532015-12-09 12:29:35 -08002212 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002213 return 128 * 1024;
2214 else if (INTEL_INFO(dev_priv)->gen >= 4)
2215 return 4 * 1024;
2216 else
Ville Syrjälä44c59052015-06-11 16:31:16 +03002217 return 0;
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002218}
2219
Ville Syrjälä603525d2016-01-12 21:08:37 +02002220static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
2221 uint64_t fb_modifier)
2222{
2223 switch (fb_modifier) {
2224 case DRM_FORMAT_MOD_NONE:
2225 return intel_linear_alignment(dev_priv);
2226 case I915_FORMAT_MOD_X_TILED:
2227 if (INTEL_INFO(dev_priv)->gen >= 9)
2228 return 256 * 1024;
2229 return 0;
2230 case I915_FORMAT_MOD_Y_TILED:
2231 case I915_FORMAT_MOD_Yf_TILED:
2232 return 1 * 1024 * 1024;
2233 default:
2234 MISSING_CASE(fb_modifier);
2235 return 0;
2236 }
2237}
2238
Chris Wilson127bd2a2010-07-23 23:32:05 +01002239int
Ville Syrjälä3465c582016-02-15 22:54:43 +02002240intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
2241 unsigned int rotation)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002242{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002243 struct drm_device *dev = fb->dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00002244 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002245 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002246 struct i915_ggtt_view view;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002247 u32 alignment;
2248 int ret;
2249
Matt Roperebcdd392014-07-09 16:22:11 -07002250 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2251
Ville Syrjälä603525d2016-01-12 21:08:37 +02002252 alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002253
Ville Syrjälä3465c582016-02-15 22:54:43 +02002254 intel_fill_fb_ggtt_view(&view, fb, rotation);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002255
Chris Wilson693db182013-03-05 14:52:39 +00002256 /* Note that the w/a also requires 64 PTE of padding following the
2257 * bo. We currently fill all unused PTE with the shadow page and so
2258 * we should always have valid PTE following the scanout preventing
2259 * the VT-d warning.
2260 */
2261 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2262 alignment = 256 * 1024;
2263
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002264 /*
2265 * Global gtt pte registers are special registers which actually forward
2266 * writes to a chunk of system memory. Which means that there is no risk
2267 * that the register values disappear as soon as we call
2268 * intel_runtime_pm_put(), so it is correct to wrap only the
2269 * pin/unpin/fence and not more.
2270 */
2271 intel_runtime_pm_get(dev_priv);
2272
Maarten Lankhorst7580d772015-08-18 13:40:06 +02002273 ret = i915_gem_object_pin_to_display_plane(obj, alignment,
2274 &view);
Chris Wilson48b956c2010-09-14 12:50:34 +01002275 if (ret)
Maarten Lankhorstb26a6b32015-09-23 13:27:09 +02002276 goto err_pm;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002277
2278 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2279 * fence, whereas 965+ only requires a fence if using
2280 * framebuffer compression. For simplicity, we always install
2281 * a fence as the cost is not that onerous.
2282 */
Vivek Kasireddy98072162015-10-29 18:54:38 -07002283 if (view.type == I915_GGTT_VIEW_NORMAL) {
2284 ret = i915_gem_object_get_fence(obj);
2285 if (ret == -EDEADLK) {
2286 /*
2287 * -EDEADLK means there are no free fences
2288 * no pending flips.
2289 *
2290 * This is propagated to atomic, but it uses
2291 * -EDEADLK to force a locking recovery, so
2292 * change the returned error to -EBUSY.
2293 */
2294 ret = -EBUSY;
2295 goto err_unpin;
2296 } else if (ret)
2297 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002298
Vivek Kasireddy98072162015-10-29 18:54:38 -07002299 i915_gem_object_pin_fence(obj);
2300 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002301
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002302 intel_runtime_pm_put(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002303 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002304
2305err_unpin:
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002306 i915_gem_object_unpin_from_display_plane(obj, &view);
Maarten Lankhorstb26a6b32015-09-23 13:27:09 +02002307err_pm:
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002308 intel_runtime_pm_put(dev_priv);
Chris Wilson48b956c2010-09-14 12:50:34 +01002309 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002310}
2311
Ville Syrjälä3465c582016-02-15 22:54:43 +02002312static void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
Chris Wilson1690e1e2011-12-14 13:57:08 +01002313{
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002314 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002315 struct i915_ggtt_view view;
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002316
Matt Roperebcdd392014-07-09 16:22:11 -07002317 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2318
Ville Syrjälä3465c582016-02-15 22:54:43 +02002319 intel_fill_fb_ggtt_view(&view, fb, rotation);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002320
Vivek Kasireddy98072162015-10-29 18:54:38 -07002321 if (view.type == I915_GGTT_VIEW_NORMAL)
2322 i915_gem_object_unpin_fence(obj);
2323
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002324 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002325}
2326
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002327/*
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002328 * Adjust the tile offset by moving the difference into
2329 * the x/y offsets.
2330 *
2331 * Input tile dimensions and pitch must already be
2332 * rotated to match x and y, and in pixel units.
2333 */
2334static u32 intel_adjust_tile_offset(int *x, int *y,
2335 unsigned int tile_width,
2336 unsigned int tile_height,
2337 unsigned int tile_size,
2338 unsigned int pitch_tiles,
2339 u32 old_offset,
2340 u32 new_offset)
2341{
2342 unsigned int tiles;
2343
2344 WARN_ON(old_offset & (tile_size - 1));
2345 WARN_ON(new_offset & (tile_size - 1));
2346 WARN_ON(new_offset > old_offset);
2347
2348 tiles = (old_offset - new_offset) / tile_size;
2349
2350 *y += tiles / pitch_tiles * tile_height;
2351 *x += tiles % pitch_tiles * tile_width;
2352
2353 return new_offset;
2354}
2355
2356/*
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002357 * Computes the linear offset to the base tile and adjusts
2358 * x, y. bytes per pixel is assumed to be a power-of-two.
2359 *
2360 * In the 90/270 rotated case, x and y are assumed
2361 * to be already rotated to match the rotated GTT view, and
2362 * pitch is the tile_height aligned framebuffer height.
2363 */
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002364u32 intel_compute_tile_offset(int *x, int *y,
2365 const struct drm_framebuffer *fb, int plane,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002366 unsigned int pitch,
2367 unsigned int rotation)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002368{
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002369 const struct drm_i915_private *dev_priv = to_i915(fb->dev);
2370 uint64_t fb_modifier = fb->modifier[plane];
2371 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002372 u32 offset, offset_aligned, alignment;
2373
2374 alignment = intel_surf_alignment(dev_priv, fb_modifier);
2375 if (alignment)
2376 alignment--;
2377
Ville Syrjäläb5c65332016-01-12 21:08:31 +02002378 if (fb_modifier != DRM_FORMAT_MOD_NONE) {
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002379 unsigned int tile_size, tile_width, tile_height;
2380 unsigned int tile_rows, tiles, pitch_tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002381
Ville Syrjäläd8433102016-01-12 21:08:35 +02002382 tile_size = intel_tile_size(dev_priv);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002383 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2384 fb_modifier, cpp);
2385
2386 if (intel_rotation_90_or_270(rotation)) {
2387 pitch_tiles = pitch / tile_height;
2388 swap(tile_width, tile_height);
2389 } else {
2390 pitch_tiles = pitch / (tile_width * cpp);
2391 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002392
Ville Syrjäläd8433102016-01-12 21:08:35 +02002393 tile_rows = *y / tile_height;
2394 *y %= tile_height;
Chris Wilsonbc752862013-02-21 20:04:31 +00002395
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002396 tiles = *x / tile_width;
2397 *x %= tile_width;
Ville Syrjäläd8433102016-01-12 21:08:35 +02002398
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002399 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2400 offset_aligned = offset & ~alignment;
Chris Wilsonbc752862013-02-21 20:04:31 +00002401
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002402 intel_adjust_tile_offset(x, y, tile_width, tile_height,
2403 tile_size, pitch_tiles,
2404 offset, offset_aligned);
2405 } else {
Chris Wilsonbc752862013-02-21 20:04:31 +00002406 offset = *y * pitch + *x * cpp;
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002407 offset_aligned = offset & ~alignment;
2408
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002409 *y = (offset & alignment) / pitch;
2410 *x = ((offset & alignment) - *y * pitch) / cpp;
Chris Wilsonbc752862013-02-21 20:04:31 +00002411 }
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002412
2413 return offset_aligned;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002414}
2415
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002416static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002417{
2418 switch (format) {
2419 case DISPPLANE_8BPP:
2420 return DRM_FORMAT_C8;
2421 case DISPPLANE_BGRX555:
2422 return DRM_FORMAT_XRGB1555;
2423 case DISPPLANE_BGRX565:
2424 return DRM_FORMAT_RGB565;
2425 default:
2426 case DISPPLANE_BGRX888:
2427 return DRM_FORMAT_XRGB8888;
2428 case DISPPLANE_RGBX888:
2429 return DRM_FORMAT_XBGR8888;
2430 case DISPPLANE_BGRX101010:
2431 return DRM_FORMAT_XRGB2101010;
2432 case DISPPLANE_RGBX101010:
2433 return DRM_FORMAT_XBGR2101010;
2434 }
2435}
2436
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002437static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2438{
2439 switch (format) {
2440 case PLANE_CTL_FORMAT_RGB_565:
2441 return DRM_FORMAT_RGB565;
2442 default:
2443 case PLANE_CTL_FORMAT_XRGB_8888:
2444 if (rgb_order) {
2445 if (alpha)
2446 return DRM_FORMAT_ABGR8888;
2447 else
2448 return DRM_FORMAT_XBGR8888;
2449 } else {
2450 if (alpha)
2451 return DRM_FORMAT_ARGB8888;
2452 else
2453 return DRM_FORMAT_XRGB8888;
2454 }
2455 case PLANE_CTL_FORMAT_XRGB_2101010:
2456 if (rgb_order)
2457 return DRM_FORMAT_XBGR2101010;
2458 else
2459 return DRM_FORMAT_XRGB2101010;
2460 }
2461}
2462
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002463static bool
Daniel Vetterf6936e22015-03-26 12:17:05 +01002464intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2465 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002466{
2467 struct drm_device *dev = crtc->base.dev;
Paulo Zanoni3badb492015-09-23 12:52:23 -03002468 struct drm_i915_private *dev_priv = to_i915(dev);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002469 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002470 struct drm_i915_gem_object *obj = NULL;
2471 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Damien Lespiau2d140302015-02-05 17:22:18 +00002472 struct drm_framebuffer *fb = &plane_config->fb->base;
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002473 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2474 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2475 PAGE_SIZE);
2476
2477 size_aligned -= base_aligned;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002478
Chris Wilsonff2652e2014-03-10 08:07:02 +00002479 if (plane_config->size == 0)
2480 return false;
2481
Paulo Zanoni3badb492015-09-23 12:52:23 -03002482 /* If the FB is too big, just don't use it since fbdev is not very
2483 * important and we should probably use that space with FBC or other
2484 * features. */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002485 if (size_aligned * 2 > ggtt->stolen_usable_size)
Paulo Zanoni3badb492015-09-23 12:52:23 -03002486 return false;
2487
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002488 mutex_lock(&dev->struct_mutex);
2489
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002490 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2491 base_aligned,
2492 base_aligned,
2493 size_aligned);
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002494 if (!obj) {
2495 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002496 return false;
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002497 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002498
Damien Lespiau49af4492015-01-20 12:51:44 +00002499 obj->tiling_mode = plane_config->tiling;
2500 if (obj->tiling_mode == I915_TILING_X)
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002501 obj->stride = fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002502
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002503 mode_cmd.pixel_format = fb->pixel_format;
2504 mode_cmd.width = fb->width;
2505 mode_cmd.height = fb->height;
2506 mode_cmd.pitches[0] = fb->pitches[0];
Daniel Vetter18c52472015-02-10 17:16:09 +00002507 mode_cmd.modifier[0] = fb->modifier[0];
2508 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002509
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002510 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002511 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002512 DRM_DEBUG_KMS("intel fb init failed\n");
2513 goto out_unref_obj;
2514 }
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002515
Jesse Barnes46f297f2014-03-07 08:57:48 -08002516 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002517
Daniel Vetterf6936e22015-03-26 12:17:05 +01002518 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002519 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002520
2521out_unref_obj:
2522 drm_gem_object_unreference(&obj->base);
2523 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002524 return false;
2525}
2526
Matt Roperafd65eb2015-02-03 13:10:04 -08002527/* Update plane->state->fb to match plane->fb after driver-internal updates */
2528static void
2529update_state_fb(struct drm_plane *plane)
2530{
2531 if (plane->fb == plane->state->fb)
2532 return;
2533
2534 if (plane->state->fb)
2535 drm_framebuffer_unreference(plane->state->fb);
2536 plane->state->fb = plane->fb;
2537 if (plane->state->fb)
2538 drm_framebuffer_reference(plane->state->fb);
2539}
2540
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002541static void
Daniel Vetterf6936e22015-03-26 12:17:05 +01002542intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2543 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002544{
2545 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnesd9ceb812014-10-09 12:57:43 -07002546 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002547 struct drm_crtc *c;
2548 struct intel_crtc *i;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002549 struct drm_i915_gem_object *obj;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002550 struct drm_plane *primary = intel_crtc->base.primary;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002551 struct drm_plane_state *plane_state = primary->state;
Matt Roper200757f2015-12-03 11:37:36 -08002552 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
2553 struct intel_plane *intel_plane = to_intel_plane(primary);
Matt Roper0a8d8a82015-12-03 11:37:38 -08002554 struct intel_plane_state *intel_state =
2555 to_intel_plane_state(plane_state);
Daniel Vetter88595ac2015-03-26 12:42:24 +01002556 struct drm_framebuffer *fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002557
Damien Lespiau2d140302015-02-05 17:22:18 +00002558 if (!plane_config->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002559 return;
2560
Daniel Vetterf6936e22015-03-26 12:17:05 +01002561 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002562 fb = &plane_config->fb->base;
2563 goto valid_fb;
Damien Lespiauf55548b2015-02-05 18:30:20 +00002564 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002565
Damien Lespiau2d140302015-02-05 17:22:18 +00002566 kfree(plane_config->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002567
2568 /*
2569 * Failed to alloc the obj, check to see if we should share
2570 * an fb with another CRTC instead
2571 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002572 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002573 i = to_intel_crtc(c);
2574
2575 if (c == &intel_crtc->base)
2576 continue;
2577
Matt Roper2ff8fde2014-07-08 07:50:07 -07002578 if (!i->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002579 continue;
2580
Daniel Vetter88595ac2015-03-26 12:42:24 +01002581 fb = c->primary->fb;
2582 if (!fb)
Matt Roper2ff8fde2014-07-08 07:50:07 -07002583 continue;
2584
Daniel Vetter88595ac2015-03-26 12:42:24 +01002585 obj = intel_fb_obj(fb);
Matt Roper2ff8fde2014-07-08 07:50:07 -07002586 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002587 drm_framebuffer_reference(fb);
2588 goto valid_fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002589 }
2590 }
Daniel Vetter88595ac2015-03-26 12:42:24 +01002591
Matt Roper200757f2015-12-03 11:37:36 -08002592 /*
2593 * We've failed to reconstruct the BIOS FB. Current display state
2594 * indicates that the primary plane is visible, but has a NULL FB,
2595 * which will lead to problems later if we don't fix it up. The
2596 * simplest solution is to just disable the primary plane now and
2597 * pretend the BIOS never had it enabled.
2598 */
2599 to_intel_plane_state(plane_state)->visible = false;
2600 crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
Ville Syrjälä2622a082016-03-09 19:07:26 +02002601 intel_pre_disable_primary_noatomic(&intel_crtc->base);
Matt Roper200757f2015-12-03 11:37:36 -08002602 intel_plane->disable_plane(primary, &intel_crtc->base);
2603
Daniel Vetter88595ac2015-03-26 12:42:24 +01002604 return;
2605
2606valid_fb:
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002607 plane_state->src_x = 0;
2608 plane_state->src_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002609 plane_state->src_w = fb->width << 16;
2610 plane_state->src_h = fb->height << 16;
2611
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002612 plane_state->crtc_x = 0;
2613 plane_state->crtc_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002614 plane_state->crtc_w = fb->width;
2615 plane_state->crtc_h = fb->height;
2616
Matt Roper0a8d8a82015-12-03 11:37:38 -08002617 intel_state->src.x1 = plane_state->src_x;
2618 intel_state->src.y1 = plane_state->src_y;
2619 intel_state->src.x2 = plane_state->src_x + plane_state->src_w;
2620 intel_state->src.y2 = plane_state->src_y + plane_state->src_h;
2621 intel_state->dst.x1 = plane_state->crtc_x;
2622 intel_state->dst.y1 = plane_state->crtc_y;
2623 intel_state->dst.x2 = plane_state->crtc_x + plane_state->crtc_w;
2624 intel_state->dst.y2 = plane_state->crtc_y + plane_state->crtc_h;
2625
Daniel Vetter88595ac2015-03-26 12:42:24 +01002626 obj = intel_fb_obj(fb);
2627 if (obj->tiling_mode != I915_TILING_NONE)
2628 dev_priv->preserve_bios_swizzle = true;
2629
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002630 drm_framebuffer_reference(fb);
2631 primary->fb = primary->state->fb = fb;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002632 primary->crtc = primary->state->crtc = &intel_crtc->base;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002633 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
Ville Syrjäläa9ff8712015-06-24 21:59:34 +03002634 obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002635}
2636
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002637static void i9xx_update_primary_plane(struct drm_plane *primary,
2638 const struct intel_crtc_state *crtc_state,
2639 const struct intel_plane_state *plane_state)
Jesse Barnes81255562010-08-02 12:07:50 -07002640{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002641 struct drm_device *dev = primary->dev;
Jesse Barnes81255562010-08-02 12:07:50 -07002642 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002643 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2644 struct drm_framebuffer *fb = plane_state->base.fb;
2645 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Jesse Barnes81255562010-08-02 12:07:50 -07002646 int plane = intel_crtc->plane;
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002647 u32 linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002648 u32 dspcntr;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002649 i915_reg_t reg = DSPCNTR(plane);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002650 unsigned int rotation = plane_state->base.rotation;
Ville Syrjäläac484962016-01-20 21:05:26 +02002651 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002652 int x = plane_state->src.x1 >> 16;
2653 int y = plane_state->src.y1 >> 16;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002654
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002655 dspcntr = DISPPLANE_GAMMA_ENABLE;
2656
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002657 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002658
2659 if (INTEL_INFO(dev)->gen < 4) {
2660 if (intel_crtc->pipe == PIPE_B)
2661 dspcntr |= DISPPLANE_SEL_PIPE_B;
2662
2663 /* pipesrc and dspsize control the size that is scaled from,
2664 * which should always be the user's requested size.
2665 */
2666 I915_WRITE(DSPSIZE(plane),
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002667 ((crtc_state->pipe_src_h - 1) << 16) |
2668 (crtc_state->pipe_src_w - 1));
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002669 I915_WRITE(DSPPOS(plane), 0);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002670 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2671 I915_WRITE(PRIMSIZE(plane),
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002672 ((crtc_state->pipe_src_h - 1) << 16) |
2673 (crtc_state->pipe_src_w - 1));
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002674 I915_WRITE(PRIMPOS(plane), 0);
2675 I915_WRITE(PRIMCNSTALPHA(plane), 0);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002676 }
2677
Ville Syrjälä57779d02012-10-31 17:50:14 +02002678 switch (fb->pixel_format) {
2679 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002680 dspcntr |= DISPPLANE_8BPP;
2681 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002682 case DRM_FORMAT_XRGB1555:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002683 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002684 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002685 case DRM_FORMAT_RGB565:
2686 dspcntr |= DISPPLANE_BGRX565;
2687 break;
2688 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002689 dspcntr |= DISPPLANE_BGRX888;
2690 break;
2691 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002692 dspcntr |= DISPPLANE_RGBX888;
2693 break;
2694 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002695 dspcntr |= DISPPLANE_BGRX101010;
2696 break;
2697 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002698 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002699 break;
2700 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002701 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002702 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002703
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002704 if (INTEL_INFO(dev)->gen >= 4 &&
2705 obj->tiling_mode != I915_TILING_NONE)
2706 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07002707
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002708 if (IS_G4X(dev))
2709 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2710
Ville Syrjäläac484962016-01-20 21:05:26 +02002711 linear_offset = y * fb->pitches[0] + x * cpp;
Jesse Barnes81255562010-08-02 12:07:50 -07002712
Daniel Vetterc2c75132012-07-05 12:17:30 +02002713 if (INTEL_INFO(dev)->gen >= 4) {
2714 intel_crtc->dspaddr_offset =
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002715 intel_compute_tile_offset(&x, &y, fb, 0,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002716 fb->pitches[0], rotation);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002717 linear_offset -= intel_crtc->dspaddr_offset;
2718 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002719 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002720 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002721
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002722 if (rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302723 dspcntr |= DISPPLANE_ROTATE_180;
2724
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002725 x += (crtc_state->pipe_src_w - 1);
2726 y += (crtc_state->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302727
2728 /* Finding the last pixel of the last line of the display
2729 data and adding to linear_offset*/
2730 linear_offset +=
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002731 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
Ville Syrjäläac484962016-01-20 21:05:26 +02002732 (crtc_state->pipe_src_w - 1) * cpp;
Sonika Jindal48404c12014-08-22 14:06:04 +05302733 }
2734
Paulo Zanoni2db33662015-09-14 15:20:03 -03002735 intel_crtc->adjusted_x = x;
2736 intel_crtc->adjusted_y = y;
2737
Sonika Jindal48404c12014-08-22 14:06:04 +05302738 I915_WRITE(reg, dspcntr);
2739
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002740 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002741 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002742 I915_WRITE(DSPSURF(plane),
2743 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002744 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002745 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002746 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002747 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002748 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002749}
2750
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002751static void i9xx_disable_primary_plane(struct drm_plane *primary,
2752 struct drm_crtc *crtc)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002753{
2754 struct drm_device *dev = crtc->dev;
2755 struct drm_i915_private *dev_priv = dev->dev_private;
2756 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002757 int plane = intel_crtc->plane;
2758
2759 I915_WRITE(DSPCNTR(plane), 0);
2760 if (INTEL_INFO(dev_priv)->gen >= 4)
2761 I915_WRITE(DSPSURF(plane), 0);
2762 else
2763 I915_WRITE(DSPADDR(plane), 0);
2764 POSTING_READ(DSPCNTR(plane));
2765}
2766
2767static void ironlake_update_primary_plane(struct drm_plane *primary,
2768 const struct intel_crtc_state *crtc_state,
2769 const struct intel_plane_state *plane_state)
2770{
2771 struct drm_device *dev = primary->dev;
2772 struct drm_i915_private *dev_priv = dev->dev_private;
2773 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2774 struct drm_framebuffer *fb = plane_state->base.fb;
2775 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002776 int plane = intel_crtc->plane;
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002777 u32 linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002778 u32 dspcntr;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002779 i915_reg_t reg = DSPCNTR(plane);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002780 unsigned int rotation = plane_state->base.rotation;
Ville Syrjäläac484962016-01-20 21:05:26 +02002781 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002782 int x = plane_state->src.x1 >> 16;
2783 int y = plane_state->src.y1 >> 16;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002784
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002785 dspcntr = DISPPLANE_GAMMA_ENABLE;
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002786 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002787
2788 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2789 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
2790
Ville Syrjälä57779d02012-10-31 17:50:14 +02002791 switch (fb->pixel_format) {
2792 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002793 dspcntr |= DISPPLANE_8BPP;
2794 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002795 case DRM_FORMAT_RGB565:
2796 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002797 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002798 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002799 dspcntr |= DISPPLANE_BGRX888;
2800 break;
2801 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002802 dspcntr |= DISPPLANE_RGBX888;
2803 break;
2804 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002805 dspcntr |= DISPPLANE_BGRX101010;
2806 break;
2807 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002808 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002809 break;
2810 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002811 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002812 }
2813
2814 if (obj->tiling_mode != I915_TILING_NONE)
2815 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002816
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002817 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002818 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002819
Ville Syrjäläac484962016-01-20 21:05:26 +02002820 linear_offset = y * fb->pitches[0] + x * cpp;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002821 intel_crtc->dspaddr_offset =
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002822 intel_compute_tile_offset(&x, &y, fb, 0,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002823 fb->pitches[0], rotation);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002824 linear_offset -= intel_crtc->dspaddr_offset;
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002825 if (rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302826 dspcntr |= DISPPLANE_ROTATE_180;
2827
2828 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002829 x += (crtc_state->pipe_src_w - 1);
2830 y += (crtc_state->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302831
2832 /* Finding the last pixel of the last line of the display
2833 data and adding to linear_offset*/
2834 linear_offset +=
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002835 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
Ville Syrjäläac484962016-01-20 21:05:26 +02002836 (crtc_state->pipe_src_w - 1) * cpp;
Sonika Jindal48404c12014-08-22 14:06:04 +05302837 }
2838 }
2839
Paulo Zanoni2db33662015-09-14 15:20:03 -03002840 intel_crtc->adjusted_x = x;
2841 intel_crtc->adjusted_y = y;
2842
Sonika Jindal48404c12014-08-22 14:06:04 +05302843 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002844
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002845 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002846 I915_WRITE(DSPSURF(plane),
2847 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002848 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002849 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2850 } else {
2851 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2852 I915_WRITE(DSPLINOFF(plane), linear_offset);
2853 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002854 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002855}
2856
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002857u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
2858 uint64_t fb_modifier, uint32_t pixel_format)
Damien Lespiaub3218032015-02-27 11:15:18 +00002859{
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002860 if (fb_modifier == DRM_FORMAT_MOD_NONE) {
2861 return 64;
2862 } else {
2863 int cpp = drm_format_plane_cpp(pixel_format, 0);
Damien Lespiaub3218032015-02-27 11:15:18 +00002864
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002865 return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
Damien Lespiaub3218032015-02-27 11:15:18 +00002866 }
2867}
2868
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002869u32 intel_plane_obj_offset(struct intel_plane *intel_plane,
2870 struct drm_i915_gem_object *obj,
2871 unsigned int plane)
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002872{
Daniel Vetterce7f1722015-10-14 16:51:06 +02002873 struct i915_ggtt_view view;
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002874 struct i915_vma *vma;
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002875 u64 offset;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002876
Ville Syrjäläe7941292016-01-19 18:23:17 +02002877 intel_fill_fb_ggtt_view(&view, intel_plane->base.state->fb,
Ville Syrjälä3465c582016-02-15 22:54:43 +02002878 intel_plane->base.state->rotation);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002879
Daniel Vetterce7f1722015-10-14 16:51:06 +02002880 vma = i915_gem_obj_to_ggtt_view(obj, &view);
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002881 if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
Daniel Vetterce7f1722015-10-14 16:51:06 +02002882 view.type))
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002883 return -1;
2884
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002885 offset = vma->node.start;
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002886
2887 if (plane == 1) {
Ville Syrjälä7723f47d2016-01-20 21:05:22 +02002888 offset += vma->ggtt_view.params.rotated.uv_start_page *
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002889 PAGE_SIZE;
2890 }
2891
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002892 WARN_ON(upper_32_bits(offset));
2893
2894 return lower_32_bits(offset);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002895}
2896
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002897static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
2898{
2899 struct drm_device *dev = intel_crtc->base.dev;
2900 struct drm_i915_private *dev_priv = dev->dev_private;
2901
2902 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
2903 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
2904 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002905}
2906
Chandra Kondurua1b22782015-04-07 15:28:45 -07002907/*
2908 * This function detaches (aka. unbinds) unused scalers in hardware
2909 */
Maarten Lankhorst05832362015-06-15 12:33:48 +02002910static void skl_detach_scalers(struct intel_crtc *intel_crtc)
Chandra Kondurua1b22782015-04-07 15:28:45 -07002911{
Chandra Kondurua1b22782015-04-07 15:28:45 -07002912 struct intel_crtc_scaler_state *scaler_state;
2913 int i;
2914
Chandra Kondurua1b22782015-04-07 15:28:45 -07002915 scaler_state = &intel_crtc->config->scaler_state;
2916
2917 /* loop through and disable scalers that aren't in use */
2918 for (i = 0; i < intel_crtc->num_scalers; i++) {
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002919 if (!scaler_state->scalers[i].in_use)
2920 skl_detach_scaler(intel_crtc, i);
Chandra Kondurua1b22782015-04-07 15:28:45 -07002921 }
2922}
2923
Chandra Konduru6156a452015-04-27 13:48:39 -07002924u32 skl_plane_ctl_format(uint32_t pixel_format)
2925{
Chandra Konduru6156a452015-04-27 13:48:39 -07002926 switch (pixel_format) {
Damien Lespiaud161cf72015-05-12 16:13:17 +01002927 case DRM_FORMAT_C8:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002928 return PLANE_CTL_FORMAT_INDEXED;
Chandra Konduru6156a452015-04-27 13:48:39 -07002929 case DRM_FORMAT_RGB565:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002930 return PLANE_CTL_FORMAT_RGB_565;
Chandra Konduru6156a452015-04-27 13:48:39 -07002931 case DRM_FORMAT_XBGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002932 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
Chandra Konduru6156a452015-04-27 13:48:39 -07002933 case DRM_FORMAT_XRGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002934 return PLANE_CTL_FORMAT_XRGB_8888;
Chandra Konduru6156a452015-04-27 13:48:39 -07002935 /*
2936 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2937 * to be already pre-multiplied. We need to add a knob (or a different
2938 * DRM_FORMAT) for user-space to configure that.
2939 */
2940 case DRM_FORMAT_ABGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002941 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
Chandra Konduru6156a452015-04-27 13:48:39 -07002942 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002943 case DRM_FORMAT_ARGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002944 return PLANE_CTL_FORMAT_XRGB_8888 |
Chandra Konduru6156a452015-04-27 13:48:39 -07002945 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002946 case DRM_FORMAT_XRGB2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002947 return PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002948 case DRM_FORMAT_XBGR2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002949 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002950 case DRM_FORMAT_YUYV:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002951 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
Chandra Konduru6156a452015-04-27 13:48:39 -07002952 case DRM_FORMAT_YVYU:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002953 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
Chandra Konduru6156a452015-04-27 13:48:39 -07002954 case DRM_FORMAT_UYVY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002955 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002956 case DRM_FORMAT_VYUY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002957 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002958 default:
Damien Lespiau4249eee2015-05-12 16:13:16 +01002959 MISSING_CASE(pixel_format);
Chandra Konduru6156a452015-04-27 13:48:39 -07002960 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002961
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002962 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002963}
2964
2965u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
2966{
Chandra Konduru6156a452015-04-27 13:48:39 -07002967 switch (fb_modifier) {
2968 case DRM_FORMAT_MOD_NONE:
2969 break;
2970 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002971 return PLANE_CTL_TILED_X;
Chandra Konduru6156a452015-04-27 13:48:39 -07002972 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002973 return PLANE_CTL_TILED_Y;
Chandra Konduru6156a452015-04-27 13:48:39 -07002974 case I915_FORMAT_MOD_Yf_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002975 return PLANE_CTL_TILED_YF;
Chandra Konduru6156a452015-04-27 13:48:39 -07002976 default:
2977 MISSING_CASE(fb_modifier);
2978 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002979
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002980 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002981}
2982
2983u32 skl_plane_ctl_rotation(unsigned int rotation)
2984{
Chandra Konduru6156a452015-04-27 13:48:39 -07002985 switch (rotation) {
2986 case BIT(DRM_ROTATE_0):
2987 break;
Sonika Jindal1e8df162015-05-20 13:40:48 +05302988 /*
2989 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
2990 * while i915 HW rotation is clockwise, thats why this swapping.
2991 */
Chandra Konduru6156a452015-04-27 13:48:39 -07002992 case BIT(DRM_ROTATE_90):
Sonika Jindal1e8df162015-05-20 13:40:48 +05302993 return PLANE_CTL_ROTATE_270;
Chandra Konduru6156a452015-04-27 13:48:39 -07002994 case BIT(DRM_ROTATE_180):
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002995 return PLANE_CTL_ROTATE_180;
Chandra Konduru6156a452015-04-27 13:48:39 -07002996 case BIT(DRM_ROTATE_270):
Sonika Jindal1e8df162015-05-20 13:40:48 +05302997 return PLANE_CTL_ROTATE_90;
Chandra Konduru6156a452015-04-27 13:48:39 -07002998 default:
2999 MISSING_CASE(rotation);
3000 }
3001
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003002 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003003}
3004
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003005static void skylake_update_primary_plane(struct drm_plane *plane,
3006 const struct intel_crtc_state *crtc_state,
3007 const struct intel_plane_state *plane_state)
Damien Lespiau70d21f02013-07-03 21:06:04 +01003008{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003009 struct drm_device *dev = plane->dev;
Damien Lespiau70d21f02013-07-03 21:06:04 +01003010 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003011 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3012 struct drm_framebuffer *fb = plane_state->base.fb;
3013 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003014 int pipe = intel_crtc->pipe;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303015 u32 plane_ctl, stride_div, stride;
3016 u32 tile_height, plane_offset, plane_size;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003017 unsigned int rotation = plane_state->base.rotation;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303018 int x_offset, y_offset;
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02003019 u32 surf_addr;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003020 int scaler_id = plane_state->scaler_id;
3021 int src_x = plane_state->src.x1 >> 16;
3022 int src_y = plane_state->src.y1 >> 16;
3023 int src_w = drm_rect_width(&plane_state->src) >> 16;
3024 int src_h = drm_rect_height(&plane_state->src) >> 16;
3025 int dst_x = plane_state->dst.x1;
3026 int dst_y = plane_state->dst.y1;
3027 int dst_w = drm_rect_width(&plane_state->dst);
3028 int dst_h = drm_rect_height(&plane_state->dst);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003029
3030 plane_ctl = PLANE_CTL_ENABLE |
3031 PLANE_CTL_PIPE_GAMMA_ENABLE |
3032 PLANE_CTL_PIPE_CSC_ENABLE;
3033
Chandra Konduru6156a452015-04-27 13:48:39 -07003034 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3035 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003036 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Chandra Konduru6156a452015-04-27 13:48:39 -07003037 plane_ctl |= skl_plane_ctl_rotation(rotation);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003038
Ville Syrjälä7b49f942016-01-12 21:08:32 +02003039 stride_div = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
Damien Lespiaub3218032015-02-27 11:15:18 +00003040 fb->pixel_format);
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01003041 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj, 0);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303042
Paulo Zanonia42e5a22015-09-30 17:05:43 -03003043 WARN_ON(drm_rect_width(&plane_state->src) == 0);
Chandra Konduru6156a452015-04-27 13:48:39 -07003044
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303045 if (intel_rotation_90_or_270(rotation)) {
Ville Syrjälä832be822016-01-12 21:08:33 +02003046 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
3047
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303048 /* stride = Surface height in tiles */
Ville Syrjälä832be822016-01-12 21:08:33 +02003049 tile_height = intel_tile_height(dev_priv, fb->modifier[0], cpp);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303050 stride = DIV_ROUND_UP(fb->height, tile_height);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003051 x_offset = stride * tile_height - src_y - src_h;
3052 y_offset = src_x;
Chandra Konduru6156a452015-04-27 13:48:39 -07003053 plane_size = (src_w - 1) << 16 | (src_h - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303054 } else {
3055 stride = fb->pitches[0] / stride_div;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003056 x_offset = src_x;
3057 y_offset = src_y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003058 plane_size = (src_h - 1) << 16 | (src_w - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303059 }
3060 plane_offset = y_offset << 16 | x_offset;
Damien Lespiaub3218032015-02-27 11:15:18 +00003061
Paulo Zanoni2db33662015-09-14 15:20:03 -03003062 intel_crtc->adjusted_x = x_offset;
3063 intel_crtc->adjusted_y = y_offset;
3064
Damien Lespiau70d21f02013-07-03 21:06:04 +01003065 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303066 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3067 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3068 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
Chandra Konduru6156a452015-04-27 13:48:39 -07003069
3070 if (scaler_id >= 0) {
3071 uint32_t ps_ctrl = 0;
3072
3073 WARN_ON(!dst_w || !dst_h);
3074 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3075 crtc_state->scaler_state.scalers[scaler_id].mode;
3076 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3077 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3078 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3079 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3080 I915_WRITE(PLANE_POS(pipe, 0), 0);
3081 } else {
3082 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3083 }
3084
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003085 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003086
3087 POSTING_READ(PLANE_SURF(pipe, 0));
3088}
3089
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003090static void skylake_disable_primary_plane(struct drm_plane *primary,
3091 struct drm_crtc *crtc)
3092{
3093 struct drm_device *dev = crtc->dev;
3094 struct drm_i915_private *dev_priv = dev->dev_private;
3095 int pipe = to_intel_crtc(crtc)->pipe;
3096
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003097 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3098 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3099 POSTING_READ(PLANE_SURF(pipe, 0));
3100}
3101
Jesse Barnes17638cd2011-06-24 12:19:23 -07003102/* Assume fb object is pinned & idle & fenced and just update base pointers */
3103static int
3104intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3105 int x, int y, enum mode_set_atomic state)
3106{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003107 /* Support for kgdboc is disabled, this needs a major rework. */
3108 DRM_ERROR("legacy panic handler not supported any more.\n");
Jesse Barnes17638cd2011-06-24 12:19:23 -07003109
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003110 return -ENODEV;
Jesse Barnes81255562010-08-02 12:07:50 -07003111}
3112
Ville Syrjälä75147472014-11-24 18:28:11 +02003113static void intel_complete_page_flips(struct drm_device *dev)
Ville Syrjälä96a02912013-02-18 19:08:49 +02003114{
Ville Syrjälä96a02912013-02-18 19:08:49 +02003115 struct drm_crtc *crtc;
3116
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003117 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003118 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3119 enum plane plane = intel_crtc->plane;
3120
3121 intel_prepare_page_flip(dev, plane);
3122 intel_finish_page_flip_plane(dev, plane);
3123 }
Ville Syrjälä75147472014-11-24 18:28:11 +02003124}
3125
3126static void intel_update_primary_planes(struct drm_device *dev)
3127{
Ville Syrjälä75147472014-11-24 18:28:11 +02003128 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003129
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003130 for_each_crtc(dev, crtc) {
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003131 struct intel_plane *plane = to_intel_plane(crtc->primary);
3132 struct intel_plane_state *plane_state;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003133
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003134 drm_modeset_lock_crtc(crtc, &plane->base);
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003135 plane_state = to_intel_plane_state(plane->base.state);
3136
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003137 if (plane_state->visible)
3138 plane->update_plane(&plane->base,
3139 to_intel_crtc_state(crtc->state),
3140 plane_state);
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003141
3142 drm_modeset_unlock_crtc(crtc);
Ville Syrjälä96a02912013-02-18 19:08:49 +02003143 }
3144}
3145
Ville Syrjälä75147472014-11-24 18:28:11 +02003146void intel_prepare_reset(struct drm_device *dev)
3147{
3148 /* no reset support for gen2 */
3149 if (IS_GEN2(dev))
3150 return;
3151
3152 /* reset doesn't touch the display */
3153 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3154 return;
3155
3156 drm_modeset_lock_all(dev);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003157 /*
3158 * Disabling the crtcs gracefully seems nicer. Also the
3159 * g33 docs say we should at least disable all the planes.
3160 */
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02003161 intel_display_suspend(dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003162}
3163
3164void intel_finish_reset(struct drm_device *dev)
3165{
3166 struct drm_i915_private *dev_priv = to_i915(dev);
3167
3168 /*
3169 * Flips in the rings will be nuked by the reset,
3170 * so complete all pending flips so that user space
3171 * will get its events and not get stuck.
3172 */
3173 intel_complete_page_flips(dev);
3174
3175 /* no reset support for gen2 */
3176 if (IS_GEN2(dev))
3177 return;
3178
3179 /* reset doesn't touch the display */
3180 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3181 /*
3182 * Flips in the rings have been nuked by the reset,
3183 * so update the base address of all primary
3184 * planes to the the last fb to make sure we're
3185 * showing the correct fb after a reset.
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003186 *
3187 * FIXME: Atomic will make this obsolete since we won't schedule
3188 * CS-based flips (which might get lost in gpu resets) any more.
Ville Syrjälä75147472014-11-24 18:28:11 +02003189 */
3190 intel_update_primary_planes(dev);
3191 return;
3192 }
3193
3194 /*
3195 * The display has been reset as well,
3196 * so need a full re-initialization.
3197 */
3198 intel_runtime_pm_disable_interrupts(dev_priv);
3199 intel_runtime_pm_enable_interrupts(dev_priv);
3200
3201 intel_modeset_init_hw(dev);
3202
3203 spin_lock_irq(&dev_priv->irq_lock);
3204 if (dev_priv->display.hpd_irq_setup)
3205 dev_priv->display.hpd_irq_setup(dev);
3206 spin_unlock_irq(&dev_priv->irq_lock);
3207
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +02003208 intel_display_resume(dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003209
3210 intel_hpd_init(dev_priv);
3211
3212 drm_modeset_unlock_all(dev);
3213}
3214
Chris Wilson7d5e3792014-03-04 13:15:08 +00003215static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3216{
3217 struct drm_device *dev = crtc->dev;
Chris Wilson7d5e3792014-03-04 13:15:08 +00003218 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilsonc19ae982016-04-13 17:35:03 +01003219 unsigned reset_counter;
Chris Wilson7d5e3792014-03-04 13:15:08 +00003220 bool pending;
3221
Chris Wilson7f1847e2016-04-13 17:35:04 +01003222 reset_counter = i915_reset_counter(&to_i915(dev)->gpu_error);
3223 if (intel_crtc->reset_counter != reset_counter)
Chris Wilson7d5e3792014-03-04 13:15:08 +00003224 return false;
3225
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003226 spin_lock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003227 pending = to_intel_crtc(crtc)->unpin_work != NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003228 spin_unlock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003229
3230 return pending;
3231}
3232
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003233static void intel_update_pipe_config(struct intel_crtc *crtc,
3234 struct intel_crtc_state *old_crtc_state)
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003235{
3236 struct drm_device *dev = crtc->base.dev;
3237 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003238 struct intel_crtc_state *pipe_config =
3239 to_intel_crtc_state(crtc->base.state);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003240
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003241 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
3242 crtc->base.mode = crtc->base.state->mode;
3243
3244 DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
3245 old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
3246 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003247
3248 /*
3249 * Update pipe size and adjust fitter if needed: the reason for this is
3250 * that in compute_mode_changes we check the native mode (not the pfit
3251 * mode) to see if we can flip rather than do a full mode set. In the
3252 * fastboot case, we'll flip, but if we don't update the pipesrc and
3253 * pfit state, we'll end up with a big fb scanned out into the wrong
3254 * sized surface.
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003255 */
3256
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003257 I915_WRITE(PIPESRC(crtc->pipe),
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003258 ((pipe_config->pipe_src_w - 1) << 16) |
3259 (pipe_config->pipe_src_h - 1));
3260
3261 /* on skylake this is done by detaching scalers */
3262 if (INTEL_INFO(dev)->gen >= 9) {
3263 skl_detach_scalers(crtc);
3264
3265 if (pipe_config->pch_pfit.enabled)
3266 skylake_pfit_enable(crtc);
3267 } else if (HAS_PCH_SPLIT(dev)) {
3268 if (pipe_config->pch_pfit.enabled)
3269 ironlake_pfit_enable(crtc);
3270 else if (old_crtc_state->pch_pfit.enabled)
3271 ironlake_pfit_disable(crtc, true);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003272 }
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003273}
3274
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003275static void intel_fdi_normal_train(struct drm_crtc *crtc)
3276{
3277 struct drm_device *dev = crtc->dev;
3278 struct drm_i915_private *dev_priv = dev->dev_private;
3279 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3280 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003281 i915_reg_t reg;
3282 u32 temp;
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003283
3284 /* enable normal train */
3285 reg = FDI_TX_CTL(pipe);
3286 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07003287 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003288 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3289 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003290 } else {
3291 temp &= ~FDI_LINK_TRAIN_NONE;
3292 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003293 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003294 I915_WRITE(reg, temp);
3295
3296 reg = FDI_RX_CTL(pipe);
3297 temp = I915_READ(reg);
3298 if (HAS_PCH_CPT(dev)) {
3299 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3300 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3301 } else {
3302 temp &= ~FDI_LINK_TRAIN_NONE;
3303 temp |= FDI_LINK_TRAIN_NONE;
3304 }
3305 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3306
3307 /* wait one idle pattern time */
3308 POSTING_READ(reg);
3309 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003310
3311 /* IVB wants error correction enabled */
3312 if (IS_IVYBRIDGE(dev))
3313 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3314 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003315}
3316
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003317/* The FDI link training functions for ILK/Ibexpeak. */
3318static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3319{
3320 struct drm_device *dev = crtc->dev;
3321 struct drm_i915_private *dev_priv = dev->dev_private;
3322 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3323 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003324 i915_reg_t reg;
3325 u32 temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003326
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003327 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003328 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003329
Adam Jacksone1a44742010-06-25 15:32:14 -04003330 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3331 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003332 reg = FDI_RX_IMR(pipe);
3333 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003334 temp &= ~FDI_RX_SYMBOL_LOCK;
3335 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003336 I915_WRITE(reg, temp);
3337 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003338 udelay(150);
3339
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003340 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003341 reg = FDI_TX_CTL(pipe);
3342 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003343 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003344 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003345 temp &= ~FDI_LINK_TRAIN_NONE;
3346 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003347 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003348
Chris Wilson5eddb702010-09-11 13:48:45 +01003349 reg = FDI_RX_CTL(pipe);
3350 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003351 temp &= ~FDI_LINK_TRAIN_NONE;
3352 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003353 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3354
3355 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003356 udelay(150);
3357
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003358 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003359 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3360 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3361 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003362
Chris Wilson5eddb702010-09-11 13:48:45 +01003363 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003364 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003365 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003366 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3367
3368 if ((temp & FDI_RX_BIT_LOCK)) {
3369 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003370 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003371 break;
3372 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003373 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003374 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003375 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003376
3377 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003378 reg = FDI_TX_CTL(pipe);
3379 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003380 temp &= ~FDI_LINK_TRAIN_NONE;
3381 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003382 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003383
Chris Wilson5eddb702010-09-11 13:48:45 +01003384 reg = FDI_RX_CTL(pipe);
3385 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003386 temp &= ~FDI_LINK_TRAIN_NONE;
3387 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003388 I915_WRITE(reg, temp);
3389
3390 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003391 udelay(150);
3392
Chris Wilson5eddb702010-09-11 13:48:45 +01003393 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003394 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003395 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003396 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3397
3398 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003399 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003400 DRM_DEBUG_KMS("FDI train 2 done.\n");
3401 break;
3402 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003403 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003404 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003405 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003406
3407 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003408
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003409}
3410
Akshay Joshi0206e352011-08-16 15:34:10 -04003411static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003412 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3413 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3414 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3415 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3416};
3417
3418/* The FDI link training functions for SNB/Cougarpoint. */
3419static void gen6_fdi_link_train(struct drm_crtc *crtc)
3420{
3421 struct drm_device *dev = crtc->dev;
3422 struct drm_i915_private *dev_priv = dev->dev_private;
3423 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3424 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003425 i915_reg_t reg;
3426 u32 temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003427
Adam Jacksone1a44742010-06-25 15:32:14 -04003428 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3429 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003430 reg = FDI_RX_IMR(pipe);
3431 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003432 temp &= ~FDI_RX_SYMBOL_LOCK;
3433 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003434 I915_WRITE(reg, temp);
3435
3436 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003437 udelay(150);
3438
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003439 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003440 reg = FDI_TX_CTL(pipe);
3441 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003442 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003443 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003444 temp &= ~FDI_LINK_TRAIN_NONE;
3445 temp |= FDI_LINK_TRAIN_PATTERN_1;
3446 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3447 /* SNB-B */
3448 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003449 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003450
Daniel Vetterd74cf322012-10-26 10:58:13 +02003451 I915_WRITE(FDI_RX_MISC(pipe),
3452 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3453
Chris Wilson5eddb702010-09-11 13:48:45 +01003454 reg = FDI_RX_CTL(pipe);
3455 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003456 if (HAS_PCH_CPT(dev)) {
3457 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3458 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3459 } else {
3460 temp &= ~FDI_LINK_TRAIN_NONE;
3461 temp |= FDI_LINK_TRAIN_PATTERN_1;
3462 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003463 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3464
3465 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003466 udelay(150);
3467
Akshay Joshi0206e352011-08-16 15:34:10 -04003468 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003469 reg = FDI_TX_CTL(pipe);
3470 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003471 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3472 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003473 I915_WRITE(reg, temp);
3474
3475 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003476 udelay(500);
3477
Sean Paulfa37d392012-03-02 12:53:39 -05003478 for (retry = 0; retry < 5; retry++) {
3479 reg = FDI_RX_IIR(pipe);
3480 temp = I915_READ(reg);
3481 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3482 if (temp & FDI_RX_BIT_LOCK) {
3483 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3484 DRM_DEBUG_KMS("FDI train 1 done.\n");
3485 break;
3486 }
3487 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003488 }
Sean Paulfa37d392012-03-02 12:53:39 -05003489 if (retry < 5)
3490 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003491 }
3492 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003493 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003494
3495 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003496 reg = FDI_TX_CTL(pipe);
3497 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003498 temp &= ~FDI_LINK_TRAIN_NONE;
3499 temp |= FDI_LINK_TRAIN_PATTERN_2;
3500 if (IS_GEN6(dev)) {
3501 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3502 /* SNB-B */
3503 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3504 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003505 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003506
Chris Wilson5eddb702010-09-11 13:48:45 +01003507 reg = FDI_RX_CTL(pipe);
3508 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003509 if (HAS_PCH_CPT(dev)) {
3510 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3511 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3512 } else {
3513 temp &= ~FDI_LINK_TRAIN_NONE;
3514 temp |= FDI_LINK_TRAIN_PATTERN_2;
3515 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003516 I915_WRITE(reg, temp);
3517
3518 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003519 udelay(150);
3520
Akshay Joshi0206e352011-08-16 15:34:10 -04003521 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003522 reg = FDI_TX_CTL(pipe);
3523 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003524 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3525 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003526 I915_WRITE(reg, temp);
3527
3528 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003529 udelay(500);
3530
Sean Paulfa37d392012-03-02 12:53:39 -05003531 for (retry = 0; retry < 5; retry++) {
3532 reg = FDI_RX_IIR(pipe);
3533 temp = I915_READ(reg);
3534 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3535 if (temp & FDI_RX_SYMBOL_LOCK) {
3536 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3537 DRM_DEBUG_KMS("FDI train 2 done.\n");
3538 break;
3539 }
3540 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003541 }
Sean Paulfa37d392012-03-02 12:53:39 -05003542 if (retry < 5)
3543 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003544 }
3545 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003546 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003547
3548 DRM_DEBUG_KMS("FDI train done.\n");
3549}
3550
Jesse Barnes357555c2011-04-28 15:09:55 -07003551/* Manual link training for Ivy Bridge A0 parts */
3552static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3553{
3554 struct drm_device *dev = crtc->dev;
3555 struct drm_i915_private *dev_priv = dev->dev_private;
3556 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3557 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003558 i915_reg_t reg;
3559 u32 temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003560
3561 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3562 for train result */
3563 reg = FDI_RX_IMR(pipe);
3564 temp = I915_READ(reg);
3565 temp &= ~FDI_RX_SYMBOL_LOCK;
3566 temp &= ~FDI_RX_BIT_LOCK;
3567 I915_WRITE(reg, temp);
3568
3569 POSTING_READ(reg);
3570 udelay(150);
3571
Daniel Vetter01a415f2012-10-27 15:58:40 +02003572 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3573 I915_READ(FDI_RX_IIR(pipe)));
3574
Jesse Barnes139ccd32013-08-19 11:04:55 -07003575 /* Try each vswing and preemphasis setting twice before moving on */
3576 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3577 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003578 reg = FDI_TX_CTL(pipe);
3579 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003580 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3581 temp &= ~FDI_TX_ENABLE;
3582 I915_WRITE(reg, temp);
3583
3584 reg = FDI_RX_CTL(pipe);
3585 temp = I915_READ(reg);
3586 temp &= ~FDI_LINK_TRAIN_AUTO;
3587 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3588 temp &= ~FDI_RX_ENABLE;
3589 I915_WRITE(reg, temp);
3590
3591 /* enable CPU FDI TX and PCH FDI RX */
3592 reg = FDI_TX_CTL(pipe);
3593 temp = I915_READ(reg);
3594 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003595 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003596 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003597 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003598 temp |= snb_b_fdi_train_param[j/2];
3599 temp |= FDI_COMPOSITE_SYNC;
3600 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3601
3602 I915_WRITE(FDI_RX_MISC(pipe),
3603 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3604
3605 reg = FDI_RX_CTL(pipe);
3606 temp = I915_READ(reg);
3607 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3608 temp |= FDI_COMPOSITE_SYNC;
3609 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3610
3611 POSTING_READ(reg);
3612 udelay(1); /* should be 0.5us */
3613
3614 for (i = 0; i < 4; i++) {
3615 reg = FDI_RX_IIR(pipe);
3616 temp = I915_READ(reg);
3617 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3618
3619 if (temp & FDI_RX_BIT_LOCK ||
3620 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3621 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3622 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3623 i);
3624 break;
3625 }
3626 udelay(1); /* should be 0.5us */
3627 }
3628 if (i == 4) {
3629 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3630 continue;
3631 }
3632
3633 /* Train 2 */
3634 reg = FDI_TX_CTL(pipe);
3635 temp = I915_READ(reg);
3636 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3637 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3638 I915_WRITE(reg, temp);
3639
3640 reg = FDI_RX_CTL(pipe);
3641 temp = I915_READ(reg);
3642 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3643 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003644 I915_WRITE(reg, temp);
3645
3646 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003647 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003648
Jesse Barnes139ccd32013-08-19 11:04:55 -07003649 for (i = 0; i < 4; i++) {
3650 reg = FDI_RX_IIR(pipe);
3651 temp = I915_READ(reg);
3652 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003653
Jesse Barnes139ccd32013-08-19 11:04:55 -07003654 if (temp & FDI_RX_SYMBOL_LOCK ||
3655 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3656 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3657 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3658 i);
3659 goto train_done;
3660 }
3661 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003662 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003663 if (i == 4)
3664 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003665 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003666
Jesse Barnes139ccd32013-08-19 11:04:55 -07003667train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003668 DRM_DEBUG_KMS("FDI train done.\n");
3669}
3670
Daniel Vetter88cefb62012-08-12 19:27:14 +02003671static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003672{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003673 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003674 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003675 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003676 i915_reg_t reg;
3677 u32 temp;
Jesse Barnesc64e3112010-09-10 11:27:03 -07003678
Jesse Barnes0e23b992010-09-10 11:10:00 -07003679 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003680 reg = FDI_RX_CTL(pipe);
3681 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003682 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003683 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003684 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003685 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3686
3687 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003688 udelay(200);
3689
3690 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003691 temp = I915_READ(reg);
3692 I915_WRITE(reg, temp | FDI_PCDCLK);
3693
3694 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003695 udelay(200);
3696
Paulo Zanoni20749732012-11-23 15:30:38 -02003697 /* Enable CPU FDI TX PLL, always on for Ironlake */
3698 reg = FDI_TX_CTL(pipe);
3699 temp = I915_READ(reg);
3700 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3701 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003702
Paulo Zanoni20749732012-11-23 15:30:38 -02003703 POSTING_READ(reg);
3704 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003705 }
3706}
3707
Daniel Vetter88cefb62012-08-12 19:27:14 +02003708static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3709{
3710 struct drm_device *dev = intel_crtc->base.dev;
3711 struct drm_i915_private *dev_priv = dev->dev_private;
3712 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003713 i915_reg_t reg;
3714 u32 temp;
Daniel Vetter88cefb62012-08-12 19:27:14 +02003715
3716 /* Switch from PCDclk to Rawclk */
3717 reg = FDI_RX_CTL(pipe);
3718 temp = I915_READ(reg);
3719 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3720
3721 /* Disable CPU FDI TX PLL */
3722 reg = FDI_TX_CTL(pipe);
3723 temp = I915_READ(reg);
3724 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3725
3726 POSTING_READ(reg);
3727 udelay(100);
3728
3729 reg = FDI_RX_CTL(pipe);
3730 temp = I915_READ(reg);
3731 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3732
3733 /* Wait for the clocks to turn off. */
3734 POSTING_READ(reg);
3735 udelay(100);
3736}
3737
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003738static void ironlake_fdi_disable(struct drm_crtc *crtc)
3739{
3740 struct drm_device *dev = crtc->dev;
3741 struct drm_i915_private *dev_priv = dev->dev_private;
3742 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3743 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003744 i915_reg_t reg;
3745 u32 temp;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003746
3747 /* disable CPU FDI tx and PCH FDI rx */
3748 reg = FDI_TX_CTL(pipe);
3749 temp = I915_READ(reg);
3750 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3751 POSTING_READ(reg);
3752
3753 reg = FDI_RX_CTL(pipe);
3754 temp = I915_READ(reg);
3755 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003756 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003757 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3758
3759 POSTING_READ(reg);
3760 udelay(100);
3761
3762 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003763 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003764 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003765
3766 /* still set train pattern 1 */
3767 reg = FDI_TX_CTL(pipe);
3768 temp = I915_READ(reg);
3769 temp &= ~FDI_LINK_TRAIN_NONE;
3770 temp |= FDI_LINK_TRAIN_PATTERN_1;
3771 I915_WRITE(reg, temp);
3772
3773 reg = FDI_RX_CTL(pipe);
3774 temp = I915_READ(reg);
3775 if (HAS_PCH_CPT(dev)) {
3776 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3777 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3778 } else {
3779 temp &= ~FDI_LINK_TRAIN_NONE;
3780 temp |= FDI_LINK_TRAIN_PATTERN_1;
3781 }
3782 /* BPC in FDI rx is consistent with that in PIPECONF */
3783 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003784 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003785 I915_WRITE(reg, temp);
3786
3787 POSTING_READ(reg);
3788 udelay(100);
3789}
3790
Chris Wilson5dce5b932014-01-20 10:17:36 +00003791bool intel_has_pending_fb_unpin(struct drm_device *dev)
3792{
3793 struct intel_crtc *crtc;
3794
3795 /* Note that we don't need to be called with mode_config.lock here
3796 * as our list of CRTC objects is static for the lifetime of the
3797 * device and so cannot disappear as we iterate. Similarly, we can
3798 * happily treat the predicates as racy, atomic checks as userspace
3799 * cannot claim and pin a new fb without at least acquring the
3800 * struct_mutex and so serialising with us.
3801 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003802 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003803 if (atomic_read(&crtc->unpin_work_count) == 0)
3804 continue;
3805
3806 if (crtc->unpin_work)
3807 intel_wait_for_vblank(dev, crtc->pipe);
3808
3809 return true;
3810 }
3811
3812 return false;
3813}
3814
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003815static void page_flip_completed(struct intel_crtc *intel_crtc)
3816{
3817 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3818 struct intel_unpin_work *work = intel_crtc->unpin_work;
3819
3820 /* ensure that the unpin work is consistent wrt ->pending. */
3821 smp_rmb();
3822 intel_crtc->unpin_work = NULL;
3823
3824 if (work->event)
Gustavo Padovan560ce1d2016-04-14 10:48:15 -07003825 drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003826
3827 drm_crtc_vblank_put(&intel_crtc->base);
3828
3829 wake_up_all(&dev_priv->pending_flip_queue);
3830 queue_work(dev_priv->wq, &work->work);
3831
3832 trace_i915_flip_complete(intel_crtc->plane,
3833 work->pending_flip_obj);
3834}
3835
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003836static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003837{
Chris Wilson0f911282012-04-17 10:05:38 +01003838 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003839 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003840 long ret;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003841
Daniel Vetter2c10d572012-12-20 21:24:07 +01003842 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003843
3844 ret = wait_event_interruptible_timeout(
3845 dev_priv->pending_flip_queue,
3846 !intel_crtc_has_pending_flip(crtc),
3847 60*HZ);
3848
3849 if (ret < 0)
3850 return ret;
3851
3852 if (ret == 0) {
Chris Wilson9c787942014-09-05 07:13:25 +01003853 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter2c10d572012-12-20 21:24:07 +01003854
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003855 spin_lock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003856 if (intel_crtc->unpin_work) {
3857 WARN_ONCE(1, "Removing stuck page flip\n");
3858 page_flip_completed(intel_crtc);
3859 }
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003860 spin_unlock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003861 }
Chris Wilson5bb61642012-09-27 21:25:58 +01003862
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003863 return 0;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003864}
3865
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003866static void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
3867{
3868 u32 temp;
3869
3870 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3871
3872 mutex_lock(&dev_priv->sb_lock);
3873
3874 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3875 temp |= SBI_SSCCTL_DISABLE;
3876 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
3877
3878 mutex_unlock(&dev_priv->sb_lock);
3879}
3880
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003881/* Program iCLKIP clock to the desired frequency */
3882static void lpt_program_iclkip(struct drm_crtc *crtc)
3883{
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003884 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003885 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003886 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3887 u32 temp;
3888
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003889 lpt_disable_iclkip(dev_priv);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003890
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003891 /* The iCLK virtual clock root frequency is in MHz,
3892 * but the adjusted_mode->crtc_clock in in KHz. To get the
3893 * divisors, it is necessary to divide one by another, so we
3894 * convert the virtual clock precision to KHz here for higher
3895 * precision.
3896 */
3897 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003898 u32 iclk_virtual_root_freq = 172800 * 1000;
3899 u32 iclk_pi_range = 64;
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003900 u32 desired_divisor;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003901
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003902 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
3903 clock << auxdiv);
3904 divsel = (desired_divisor / iclk_pi_range) - 2;
3905 phaseinc = desired_divisor % iclk_pi_range;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003906
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003907 /*
3908 * Near 20MHz is a corner case which is
3909 * out of range for the 7-bit divisor
3910 */
3911 if (divsel <= 0x7f)
3912 break;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003913 }
3914
3915 /* This should not happen with any sane values */
3916 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3917 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3918 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3919 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3920
3921 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 +03003922 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003923 auxdiv,
3924 divsel,
3925 phasedir,
3926 phaseinc);
3927
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003928 mutex_lock(&dev_priv->sb_lock);
3929
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003930 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003931 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003932 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3933 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3934 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3935 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3936 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3937 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003938 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003939
3940 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003941 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003942 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3943 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003944 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003945
3946 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003947 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003948 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003949 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003950
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003951 mutex_unlock(&dev_priv->sb_lock);
3952
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003953 /* Wait for initialization time */
3954 udelay(24);
3955
3956 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
3957}
3958
Ville Syrjälä8802e5b2016-02-17 21:41:12 +02003959int lpt_get_iclkip(struct drm_i915_private *dev_priv)
3960{
3961 u32 divsel, phaseinc, auxdiv;
3962 u32 iclk_virtual_root_freq = 172800 * 1000;
3963 u32 iclk_pi_range = 64;
3964 u32 desired_divisor;
3965 u32 temp;
3966
3967 if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
3968 return 0;
3969
3970 mutex_lock(&dev_priv->sb_lock);
3971
3972 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3973 if (temp & SBI_SSCCTL_DISABLE) {
3974 mutex_unlock(&dev_priv->sb_lock);
3975 return 0;
3976 }
3977
3978 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
3979 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
3980 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
3981 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
3982 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
3983
3984 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
3985 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
3986 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
3987
3988 mutex_unlock(&dev_priv->sb_lock);
3989
3990 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
3991
3992 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
3993 desired_divisor << auxdiv);
3994}
3995
Daniel Vetter275f01b22013-05-03 11:49:47 +02003996static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3997 enum pipe pch_transcoder)
3998{
3999 struct drm_device *dev = crtc->base.dev;
4000 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004001 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02004002
4003 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4004 I915_READ(HTOTAL(cpu_transcoder)));
4005 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4006 I915_READ(HBLANK(cpu_transcoder)));
4007 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4008 I915_READ(HSYNC(cpu_transcoder)));
4009
4010 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4011 I915_READ(VTOTAL(cpu_transcoder)));
4012 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4013 I915_READ(VBLANK(cpu_transcoder)));
4014 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4015 I915_READ(VSYNC(cpu_transcoder)));
4016 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4017 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4018}
4019
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004020static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004021{
4022 struct drm_i915_private *dev_priv = dev->dev_private;
4023 uint32_t temp;
4024
4025 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004026 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004027 return;
4028
4029 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4030 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4031
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004032 temp &= ~FDI_BC_BIFURCATION_SELECT;
4033 if (enable)
4034 temp |= FDI_BC_BIFURCATION_SELECT;
4035
4036 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004037 I915_WRITE(SOUTH_CHICKEN1, temp);
4038 POSTING_READ(SOUTH_CHICKEN1);
4039}
4040
4041static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4042{
4043 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004044
4045 switch (intel_crtc->pipe) {
4046 case PIPE_A:
4047 break;
4048 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004049 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004050 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004051 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004052 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004053
4054 break;
4055 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004056 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004057
4058 break;
4059 default:
4060 BUG();
4061 }
4062}
4063
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004064/* Return which DP Port should be selected for Transcoder DP control */
4065static enum port
4066intel_trans_dp_port_sel(struct drm_crtc *crtc)
4067{
4068 struct drm_device *dev = crtc->dev;
4069 struct intel_encoder *encoder;
4070
4071 for_each_encoder_on_crtc(dev, crtc, encoder) {
4072 if (encoder->type == INTEL_OUTPUT_DISPLAYPORT ||
4073 encoder->type == INTEL_OUTPUT_EDP)
4074 return enc_to_dig_port(&encoder->base)->port;
4075 }
4076
4077 return -1;
4078}
4079
Jesse Barnesf67a5592011-01-05 10:31:48 -08004080/*
4081 * Enable PCH resources required for PCH ports:
4082 * - PCH PLLs
4083 * - FDI training & RX/TX
4084 * - update transcoder timings
4085 * - DP transcoding bits
4086 * - transcoder
4087 */
4088static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08004089{
4090 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004091 struct drm_i915_private *dev_priv = dev->dev_private;
4092 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4093 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004094 u32 temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004095
Daniel Vetterab9412b2013-05-03 11:49:46 +02004096 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004097
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004098 if (IS_IVYBRIDGE(dev))
4099 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4100
Daniel Vettercd986ab2012-10-26 10:58:12 +02004101 /* Write the TU size bits before fdi link training, so that error
4102 * detection works. */
4103 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4104 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4105
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004106 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07004107 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004108
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004109 /* We need to program the right clock selection before writing the pixel
4110 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004111 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004112 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004113
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004114 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004115 temp |= TRANS_DPLL_ENABLE(pipe);
4116 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02004117 if (intel_crtc->config->shared_dpll ==
4118 intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004119 temp |= sel;
4120 else
4121 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004122 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004123 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004124
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004125 /* XXX: pch pll's can be enabled any time before we enable the PCH
4126 * transcoder, and we actually should do this to not upset any PCH
4127 * transcoder that already use the clock when we share it.
4128 *
4129 * Note that enable_shared_dpll tries to do the right thing, but
4130 * get_shared_dpll unconditionally resets the pll - we need that to have
4131 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02004132 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004133
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004134 /* set transcoder timing, panel must allow it */
4135 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02004136 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004137
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004138 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004139
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004140 /* For PCH DP, enable TRANS_DP_CTL */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004141 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004142 const struct drm_display_mode *adjusted_mode =
4143 &intel_crtc->config->base.adjusted_mode;
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004144 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004145 i915_reg_t reg = TRANS_DP_CTL(pipe);
Chris Wilson5eddb702010-09-11 13:48:45 +01004146 temp = I915_READ(reg);
4147 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004148 TRANS_DP_SYNC_MASK |
4149 TRANS_DP_BPC_MASK);
Ville Syrjäläe3ef4472015-05-05 17:17:31 +03004150 temp |= TRANS_DP_OUTPUT_ENABLE;
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004151 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004152
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004153 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004154 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004155 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004156 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004157
4158 switch (intel_trans_dp_port_sel(crtc)) {
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004159 case PORT_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004160 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004161 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004162 case PORT_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004163 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004164 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004165 case PORT_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004166 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004167 break;
4168 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004169 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004170 }
4171
Chris Wilson5eddb702010-09-11 13:48:45 +01004172 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004173 }
4174
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004175 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004176}
4177
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004178static void lpt_pch_enable(struct drm_crtc *crtc)
4179{
4180 struct drm_device *dev = crtc->dev;
4181 struct drm_i915_private *dev_priv = dev->dev_private;
4182 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004183 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004184
Daniel Vetterab9412b2013-05-03 11:49:46 +02004185 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004186
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004187 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004188
Paulo Zanoni0540e482012-10-31 18:12:40 -02004189 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02004190 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004191
Paulo Zanoni937bb612012-10-31 18:12:47 -02004192 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004193}
4194
Daniel Vettera1520312013-05-03 11:49:50 +02004195static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004196{
4197 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004198 i915_reg_t dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004199 u32 temp;
4200
4201 temp = I915_READ(dslreg);
4202 udelay(500);
4203 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004204 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004205 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004206 }
4207}
4208
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004209static int
4210skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4211 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4212 int src_w, int src_h, int dst_w, int dst_h)
Chandra Kondurua1b22782015-04-07 15:28:45 -07004213{
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004214 struct intel_crtc_scaler_state *scaler_state =
4215 &crtc_state->scaler_state;
4216 struct intel_crtc *intel_crtc =
4217 to_intel_crtc(crtc_state->base.crtc);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004218 int need_scaling;
Chandra Konduru6156a452015-04-27 13:48:39 -07004219
4220 need_scaling = intel_rotation_90_or_270(rotation) ?
4221 (src_h != dst_w || src_w != dst_h):
4222 (src_w != dst_w || src_h != dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004223
4224 /*
4225 * if plane is being disabled or scaler is no more required or force detach
4226 * - free scaler binded to this plane/crtc
4227 * - in order to do this, update crtc->scaler_usage
4228 *
4229 * Here scaler state in crtc_state is set free so that
4230 * scaler can be assigned to other user. Actual register
4231 * update to free the scaler is done in plane/panel-fit programming.
4232 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4233 */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004234 if (force_detach || !need_scaling) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004235 if (*scaler_id >= 0) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004236 scaler_state->scaler_users &= ~(1 << scaler_user);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004237 scaler_state->scalers[*scaler_id].in_use = 0;
4238
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004239 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4240 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4241 intel_crtc->pipe, scaler_user, *scaler_id,
Chandra Kondurua1b22782015-04-07 15:28:45 -07004242 scaler_state->scaler_users);
4243 *scaler_id = -1;
4244 }
4245 return 0;
4246 }
4247
4248 /* range checks */
4249 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4250 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4251
4252 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4253 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004254 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
Chandra Kondurua1b22782015-04-07 15:28:45 -07004255 "size is out of scaler range\n",
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004256 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004257 return -EINVAL;
4258 }
4259
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004260 /* mark this plane as a scaler user in crtc_state */
4261 scaler_state->scaler_users |= (1 << scaler_user);
4262 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4263 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4264 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4265 scaler_state->scaler_users);
4266
4267 return 0;
4268}
4269
4270/**
4271 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4272 *
4273 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004274 *
4275 * Return
4276 * 0 - scaler_usage updated successfully
4277 * error - requested scaling cannot be supported or other error condition
4278 */
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004279int skl_update_scaler_crtc(struct intel_crtc_state *state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004280{
4281 struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03004282 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004283
4284 DRM_DEBUG_KMS("Updating scaler for [CRTC:%i] scaler_user index %u.%u\n",
4285 intel_crtc->base.base.id, intel_crtc->pipe, SKL_CRTC_INDEX);
4286
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004287 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
Ville Syrjäläfa5a7972015-10-15 17:01:58 +03004288 &state->scaler_state.scaler_id, BIT(DRM_ROTATE_0),
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004289 state->pipe_src_w, state->pipe_src_h,
Ville Syrjäläaad941d2015-09-25 16:38:56 +03004290 adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004291}
4292
4293/**
4294 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4295 *
4296 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004297 * @plane_state: atomic plane state to update
4298 *
4299 * Return
4300 * 0 - scaler_usage updated successfully
4301 * error - requested scaling cannot be supported or other error condition
4302 */
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004303static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4304 struct intel_plane_state *plane_state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004305{
4306
4307 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004308 struct intel_plane *intel_plane =
4309 to_intel_plane(plane_state->base.plane);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004310 struct drm_framebuffer *fb = plane_state->base.fb;
4311 int ret;
4312
4313 bool force_detach = !fb || !plane_state->visible;
4314
4315 DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
4316 intel_plane->base.base.id, intel_crtc->pipe,
4317 drm_plane_index(&intel_plane->base));
4318
4319 ret = skl_update_scaler(crtc_state, force_detach,
4320 drm_plane_index(&intel_plane->base),
4321 &plane_state->scaler_id,
4322 plane_state->base.rotation,
4323 drm_rect_width(&plane_state->src) >> 16,
4324 drm_rect_height(&plane_state->src) >> 16,
4325 drm_rect_width(&plane_state->dst),
4326 drm_rect_height(&plane_state->dst));
4327
4328 if (ret || plane_state->scaler_id < 0)
4329 return ret;
4330
Chandra Kondurua1b22782015-04-07 15:28:45 -07004331 /* check colorkey */
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004332 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004333 DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004334 intel_plane->base.base.id);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004335 return -EINVAL;
4336 }
4337
4338 /* Check src format */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004339 switch (fb->pixel_format) {
4340 case DRM_FORMAT_RGB565:
4341 case DRM_FORMAT_XBGR8888:
4342 case DRM_FORMAT_XRGB8888:
4343 case DRM_FORMAT_ABGR8888:
4344 case DRM_FORMAT_ARGB8888:
4345 case DRM_FORMAT_XRGB2101010:
4346 case DRM_FORMAT_XBGR2101010:
4347 case DRM_FORMAT_YUYV:
4348 case DRM_FORMAT_YVYU:
4349 case DRM_FORMAT_UYVY:
4350 case DRM_FORMAT_VYUY:
4351 break;
4352 default:
4353 DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
4354 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4355 return -EINVAL;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004356 }
4357
Chandra Kondurua1b22782015-04-07 15:28:45 -07004358 return 0;
4359}
4360
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004361static void skylake_scaler_disable(struct intel_crtc *crtc)
4362{
4363 int i;
4364
4365 for (i = 0; i < crtc->num_scalers; i++)
4366 skl_detach_scaler(crtc, i);
4367}
4368
4369static void skylake_pfit_enable(struct intel_crtc *crtc)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004370{
4371 struct drm_device *dev = crtc->base.dev;
4372 struct drm_i915_private *dev_priv = dev->dev_private;
4373 int pipe = crtc->pipe;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004374 struct intel_crtc_scaler_state *scaler_state =
4375 &crtc->config->scaler_state;
4376
4377 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4378
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004379 if (crtc->config->pch_pfit.enabled) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004380 int id;
4381
4382 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4383 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4384 return;
4385 }
4386
4387 id = scaler_state->scaler_id;
4388 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4389 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4390 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4391 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4392
4393 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004394 }
4395}
4396
Jesse Barnesb074cec2013-04-25 12:55:02 -07004397static void ironlake_pfit_enable(struct intel_crtc *crtc)
4398{
4399 struct drm_device *dev = crtc->base.dev;
4400 struct drm_i915_private *dev_priv = dev->dev_private;
4401 int pipe = crtc->pipe;
4402
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004403 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004404 /* Force use of hard-coded filter coefficients
4405 * as some pre-programmed values are broken,
4406 * e.g. x201.
4407 */
4408 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4409 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4410 PF_PIPE_SEL_IVB(pipe));
4411 else
4412 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004413 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4414 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004415 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004416}
4417
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004418void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004419{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004420 struct drm_device *dev = crtc->base.dev;
4421 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid77e4532013-09-24 13:52:55 -03004422
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004423 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004424 return;
4425
Maarten Lankhorst307e4492016-03-23 14:33:28 +01004426 /*
4427 * We can only enable IPS after we enable a plane and wait for a vblank
4428 * This function is called from post_plane_update, which is run after
4429 * a vblank wait.
4430 */
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004431
Paulo Zanonid77e4532013-09-24 13:52:55 -03004432 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004433 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004434 mutex_lock(&dev_priv->rps.hw_lock);
4435 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4436 mutex_unlock(&dev_priv->rps.hw_lock);
4437 /* Quoting Art Runyan: "its not safe to expect any particular
4438 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004439 * mailbox." Moreover, the mailbox may return a bogus state,
4440 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004441 */
4442 } else {
4443 I915_WRITE(IPS_CTL, IPS_ENABLE);
4444 /* The bit only becomes 1 in the next vblank, so this wait here
4445 * is essentially intel_wait_for_vblank. If we don't have this
4446 * and don't wait for vblanks until the end of crtc_enable, then
4447 * the HW state readout code will complain that the expected
4448 * IPS_CTL value is not the one we read. */
4449 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4450 DRM_ERROR("Timed out waiting for IPS enable\n");
4451 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004452}
4453
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004454void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004455{
4456 struct drm_device *dev = crtc->base.dev;
4457 struct drm_i915_private *dev_priv = dev->dev_private;
4458
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004459 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004460 return;
4461
4462 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004463 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004464 mutex_lock(&dev_priv->rps.hw_lock);
4465 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4466 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004467 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4468 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4469 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004470 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004471 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004472 POSTING_READ(IPS_CTL);
4473 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004474
4475 /* We need to wait for a vblank before we can disable the plane. */
4476 intel_wait_for_vblank(dev, crtc->pipe);
4477}
4478
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004479static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004480{
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004481 if (intel_crtc->overlay) {
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004482 struct drm_device *dev = intel_crtc->base.dev;
4483 struct drm_i915_private *dev_priv = dev->dev_private;
4484
4485 mutex_lock(&dev->struct_mutex);
4486 dev_priv->mm.interruptible = false;
4487 (void) intel_overlay_switch_off(intel_crtc->overlay);
4488 dev_priv->mm.interruptible = true;
4489 mutex_unlock(&dev->struct_mutex);
4490 }
4491
4492 /* Let userspace switch the overlay on again. In most cases userspace
4493 * has to recompute where to put it anyway.
4494 */
4495}
4496
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004497/**
4498 * intel_post_enable_primary - Perform operations after enabling primary plane
4499 * @crtc: the CRTC whose primary plane was just enabled
4500 *
4501 * Performs potentially sleeping operations that must be done after the primary
4502 * plane is enabled, such as updating FBC and IPS. Note that this may be
4503 * called due to an explicit primary plane update, or due to an implicit
4504 * re-enable that is caused when a sprite plane is updated to no longer
4505 * completely hide the primary plane.
4506 */
4507static void
4508intel_post_enable_primary(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004509{
4510 struct drm_device *dev = crtc->dev;
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004511 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004512 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4513 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004514
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004515 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004516 * FIXME IPS should be fine as long as one plane is
4517 * enabled, but in practice it seems to have problems
4518 * when going from primary only to sprite only and vice
4519 * versa.
4520 */
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004521 hsw_enable_ips(intel_crtc);
4522
Daniel Vetterf99d7062014-06-19 16:01:59 +02004523 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004524 * Gen2 reports pipe underruns whenever all planes are disabled.
4525 * So don't enable underrun reporting before at least some planes
4526 * are enabled.
4527 * FIXME: Need to fix the logic to work when we turn off all planes
4528 * but leave the pipe running.
Daniel Vetterf99d7062014-06-19 16:01:59 +02004529 */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004530 if (IS_GEN2(dev))
4531 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4532
Ville Syrjäläaca7b682015-10-30 19:22:21 +02004533 /* Underruns don't always raise interrupts, so check manually. */
4534 intel_check_cpu_fifo_underruns(dev_priv);
4535 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004536}
4537
Ville Syrjälä2622a082016-03-09 19:07:26 +02004538/* FIXME move all this to pre_plane_update() with proper state tracking */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004539static void
4540intel_pre_disable_primary(struct drm_crtc *crtc)
4541{
4542 struct drm_device *dev = crtc->dev;
4543 struct drm_i915_private *dev_priv = dev->dev_private;
4544 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4545 int pipe = intel_crtc->pipe;
4546
4547 /*
4548 * Gen2 reports pipe underruns whenever all planes are disabled.
4549 * So diasble underrun reporting before all the planes get disabled.
4550 * FIXME: Need to fix the logic to work when we turn off all planes
4551 * but leave the pipe running.
4552 */
4553 if (IS_GEN2(dev))
4554 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4555
4556 /*
Ville Syrjälä2622a082016-03-09 19:07:26 +02004557 * FIXME IPS should be fine as long as one plane is
4558 * enabled, but in practice it seems to have problems
4559 * when going from primary only to sprite only and vice
4560 * versa.
4561 */
4562 hsw_disable_ips(intel_crtc);
4563}
4564
4565/* FIXME get rid of this and use pre_plane_update */
4566static void
4567intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
4568{
4569 struct drm_device *dev = crtc->dev;
4570 struct drm_i915_private *dev_priv = dev->dev_private;
4571 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4572 int pipe = intel_crtc->pipe;
4573
4574 intel_pre_disable_primary(crtc);
4575
4576 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004577 * Vblank time updates from the shadow to live plane control register
4578 * are blocked if the memory self-refresh mode is active at that
4579 * moment. So to make sure the plane gets truly disabled, disable
4580 * first the self-refresh mode. The self-refresh enable bit in turn
4581 * will be checked/applied by the HW only at the next frame start
4582 * event which is after the vblank start event, so we need to have a
4583 * wait-for-vblank between disabling the plane and the pipe.
4584 */
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004585 if (HAS_GMCH_DISPLAY(dev)) {
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004586 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004587 dev_priv->wm.vlv.cxsr = false;
4588 intel_wait_for_vblank(dev, pipe);
4589 }
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004590}
4591
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004592static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004593{
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004594 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
4595 struct drm_atomic_state *old_state = old_crtc_state->base.state;
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004596 struct intel_crtc_state *pipe_config =
4597 to_intel_crtc_state(crtc->base.state);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004598 struct drm_device *dev = crtc->base.dev;
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004599 struct drm_plane *primary = crtc->base.primary;
4600 struct drm_plane_state *old_pri_state =
4601 drm_atomic_get_existing_plane_state(old_state, primary);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004602
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004603 intel_frontbuffer_flip(dev, pipe_config->fb_bits);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004604
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01004605 crtc->wm.cxsr_allowed = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +03004606
Ville Syrjäläcaed3612016-03-09 19:07:25 +02004607 if (pipe_config->update_wm_post && pipe_config->base.active)
Ville Syrjäläf015c552015-06-24 22:00:02 +03004608 intel_update_watermarks(&crtc->base);
4609
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004610 if (old_pri_state) {
4611 struct intel_plane_state *primary_state =
4612 to_intel_plane_state(primary->state);
4613 struct intel_plane_state *old_primary_state =
4614 to_intel_plane_state(old_pri_state);
4615
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +01004616 intel_fbc_post_update(crtc);
4617
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004618 if (primary_state->visible &&
4619 (needs_modeset(&pipe_config->base) ||
4620 !old_primary_state->visible))
4621 intel_post_enable_primary(&crtc->base);
4622 }
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004623}
4624
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004625static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004626{
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004627 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004628 struct drm_device *dev = crtc->base.dev;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +02004629 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01004630 struct intel_crtc_state *pipe_config =
4631 to_intel_crtc_state(crtc->base.state);
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004632 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4633 struct drm_plane *primary = crtc->base.primary;
4634 struct drm_plane_state *old_pri_state =
4635 drm_atomic_get_existing_plane_state(old_state, primary);
4636 bool modeset = needs_modeset(&pipe_config->base);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004637
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004638 if (old_pri_state) {
4639 struct intel_plane_state *primary_state =
4640 to_intel_plane_state(primary->state);
4641 struct intel_plane_state *old_primary_state =
4642 to_intel_plane_state(old_pri_state);
4643
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +01004644 intel_fbc_pre_update(crtc);
4645
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004646 if (old_primary_state->visible &&
4647 (modeset || !primary_state->visible))
4648 intel_pre_disable_primary(&crtc->base);
4649 }
Ville Syrjälä852eb002015-06-24 22:00:07 +03004650
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01004651 if (pipe_config->disable_cxsr) {
Ville Syrjälä852eb002015-06-24 22:00:07 +03004652 crtc->wm.cxsr_allowed = false;
Maarten Lankhorst2dfd1782016-02-03 16:53:25 +01004653
Ville Syrjälä2622a082016-03-09 19:07:26 +02004654 /*
4655 * Vblank time updates from the shadow to live plane control register
4656 * are blocked if the memory self-refresh mode is active at that
4657 * moment. So to make sure the plane gets truly disabled, disable
4658 * first the self-refresh mode. The self-refresh enable bit in turn
4659 * will be checked/applied by the HW only at the next frame start
4660 * event which is after the vblank start event, so we need to have a
4661 * wait-for-vblank between disabling the plane and the pipe.
4662 */
4663 if (old_crtc_state->base.active) {
Maarten Lankhorst2dfd1782016-02-03 16:53:25 +01004664 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä2622a082016-03-09 19:07:26 +02004665 dev_priv->wm.vlv.cxsr = false;
4666 intel_wait_for_vblank(dev, crtc->pipe);
4667 }
Ville Syrjälä852eb002015-06-24 22:00:07 +03004668 }
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004669
Matt Ropered4a6a72016-02-23 17:20:13 -08004670 /*
4671 * IVB workaround: must disable low power watermarks for at least
4672 * one frame before enabling scaling. LP watermarks can be re-enabled
4673 * when scaling is disabled.
4674 *
4675 * WaCxSRDisabledForSpriteScaling:ivb
4676 */
4677 if (pipe_config->disable_lp_wm) {
4678 ilk_disable_lp_wm(dev);
4679 intel_wait_for_vblank(dev, crtc->pipe);
4680 }
4681
4682 /*
4683 * If we're doing a modeset, we're done. No need to do any pre-vblank
4684 * watermark programming here.
4685 */
4686 if (needs_modeset(&pipe_config->base))
4687 return;
4688
4689 /*
4690 * For platforms that support atomic watermarks, program the
4691 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
4692 * will be the intermediate values that are safe for both pre- and
4693 * post- vblank; when vblank happens, the 'active' values will be set
4694 * to the final 'target' values and we'll do this again to get the
4695 * optimal watermarks. For gen9+ platforms, the values we program here
4696 * will be the final target values which will get automatically latched
4697 * at vblank time; no further programming will be necessary.
4698 *
4699 * If a platform hasn't been transitioned to atomic watermarks yet,
4700 * we'll continue to update watermarks the old way, if flags tell
4701 * us to.
4702 */
4703 if (dev_priv->display.initial_watermarks != NULL)
4704 dev_priv->display.initial_watermarks(pipe_config);
Ville Syrjäläcaed3612016-03-09 19:07:25 +02004705 else if (pipe_config->update_wm_pre)
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004706 intel_update_watermarks(&crtc->base);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004707}
4708
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004709static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004710{
4711 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004712 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004713 struct drm_plane *p;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004714 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004715
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004716 intel_crtc_dpms_overlay_disable(intel_crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004717
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004718 drm_for_each_plane_mask(p, dev, plane_mask)
4719 to_intel_plane(p)->disable_plane(p, crtc);
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004720
Daniel Vetterf99d7062014-06-19 16:01:59 +02004721 /*
4722 * FIXME: Once we grow proper nuclear flip support out of this we need
4723 * to compute the mask of flip planes precisely. For the time being
4724 * consider this a flip to a NULL plane.
4725 */
4726 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004727}
4728
Jesse Barnesf67a5592011-01-05 10:31:48 -08004729static void ironlake_crtc_enable(struct drm_crtc *crtc)
4730{
4731 struct drm_device *dev = crtc->dev;
4732 struct drm_i915_private *dev_priv = dev->dev_private;
4733 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004734 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004735 int pipe = intel_crtc->pipe;
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004736 struct intel_crtc_state *pipe_config =
4737 to_intel_crtc_state(crtc->state);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004738
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004739 if (WARN_ON(intel_crtc->active))
Jesse Barnesf67a5592011-01-05 10:31:48 -08004740 return;
4741
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004742 /*
4743 * Sometimes spurious CPU pipe underruns happen during FDI
4744 * training, at least with VGA+HDMI cloning. Suppress them.
4745 *
4746 * On ILK we get an occasional spurious CPU pipe underruns
4747 * between eDP port A enable and vdd enable. Also PCH port
4748 * enable seems to result in the occasional CPU pipe underrun.
4749 *
4750 * Spurious PCH underruns also occur during PCH enabling.
4751 */
4752 if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
4753 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004754 if (intel_crtc->config->has_pch_encoder)
Ville Syrjälä81b088c2015-10-30 19:21:31 +02004755 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
4756
4757 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02004758 intel_prepare_shared_dpll(intel_crtc);
4759
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004760 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05304761 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004762
4763 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02004764 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004765
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004766 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02004767 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004768 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004769 }
4770
4771 ironlake_set_pipeconf(crtc);
4772
Jesse Barnesf67a5592011-01-05 10:31:48 -08004773 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004774
Daniel Vetterf6736a12013-06-05 13:34:30 +02004775 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004776 if (encoder->pre_enable)
4777 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004778
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004779 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004780 /* Note: FDI PLL enabling _must_ be done before we enable the
4781 * cpu pipes, hence this is separate from all the other fdi/pch
4782 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004783 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004784 } else {
4785 assert_fdi_tx_disabled(dev_priv, pipe);
4786 assert_fdi_rx_disabled(dev_priv, pipe);
4787 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004788
Jesse Barnesb074cec2013-04-25 12:55:02 -07004789 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004790
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004791 /*
4792 * On ILK+ LUT must be loaded before the pipe is running but with
4793 * clocks enabled
4794 */
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004795 intel_color_load_luts(&pipe_config->base);
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004796
Imre Deak1d5bf5d2016-02-29 22:10:33 +02004797 if (dev_priv->display.initial_watermarks != NULL)
4798 dev_priv->display.initial_watermarks(intel_crtc->config);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004799 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004800
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004801 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004802 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004803
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004804 assert_vblank_disabled(crtc);
4805 drm_crtc_vblank_on(crtc);
4806
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004807 for_each_encoder_on_crtc(dev, crtc, encoder)
4808 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004809
4810 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004811 cpt_verify_modeset(dev, intel_crtc->pipe);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004812
4813 /* Must wait for vblank to avoid spurious PCH FIFO underruns */
4814 if (intel_crtc->config->has_pch_encoder)
4815 intel_wait_for_vblank(dev, pipe);
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004816 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004817 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004818}
4819
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004820/* IPS only exists on ULT machines and is tied to pipe A. */
4821static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4822{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004823 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004824}
4825
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004826static void haswell_crtc_enable(struct drm_crtc *crtc)
4827{
4828 struct drm_device *dev = crtc->dev;
4829 struct drm_i915_private *dev_priv = dev->dev_private;
4830 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4831 struct intel_encoder *encoder;
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004832 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
Jani Nikula4d1de972016-03-18 17:05:42 +02004833 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004834 struct intel_crtc_state *pipe_config =
4835 to_intel_crtc_state(crtc->state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004836
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004837 if (WARN_ON(intel_crtc->active))
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004838 return;
4839
Ville Syrjälä81b088c2015-10-30 19:21:31 +02004840 if (intel_crtc->config->has_pch_encoder)
4841 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4842 false);
4843
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02004844 if (intel_crtc->config->shared_dpll)
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004845 intel_enable_shared_dpll(intel_crtc);
4846
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004847 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05304848 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02004849
Jani Nikula4d1de972016-03-18 17:05:42 +02004850 if (!intel_crtc->config->has_dsi_encoder)
4851 intel_set_pipe_timings(intel_crtc);
4852
Jani Nikulabc58be62016-03-18 17:05:39 +02004853 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02004854
Jani Nikula4d1de972016-03-18 17:05:42 +02004855 if (cpu_transcoder != TRANSCODER_EDP &&
4856 !transcoder_is_dsi(cpu_transcoder)) {
4857 I915_WRITE(PIPE_MULT(cpu_transcoder),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004858 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07004859 }
4860
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004861 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02004862 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004863 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02004864 }
4865
Jani Nikula4d1de972016-03-18 17:05:42 +02004866 if (!intel_crtc->config->has_dsi_encoder)
4867 haswell_set_pipeconf(crtc);
4868
Jani Nikula391bf042016-03-18 17:05:40 +02004869 haswell_set_pipemisc(crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02004870
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004871 intel_color_set_csc(&pipe_config->base);
Daniel Vetter229fca92014-04-24 23:55:09 +02004872
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004873 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004874
Daniel Vetter6b698512015-11-28 11:05:39 +01004875 if (intel_crtc->config->has_pch_encoder)
4876 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4877 else
4878 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4879
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304880 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004881 if (encoder->pre_enable)
4882 encoder->pre_enable(encoder);
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304883 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004884
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004885 if (intel_crtc->config->has_pch_encoder)
Imre Deak4fe94672014-06-25 22:01:49 +03004886 dev_priv->display.fdi_link_train(crtc);
Imre Deak4fe94672014-06-25 22:01:49 +03004887
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_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004890
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07004891 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004892 skylake_pfit_enable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08004893 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07004894 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004895
4896 /*
4897 * On ILK+ LUT must be loaded before the pipe is running but with
4898 * clocks enabled
4899 */
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004900 intel_color_load_luts(&pipe_config->base);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004901
Paulo Zanoni1f544382012-10-24 11:32:00 -02004902 intel_ddi_set_pipe_settings(crtc);
Jani Nikulaa65347b2015-11-27 12:21:46 +02004903 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304904 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004905
Imre Deak1d5bf5d2016-02-29 22:10:33 +02004906 if (dev_priv->display.initial_watermarks != NULL)
4907 dev_priv->display.initial_watermarks(pipe_config);
4908 else
4909 intel_update_watermarks(crtc);
Jani Nikula4d1de972016-03-18 17:05:42 +02004910
4911 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
4912 if (!intel_crtc->config->has_dsi_encoder)
4913 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004914
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004915 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004916 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004917
Jani Nikulaa65347b2015-11-27 12:21:46 +02004918 if (intel_crtc->config->dp_encoder_is_mst)
Dave Airlie0e32b392014-05-02 14:02:48 +10004919 intel_ddi_set_vc_payload_alloc(crtc, true);
4920
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004921 assert_vblank_disabled(crtc);
4922 drm_crtc_vblank_on(crtc);
4923
Jani Nikula8807e552013-08-30 19:40:32 +03004924 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004925 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004926 intel_opregion_notify_encoder(encoder, true);
4927 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004928
Daniel Vetter6b698512015-11-28 11:05:39 +01004929 if (intel_crtc->config->has_pch_encoder) {
4930 intel_wait_for_vblank(dev, pipe);
4931 intel_wait_for_vblank(dev, pipe);
4932 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004933 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4934 true);
Daniel Vetter6b698512015-11-28 11:05:39 +01004935 }
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004936
Paulo Zanonie4916942013-09-20 16:21:19 -03004937 /* If we change the relative order between pipe/planes enabling, we need
4938 * to change the workaround. */
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004939 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
4940 if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
4941 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4942 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4943 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004944}
4945
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02004946static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004947{
4948 struct drm_device *dev = crtc->base.dev;
4949 struct drm_i915_private *dev_priv = dev->dev_private;
4950 int pipe = crtc->pipe;
4951
4952 /* To avoid upsetting the power well on haswell only disable the pfit if
4953 * it's in use. The hw state code will make sure we get this right. */
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02004954 if (force || crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004955 I915_WRITE(PF_CTL(pipe), 0);
4956 I915_WRITE(PF_WIN_POS(pipe), 0);
4957 I915_WRITE(PF_WIN_SZ(pipe), 0);
4958 }
4959}
4960
Jesse Barnes6be4a602010-09-10 10:26:01 -07004961static void ironlake_crtc_disable(struct drm_crtc *crtc)
4962{
4963 struct drm_device *dev = crtc->dev;
4964 struct drm_i915_private *dev_priv = dev->dev_private;
4965 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004966 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004967 int pipe = intel_crtc->pipe;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004968
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004969 /*
4970 * Sometimes spurious CPU pipe underruns happen when the
4971 * pipe is already disabled, but FDI RX/TX is still enabled.
4972 * Happens at least with VGA+HDMI cloning. Suppress them.
4973 */
4974 if (intel_crtc->config->has_pch_encoder) {
4975 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004976 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004977 }
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004978
Daniel Vetterea9d7582012-07-10 10:42:52 +02004979 for_each_encoder_on_crtc(dev, crtc, encoder)
4980 encoder->disable(encoder);
4981
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004982 drm_crtc_vblank_off(crtc);
4983 assert_vblank_disabled(crtc);
4984
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03004985 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004986
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02004987 ironlake_pfit_disable(intel_crtc, false);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004988
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004989 if (intel_crtc->config->has_pch_encoder)
Ville Syrjälä5a74f702015-05-05 17:17:38 +03004990 ironlake_fdi_disable(crtc);
4991
Daniel Vetterbf49ec82012-09-06 22:15:40 +02004992 for_each_encoder_on_crtc(dev, crtc, encoder)
4993 if (encoder->post_disable)
4994 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004995
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004996 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02004997 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004998
Daniel Vetterd925c592013-06-05 13:34:04 +02004999 if (HAS_PCH_CPT(dev)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02005000 i915_reg_t reg;
5001 u32 temp;
5002
Daniel Vetterd925c592013-06-05 13:34:04 +02005003 /* disable TRANS_DP_CTL */
5004 reg = TRANS_DP_CTL(pipe);
5005 temp = I915_READ(reg);
5006 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5007 TRANS_DP_PORT_SEL_MASK);
5008 temp |= TRANS_DP_PORT_SEL_NONE;
5009 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005010
Daniel Vetterd925c592013-06-05 13:34:04 +02005011 /* disable DPLL_SEL */
5012 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02005013 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02005014 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005015 }
Daniel Vetterd925c592013-06-05 13:34:04 +02005016
Daniel Vetterd925c592013-06-05 13:34:04 +02005017 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005018 }
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005019
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005020 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005021 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005022}
5023
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005024static void haswell_crtc_disable(struct drm_crtc *crtc)
5025{
5026 struct drm_device *dev = crtc->dev;
5027 struct drm_i915_private *dev_priv = dev->dev_private;
5028 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5029 struct intel_encoder *encoder;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005030 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005031
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005032 if (intel_crtc->config->has_pch_encoder)
5033 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5034 false);
5035
Jani Nikula8807e552013-08-30 19:40:32 +03005036 for_each_encoder_on_crtc(dev, crtc, encoder) {
5037 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005038 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005039 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005040
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005041 drm_crtc_vblank_off(crtc);
5042 assert_vblank_disabled(crtc);
5043
Jani Nikula4d1de972016-03-18 17:05:42 +02005044 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
5045 if (!intel_crtc->config->has_dsi_encoder)
5046 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005047
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005048 if (intel_crtc->config->dp_encoder_is_mst)
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03005049 intel_ddi_set_vc_payload_alloc(crtc, false);
5050
Jani Nikulaa65347b2015-11-27 12:21:46 +02005051 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305052 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005053
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07005054 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02005055 skylake_scaler_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005056 else
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005057 ironlake_pfit_disable(intel_crtc, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005058
Jani Nikulaa65347b2015-11-27 12:21:46 +02005059 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305060 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005061
Imre Deak97b040a2014-06-25 22:01:50 +03005062 for_each_encoder_on_crtc(dev, crtc, encoder)
5063 if (encoder->post_disable)
5064 encoder->post_disable(encoder);
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005065
Ville Syrjälä92966a32015-12-08 16:05:48 +02005066 if (intel_crtc->config->has_pch_encoder) {
5067 lpt_disable_pch_transcoder(dev_priv);
Ville Syrjälä503a74e2015-12-04 22:22:14 +02005068 lpt_disable_iclkip(dev_priv);
Ville Syrjälä92966a32015-12-08 16:05:48 +02005069 intel_ddi_fdi_disable(crtc);
5070
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005071 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5072 true);
Ville Syrjälä92966a32015-12-08 16:05:48 +02005073 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005074}
5075
Jesse Barnes2dd24552013-04-25 12:55:01 -07005076static void i9xx_pfit_enable(struct intel_crtc *crtc)
5077{
5078 struct drm_device *dev = crtc->base.dev;
5079 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005080 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07005081
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02005082 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005083 return;
5084
Daniel Vetterc0b03412013-05-28 12:05:54 +02005085 /*
5086 * The panel fitter should only be adjusted whilst the pipe is disabled,
5087 * according to register description and PRM.
5088 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005089 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5090 assert_pipe_disabled(dev_priv, crtc->pipe);
5091
Jesse Barnesb074cec2013-04-25 12:55:02 -07005092 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5093 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005094
5095 /* Border color in case we don't scale up to the full screen. Black by
5096 * default, change to something else for debugging. */
5097 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005098}
5099
Dave Airlied05410f2014-06-05 13:22:59 +10005100static enum intel_display_power_domain port_to_power_domain(enum port port)
5101{
5102 switch (port) {
5103 case PORT_A:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005104 return POWER_DOMAIN_PORT_DDI_A_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005105 case PORT_B:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005106 return POWER_DOMAIN_PORT_DDI_B_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005107 case PORT_C:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005108 return POWER_DOMAIN_PORT_DDI_C_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005109 case PORT_D:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005110 return POWER_DOMAIN_PORT_DDI_D_LANES;
Xiong Zhangd8e19f92015-08-13 18:00:12 +08005111 case PORT_E:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005112 return POWER_DOMAIN_PORT_DDI_E_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005113 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005114 MISSING_CASE(port);
Dave Airlied05410f2014-06-05 13:22:59 +10005115 return POWER_DOMAIN_PORT_OTHER;
5116 }
5117}
5118
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005119static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
5120{
5121 switch (port) {
5122 case PORT_A:
5123 return POWER_DOMAIN_AUX_A;
5124 case PORT_B:
5125 return POWER_DOMAIN_AUX_B;
5126 case PORT_C:
5127 return POWER_DOMAIN_AUX_C;
5128 case PORT_D:
5129 return POWER_DOMAIN_AUX_D;
5130 case PORT_E:
5131 /* FIXME: Check VBT for actual wiring of PORT E */
5132 return POWER_DOMAIN_AUX_D;
5133 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005134 MISSING_CASE(port);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005135 return POWER_DOMAIN_AUX_A;
5136 }
5137}
5138
Imre Deak319be8a2014-03-04 19:22:57 +02005139enum intel_display_power_domain
5140intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02005141{
Imre Deak319be8a2014-03-04 19:22:57 +02005142 struct drm_device *dev = intel_encoder->base.dev;
5143 struct intel_digital_port *intel_dig_port;
5144
5145 switch (intel_encoder->type) {
5146 case INTEL_OUTPUT_UNKNOWN:
5147 /* Only DDI platforms should ever use this output type */
5148 WARN_ON_ONCE(!HAS_DDI(dev));
5149 case INTEL_OUTPUT_DISPLAYPORT:
5150 case INTEL_OUTPUT_HDMI:
5151 case INTEL_OUTPUT_EDP:
5152 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10005153 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10005154 case INTEL_OUTPUT_DP_MST:
5155 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5156 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02005157 case INTEL_OUTPUT_ANALOG:
5158 return POWER_DOMAIN_PORT_CRT;
5159 case INTEL_OUTPUT_DSI:
5160 return POWER_DOMAIN_PORT_DSI;
5161 default:
5162 return POWER_DOMAIN_PORT_OTHER;
5163 }
5164}
5165
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005166enum intel_display_power_domain
5167intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
5168{
5169 struct drm_device *dev = intel_encoder->base.dev;
5170 struct intel_digital_port *intel_dig_port;
5171
5172 switch (intel_encoder->type) {
5173 case INTEL_OUTPUT_UNKNOWN:
Imre Deak651174a2015-11-18 15:57:24 +02005174 case INTEL_OUTPUT_HDMI:
5175 /*
5176 * Only DDI platforms should ever use these output types.
5177 * We can get here after the HDMI detect code has already set
5178 * the type of the shared encoder. Since we can't be sure
5179 * what's the status of the given connectors, play safe and
5180 * run the DP detection too.
5181 */
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005182 WARN_ON_ONCE(!HAS_DDI(dev));
5183 case INTEL_OUTPUT_DISPLAYPORT:
5184 case INTEL_OUTPUT_EDP:
5185 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
5186 return port_to_aux_power_domain(intel_dig_port->port);
5187 case INTEL_OUTPUT_DP_MST:
5188 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5189 return port_to_aux_power_domain(intel_dig_port->port);
5190 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005191 MISSING_CASE(intel_encoder->type);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005192 return POWER_DOMAIN_AUX_A;
5193 }
5194}
5195
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005196static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
5197 struct intel_crtc_state *crtc_state)
Imre Deak319be8a2014-03-04 19:22:57 +02005198{
5199 struct drm_device *dev = crtc->dev;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005200 struct drm_encoder *encoder;
Imre Deak319be8a2014-03-04 19:22:57 +02005201 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5202 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02005203 unsigned long mask;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005204 enum transcoder transcoder = crtc_state->cpu_transcoder;
Imre Deak77d22dc2014-03-05 16:20:52 +02005205
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005206 if (!crtc_state->base.active)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005207 return 0;
5208
Imre Deak77d22dc2014-03-05 16:20:52 +02005209 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5210 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005211 if (crtc_state->pch_pfit.enabled ||
5212 crtc_state->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02005213 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5214
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005215 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
5216 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5217
Imre Deak319be8a2014-03-04 19:22:57 +02005218 mask |= BIT(intel_display_port_power_domain(intel_encoder));
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005219 }
Imre Deak319be8a2014-03-04 19:22:57 +02005220
Maarten Lankhorst15e7ec22016-03-14 09:27:54 +01005221 if (crtc_state->shared_dpll)
5222 mask |= BIT(POWER_DOMAIN_PLLS);
5223
Imre Deak77d22dc2014-03-05 16:20:52 +02005224 return mask;
5225}
5226
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005227static unsigned long
5228modeset_get_crtc_power_domains(struct drm_crtc *crtc,
5229 struct intel_crtc_state *crtc_state)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005230{
5231 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5232 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5233 enum intel_display_power_domain domain;
5234 unsigned long domains, new_domains, old_domains;
5235
5236 old_domains = intel_crtc->enabled_power_domains;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005237 intel_crtc->enabled_power_domains = new_domains =
5238 get_crtc_power_domains(crtc, crtc_state);
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005239
5240 domains = new_domains & ~old_domains;
5241
5242 for_each_power_domain(domain, domains)
5243 intel_display_power_get(dev_priv, domain);
5244
5245 return old_domains & ~new_domains;
5246}
5247
5248static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
5249 unsigned long domains)
5250{
5251 enum intel_display_power_domain domain;
5252
5253 for_each_power_domain(domain, domains)
5254 intel_display_power_put(dev_priv, domain);
5255}
5256
Mika Kaholaadafdc62015-08-18 14:36:59 +03005257static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
5258{
5259 int max_cdclk_freq = dev_priv->max_cdclk_freq;
5260
5261 if (INTEL_INFO(dev_priv)->gen >= 9 ||
5262 IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
5263 return max_cdclk_freq;
5264 else if (IS_CHERRYVIEW(dev_priv))
5265 return max_cdclk_freq*95/100;
5266 else if (INTEL_INFO(dev_priv)->gen < 4)
5267 return 2*max_cdclk_freq*90/100;
5268 else
5269 return max_cdclk_freq*90/100;
5270}
5271
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005272static void intel_update_max_cdclk(struct drm_device *dev)
5273{
5274 struct drm_i915_private *dev_priv = dev->dev_private;
5275
Rodrigo Vivief11bdb2015-10-28 04:16:45 -07005276 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005277 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
5278
5279 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
5280 dev_priv->max_cdclk_freq = 675000;
5281 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
5282 dev_priv->max_cdclk_freq = 540000;
5283 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
5284 dev_priv->max_cdclk_freq = 450000;
5285 else
5286 dev_priv->max_cdclk_freq = 337500;
Matt Roper281c1142016-04-05 14:37:19 -07005287 } else if (IS_BROXTON(dev)) {
5288 dev_priv->max_cdclk_freq = 624000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005289 } else if (IS_BROADWELL(dev)) {
5290 /*
5291 * FIXME with extra cooling we can allow
5292 * 540 MHz for ULX and 675 Mhz for ULT.
5293 * How can we know if extra cooling is
5294 * available? PCI ID, VTB, something else?
5295 */
5296 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5297 dev_priv->max_cdclk_freq = 450000;
5298 else if (IS_BDW_ULX(dev))
5299 dev_priv->max_cdclk_freq = 450000;
5300 else if (IS_BDW_ULT(dev))
5301 dev_priv->max_cdclk_freq = 540000;
5302 else
5303 dev_priv->max_cdclk_freq = 675000;
Mika Kahola0904dea2015-06-12 10:11:32 +03005304 } else if (IS_CHERRYVIEW(dev)) {
5305 dev_priv->max_cdclk_freq = 320000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005306 } else if (IS_VALLEYVIEW(dev)) {
5307 dev_priv->max_cdclk_freq = 400000;
5308 } else {
5309 /* otherwise assume cdclk is fixed */
5310 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5311 }
5312
Mika Kaholaadafdc62015-08-18 14:36:59 +03005313 dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
5314
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005315 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5316 dev_priv->max_cdclk_freq);
Mika Kaholaadafdc62015-08-18 14:36:59 +03005317
5318 DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
5319 dev_priv->max_dotclk_freq);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005320}
5321
5322static void intel_update_cdclk(struct drm_device *dev)
5323{
5324 struct drm_i915_private *dev_priv = dev->dev_private;
5325
5326 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
5327 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5328 dev_priv->cdclk_freq);
5329
5330 /*
5331 * Program the gmbus_freq based on the cdclk frequency.
5332 * BSpec erroneously claims we should aim for 4MHz, but
5333 * in fact 1MHz is the correct frequency.
5334 */
Wayne Boyer666a4532015-12-09 12:29:35 -08005335 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005336 /*
5337 * Program the gmbus_freq based on the cdclk frequency.
5338 * BSpec erroneously claims we should aim for 4MHz, but
5339 * in fact 1MHz is the correct frequency.
5340 */
5341 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
5342 }
5343
5344 if (dev_priv->max_cdclk_freq == 0)
5345 intel_update_max_cdclk(dev);
5346}
5347
Imre Deakc6c46962016-04-01 16:02:40 +03005348static void broxton_set_cdclk(struct drm_i915_private *dev_priv, int frequency)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305349{
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305350 uint32_t divider;
5351 uint32_t ratio;
5352 uint32_t current_freq;
5353 int ret;
5354
5355 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
5356 switch (frequency) {
5357 case 144000:
5358 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5359 ratio = BXT_DE_PLL_RATIO(60);
5360 break;
5361 case 288000:
5362 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5363 ratio = BXT_DE_PLL_RATIO(60);
5364 break;
5365 case 384000:
5366 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5367 ratio = BXT_DE_PLL_RATIO(60);
5368 break;
5369 case 576000:
5370 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5371 ratio = BXT_DE_PLL_RATIO(60);
5372 break;
5373 case 624000:
5374 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5375 ratio = BXT_DE_PLL_RATIO(65);
5376 break;
5377 case 19200:
5378 /*
5379 * Bypass frequency with DE PLL disabled. Init ratio, divider
5380 * to suppress GCC warning.
5381 */
5382 ratio = 0;
5383 divider = 0;
5384 break;
5385 default:
5386 DRM_ERROR("unsupported CDCLK freq %d", frequency);
5387
5388 return;
5389 }
5390
5391 mutex_lock(&dev_priv->rps.hw_lock);
5392 /* Inform power controller of upcoming frequency change */
5393 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5394 0x80000000);
5395 mutex_unlock(&dev_priv->rps.hw_lock);
5396
5397 if (ret) {
5398 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
5399 ret, frequency);
5400 return;
5401 }
5402
5403 current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
5404 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
5405 current_freq = current_freq * 500 + 1000;
5406
5407 /*
5408 * DE PLL has to be disabled when
5409 * - setting to 19.2MHz (bypass, PLL isn't used)
5410 * - before setting to 624MHz (PLL needs toggling)
5411 * - before setting to any frequency from 624MHz (PLL needs toggling)
5412 */
5413 if (frequency == 19200 || frequency == 624000 ||
5414 current_freq == 624000) {
5415 I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5416 /* Timeout 200us */
5417 if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5418 1))
5419 DRM_ERROR("timout waiting for DE PLL unlock\n");
5420 }
5421
5422 if (frequency != 19200) {
5423 uint32_t val;
5424
5425 val = I915_READ(BXT_DE_PLL_CTL);
5426 val &= ~BXT_DE_PLL_RATIO_MASK;
5427 val |= ratio;
5428 I915_WRITE(BXT_DE_PLL_CTL, val);
5429
5430 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5431 /* Timeout 200us */
5432 if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5433 DRM_ERROR("timeout waiting for DE PLL lock\n");
5434
5435 val = I915_READ(CDCLK_CTL);
5436 val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
5437 val |= divider;
5438 /*
5439 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5440 * enable otherwise.
5441 */
5442 val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5443 if (frequency >= 500000)
5444 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5445
5446 val &= ~CDCLK_FREQ_DECIMAL_MASK;
5447 /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5448 val |= (frequency - 1000) / 500;
5449 I915_WRITE(CDCLK_CTL, val);
5450 }
5451
5452 mutex_lock(&dev_priv->rps.hw_lock);
5453 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5454 DIV_ROUND_UP(frequency, 25000));
5455 mutex_unlock(&dev_priv->rps.hw_lock);
5456
5457 if (ret) {
5458 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
5459 ret, frequency);
5460 return;
5461 }
5462
Imre Deakc6c46962016-04-01 16:02:40 +03005463 intel_update_cdclk(dev_priv->dev);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305464}
5465
Imre Deakc2e001e2016-04-01 16:02:43 +03005466static bool broxton_cdclk_is_enabled(struct drm_i915_private *dev_priv)
5467{
5468 if (!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE))
5469 return false;
5470
5471 /* TODO: Check for a valid CDCLK rate */
5472
5473 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_REQUEST)) {
5474 DRM_DEBUG_DRIVER("CDCLK enabled, but DBUF power not requested\n");
5475
5476 return false;
5477 }
5478
5479 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE)) {
5480 DRM_DEBUG_DRIVER("CDCLK enabled, but DBUF power hasn't settled\n");
5481
5482 return false;
5483 }
5484
5485 return true;
5486}
5487
Imre Deakadc7f042016-04-04 17:27:10 +03005488bool broxton_cdclk_verify_state(struct drm_i915_private *dev_priv)
5489{
5490 return broxton_cdclk_is_enabled(dev_priv);
5491}
5492
Imre Deakc6c46962016-04-01 16:02:40 +03005493void broxton_init_cdclk(struct drm_i915_private *dev_priv)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305494{
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305495 /* check if cd clock is enabled */
Imre Deakc2e001e2016-04-01 16:02:43 +03005496 if (broxton_cdclk_is_enabled(dev_priv)) {
5497 DRM_DEBUG_KMS("CDCLK already enabled, won't reprogram it\n");
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305498 return;
5499 }
5500
Imre Deakc2e001e2016-04-01 16:02:43 +03005501 DRM_DEBUG_KMS("CDCLK not enabled, enabling it\n");
5502
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305503 /*
5504 * FIXME:
5505 * - The initial CDCLK needs to be read from VBT.
5506 * Need to make this change after VBT has changes for BXT.
5507 * - check if setting the max (or any) cdclk freq is really necessary
5508 * here, it belongs to modeset time
5509 */
Imre Deakc6c46962016-04-01 16:02:40 +03005510 broxton_set_cdclk(dev_priv, 624000);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305511
5512 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005513 POSTING_READ(DBUF_CTL);
5514
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305515 udelay(10);
5516
5517 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5518 DRM_ERROR("DBuf power enable timeout!\n");
5519}
5520
Imre Deakc6c46962016-04-01 16:02:40 +03005521void broxton_uninit_cdclk(struct drm_i915_private *dev_priv)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305522{
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305523 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005524 POSTING_READ(DBUF_CTL);
5525
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305526 udelay(10);
5527
5528 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5529 DRM_ERROR("DBuf power disable timeout!\n");
5530
5531 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
Imre Deakc6c46962016-04-01 16:02:40 +03005532 broxton_set_cdclk(dev_priv, 19200);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305533}
5534
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005535static const struct skl_cdclk_entry {
5536 unsigned int freq;
5537 unsigned int vco;
5538} skl_cdclk_frequencies[] = {
5539 { .freq = 308570, .vco = 8640 },
5540 { .freq = 337500, .vco = 8100 },
5541 { .freq = 432000, .vco = 8640 },
5542 { .freq = 450000, .vco = 8100 },
5543 { .freq = 540000, .vco = 8100 },
5544 { .freq = 617140, .vco = 8640 },
5545 { .freq = 675000, .vco = 8100 },
5546};
5547
5548static unsigned int skl_cdclk_decimal(unsigned int freq)
5549{
5550 return (freq - 1000) / 500;
5551}
5552
5553static unsigned int skl_cdclk_get_vco(unsigned int freq)
5554{
5555 unsigned int i;
5556
5557 for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
5558 const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
5559
5560 if (e->freq == freq)
5561 return e->vco;
5562 }
5563
5564 return 8100;
5565}
5566
5567static void
5568skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
5569{
5570 unsigned int min_freq;
5571 u32 val;
5572
5573 /* select the minimum CDCLK before enabling DPLL 0 */
5574 val = I915_READ(CDCLK_CTL);
5575 val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
5576 val |= CDCLK_FREQ_337_308;
5577
5578 if (required_vco == 8640)
5579 min_freq = 308570;
5580 else
5581 min_freq = 337500;
5582
5583 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
5584
5585 I915_WRITE(CDCLK_CTL, val);
5586 POSTING_READ(CDCLK_CTL);
5587
5588 /*
5589 * We always enable DPLL0 with the lowest link rate possible, but still
5590 * taking into account the VCO required to operate the eDP panel at the
5591 * desired frequency. The usual DP link rates operate with a VCO of
5592 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5593 * The modeset code is responsible for the selection of the exact link
5594 * rate later on, with the constraint of choosing a frequency that
5595 * works with required_vco.
5596 */
5597 val = I915_READ(DPLL_CTRL1);
5598
5599 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5600 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5601 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
5602 if (required_vco == 8640)
5603 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5604 SKL_DPLL0);
5605 else
5606 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5607 SKL_DPLL0);
5608
5609 I915_WRITE(DPLL_CTRL1, val);
5610 POSTING_READ(DPLL_CTRL1);
5611
5612 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5613
5614 if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
5615 DRM_ERROR("DPLL0 not locked\n");
5616}
5617
5618static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5619{
5620 int ret;
5621 u32 val;
5622
5623 /* inform PCU we want to change CDCLK */
5624 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5625 mutex_lock(&dev_priv->rps.hw_lock);
5626 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5627 mutex_unlock(&dev_priv->rps.hw_lock);
5628
5629 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5630}
5631
5632static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5633{
5634 unsigned int i;
5635
5636 for (i = 0; i < 15; i++) {
5637 if (skl_cdclk_pcu_ready(dev_priv))
5638 return true;
5639 udelay(10);
5640 }
5641
5642 return false;
5643}
5644
5645static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
5646{
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005647 struct drm_device *dev = dev_priv->dev;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005648 u32 freq_select, pcu_ack;
5649
5650 DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
5651
5652 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5653 DRM_ERROR("failed to inform PCU about cdclk change\n");
5654 return;
5655 }
5656
5657 /* set CDCLK_CTL */
5658 switch(freq) {
5659 case 450000:
5660 case 432000:
5661 freq_select = CDCLK_FREQ_450_432;
5662 pcu_ack = 1;
5663 break;
5664 case 540000:
5665 freq_select = CDCLK_FREQ_540;
5666 pcu_ack = 2;
5667 break;
5668 case 308570:
5669 case 337500:
5670 default:
5671 freq_select = CDCLK_FREQ_337_308;
5672 pcu_ack = 0;
5673 break;
5674 case 617140:
5675 case 675000:
5676 freq_select = CDCLK_FREQ_675_617;
5677 pcu_ack = 3;
5678 break;
5679 }
5680
5681 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
5682 POSTING_READ(CDCLK_CTL);
5683
5684 /* inform PCU of the change */
5685 mutex_lock(&dev_priv->rps.hw_lock);
5686 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5687 mutex_unlock(&dev_priv->rps.hw_lock);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005688
5689 intel_update_cdclk(dev);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005690}
5691
5692void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5693{
5694 /* disable DBUF power */
5695 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
5696 POSTING_READ(DBUF_CTL);
5697
5698 udelay(10);
5699
5700 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5701 DRM_ERROR("DBuf power disable timeout\n");
5702
Imre Deakab96c1ee2015-11-04 19:24:18 +02005703 /* disable DPLL0 */
5704 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
5705 if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
5706 DRM_ERROR("Couldn't disable DPLL0\n");
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005707}
5708
5709void skl_init_cdclk(struct drm_i915_private *dev_priv)
5710{
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005711 unsigned int required_vco;
5712
Gary Wang39d9b852015-08-28 16:40:34 +08005713 /* DPLL0 not enabled (happens on early BIOS versions) */
5714 if (!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE)) {
5715 /* enable DPLL0 */
5716 required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
5717 skl_dpll0_enable(dev_priv, required_vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005718 }
5719
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005720 /* set CDCLK to the frequency the BIOS chose */
5721 skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
5722
5723 /* enable DBUF power */
5724 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
5725 POSTING_READ(DBUF_CTL);
5726
5727 udelay(10);
5728
5729 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5730 DRM_ERROR("DBuf power enable timeout\n");
5731}
5732
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305733int skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
5734{
5735 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
5736 uint32_t cdctl = I915_READ(CDCLK_CTL);
5737 int freq = dev_priv->skl_boot_cdclk;
5738
Shobhit Kumarf1b391a2015-11-05 18:05:32 +05305739 /*
5740 * check if the pre-os intialized the display
5741 * There is SWF18 scratchpad register defined which is set by the
5742 * pre-os which can be used by the OS drivers to check the status
5743 */
5744 if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
5745 goto sanitize;
5746
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305747 /* Is PLL enabled and locked ? */
5748 if (!((lcpll1 & LCPLL_PLL_ENABLE) && (lcpll1 & LCPLL_PLL_LOCK)))
5749 goto sanitize;
5750
5751 /* DPLL okay; verify the cdclock
5752 *
5753 * Noticed in some instances that the freq selection is correct but
5754 * decimal part is programmed wrong from BIOS where pre-os does not
5755 * enable display. Verify the same as well.
5756 */
5757 if (cdctl == ((cdctl & CDCLK_FREQ_SEL_MASK) | skl_cdclk_decimal(freq)))
5758 /* All well; nothing to sanitize */
5759 return false;
5760sanitize:
5761 /*
5762 * As of now initialize with max cdclk till
5763 * we get dynamic cdclk support
5764 * */
5765 dev_priv->skl_boot_cdclk = dev_priv->max_cdclk_freq;
5766 skl_init_cdclk(dev_priv);
5767
5768 /* we did have to sanitize */
5769 return true;
5770}
5771
Jesse Barnes30a970c2013-11-04 13:48:12 -08005772/* Adjust CDclk dividers to allow high res or save power if possible */
5773static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5774{
5775 struct drm_i915_private *dev_priv = dev->dev_private;
5776 u32 val, cmd;
5777
Vandana Kannan164dfd22014-11-24 13:37:41 +05305778 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5779 != dev_priv->cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02005780
Ville Syrjälädfcab172014-06-13 13:37:47 +03005781 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08005782 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03005783 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005784 cmd = 1;
5785 else
5786 cmd = 0;
5787
5788 mutex_lock(&dev_priv->rps.hw_lock);
5789 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5790 val &= ~DSPFREQGUAR_MASK;
5791 val |= (cmd << DSPFREQGUAR_SHIFT);
5792 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5793 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5794 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5795 50)) {
5796 DRM_ERROR("timed out waiting for CDclk change\n");
5797 }
5798 mutex_unlock(&dev_priv->rps.hw_lock);
5799
Ville Syrjälä54433e92015-05-26 20:42:31 +03005800 mutex_lock(&dev_priv->sb_lock);
5801
Ville Syrjälädfcab172014-06-13 13:37:47 +03005802 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005803 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005804
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005805 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005806
Jesse Barnes30a970c2013-11-04 13:48:12 -08005807 /* adjust cdclk divider */
5808 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Vandana Kannan87d5d252015-09-24 23:29:17 +03005809 val &= ~CCK_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005810 val |= divider;
5811 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03005812
5813 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
Vandana Kannan87d5d252015-09-24 23:29:17 +03005814 CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
Ville Syrjäläa877e802014-06-13 13:37:52 +03005815 50))
5816 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08005817 }
5818
Jesse Barnes30a970c2013-11-04 13:48:12 -08005819 /* adjust self-refresh exit latency value */
5820 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5821 val &= ~0x7f;
5822
5823 /*
5824 * For high bandwidth configs, we set a higher latency in the bunit
5825 * so that the core display fetch happens in time to avoid underruns.
5826 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03005827 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005828 val |= 4500 / 250; /* 4.5 usec */
5829 else
5830 val |= 3000 / 250; /* 3.0 usec */
5831 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
Ville Syrjälä54433e92015-05-26 20:42:31 +03005832
Ville Syrjäläa5805162015-05-26 20:42:30 +03005833 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005834
Ville Syrjäläb6283052015-06-03 15:45:07 +03005835 intel_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005836}
5837
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005838static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5839{
5840 struct drm_i915_private *dev_priv = dev->dev_private;
5841 u32 val, cmd;
5842
Vandana Kannan164dfd22014-11-24 13:37:41 +05305843 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5844 != dev_priv->cdclk_freq);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005845
5846 switch (cdclk) {
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005847 case 333333:
5848 case 320000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005849 case 266667:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005850 case 200000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005851 break;
5852 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01005853 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005854 return;
5855 }
5856
Ville Syrjälä9d0d3fd2015-03-02 20:07:17 +02005857 /*
5858 * Specs are full of misinformation, but testing on actual
5859 * hardware has shown that we just need to write the desired
5860 * CCK divider into the Punit register.
5861 */
5862 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5863
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005864 mutex_lock(&dev_priv->rps.hw_lock);
5865 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5866 val &= ~DSPFREQGUAR_MASK_CHV;
5867 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5868 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5869 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5870 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5871 50)) {
5872 DRM_ERROR("timed out waiting for CDclk change\n");
5873 }
5874 mutex_unlock(&dev_priv->rps.hw_lock);
5875
Ville Syrjäläb6283052015-06-03 15:45:07 +03005876 intel_update_cdclk(dev);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005877}
5878
Jesse Barnes30a970c2013-11-04 13:48:12 -08005879static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5880 int max_pixclk)
5881{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005882 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005883 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005884
Jesse Barnes30a970c2013-11-04 13:48:12 -08005885 /*
5886 * Really only a few cases to deal with, as only 4 CDclks are supported:
5887 * 200MHz
5888 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005889 * 320/333MHz (depends on HPLL freq)
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005890 * 400MHz (VLV only)
5891 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5892 * of the lower bin and adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005893 *
5894 * We seem to get an unstable or solid color picture at 200MHz.
5895 * Not sure what's wrong. For now use 200MHz only when all pipes
5896 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08005897 */
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005898 if (!IS_CHERRYVIEW(dev_priv) &&
5899 max_pixclk > freq_320*limit/100)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005900 return 400000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005901 else if (max_pixclk > 266667*limit/100)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005902 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005903 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005904 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005905 else
5906 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005907}
5908
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305909static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
5910 int max_pixclk)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005911{
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305912 /*
5913 * FIXME:
5914 * - remove the guardband, it's not needed on BXT
5915 * - set 19.2MHz bypass frequency if there are no active pipes
5916 */
5917 if (max_pixclk > 576000*9/10)
5918 return 624000;
5919 else if (max_pixclk > 384000*9/10)
5920 return 576000;
5921 else if (max_pixclk > 288000*9/10)
5922 return 384000;
5923 else if (max_pixclk > 144000*9/10)
5924 return 288000;
5925 else
5926 return 144000;
5927}
5928
Maarten Lankhorste8788cb2016-02-16 10:25:11 +01005929/* Compute the max pixel clock for new configuration. */
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005930static int intel_mode_max_pixclk(struct drm_device *dev,
5931 struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005932{
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005933 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
5934 struct drm_i915_private *dev_priv = dev->dev_private;
5935 struct drm_crtc *crtc;
5936 struct drm_crtc_state *crtc_state;
5937 unsigned max_pixclk = 0, i;
5938 enum pipe pipe;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005939
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005940 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
5941 sizeof(intel_state->min_pixclk));
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005942
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005943 for_each_crtc_in_state(state, crtc, crtc_state, i) {
5944 int pixclk = 0;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005945
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005946 if (crtc_state->enable)
5947 pixclk = crtc_state->adjusted_mode.crtc_clock;
5948
5949 intel_state->min_pixclk[i] = pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005950 }
5951
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005952 for_each_pipe(dev_priv, pipe)
5953 max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
5954
Jesse Barnes30a970c2013-11-04 13:48:12 -08005955 return max_pixclk;
5956}
5957
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005958static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005959{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005960 struct drm_device *dev = state->dev;
5961 struct drm_i915_private *dev_priv = dev->dev_private;
5962 int max_pixclk = intel_mode_max_pixclk(dev, state);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005963 struct intel_atomic_state *intel_state =
5964 to_intel_atomic_state(state);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005965
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005966 if (max_pixclk < 0)
5967 return max_pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005968
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005969 intel_state->cdclk = intel_state->dev_cdclk =
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005970 valleyview_calc_cdclk(dev_priv, max_pixclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305971
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005972 if (!intel_state->active_crtcs)
5973 intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
5974
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005975 return 0;
5976}
Jesse Barnes30a970c2013-11-04 13:48:12 -08005977
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005978static int broxton_modeset_calc_cdclk(struct drm_atomic_state *state)
5979{
5980 struct drm_device *dev = state->dev;
5981 struct drm_i915_private *dev_priv = dev->dev_private;
5982 int max_pixclk = intel_mode_max_pixclk(dev, state);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005983 struct intel_atomic_state *intel_state =
5984 to_intel_atomic_state(state);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005985
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005986 if (max_pixclk < 0)
5987 return max_pixclk;
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005988
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005989 intel_state->cdclk = intel_state->dev_cdclk =
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005990 broxton_calc_cdclk(dev_priv, max_pixclk);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005991
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005992 if (!intel_state->active_crtcs)
5993 intel_state->dev_cdclk = broxton_calc_cdclk(dev_priv, 0);
5994
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005995 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005996}
5997
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005998static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
5999{
6000 unsigned int credits, default_credits;
6001
6002 if (IS_CHERRYVIEW(dev_priv))
6003 default_credits = PFI_CREDIT(12);
6004 else
6005 default_credits = PFI_CREDIT(8);
6006
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03006007 if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006008 /* CHV suggested value is 31 or 63 */
6009 if (IS_CHERRYVIEW(dev_priv))
Ville Syrjäläfcc00082015-05-26 20:22:40 +03006010 credits = PFI_CREDIT_63;
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006011 else
6012 credits = PFI_CREDIT(15);
6013 } else {
6014 credits = default_credits;
6015 }
6016
6017 /*
6018 * WA - write default credits before re-programming
6019 * FIXME: should we also set the resend bit here?
6020 */
6021 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6022 default_credits);
6023
6024 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6025 credits | PFI_CREDIT_RESEND);
6026
6027 /*
6028 * FIXME is this guaranteed to clear
6029 * immediately or should we poll for it?
6030 */
6031 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
6032}
6033
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006034static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006035{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006036 struct drm_device *dev = old_state->dev;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006037 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006038 struct intel_atomic_state *old_intel_state =
6039 to_intel_atomic_state(old_state);
6040 unsigned req_cdclk = old_intel_state->dev_cdclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006041
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006042 /*
6043 * FIXME: We can end up here with all power domains off, yet
6044 * with a CDCLK frequency other than the minimum. To account
6045 * for this take the PIPE-A power domain, which covers the HW
6046 * blocks needed for the following programming. This can be
6047 * removed once it's guaranteed that we get here either with
6048 * the minimum CDCLK set, or the required power domains
6049 * enabled.
6050 */
6051 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006052
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006053 if (IS_CHERRYVIEW(dev))
6054 cherryview_set_cdclk(dev, req_cdclk);
6055 else
6056 valleyview_set_cdclk(dev, req_cdclk);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006057
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006058 vlv_program_pfi_credits(dev_priv);
Imre Deak738c05c2014-11-19 16:25:37 +02006059
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006060 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006061}
6062
Jesse Barnes89b667f2013-04-18 14:51:36 -07006063static void valleyview_crtc_enable(struct drm_crtc *crtc)
6064{
6065 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006066 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006067 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6068 struct intel_encoder *encoder;
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006069 struct intel_crtc_state *pipe_config =
6070 to_intel_crtc_state(crtc->state);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006071 int pipe = intel_crtc->pipe;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006072
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006073 if (WARN_ON(intel_crtc->active))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006074 return;
6075
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006076 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306077 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006078
6079 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02006080 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006081
Ville Syrjäläc14b0482014-10-16 20:52:34 +03006082 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
6083 struct drm_i915_private *dev_priv = dev->dev_private;
6084
6085 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6086 I915_WRITE(CHV_CANVAS(pipe), 0);
6087 }
6088
Daniel Vetter5b18e572014-04-24 23:55:06 +02006089 i9xx_set_pipeconf(intel_crtc);
6090
Jesse Barnes89b667f2013-04-18 14:51:36 -07006091 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006092
Daniel Vettera72e4c92014-09-30 10:56:47 +02006093 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006094
Jesse Barnes89b667f2013-04-18 14:51:36 -07006095 for_each_encoder_on_crtc(dev, crtc, encoder)
6096 if (encoder->pre_pll_enable)
6097 encoder->pre_pll_enable(encoder);
6098
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006099 if (IS_CHERRYVIEW(dev)) {
6100 chv_prepare_pll(intel_crtc, intel_crtc->config);
6101 chv_enable_pll(intel_crtc, intel_crtc->config);
6102 } else {
6103 vlv_prepare_pll(intel_crtc, intel_crtc->config);
6104 vlv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006105 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07006106
6107 for_each_encoder_on_crtc(dev, crtc, encoder)
6108 if (encoder->pre_enable)
6109 encoder->pre_enable(encoder);
6110
Jesse Barnes2dd24552013-04-25 12:55:01 -07006111 i9xx_pfit_enable(intel_crtc);
6112
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006113 intel_color_load_luts(&pipe_config->base);
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006114
Ville Syrjäläcaed3612016-03-09 19:07:25 +02006115 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006116 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006117
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006118 assert_vblank_disabled(crtc);
6119 drm_crtc_vblank_on(crtc);
6120
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006121 for_each_encoder_on_crtc(dev, crtc, encoder)
6122 encoder->enable(encoder);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006123}
6124
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006125static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6126{
6127 struct drm_device *dev = crtc->base.dev;
6128 struct drm_i915_private *dev_priv = dev->dev_private;
6129
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006130 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6131 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006132}
6133
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006134static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006135{
6136 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006137 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006138 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006139 struct intel_encoder *encoder;
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006140 struct intel_crtc_state *pipe_config =
6141 to_intel_crtc_state(crtc->state);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006142 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08006143
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006144 if (WARN_ON(intel_crtc->active))
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006145 return;
6146
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006147 i9xx_set_pll_dividers(intel_crtc);
6148
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006149 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306150 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006151
6152 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02006153 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006154
Daniel Vetter5b18e572014-04-24 23:55:06 +02006155 i9xx_set_pipeconf(intel_crtc);
6156
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006157 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01006158
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006159 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006160 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006161
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02006162 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02006163 if (encoder->pre_enable)
6164 encoder->pre_enable(encoder);
6165
Daniel Vetterf6736a12013-06-05 13:34:30 +02006166 i9xx_enable_pll(intel_crtc);
6167
Jesse Barnes2dd24552013-04-25 12:55:01 -07006168 i9xx_pfit_enable(intel_crtc);
6169
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006170 intel_color_load_luts(&pipe_config->base);
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006171
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006172 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006173 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006174
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006175 assert_vblank_disabled(crtc);
6176 drm_crtc_vblank_on(crtc);
6177
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006178 for_each_encoder_on_crtc(dev, crtc, encoder)
6179 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006180}
6181
Daniel Vetter87476d62013-04-11 16:29:06 +02006182static void i9xx_pfit_disable(struct intel_crtc *crtc)
6183{
6184 struct drm_device *dev = crtc->base.dev;
6185 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02006186
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006187 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02006188 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02006189
6190 assert_pipe_disabled(dev_priv, crtc->pipe);
6191
Daniel Vetter328d8e82013-05-08 10:36:31 +02006192 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6193 I915_READ(PFIT_CONTROL));
6194 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02006195}
6196
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006197static void i9xx_crtc_disable(struct drm_crtc *crtc)
6198{
6199 struct drm_device *dev = crtc->dev;
6200 struct drm_i915_private *dev_priv = dev->dev_private;
6201 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006202 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006203 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006204
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006205 /*
6206 * On gen2 planes are double buffered but the pipe isn't, so we must
6207 * wait for planes to fully turn off before disabling the pipe.
6208 */
Ander Conselvan de Oliveira90e83e52016-03-22 10:11:24 +02006209 if (IS_GEN2(dev))
6210 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006211
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006212 for_each_encoder_on_crtc(dev, crtc, encoder)
6213 encoder->disable(encoder);
6214
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006215 drm_crtc_vblank_off(crtc);
6216 assert_vblank_disabled(crtc);
6217
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03006218 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006219
Daniel Vetter87476d62013-04-11 16:29:06 +02006220 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006221
Jesse Barnes89b667f2013-04-18 14:51:36 -07006222 for_each_encoder_on_crtc(dev, crtc, encoder)
6223 if (encoder->post_disable)
6224 encoder->post_disable(encoder);
6225
Jani Nikulaa65347b2015-11-27 12:21:46 +02006226 if (!intel_crtc->config->has_dsi_encoder) {
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006227 if (IS_CHERRYVIEW(dev))
6228 chv_disable_pll(dev_priv, pipe);
6229 else if (IS_VALLEYVIEW(dev))
6230 vlv_disable_pll(dev_priv, pipe);
6231 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03006232 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006233 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006234
Ville Syrjäläd6db9952015-07-08 23:45:49 +03006235 for_each_encoder_on_crtc(dev, crtc, encoder)
6236 if (encoder->post_pll_disable)
6237 encoder->post_pll_disable(encoder);
6238
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006239 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006240 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006241}
6242
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006243static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006244{
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006245 struct intel_encoder *encoder;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006246 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006247 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006248 enum intel_display_power_domain domain;
6249 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006250
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006251 if (!intel_crtc->active)
6252 return;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006253
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006254 if (to_intel_plane_state(crtc->primary->state)->visible) {
Maarten Lankhorstfc32b1f2015-10-19 17:09:23 +02006255 WARN_ON(intel_crtc->unpin_work);
6256
Ville Syrjälä2622a082016-03-09 19:07:26 +02006257 intel_pre_disable_primary_noatomic(crtc);
Maarten Lankhorst54a419612015-11-23 10:25:28 +01006258
6259 intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
6260 to_intel_plane_state(crtc->primary->state)->visible = false;
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006261 }
6262
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006263 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006264
6265 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was enabled, now disabled\n",
6266 crtc->base.id);
6267
6268 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
6269 crtc->state->active = false;
Matt Roper37d90782015-09-24 15:53:06 -07006270 intel_crtc->active = false;
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006271 crtc->enabled = false;
6272 crtc->state->connector_mask = 0;
6273 crtc->state->encoder_mask = 0;
6274
6275 for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
6276 encoder->base.crtc = NULL;
6277
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -02006278 intel_fbc_disable(intel_crtc);
Matt Roper37d90782015-09-24 15:53:06 -07006279 intel_update_watermarks(crtc);
Maarten Lankhorst1f7457b2015-07-13 11:55:05 +02006280 intel_disable_shared_dpll(intel_crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006281
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006282 domains = intel_crtc->enabled_power_domains;
6283 for_each_power_domain(domain, domains)
6284 intel_display_power_put(dev_priv, domain);
6285 intel_crtc->enabled_power_domains = 0;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006286
6287 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
6288 dev_priv->min_pixclk[intel_crtc->pipe] = 0;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006289}
6290
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006291/*
6292 * turn all crtc's off, but do not adjust state
6293 * This has to be paired with a call to intel_modeset_setup_hw_state.
6294 */
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006295int intel_display_suspend(struct drm_device *dev)
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006296{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006297 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006298 struct drm_atomic_state *state;
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006299 int ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006300
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006301 state = drm_atomic_helper_suspend(dev);
6302 ret = PTR_ERR_OR_ZERO(state);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006303 if (ret)
6304 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006305 else
6306 dev_priv->modeset_restore_state = state;
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006307 return ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006308}
6309
Chris Wilsonea5b2132010-08-04 13:50:23 +01006310void intel_encoder_destroy(struct drm_encoder *encoder)
6311{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006312 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01006313
Chris Wilsonea5b2132010-08-04 13:50:23 +01006314 drm_encoder_cleanup(encoder);
6315 kfree(intel_encoder);
6316}
6317
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006318/* Cross check the actual hw state with our own modeset state tracking (and it's
6319 * internal consistency). */
Maarten Lankhorstc0ead702016-03-30 10:00:05 +02006320static void intel_connector_verify_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006321{
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006322 struct drm_crtc *crtc = connector->base.state->crtc;
6323
6324 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6325 connector->base.base.id,
6326 connector->base.name);
6327
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006328 if (connector->get_hw_state(connector)) {
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006329 struct intel_encoder *encoder = connector->encoder;
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006330 struct drm_connector_state *conn_state = connector->base.state;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006331
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006332 I915_STATE_WARN(!crtc,
6333 "connector enabled without attached crtc\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006334
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006335 if (!crtc)
6336 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006337
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006338 I915_STATE_WARN(!crtc->state->active,
6339 "connector is active, but attached crtc isn't\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006340
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006341 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006342 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006343
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006344 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006345 "atomic encoder doesn't match attached encoder\n");
Dave Airlie36cd7442014-05-02 13:44:18 +10006346
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006347 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006348 "attached encoder crtc differs from connector crtc\n");
6349 } else {
Maarten Lankhorst4d688a22015-08-05 12:37:06 +02006350 I915_STATE_WARN(crtc && crtc->state->active,
6351 "attached crtc is active, but connector isn't\n");
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006352 I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
6353 "best encoder set without crtc!\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006354 }
6355}
6356
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006357int intel_connector_init(struct intel_connector *connector)
6358{
Maarten Lankhorst5350a032016-01-04 12:53:15 +01006359 drm_atomic_helper_connector_reset(&connector->base);
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006360
Maarten Lankhorst5350a032016-01-04 12:53:15 +01006361 if (!connector->base.state)
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006362 return -ENOMEM;
6363
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006364 return 0;
6365}
6366
6367struct intel_connector *intel_connector_alloc(void)
6368{
6369 struct intel_connector *connector;
6370
6371 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6372 if (!connector)
6373 return NULL;
6374
6375 if (intel_connector_init(connector) < 0) {
6376 kfree(connector);
6377 return NULL;
6378 }
6379
6380 return connector;
6381}
6382
Daniel Vetterf0947c32012-07-02 13:10:34 +02006383/* Simple connector->get_hw_state implementation for encoders that support only
6384 * one connector and no cloning and hence the encoder state determines the state
6385 * of the connector. */
6386bool intel_connector_get_hw_state(struct intel_connector *connector)
6387{
Daniel Vetter24929352012-07-02 20:28:59 +02006388 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02006389 struct intel_encoder *encoder = connector->encoder;
6390
6391 return encoder->get_hw_state(encoder, &pipe);
6392}
6393
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006394static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006395{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006396 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6397 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006398
6399 return 0;
6400}
6401
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006402static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006403 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006404{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006405 struct drm_atomic_state *state = pipe_config->base.state;
6406 struct intel_crtc *other_crtc;
6407 struct intel_crtc_state *other_crtc_state;
6408
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006409 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6410 pipe_name(pipe), pipe_config->fdi_lanes);
6411 if (pipe_config->fdi_lanes > 4) {
6412 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6413 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006414 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006415 }
6416
Paulo Zanonibafb6552013-11-02 21:07:44 -07006417 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006418 if (pipe_config->fdi_lanes > 2) {
6419 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6420 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006421 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006422 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006423 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006424 }
6425 }
6426
6427 if (INTEL_INFO(dev)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006428 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006429
6430 /* Ivybridge 3 pipe is really complicated */
6431 switch (pipe) {
6432 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006433 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006434 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006435 if (pipe_config->fdi_lanes <= 2)
6436 return 0;
6437
6438 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6439 other_crtc_state =
6440 intel_atomic_get_crtc_state(state, other_crtc);
6441 if (IS_ERR(other_crtc_state))
6442 return PTR_ERR(other_crtc_state);
6443
6444 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006445 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6446 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006447 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006448 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006449 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006450 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02006451 if (pipe_config->fdi_lanes > 2) {
6452 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6453 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006454 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02006455 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006456
6457 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6458 other_crtc_state =
6459 intel_atomic_get_crtc_state(state, other_crtc);
6460 if (IS_ERR(other_crtc_state))
6461 return PTR_ERR(other_crtc_state);
6462
6463 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006464 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006465 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006466 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006467 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006468 default:
6469 BUG();
6470 }
6471}
6472
Daniel Vettere29c22c2013-02-21 00:00:16 +01006473#define RETRY 1
6474static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006475 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02006476{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006477 struct drm_device *dev = intel_crtc->base.dev;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006478 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006479 int lane, link_bw, fdi_dotclock, ret;
6480 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006481
Daniel Vettere29c22c2013-02-21 00:00:16 +01006482retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02006483 /* FDI is a binary signal running at ~2.7GHz, encoding
6484 * each output octet as 10 bits. The actual frequency
6485 * is stored as a divider into a 100MHz clock, and the
6486 * mode pixel clock is stored in units of 1KHz.
6487 * Hence the bw of each lane in terms of the mode signal
6488 * is:
6489 */
Ville Syrjälä21a727b2016-02-17 21:41:10 +02006490 link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006491
Damien Lespiau241bfc32013-09-25 16:45:37 +01006492 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006493
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006494 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006495 pipe_config->pipe_bpp);
6496
6497 pipe_config->fdi_lanes = lane;
6498
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006499 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006500 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006501
Ville Syrjäläe3b247d2016-02-17 21:41:09 +02006502 ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006503 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01006504 pipe_config->pipe_bpp -= 2*3;
6505 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6506 pipe_config->pipe_bpp);
6507 needs_recompute = true;
6508 pipe_config->bw_constrained = true;
6509
6510 goto retry;
6511 }
6512
6513 if (needs_recompute)
6514 return RETRY;
6515
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006516 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006517}
6518
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006519static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6520 struct intel_crtc_state *pipe_config)
6521{
6522 if (pipe_config->pipe_bpp > 24)
6523 return false;
6524
6525 /* HSW can handle pixel rate up to cdclk? */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03006526 if (IS_HASWELL(dev_priv))
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006527 return true;
6528
6529 /*
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03006530 * We compare against max which means we must take
6531 * the increased cdclk requirement into account when
6532 * calculating the new cdclk.
6533 *
6534 * Should measure whether using a lower cdclk w/o IPS
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006535 */
6536 return ilk_pipe_pixel_rate(pipe_config) <=
6537 dev_priv->max_cdclk_freq * 95 / 100;
6538}
6539
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006540static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006541 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006542{
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006543 struct drm_device *dev = crtc->base.dev;
6544 struct drm_i915_private *dev_priv = dev->dev_private;
6545
Jani Nikulad330a952014-01-21 11:24:25 +02006546 pipe_config->ips_enabled = i915.enable_ips &&
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006547 hsw_crtc_supports_ips(crtc) &&
6548 pipe_config_supports_ips(dev_priv, pipe_config);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006549}
6550
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006551static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
6552{
6553 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6554
6555 /* GDG double wide on either pipe, otherwise pipe A only */
6556 return INTEL_INFO(dev_priv)->gen < 4 &&
6557 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
6558}
6559
Daniel Vettera43f6e02013-06-07 23:10:32 +02006560static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006561 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006562{
Daniel Vettera43f6e02013-06-07 23:10:32 +02006563 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02006564 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006565 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Chris Wilson89749352010-09-12 18:25:19 +01006566
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006567 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006568 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006569 int clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006570
6571 /*
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006572 * Enable double wide mode when the dot clock
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006573 * is > 90% of the (display) core speed.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006574 */
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006575 if (intel_crtc_supports_double_wide(crtc) &&
6576 adjusted_mode->crtc_clock > clock_limit) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006577 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006578 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006579 }
6580
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006581 if (adjusted_mode->crtc_clock > clock_limit) {
6582 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
6583 adjusted_mode->crtc_clock, clock_limit,
6584 yesno(pipe_config->double_wide));
Daniel Vettere29c22c2013-02-21 00:00:16 +01006585 return -EINVAL;
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006586 }
Zhenyu Wang2c072452009-06-05 15:38:42 +08006587 }
Chris Wilson89749352010-09-12 18:25:19 +01006588
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006589 /*
6590 * Pipe horizontal size must be even in:
6591 * - DVO ganged mode
6592 * - LVDS dual channel mode
6593 * - Double wide pipe
6594 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006595 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006596 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6597 pipe_config->pipe_src_w &= ~1;
6598
Damien Lespiau8693a822013-05-03 18:48:11 +01006599 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6600 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03006601 */
6602 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
Ville Syrjäläaad941d2015-09-25 16:38:56 +03006603 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006604 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03006605
Damien Lespiauf5adf942013-06-24 18:29:34 +01006606 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02006607 hsw_compute_ips_config(crtc, pipe_config);
6608
Daniel Vetter877d48d2013-04-19 11:24:43 +02006609 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02006610 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006611
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +02006612 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006613}
6614
Ville Syrjälä1652d192015-03-31 14:12:01 +03006615static int skylake_get_display_clock_speed(struct drm_device *dev)
6616{
6617 struct drm_i915_private *dev_priv = to_i915(dev);
6618 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6619 uint32_t cdctl = I915_READ(CDCLK_CTL);
6620 uint32_t linkrate;
6621
Damien Lespiau414355a2015-06-04 18:21:31 +01006622 if (!(lcpll1 & LCPLL_PLL_ENABLE))
Ville Syrjälä1652d192015-03-31 14:12:01 +03006623 return 24000; /* 24MHz is the cd freq with NSSC ref */
Ville Syrjälä1652d192015-03-31 14:12:01 +03006624
6625 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6626 return 540000;
6627
6628 linkrate = (I915_READ(DPLL_CTRL1) &
Damien Lespiau71cd8422015-04-30 16:39:17 +01006629 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006630
Damien Lespiau71cd8422015-04-30 16:39:17 +01006631 if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
6632 linkrate == DPLL_CTRL1_LINK_RATE_1080) {
Ville Syrjälä1652d192015-03-31 14:12:01 +03006633 /* vco 8640 */
6634 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6635 case CDCLK_FREQ_450_432:
6636 return 432000;
6637 case CDCLK_FREQ_337_308:
6638 return 308570;
6639 case CDCLK_FREQ_675_617:
6640 return 617140;
6641 default:
6642 WARN(1, "Unknown cd freq selection\n");
6643 }
6644 } else {
6645 /* vco 8100 */
6646 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6647 case CDCLK_FREQ_450_432:
6648 return 450000;
6649 case CDCLK_FREQ_337_308:
6650 return 337500;
6651 case CDCLK_FREQ_675_617:
6652 return 675000;
6653 default:
6654 WARN(1, "Unknown cd freq selection\n");
6655 }
6656 }
6657
6658 /* error case, do as if DPLL0 isn't enabled */
6659 return 24000;
6660}
6661
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006662static int broxton_get_display_clock_speed(struct drm_device *dev)
6663{
6664 struct drm_i915_private *dev_priv = to_i915(dev);
6665 uint32_t cdctl = I915_READ(CDCLK_CTL);
6666 uint32_t pll_ratio = I915_READ(BXT_DE_PLL_CTL) & BXT_DE_PLL_RATIO_MASK;
6667 uint32_t pll_enab = I915_READ(BXT_DE_PLL_ENABLE);
6668 int cdclk;
6669
6670 if (!(pll_enab & BXT_DE_PLL_PLL_ENABLE))
6671 return 19200;
6672
6673 cdclk = 19200 * pll_ratio / 2;
6674
6675 switch (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) {
6676 case BXT_CDCLK_CD2X_DIV_SEL_1:
6677 return cdclk; /* 576MHz or 624MHz */
6678 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
6679 return cdclk * 2 / 3; /* 384MHz */
6680 case BXT_CDCLK_CD2X_DIV_SEL_2:
6681 return cdclk / 2; /* 288MHz */
6682 case BXT_CDCLK_CD2X_DIV_SEL_4:
6683 return cdclk / 4; /* 144MHz */
6684 }
6685
6686 /* error case, do as if DE PLL isn't enabled */
6687 return 19200;
6688}
6689
Ville Syrjälä1652d192015-03-31 14:12:01 +03006690static int broadwell_get_display_clock_speed(struct drm_device *dev)
6691{
6692 struct drm_i915_private *dev_priv = dev->dev_private;
6693 uint32_t lcpll = I915_READ(LCPLL_CTL);
6694 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6695
6696 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6697 return 800000;
6698 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6699 return 450000;
6700 else if (freq == LCPLL_CLK_FREQ_450)
6701 return 450000;
6702 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6703 return 540000;
6704 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6705 return 337500;
6706 else
6707 return 675000;
6708}
6709
6710static int haswell_get_display_clock_speed(struct drm_device *dev)
6711{
6712 struct drm_i915_private *dev_priv = dev->dev_private;
6713 uint32_t lcpll = I915_READ(LCPLL_CTL);
6714 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6715
6716 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6717 return 800000;
6718 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6719 return 450000;
6720 else if (freq == LCPLL_CLK_FREQ_450)
6721 return 450000;
6722 else if (IS_HSW_ULT(dev))
6723 return 337500;
6724 else
6725 return 540000;
Jesse Barnes79e53942008-11-07 14:24:08 -08006726}
6727
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006728static int valleyview_get_display_clock_speed(struct drm_device *dev)
6729{
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03006730 return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
6731 CCK_DISPLAY_CLOCK_CONTROL);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006732}
6733
Ville Syrjäläb37a6432015-03-31 14:11:54 +03006734static int ilk_get_display_clock_speed(struct drm_device *dev)
6735{
6736 return 450000;
6737}
6738
Jesse Barnese70236a2009-09-21 10:42:27 -07006739static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006740{
Jesse Barnese70236a2009-09-21 10:42:27 -07006741 return 400000;
6742}
Jesse Barnes79e53942008-11-07 14:24:08 -08006743
Jesse Barnese70236a2009-09-21 10:42:27 -07006744static int i915_get_display_clock_speed(struct drm_device *dev)
6745{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006746 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006747}
Jesse Barnes79e53942008-11-07 14:24:08 -08006748
Jesse Barnese70236a2009-09-21 10:42:27 -07006749static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6750{
6751 return 200000;
6752}
Jesse Barnes79e53942008-11-07 14:24:08 -08006753
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006754static int pnv_get_display_clock_speed(struct drm_device *dev)
6755{
6756 u16 gcfgc = 0;
6757
6758 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6759
6760 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6761 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006762 return 266667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006763 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006764 return 333333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006765 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006766 return 444444;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006767 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6768 return 200000;
6769 default:
6770 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6771 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006772 return 133333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006773 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006774 return 166667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006775 }
6776}
6777
Jesse Barnese70236a2009-09-21 10:42:27 -07006778static int i915gm_get_display_clock_speed(struct drm_device *dev)
6779{
6780 u16 gcfgc = 0;
6781
6782 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6783
6784 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Ville Syrjäläe907f172015-03-31 14:09:47 +03006785 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006786 else {
6787 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6788 case GC_DISPLAY_CLOCK_333_MHZ:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006789 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006790 default:
6791 case GC_DISPLAY_CLOCK_190_200_MHZ:
6792 return 190000;
6793 }
6794 }
6795}
Jesse Barnes79e53942008-11-07 14:24:08 -08006796
Jesse Barnese70236a2009-09-21 10:42:27 -07006797static int i865_get_display_clock_speed(struct drm_device *dev)
6798{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006799 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006800}
6801
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006802static int i85x_get_display_clock_speed(struct drm_device *dev)
Jesse Barnese70236a2009-09-21 10:42:27 -07006803{
6804 u16 hpllcc = 0;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006805
Ville Syrjälä65cd2b32015-05-22 11:22:32 +03006806 /*
6807 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6808 * encoding is different :(
6809 * FIXME is this the right way to detect 852GM/852GMV?
6810 */
6811 if (dev->pdev->revision == 0x1)
6812 return 133333;
6813
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006814 pci_bus_read_config_word(dev->pdev->bus,
6815 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6816
Jesse Barnese70236a2009-09-21 10:42:27 -07006817 /* Assume that the hardware is in the high speed state. This
6818 * should be the default.
6819 */
6820 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6821 case GC_CLOCK_133_200:
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006822 case GC_CLOCK_133_200_2:
Jesse Barnese70236a2009-09-21 10:42:27 -07006823 case GC_CLOCK_100_200:
6824 return 200000;
6825 case GC_CLOCK_166_250:
6826 return 250000;
6827 case GC_CLOCK_100_133:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006828 return 133333;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006829 case GC_CLOCK_133_266:
6830 case GC_CLOCK_133_266_2:
6831 case GC_CLOCK_166_266:
6832 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006833 }
6834
6835 /* Shouldn't happen */
6836 return 0;
6837}
6838
6839static int i830_get_display_clock_speed(struct drm_device *dev)
6840{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006841 return 133333;
Jesse Barnes79e53942008-11-07 14:24:08 -08006842}
6843
Ville Syrjälä34edce22015-05-22 11:22:33 +03006844static unsigned int intel_hpll_vco(struct drm_device *dev)
6845{
6846 struct drm_i915_private *dev_priv = dev->dev_private;
6847 static const unsigned int blb_vco[8] = {
6848 [0] = 3200000,
6849 [1] = 4000000,
6850 [2] = 5333333,
6851 [3] = 4800000,
6852 [4] = 6400000,
6853 };
6854 static const unsigned int pnv_vco[8] = {
6855 [0] = 3200000,
6856 [1] = 4000000,
6857 [2] = 5333333,
6858 [3] = 4800000,
6859 [4] = 2666667,
6860 };
6861 static const unsigned int cl_vco[8] = {
6862 [0] = 3200000,
6863 [1] = 4000000,
6864 [2] = 5333333,
6865 [3] = 6400000,
6866 [4] = 3333333,
6867 [5] = 3566667,
6868 [6] = 4266667,
6869 };
6870 static const unsigned int elk_vco[8] = {
6871 [0] = 3200000,
6872 [1] = 4000000,
6873 [2] = 5333333,
6874 [3] = 4800000,
6875 };
6876 static const unsigned int ctg_vco[8] = {
6877 [0] = 3200000,
6878 [1] = 4000000,
6879 [2] = 5333333,
6880 [3] = 6400000,
6881 [4] = 2666667,
6882 [5] = 4266667,
6883 };
6884 const unsigned int *vco_table;
6885 unsigned int vco;
6886 uint8_t tmp = 0;
6887
6888 /* FIXME other chipsets? */
6889 if (IS_GM45(dev))
6890 vco_table = ctg_vco;
6891 else if (IS_G4X(dev))
6892 vco_table = elk_vco;
6893 else if (IS_CRESTLINE(dev))
6894 vco_table = cl_vco;
6895 else if (IS_PINEVIEW(dev))
6896 vco_table = pnv_vco;
6897 else if (IS_G33(dev))
6898 vco_table = blb_vco;
6899 else
6900 return 0;
6901
6902 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
6903
6904 vco = vco_table[tmp & 0x7];
6905 if (vco == 0)
6906 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
6907 else
6908 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
6909
6910 return vco;
6911}
6912
6913static int gm45_get_display_clock_speed(struct drm_device *dev)
6914{
6915 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6916 uint16_t tmp = 0;
6917
6918 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6919
6920 cdclk_sel = (tmp >> 12) & 0x1;
6921
6922 switch (vco) {
6923 case 2666667:
6924 case 4000000:
6925 case 5333333:
6926 return cdclk_sel ? 333333 : 222222;
6927 case 3200000:
6928 return cdclk_sel ? 320000 : 228571;
6929 default:
6930 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
6931 return 222222;
6932 }
6933}
6934
6935static int i965gm_get_display_clock_speed(struct drm_device *dev)
6936{
6937 static const uint8_t div_3200[] = { 16, 10, 8 };
6938 static const uint8_t div_4000[] = { 20, 12, 10 };
6939 static const uint8_t div_5333[] = { 24, 16, 14 };
6940 const uint8_t *div_table;
6941 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6942 uint16_t tmp = 0;
6943
6944 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6945
6946 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
6947
6948 if (cdclk_sel >= ARRAY_SIZE(div_3200))
6949 goto fail;
6950
6951 switch (vco) {
6952 case 3200000:
6953 div_table = div_3200;
6954 break;
6955 case 4000000:
6956 div_table = div_4000;
6957 break;
6958 case 5333333:
6959 div_table = div_5333;
6960 break;
6961 default:
6962 goto fail;
6963 }
6964
6965 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
6966
Damien Lespiaucaf4e252015-06-04 16:56:18 +01006967fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03006968 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
6969 return 200000;
6970}
6971
6972static int g33_get_display_clock_speed(struct drm_device *dev)
6973{
6974 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
6975 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
6976 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
6977 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
6978 const uint8_t *div_table;
6979 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6980 uint16_t tmp = 0;
6981
6982 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6983
6984 cdclk_sel = (tmp >> 4) & 0x7;
6985
6986 if (cdclk_sel >= ARRAY_SIZE(div_3200))
6987 goto fail;
6988
6989 switch (vco) {
6990 case 3200000:
6991 div_table = div_3200;
6992 break;
6993 case 4000000:
6994 div_table = div_4000;
6995 break;
6996 case 4800000:
6997 div_table = div_4800;
6998 break;
6999 case 5333333:
7000 div_table = div_5333;
7001 break;
7002 default:
7003 goto fail;
7004 }
7005
7006 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7007
Damien Lespiaucaf4e252015-06-04 16:56:18 +01007008fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03007009 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
7010 return 190476;
7011}
7012
Zhenyu Wang2c072452009-06-05 15:38:42 +08007013static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007014intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007015{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007016 while (*num > DATA_LINK_M_N_MASK ||
7017 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08007018 *num >>= 1;
7019 *den >>= 1;
7020 }
7021}
7022
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007023static void compute_m_n(unsigned int m, unsigned int n,
7024 uint32_t *ret_m, uint32_t *ret_n)
7025{
7026 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7027 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7028 intel_reduce_m_n_ratio(ret_m, ret_n);
7029}
7030
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007031void
7032intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7033 int pixel_clock, int link_clock,
7034 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007035{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007036 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007037
7038 compute_m_n(bits_per_pixel * pixel_clock,
7039 link_clock * nlanes * 8,
7040 &m_n->gmch_m, &m_n->gmch_n);
7041
7042 compute_m_n(pixel_clock, link_clock,
7043 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08007044}
7045
Chris Wilsona7615032011-01-12 17:04:08 +00007046static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7047{
Jani Nikulad330a952014-01-21 11:24:25 +02007048 if (i915.panel_use_ssc >= 0)
7049 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007050 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07007051 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00007052}
7053
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007054static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007055{
Daniel Vetter7df00d72013-05-21 21:54:55 +02007056 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007057}
Daniel Vetterf47709a2013-03-28 10:42:02 +01007058
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007059static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7060{
7061 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007062}
7063
Daniel Vetterf47709a2013-03-28 10:42:02 +01007064static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007065 struct intel_crtc_state *crtc_state,
Jesse Barnesa7516a02011-12-15 12:30:37 -08007066 intel_clock_t *reduced_clock)
7067{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007068 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007069 u32 fp, fp2 = 0;
7070
7071 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007072 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007073 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007074 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007075 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007076 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007077 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007078 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007079 }
7080
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007081 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007082
Daniel Vetterf47709a2013-03-28 10:42:02 +01007083 crtc->lowfreq_avail = false;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007084 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07007085 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007086 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007087 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007088 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007089 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007090 }
7091}
7092
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007093static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7094 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07007095{
7096 u32 reg_val;
7097
7098 /*
7099 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7100 * and set it to a reasonable value instead.
7101 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007102 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007103 reg_val &= 0xffffff00;
7104 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007105 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007106
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007107 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007108 reg_val &= 0x8cffffff;
7109 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007110 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007111
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007112 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007113 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007114 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007115
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007116 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007117 reg_val &= 0x00ffffff;
7118 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007119 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007120}
7121
Daniel Vetterb5518422013-05-03 11:49:48 +02007122static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7123 struct intel_link_m_n *m_n)
7124{
7125 struct drm_device *dev = crtc->base.dev;
7126 struct drm_i915_private *dev_priv = dev->dev_private;
7127 int pipe = crtc->pipe;
7128
Daniel Vettere3b95f12013-05-03 11:49:49 +02007129 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7130 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7131 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7132 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007133}
7134
7135static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07007136 struct intel_link_m_n *m_n,
7137 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02007138{
7139 struct drm_device *dev = crtc->base.dev;
7140 struct drm_i915_private *dev_priv = dev->dev_private;
7141 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007142 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02007143
7144 if (INTEL_INFO(dev)->gen >= 5) {
7145 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7146 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7147 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7148 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07007149 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7150 * for gen < 8) and if DRRS is supported (to make sure the
7151 * registers are not unnecessarily accessed).
7152 */
Durgadoss R44395bf2015-02-13 15:33:02 +05307153 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007154 crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07007155 I915_WRITE(PIPE_DATA_M2(transcoder),
7156 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7157 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7158 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7159 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7160 }
Daniel Vetterb5518422013-05-03 11:49:48 +02007161 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02007162 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7163 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7164 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7165 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007166 }
7167}
7168
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307169void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007170{
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307171 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7172
7173 if (m_n == M1_N1) {
7174 dp_m_n = &crtc->config->dp_m_n;
7175 dp_m2_n2 = &crtc->config->dp_m2_n2;
7176 } else if (m_n == M2_N2) {
7177
7178 /*
7179 * M2_N2 registers are not supported. Hence m2_n2 divider value
7180 * needs to be programmed into M1_N1.
7181 */
7182 dp_m_n = &crtc->config->dp_m2_n2;
7183 } else {
7184 DRM_ERROR("Unsupported divider value\n");
7185 return;
7186 }
7187
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007188 if (crtc->config->has_pch_encoder)
7189 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007190 else
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307191 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007192}
7193
Daniel Vetter251ac862015-06-18 10:30:24 +02007194static void vlv_compute_dpll(struct intel_crtc *crtc,
7195 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007196{
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007197 pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007198 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007199 if (crtc->pipe != PIPE_A)
7200 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007201
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007202 /* DPLL not used with DSI, but still need the rest set up */
Ville Syrjälä187a1c02016-04-18 20:34:04 +03007203 if (!pipe_config->has_dsi_encoder)
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007204 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
7205 DPLL_EXT_BUFFER_ENABLE_VLV;
7206
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007207 pipe_config->dpll_hw_state.dpll_md =
7208 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
7209}
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007210
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007211static void chv_compute_dpll(struct intel_crtc *crtc,
7212 struct intel_crtc_state *pipe_config)
7213{
7214 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007215 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007216 if (crtc->pipe != PIPE_A)
7217 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7218
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007219 /* DPLL not used with DSI, but still need the rest set up */
Ville Syrjälä187a1c02016-04-18 20:34:04 +03007220 if (!pipe_config->has_dsi_encoder)
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007221 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
7222
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007223 pipe_config->dpll_hw_state.dpll_md =
7224 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007225}
7226
Ville Syrjäläd288f652014-10-28 13:20:22 +02007227static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007228 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007229{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007230 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007231 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007232 enum pipe pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007233 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007234 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007235 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007236
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007237 /* Enable Refclk */
7238 I915_WRITE(DPLL(pipe),
7239 pipe_config->dpll_hw_state.dpll &
7240 ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
7241
7242 /* No need to actually set up the DPLL with DSI */
7243 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7244 return;
7245
Ville Syrjäläa5805162015-05-26 20:42:30 +03007246 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01007247
Ville Syrjäläd288f652014-10-28 13:20:22 +02007248 bestn = pipe_config->dpll.n;
7249 bestm1 = pipe_config->dpll.m1;
7250 bestm2 = pipe_config->dpll.m2;
7251 bestp1 = pipe_config->dpll.p1;
7252 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007253
Jesse Barnes89b667f2013-04-18 14:51:36 -07007254 /* See eDP HDMI DPIO driver vbios notes doc */
7255
7256 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007257 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007258 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007259
7260 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007261 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007262
7263 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007264 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007265 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007266 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007267
7268 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007269 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007270
7271 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007272 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7273 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7274 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007275 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07007276
7277 /*
7278 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7279 * but we don't support that).
7280 * Note: don't use the DAC post divider as it seems unstable.
7281 */
7282 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007283 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007284
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007285 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007286 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007287
Jesse Barnes89b667f2013-04-18 14:51:36 -07007288 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02007289 if (pipe_config->port_clock == 162000 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007290 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
7291 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007292 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b01202013-07-05 19:21:38 +03007293 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007294 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007295 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007296 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007297
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02007298 if (pipe_config->has_dp_encoder) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07007299 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007300 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007301 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007302 0x0df40000);
7303 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007304 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007305 0x0df70000);
7306 } else { /* HDMI or VGA */
7307 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007308 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007309 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007310 0x0df70000);
7311 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007312 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007313 0x0df40000);
7314 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007315
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007316 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007317 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007318 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
7319 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
Jesse Barnes89b667f2013-04-18 14:51:36 -07007320 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007321 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007322
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007323 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Ville Syrjäläa5805162015-05-26 20:42:30 +03007324 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007325}
7326
Ville Syrjäläd288f652014-10-28 13:20:22 +02007327static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007328 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007329{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007330 struct drm_device *dev = crtc->base.dev;
7331 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007332 enum pipe pipe = crtc->pipe;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007333 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307334 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007335 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307336 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307337 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007338
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007339 /* Enable Refclk and SSC */
7340 I915_WRITE(DPLL(pipe),
7341 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
7342
7343 /* No need to actually set up the DPLL with DSI */
7344 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7345 return;
7346
Ville Syrjäläd288f652014-10-28 13:20:22 +02007347 bestn = pipe_config->dpll.n;
7348 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7349 bestm1 = pipe_config->dpll.m1;
7350 bestm2 = pipe_config->dpll.m2 >> 22;
7351 bestp1 = pipe_config->dpll.p1;
7352 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307353 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307354 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307355 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007356
Ville Syrjäläa5805162015-05-26 20:42:30 +03007357 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007358
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007359 /* p1 and p2 divider */
7360 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7361 5 << DPIO_CHV_S1_DIV_SHIFT |
7362 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7363 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7364 1 << DPIO_CHV_K_DIV_SHIFT);
7365
7366 /* Feedback post-divider - m2 */
7367 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7368
7369 /* Feedback refclk divider - n and m1 */
7370 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7371 DPIO_CHV_M1_DIV_BY_2 |
7372 1 << DPIO_CHV_N_DIV_SHIFT);
7373
7374 /* M2 fraction division */
Ville Syrjälä25a25df2015-07-08 23:45:47 +03007375 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007376
7377 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307378 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7379 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7380 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7381 if (bestm2_frac)
7382 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7383 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007384
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05307385 /* Program digital lock detect threshold */
7386 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7387 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7388 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7389 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7390 if (!bestm2_frac)
7391 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7392 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7393
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007394 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307395 if (vco == 5400000) {
7396 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7397 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7398 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7399 tribuf_calcntr = 0x9;
7400 } else if (vco <= 6200000) {
7401 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7402 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7403 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7404 tribuf_calcntr = 0x9;
7405 } else if (vco <= 6480000) {
7406 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7407 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7408 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7409 tribuf_calcntr = 0x8;
7410 } else {
7411 /* Not supported. Apply the same limits as in the max case */
7412 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7413 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7414 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7415 tribuf_calcntr = 0;
7416 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007417 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7418
Ville Syrjälä968040b2015-03-11 22:52:08 +02007419 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307420 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7421 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7422 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7423
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007424 /* AFC Recal */
7425 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7426 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7427 DPIO_AFC_RECAL);
7428
Ville Syrjäläa5805162015-05-26 20:42:30 +03007429 mutex_unlock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007430}
7431
Ville Syrjäläd288f652014-10-28 13:20:22 +02007432/**
7433 * vlv_force_pll_on - forcibly enable just the PLL
7434 * @dev_priv: i915 private structure
7435 * @pipe: pipe PLL to enable
7436 * @dpll: PLL configuration
7437 *
7438 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7439 * in cases where we need the PLL enabled even when @pipe is not going to
7440 * be enabled.
7441 */
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007442int vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7443 const struct dpll *dpll)
Ville Syrjäläd288f652014-10-28 13:20:22 +02007444{
7445 struct intel_crtc *crtc =
7446 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007447 struct intel_crtc_state *pipe_config;
7448
7449 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
7450 if (!pipe_config)
7451 return -ENOMEM;
7452
7453 pipe_config->base.crtc = &crtc->base;
7454 pipe_config->pixel_multiplier = 1;
7455 pipe_config->dpll = *dpll;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007456
7457 if (IS_CHERRYVIEW(dev)) {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007458 chv_compute_dpll(crtc, pipe_config);
7459 chv_prepare_pll(crtc, pipe_config);
7460 chv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007461 } else {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007462 vlv_compute_dpll(crtc, pipe_config);
7463 vlv_prepare_pll(crtc, pipe_config);
7464 vlv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007465 }
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007466
7467 kfree(pipe_config);
7468
7469 return 0;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007470}
7471
7472/**
7473 * vlv_force_pll_off - forcibly disable just the PLL
7474 * @dev_priv: i915 private structure
7475 * @pipe: pipe PLL to disable
7476 *
7477 * Disable the PLL for @pipe. To be used in cases where we need
7478 * the PLL enabled even when @pipe is not going to be enabled.
7479 */
7480void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7481{
7482 if (IS_CHERRYVIEW(dev))
7483 chv_disable_pll(to_i915(dev), pipe);
7484 else
7485 vlv_disable_pll(to_i915(dev), pipe);
7486}
7487
Daniel Vetter251ac862015-06-18 10:30:24 +02007488static void i9xx_compute_dpll(struct intel_crtc *crtc,
7489 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007490 intel_clock_t *reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007491{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007492 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007493 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007494 u32 dpll;
7495 bool is_sdvo;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007496 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007497
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007498 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307499
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007500 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7501 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007502
7503 dpll = DPLL_VGA_MODE_DIS;
7504
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007505 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007506 dpll |= DPLLB_MODE_LVDS;
7507 else
7508 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007509
Daniel Vetteref1b4602013-06-01 17:17:04 +02007510 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007511 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02007512 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007513 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02007514
7515 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007516 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007517
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007518 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007519 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007520
7521 /* compute bitmask from p1 value */
7522 if (IS_PINEVIEW(dev))
7523 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7524 else {
7525 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7526 if (IS_G4X(dev) && reduced_clock)
7527 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7528 }
7529 switch (clock->p2) {
7530 case 5:
7531 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7532 break;
7533 case 7:
7534 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7535 break;
7536 case 10:
7537 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7538 break;
7539 case 14:
7540 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7541 break;
7542 }
7543 if (INTEL_INFO(dev)->gen >= 4)
7544 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7545
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007546 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007547 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007548 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007549 intel_panel_use_ssc(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007550 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7551 else
7552 dpll |= PLL_REF_INPUT_DREFCLK;
7553
7554 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007555 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007556
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007557 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007558 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007559 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007560 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007561 }
7562}
7563
Daniel Vetter251ac862015-06-18 10:30:24 +02007564static void i8xx_compute_dpll(struct intel_crtc *crtc,
7565 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007566 intel_clock_t *reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007567{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007568 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007569 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007570 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007571 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007572
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007573 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307574
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007575 dpll = DPLL_VGA_MODE_DIS;
7576
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007577 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007578 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7579 } else {
7580 if (clock->p1 == 2)
7581 dpll |= PLL_P1_DIVIDE_BY_TWO;
7582 else
7583 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7584 if (clock->p2 == 4)
7585 dpll |= PLL_P2_DIVIDE_BY_4;
7586 }
7587
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007588 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007589 dpll |= DPLL_DVO_2X_MODE;
7590
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007591 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007592 intel_panel_use_ssc(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007593 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7594 else
7595 dpll |= PLL_REF_INPUT_DREFCLK;
7596
7597 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007598 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007599}
7600
Daniel Vetter8a654f32013-06-01 17:16:22 +02007601static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007602{
7603 struct drm_device *dev = intel_crtc->base.dev;
7604 struct drm_i915_private *dev_priv = dev->dev_private;
7605 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007606 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03007607 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007608 uint32_t crtc_vtotal, crtc_vblank_end;
7609 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007610
7611 /* We need to be careful not to changed the adjusted mode, for otherwise
7612 * the hw state checker will get angry at the mismatch. */
7613 crtc_vtotal = adjusted_mode->crtc_vtotal;
7614 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007615
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007616 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007617 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007618 crtc_vtotal -= 1;
7619 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007620
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007621 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007622 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7623 else
7624 vsyncshift = adjusted_mode->crtc_hsync_start -
7625 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007626 if (vsyncshift < 0)
7627 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007628 }
7629
7630 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007631 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007632
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007633 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007634 (adjusted_mode->crtc_hdisplay - 1) |
7635 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007636 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007637 (adjusted_mode->crtc_hblank_start - 1) |
7638 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007639 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007640 (adjusted_mode->crtc_hsync_start - 1) |
7641 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7642
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007643 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007644 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007645 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007646 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007647 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007648 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007649 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007650 (adjusted_mode->crtc_vsync_start - 1) |
7651 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7652
Paulo Zanonib5e508d2012-10-24 11:34:43 -02007653 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7654 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7655 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7656 * bits. */
7657 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7658 (pipe == PIPE_B || pipe == PIPE_C))
7659 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7660
Jani Nikulabc58be62016-03-18 17:05:39 +02007661}
7662
7663static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
7664{
7665 struct drm_device *dev = intel_crtc->base.dev;
7666 struct drm_i915_private *dev_priv = dev->dev_private;
7667 enum pipe pipe = intel_crtc->pipe;
7668
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007669 /* pipesrc controls the size that is scaled from, which should
7670 * always be the user's requested size.
7671 */
7672 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007673 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7674 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007675}
7676
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007677static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007678 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007679{
7680 struct drm_device *dev = crtc->base.dev;
7681 struct drm_i915_private *dev_priv = dev->dev_private;
7682 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7683 uint32_t tmp;
7684
7685 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007686 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7687 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007688 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007689 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7690 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007691 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007692 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7693 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007694
7695 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007696 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7697 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007698 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007699 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7700 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007701 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007702 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7703 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007704
7705 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007706 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7707 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7708 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007709 }
Jani Nikulabc58be62016-03-18 17:05:39 +02007710}
7711
7712static void intel_get_pipe_src_size(struct intel_crtc *crtc,
7713 struct intel_crtc_state *pipe_config)
7714{
7715 struct drm_device *dev = crtc->base.dev;
7716 struct drm_i915_private *dev_priv = dev->dev_private;
7717 u32 tmp;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007718
7719 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03007720 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7721 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7722
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007723 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7724 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007725}
7726
Daniel Vetterf6a83282014-02-11 15:28:57 -08007727void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007728 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03007729{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007730 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7731 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7732 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7733 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007734
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007735 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7736 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7737 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7738 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007739
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007740 mode->flags = pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007741 mode->type = DRM_MODE_TYPE_DRIVER;
Jesse Barnesbabea612013-06-26 18:57:38 +03007742
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007743 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7744 mode->flags |= pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007745
7746 mode->hsync = drm_mode_hsync(mode);
7747 mode->vrefresh = drm_mode_vrefresh(mode);
7748 drm_mode_set_name(mode);
Jesse Barnesbabea612013-06-26 18:57:38 +03007749}
7750
Daniel Vetter84b046f2013-02-19 18:48:54 +01007751static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7752{
7753 struct drm_device *dev = intel_crtc->base.dev;
7754 struct drm_i915_private *dev_priv = dev->dev_private;
7755 uint32_t pipeconf;
7756
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007757 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007758
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03007759 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7760 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7761 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02007762
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007763 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007764 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007765
Daniel Vetterff9ce462013-04-24 14:57:17 +02007766 /* only g4x and later have fancy bpc/dither controls */
Wayne Boyer666a4532015-12-09 12:29:35 -08007767 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007768 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007769 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02007770 pipeconf |= PIPECONF_DITHER_EN |
7771 PIPECONF_DITHER_TYPE_SP;
7772
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007773 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007774 case 18:
7775 pipeconf |= PIPECONF_6BPC;
7776 break;
7777 case 24:
7778 pipeconf |= PIPECONF_8BPC;
7779 break;
7780 case 30:
7781 pipeconf |= PIPECONF_10BPC;
7782 break;
7783 default:
7784 /* Case prevented by intel_choose_pipe_bpp_dither. */
7785 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01007786 }
7787 }
7788
7789 if (HAS_PIPE_CXSR(dev)) {
7790 if (intel_crtc->lowfreq_avail) {
7791 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7792 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7793 } else {
7794 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01007795 }
7796 }
7797
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007798 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007799 if (INTEL_INFO(dev)->gen < 4 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007800 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007801 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7802 else
7803 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7804 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01007805 pipeconf |= PIPECONF_PROGRESSIVE;
7806
Wayne Boyer666a4532015-12-09 12:29:35 -08007807 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
7808 intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007809 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03007810
Daniel Vetter84b046f2013-02-19 18:48:54 +01007811 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7812 POSTING_READ(PIPECONF(intel_crtc->pipe));
7813}
7814
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007815static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
7816 struct intel_crtc_state *crtc_state)
7817{
7818 struct drm_device *dev = crtc->base.dev;
7819 struct drm_i915_private *dev_priv = dev->dev_private;
7820 const intel_limit_t *limit;
7821 int refclk = 48000;
7822
7823 memset(&crtc_state->dpll_hw_state, 0,
7824 sizeof(crtc_state->dpll_hw_state));
7825
7826 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7827 if (intel_panel_use_ssc(dev_priv)) {
7828 refclk = dev_priv->vbt.lvds_ssc_freq;
7829 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7830 }
7831
7832 limit = &intel_limits_i8xx_lvds;
7833 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO)) {
7834 limit = &intel_limits_i8xx_dvo;
7835 } else {
7836 limit = &intel_limits_i8xx_dac;
7837 }
7838
7839 if (!crtc_state->clock_set &&
7840 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7841 refclk, NULL, &crtc_state->dpll)) {
7842 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7843 return -EINVAL;
7844 }
7845
7846 i8xx_compute_dpll(crtc, crtc_state, NULL);
7847
7848 return 0;
7849}
7850
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007851static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
7852 struct intel_crtc_state *crtc_state)
7853{
7854 struct drm_device *dev = crtc->base.dev;
7855 struct drm_i915_private *dev_priv = dev->dev_private;
7856 const intel_limit_t *limit;
7857 int refclk = 96000;
7858
7859 memset(&crtc_state->dpll_hw_state, 0,
7860 sizeof(crtc_state->dpll_hw_state));
7861
7862 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7863 if (intel_panel_use_ssc(dev_priv)) {
7864 refclk = dev_priv->vbt.lvds_ssc_freq;
7865 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7866 }
7867
7868 if (intel_is_dual_link_lvds(dev))
7869 limit = &intel_limits_g4x_dual_channel_lvds;
7870 else
7871 limit = &intel_limits_g4x_single_channel_lvds;
7872 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
7873 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
7874 limit = &intel_limits_g4x_hdmi;
7875 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
7876 limit = &intel_limits_g4x_sdvo;
7877 } else {
7878 /* The option is for other outputs */
7879 limit = &intel_limits_i9xx_sdvo;
7880 }
7881
7882 if (!crtc_state->clock_set &&
7883 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7884 refclk, NULL, &crtc_state->dpll)) {
7885 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7886 return -EINVAL;
7887 }
7888
7889 i9xx_compute_dpll(crtc, crtc_state, NULL);
7890
7891 return 0;
7892}
7893
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007894static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
7895 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08007896{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007897 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007898 struct drm_i915_private *dev_priv = dev->dev_private;
Ma Lingd4906092009-03-18 20:13:27 +08007899 const intel_limit_t *limit;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007900 int refclk = 96000;
Jesse Barnes79e53942008-11-07 14:24:08 -08007901
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03007902 memset(&crtc_state->dpll_hw_state, 0,
7903 sizeof(crtc_state->dpll_hw_state));
7904
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007905 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7906 if (intel_panel_use_ssc(dev_priv)) {
7907 refclk = dev_priv->vbt.lvds_ssc_freq;
7908 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7909 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007910
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007911 limit = &intel_limits_pineview_lvds;
7912 } else {
7913 limit = &intel_limits_pineview_sdvo;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007914 }
Jani Nikulaf2335332013-09-13 11:03:09 +03007915
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007916 if (!crtc_state->clock_set &&
7917 !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7918 refclk, NULL, &crtc_state->dpll)) {
7919 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7920 return -EINVAL;
7921 }
7922
7923 i9xx_compute_dpll(crtc, crtc_state, NULL);
7924
7925 return 0;
7926}
7927
7928static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7929 struct intel_crtc_state *crtc_state)
7930{
7931 struct drm_device *dev = crtc->base.dev;
7932 struct drm_i915_private *dev_priv = dev->dev_private;
7933 const intel_limit_t *limit;
7934 int refclk = 96000;
7935
7936 memset(&crtc_state->dpll_hw_state, 0,
7937 sizeof(crtc_state->dpll_hw_state));
7938
7939 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7940 if (intel_panel_use_ssc(dev_priv)) {
7941 refclk = dev_priv->vbt.lvds_ssc_freq;
7942 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007943 }
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007944
7945 limit = &intel_limits_i9xx_lvds;
7946 } else {
7947 limit = &intel_limits_i9xx_sdvo;
7948 }
7949
7950 if (!crtc_state->clock_set &&
7951 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7952 refclk, NULL, &crtc_state->dpll)) {
7953 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7954 return -EINVAL;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007955 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007956
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007957 i9xx_compute_dpll(crtc, crtc_state, NULL);
Eric Anholtf564048e2011-03-30 13:01:02 -07007958
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007959 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07007960}
7961
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02007962static int chv_crtc_compute_clock(struct intel_crtc *crtc,
7963 struct intel_crtc_state *crtc_state)
7964{
7965 int refclk = 100000;
7966 const intel_limit_t *limit = &intel_limits_chv;
7967
7968 memset(&crtc_state->dpll_hw_state, 0,
7969 sizeof(crtc_state->dpll_hw_state));
7970
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02007971 if (!crtc_state->clock_set &&
7972 !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7973 refclk, NULL, &crtc_state->dpll)) {
7974 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7975 return -EINVAL;
7976 }
7977
7978 chv_compute_dpll(crtc, crtc_state);
7979
7980 return 0;
7981}
7982
7983static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
7984 struct intel_crtc_state *crtc_state)
7985{
7986 int refclk = 100000;
7987 const intel_limit_t *limit = &intel_limits_vlv;
7988
7989 memset(&crtc_state->dpll_hw_state, 0,
7990 sizeof(crtc_state->dpll_hw_state));
7991
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02007992 if (!crtc_state->clock_set &&
7993 !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7994 refclk, NULL, &crtc_state->dpll)) {
7995 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7996 return -EINVAL;
7997 }
7998
7999 vlv_compute_dpll(crtc, crtc_state);
8000
8001 return 0;
8002}
8003
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008004static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008005 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008006{
8007 struct drm_device *dev = crtc->base.dev;
8008 struct drm_i915_private *dev_priv = dev->dev_private;
8009 uint32_t tmp;
8010
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02008011 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
8012 return;
8013
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008014 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02008015 if (!(tmp & PFIT_ENABLE))
8016 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008017
Daniel Vetter06922822013-07-11 13:35:40 +02008018 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008019 if (INTEL_INFO(dev)->gen < 4) {
8020 if (crtc->pipe != PIPE_B)
8021 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008022 } else {
8023 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
8024 return;
8025 }
8026
Daniel Vetter06922822013-07-11 13:35:40 +02008027 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008028 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
8029 if (INTEL_INFO(dev)->gen < 5)
8030 pipe_config->gmch_pfit.lvds_border_bits =
8031 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
8032}
8033
Jesse Barnesacbec812013-09-20 11:29:32 -07008034static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008035 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07008036{
8037 struct drm_device *dev = crtc->base.dev;
8038 struct drm_i915_private *dev_priv = dev->dev_private;
8039 int pipe = pipe_config->cpu_transcoder;
8040 intel_clock_t clock;
8041 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07008042 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07008043
Ville Syrjäläb5219732016-03-15 16:40:01 +02008044 /* In case of DSI, DPLL will not be used */
8045 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
Shobhit Kumarf573de52014-07-30 20:32:37 +05308046 return;
8047
Ville Syrjäläa5805162015-05-26 20:42:30 +03008048 mutex_lock(&dev_priv->sb_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08008049 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008050 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesacbec812013-09-20 11:29:32 -07008051
8052 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
8053 clock.m2 = mdiv & DPIO_M2DIV_MASK;
8054 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
8055 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
8056 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
8057
Imre Deakdccbea32015-06-22 23:35:51 +03008058 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07008059}
8060
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008061static void
8062i9xx_get_initial_plane_config(struct intel_crtc *crtc,
8063 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008064{
8065 struct drm_device *dev = crtc->base.dev;
8066 struct drm_i915_private *dev_priv = dev->dev_private;
8067 u32 val, base, offset;
8068 int pipe = crtc->pipe, plane = crtc->plane;
8069 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008070 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008071 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008072 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008073
Damien Lespiau42a7b082015-02-05 19:35:13 +00008074 val = I915_READ(DSPCNTR(plane));
8075 if (!(val & DISPLAY_PLANE_ENABLE))
8076 return;
8077
Damien Lespiaud9806c92015-01-21 14:07:19 +00008078 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008079 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008080 DRM_DEBUG_KMS("failed to alloc fb\n");
8081 return;
8082 }
8083
Damien Lespiau1b842c82015-01-21 13:50:54 +00008084 fb = &intel_fb->base;
8085
Daniel Vetter18c52472015-02-10 17:16:09 +00008086 if (INTEL_INFO(dev)->gen >= 4) {
8087 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008088 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00008089 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8090 }
8091 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008092
8093 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00008094 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008095 fb->pixel_format = fourcc;
8096 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008097
8098 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008099 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008100 offset = I915_READ(DSPTILEOFF(plane));
8101 else
8102 offset = I915_READ(DSPLINOFF(plane));
8103 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8104 } else {
8105 base = I915_READ(DSPADDR(plane));
8106 }
8107 plane_config->base = base;
8108
8109 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008110 fb->width = ((val >> 16) & 0xfff) + 1;
8111 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008112
8113 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008114 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008115
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008116 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008117 fb->pixel_format,
8118 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008119
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008120 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008121
Damien Lespiau2844a922015-01-20 12:51:48 +00008122 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8123 pipe_name(pipe), plane, fb->width, fb->height,
8124 fb->bits_per_pixel, base, fb->pitches[0],
8125 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008126
Damien Lespiau2d140302015-02-05 17:22:18 +00008127 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008128}
8129
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008130static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008131 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008132{
8133 struct drm_device *dev = crtc->base.dev;
8134 struct drm_i915_private *dev_priv = dev->dev_private;
8135 int pipe = pipe_config->cpu_transcoder;
8136 enum dpio_channel port = vlv_pipe_to_channel(pipe);
8137 intel_clock_t clock;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008138 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008139 int refclk = 100000;
8140
Ville Syrjäläb5219732016-03-15 16:40:01 +02008141 /* In case of DSI, DPLL will not be used */
8142 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
8143 return;
8144
Ville Syrjäläa5805162015-05-26 20:42:30 +03008145 mutex_lock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008146 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8147 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8148 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8149 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
Imre Deak0d7b6b12015-07-02 14:29:58 +03008150 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008151 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008152
8153 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008154 clock.m2 = (pll_dw0 & 0xff) << 22;
8155 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
8156 clock.m2 |= pll_dw2 & 0x3fffff;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008157 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8158 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8159 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8160
Imre Deakdccbea32015-06-22 23:35:51 +03008161 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008162}
8163
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008164static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008165 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008166{
8167 struct drm_device *dev = crtc->base.dev;
8168 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak17290502016-02-12 18:55:11 +02008169 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008170 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02008171 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008172
Imre Deak17290502016-02-12 18:55:11 +02008173 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8174 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +02008175 return false;
8176
Daniel Vettere143a212013-07-04 12:01:15 +02008177 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008178 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02008179
Imre Deak17290502016-02-12 18:55:11 +02008180 ret = false;
8181
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008182 tmp = I915_READ(PIPECONF(crtc->pipe));
8183 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02008184 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008185
Wayne Boyer666a4532015-12-09 12:29:35 -08008186 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008187 switch (tmp & PIPECONF_BPC_MASK) {
8188 case PIPECONF_6BPC:
8189 pipe_config->pipe_bpp = 18;
8190 break;
8191 case PIPECONF_8BPC:
8192 pipe_config->pipe_bpp = 24;
8193 break;
8194 case PIPECONF_10BPC:
8195 pipe_config->pipe_bpp = 30;
8196 break;
8197 default:
8198 break;
8199 }
8200 }
8201
Wayne Boyer666a4532015-12-09 12:29:35 -08008202 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
8203 (tmp & PIPECONF_COLOR_RANGE_SELECT))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008204 pipe_config->limited_color_range = true;
8205
Ville Syrjälä282740f2013-09-04 18:30:03 +03008206 if (INTEL_INFO(dev)->gen < 4)
8207 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8208
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008209 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +02008210 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008211
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008212 i9xx_get_pfit_config(crtc, pipe_config);
8213
Daniel Vetter6c49f242013-06-06 12:45:25 +02008214 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjäläc2317752016-03-15 16:39:56 +02008215 /* No way to read it out on pipes B and C */
8216 if (IS_CHERRYVIEW(dev) && crtc->pipe != PIPE_A)
8217 tmp = dev_priv->chv_dpll_md[crtc->pipe];
8218 else
8219 tmp = I915_READ(DPLL_MD(crtc->pipe));
Daniel Vetter6c49f242013-06-06 12:45:25 +02008220 pipe_config->pixel_multiplier =
8221 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8222 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008223 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02008224 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8225 tmp = I915_READ(DPLL(crtc->pipe));
8226 pipe_config->pixel_multiplier =
8227 ((tmp & SDVO_MULTIPLIER_MASK)
8228 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8229 } else {
8230 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8231 * port and will be fixed up in the encoder->get_config
8232 * function. */
8233 pipe_config->pixel_multiplier = 1;
8234 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008235 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
Wayne Boyer666a4532015-12-09 12:29:35 -08008236 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03008237 /*
8238 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8239 * on 830. Filter it out here so that we don't
8240 * report errors due to that.
8241 */
8242 if (IS_I830(dev))
8243 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8244
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008245 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8246 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03008247 } else {
8248 /* Mask out read-only status bits. */
8249 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8250 DPLL_PORTC_READY_MASK |
8251 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008252 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02008253
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008254 if (IS_CHERRYVIEW(dev))
8255 chv_crtc_clock_get(crtc, pipe_config);
8256 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07008257 vlv_crtc_clock_get(crtc, pipe_config);
8258 else
8259 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03008260
Ville Syrjälä0f646142015-08-26 19:39:18 +03008261 /*
8262 * Normally the dotclock is filled in by the encoder .get_config()
8263 * but in case the pipe is enabled w/o any ports we need a sane
8264 * default.
8265 */
8266 pipe_config->base.adjusted_mode.crtc_clock =
8267 pipe_config->port_clock / pipe_config->pixel_multiplier;
8268
Imre Deak17290502016-02-12 18:55:11 +02008269 ret = true;
8270
8271out:
8272 intel_display_power_put(dev_priv, power_domain);
8273
8274 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008275}
8276
Paulo Zanonidde86e22012-12-01 12:04:25 -02008277static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07008278{
8279 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008280 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008281 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008282 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008283 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008284 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07008285 bool has_ck505 = false;
8286 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008287
8288 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01008289 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07008290 switch (encoder->type) {
8291 case INTEL_OUTPUT_LVDS:
8292 has_panel = true;
8293 has_lvds = true;
8294 break;
8295 case INTEL_OUTPUT_EDP:
8296 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03008297 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07008298 has_cpu_edp = true;
8299 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008300 default:
8301 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008302 }
8303 }
8304
Keith Packard99eb6a02011-09-26 14:29:12 -07008305 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008306 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07008307 can_ssc = has_ck505;
8308 } else {
8309 has_ck505 = false;
8310 can_ssc = true;
8311 }
8312
Imre Deak2de69052013-05-08 13:14:04 +03008313 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
8314 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008315
8316 /* Ironlake: try to setup display ref clock before DPLL
8317 * enabling. This is only under driver's control after
8318 * PCH B stepping, previous chipset stepping should be
8319 * ignoring this setting.
8320 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008321 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008322
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008323 /* As we must carefully and slowly disable/enable each source in turn,
8324 * compute the final state we want first and check if we need to
8325 * make any changes at all.
8326 */
8327 final = val;
8328 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07008329 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008330 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07008331 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008332 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8333
8334 final &= ~DREF_SSC_SOURCE_MASK;
8335 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8336 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008337
Keith Packard199e5d72011-09-22 12:01:57 -07008338 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008339 final |= DREF_SSC_SOURCE_ENABLE;
8340
8341 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8342 final |= DREF_SSC1_ENABLE;
8343
8344 if (has_cpu_edp) {
8345 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8346 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8347 else
8348 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8349 } else
8350 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8351 } else {
8352 final |= DREF_SSC_SOURCE_DISABLE;
8353 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8354 }
8355
8356 if (final == val)
8357 return;
8358
8359 /* Always enable nonspread source */
8360 val &= ~DREF_NONSPREAD_SOURCE_MASK;
8361
8362 if (has_ck505)
8363 val |= DREF_NONSPREAD_CK505_ENABLE;
8364 else
8365 val |= DREF_NONSPREAD_SOURCE_ENABLE;
8366
8367 if (has_panel) {
8368 val &= ~DREF_SSC_SOURCE_MASK;
8369 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008370
Keith Packard199e5d72011-09-22 12:01:57 -07008371 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07008372 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008373 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008374 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02008375 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008376 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008377
8378 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008379 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008380 POSTING_READ(PCH_DREF_CONTROL);
8381 udelay(200);
8382
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008383 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008384
8385 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07008386 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07008387 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008388 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008389 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02008390 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008391 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07008392 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008393 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008394
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008395 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008396 POSTING_READ(PCH_DREF_CONTROL);
8397 udelay(200);
8398 } else {
8399 DRM_DEBUG_KMS("Disabling SSC entirely\n");
8400
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008401 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07008402
8403 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008404 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008405
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008406 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008407 POSTING_READ(PCH_DREF_CONTROL);
8408 udelay(200);
8409
8410 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008411 val &= ~DREF_SSC_SOURCE_MASK;
8412 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008413
8414 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008415 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008416
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008417 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008418 POSTING_READ(PCH_DREF_CONTROL);
8419 udelay(200);
8420 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008421
8422 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008423}
8424
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008425static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02008426{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008427 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008428
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008429 tmp = I915_READ(SOUTH_CHICKEN2);
8430 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8431 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008432
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008433 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
8434 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
8435 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02008436
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008437 tmp = I915_READ(SOUTH_CHICKEN2);
8438 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8439 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008440
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008441 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
8442 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
8443 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008444}
8445
8446/* WaMPhyProgramming:hsw */
8447static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8448{
8449 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008450
8451 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8452 tmp &= ~(0xFF << 24);
8453 tmp |= (0x12 << 24);
8454 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8455
Paulo Zanonidde86e22012-12-01 12:04:25 -02008456 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8457 tmp |= (1 << 11);
8458 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8459
8460 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8461 tmp |= (1 << 11);
8462 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8463
Paulo Zanonidde86e22012-12-01 12:04:25 -02008464 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8465 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8466 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8467
8468 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8469 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8470 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8471
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008472 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8473 tmp &= ~(7 << 13);
8474 tmp |= (5 << 13);
8475 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008476
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008477 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8478 tmp &= ~(7 << 13);
8479 tmp |= (5 << 13);
8480 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008481
8482 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8483 tmp &= ~0xFF;
8484 tmp |= 0x1C;
8485 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8486
8487 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8488 tmp &= ~0xFF;
8489 tmp |= 0x1C;
8490 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8491
8492 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8493 tmp &= ~(0xFF << 16);
8494 tmp |= (0x1C << 16);
8495 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8496
8497 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8498 tmp &= ~(0xFF << 16);
8499 tmp |= (0x1C << 16);
8500 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8501
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008502 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8503 tmp |= (1 << 27);
8504 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008505
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008506 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8507 tmp |= (1 << 27);
8508 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008509
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008510 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8511 tmp &= ~(0xF << 28);
8512 tmp |= (4 << 28);
8513 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008514
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008515 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8516 tmp &= ~(0xF << 28);
8517 tmp |= (4 << 28);
8518 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008519}
8520
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008521/* Implements 3 different sequences from BSpec chapter "Display iCLK
8522 * Programming" based on the parameters passed:
8523 * - Sequence to enable CLKOUT_DP
8524 * - Sequence to enable CLKOUT_DP without spread
8525 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8526 */
8527static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8528 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008529{
8530 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008531 uint32_t reg, tmp;
8532
8533 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8534 with_spread = true;
Ville Syrjäläc2699522015-08-27 23:55:59 +03008535 if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008536 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008537
Ville Syrjäläa5805162015-05-26 20:42:30 +03008538 mutex_lock(&dev_priv->sb_lock);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008539
8540 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8541 tmp &= ~SBI_SSCCTL_DISABLE;
8542 tmp |= SBI_SSCCTL_PATHALT;
8543 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8544
8545 udelay(24);
8546
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008547 if (with_spread) {
8548 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8549 tmp &= ~SBI_SSCCTL_PATHALT;
8550 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008551
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008552 if (with_fdi) {
8553 lpt_reset_fdi_mphy(dev_priv);
8554 lpt_program_fdi_mphy(dev_priv);
8555 }
8556 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02008557
Ville Syrjäläc2699522015-08-27 23:55:59 +03008558 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008559 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8560 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8561 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01008562
Ville Syrjäläa5805162015-05-26 20:42:30 +03008563 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008564}
8565
Paulo Zanoni47701c32013-07-23 11:19:25 -03008566/* Sequence to disable CLKOUT_DP */
8567static void lpt_disable_clkout_dp(struct drm_device *dev)
8568{
8569 struct drm_i915_private *dev_priv = dev->dev_private;
8570 uint32_t reg, tmp;
8571
Ville Syrjäläa5805162015-05-26 20:42:30 +03008572 mutex_lock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008573
Ville Syrjäläc2699522015-08-27 23:55:59 +03008574 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni47701c32013-07-23 11:19:25 -03008575 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8576 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8577 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8578
8579 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8580 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8581 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8582 tmp |= SBI_SSCCTL_PATHALT;
8583 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8584 udelay(32);
8585 }
8586 tmp |= SBI_SSCCTL_DISABLE;
8587 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8588 }
8589
Ville Syrjäläa5805162015-05-26 20:42:30 +03008590 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008591}
8592
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008593#define BEND_IDX(steps) ((50 + (steps)) / 5)
8594
8595static const uint16_t sscdivintphase[] = {
8596 [BEND_IDX( 50)] = 0x3B23,
8597 [BEND_IDX( 45)] = 0x3B23,
8598 [BEND_IDX( 40)] = 0x3C23,
8599 [BEND_IDX( 35)] = 0x3C23,
8600 [BEND_IDX( 30)] = 0x3D23,
8601 [BEND_IDX( 25)] = 0x3D23,
8602 [BEND_IDX( 20)] = 0x3E23,
8603 [BEND_IDX( 15)] = 0x3E23,
8604 [BEND_IDX( 10)] = 0x3F23,
8605 [BEND_IDX( 5)] = 0x3F23,
8606 [BEND_IDX( 0)] = 0x0025,
8607 [BEND_IDX( -5)] = 0x0025,
8608 [BEND_IDX(-10)] = 0x0125,
8609 [BEND_IDX(-15)] = 0x0125,
8610 [BEND_IDX(-20)] = 0x0225,
8611 [BEND_IDX(-25)] = 0x0225,
8612 [BEND_IDX(-30)] = 0x0325,
8613 [BEND_IDX(-35)] = 0x0325,
8614 [BEND_IDX(-40)] = 0x0425,
8615 [BEND_IDX(-45)] = 0x0425,
8616 [BEND_IDX(-50)] = 0x0525,
8617};
8618
8619/*
8620 * Bend CLKOUT_DP
8621 * steps -50 to 50 inclusive, in steps of 5
8622 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
8623 * change in clock period = -(steps / 10) * 5.787 ps
8624 */
8625static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
8626{
8627 uint32_t tmp;
8628 int idx = BEND_IDX(steps);
8629
8630 if (WARN_ON(steps % 5 != 0))
8631 return;
8632
8633 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
8634 return;
8635
8636 mutex_lock(&dev_priv->sb_lock);
8637
8638 if (steps % 10 != 0)
8639 tmp = 0xAAAAAAAB;
8640 else
8641 tmp = 0x00000000;
8642 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
8643
8644 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
8645 tmp &= 0xffff0000;
8646 tmp |= sscdivintphase[idx];
8647 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
8648
8649 mutex_unlock(&dev_priv->sb_lock);
8650}
8651
8652#undef BEND_IDX
8653
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008654static void lpt_init_pch_refclk(struct drm_device *dev)
8655{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008656 struct intel_encoder *encoder;
8657 bool has_vga = false;
8658
Damien Lespiaub2784e12014-08-05 11:29:37 +01008659 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008660 switch (encoder->type) {
8661 case INTEL_OUTPUT_ANALOG:
8662 has_vga = true;
8663 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008664 default:
8665 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008666 }
8667 }
8668
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008669 if (has_vga) {
8670 lpt_bend_clkout_dp(to_i915(dev), 0);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008671 lpt_enable_clkout_dp(dev, true, true);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008672 } else {
Paulo Zanoni47701c32013-07-23 11:19:25 -03008673 lpt_disable_clkout_dp(dev);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008674 }
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008675}
8676
Paulo Zanonidde86e22012-12-01 12:04:25 -02008677/*
8678 * Initialize reference clocks when the driver loads
8679 */
8680void intel_init_pch_refclk(struct drm_device *dev)
8681{
8682 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8683 ironlake_init_pch_refclk(dev);
8684 else if (HAS_PCH_LPT(dev))
8685 lpt_init_pch_refclk(dev);
8686}
8687
Daniel Vetter6ff93602013-04-19 11:24:36 +02008688static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03008689{
8690 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8691 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8692 int pipe = intel_crtc->pipe;
8693 uint32_t val;
8694
Daniel Vetter78114072013-06-13 00:54:57 +02008695 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03008696
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008697 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03008698 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008699 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008700 break;
8701 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008702 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008703 break;
8704 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008705 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008706 break;
8707 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008708 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008709 break;
8710 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03008711 /* Case prevented by intel_choose_pipe_bpp_dither. */
8712 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03008713 }
8714
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008715 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03008716 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8717
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008718 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03008719 val |= PIPECONF_INTERLACED_ILK;
8720 else
8721 val |= PIPECONF_PROGRESSIVE;
8722
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008723 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008724 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008725
Paulo Zanonic8203562012-09-12 10:06:29 -03008726 I915_WRITE(PIPECONF(pipe), val);
8727 POSTING_READ(PIPECONF(pipe));
8728}
8729
Daniel Vetter6ff93602013-04-19 11:24:36 +02008730static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008731{
Jani Nikula391bf042016-03-18 17:05:40 +02008732 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008733 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008734 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jani Nikula391bf042016-03-18 17:05:40 +02008735 u32 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008736
Jani Nikula391bf042016-03-18 17:05:40 +02008737 if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008738 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8739
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008740 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008741 val |= PIPECONF_INTERLACED_ILK;
8742 else
8743 val |= PIPECONF_PROGRESSIVE;
8744
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008745 I915_WRITE(PIPECONF(cpu_transcoder), val);
8746 POSTING_READ(PIPECONF(cpu_transcoder));
Jani Nikula391bf042016-03-18 17:05:40 +02008747}
8748
Jani Nikula391bf042016-03-18 17:05:40 +02008749static void haswell_set_pipemisc(struct drm_crtc *crtc)
8750{
8751 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8752 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8753
8754 if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
8755 u32 val = 0;
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008756
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008757 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008758 case 18:
8759 val |= PIPEMISC_DITHER_6_BPC;
8760 break;
8761 case 24:
8762 val |= PIPEMISC_DITHER_8_BPC;
8763 break;
8764 case 30:
8765 val |= PIPEMISC_DITHER_10_BPC;
8766 break;
8767 case 36:
8768 val |= PIPEMISC_DITHER_12_BPC;
8769 break;
8770 default:
8771 /* Case prevented by pipe_config_set_bpp. */
8772 BUG();
8773 }
8774
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008775 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008776 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8777
Jani Nikula391bf042016-03-18 17:05:40 +02008778 I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008779 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008780}
8781
Paulo Zanonid4b19312012-11-29 11:29:32 -02008782int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8783{
8784 /*
8785 * Account for spread spectrum to avoid
8786 * oversubscribing the link. Max center spread
8787 * is 2.5%; use 5% for safety's sake.
8788 */
8789 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02008790 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02008791}
8792
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008793static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02008794{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008795 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03008796}
8797
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008798static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
8799 struct intel_crtc_state *crtc_state,
8800 intel_clock_t *reduced_clock)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008801{
8802 struct drm_crtc *crtc = &intel_crtc->base;
8803 struct drm_device *dev = crtc->dev;
8804 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008805 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008806 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008807 struct drm_connector_state *connector_state;
8808 struct intel_encoder *encoder;
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008809 u32 dpll, fp, fp2;
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02008810 int factor, i;
Daniel Vetter09ede542013-04-30 14:01:45 +02008811 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008812
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008813 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008814 if (connector_state->crtc != crtc_state->base.crtc)
8815 continue;
8816
8817 encoder = to_intel_encoder(connector_state->best_encoder);
8818
8819 switch (encoder->type) {
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008820 case INTEL_OUTPUT_LVDS:
8821 is_lvds = true;
8822 break;
8823 case INTEL_OUTPUT_SDVO:
8824 case INTEL_OUTPUT_HDMI:
8825 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008826 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008827 default:
8828 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008829 }
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008830 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008831
Chris Wilsonc1858122010-12-03 21:35:48 +00008832 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07008833 factor = 21;
8834 if (is_lvds) {
8835 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008836 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02008837 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07008838 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008839 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07008840 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00008841
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008842 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Chris Wilsonc1858122010-12-03 21:35:48 +00008843
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008844 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
8845 fp |= FP_CB_TUNE;
8846
8847 if (reduced_clock) {
8848 fp2 = i9xx_dpll_compute_fp(reduced_clock);
8849
8850 if (reduced_clock->m < factor * reduced_clock->n)
8851 fp2 |= FP_CB_TUNE;
8852 } else {
8853 fp2 = fp;
8854 }
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008855
Chris Wilson5eddb702010-09-11 13:48:45 +01008856 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08008857
Eric Anholta07d6782011-03-30 13:01:08 -07008858 if (is_lvds)
8859 dpll |= DPLLB_MODE_LVDS;
8860 else
8861 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008862
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008863 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02008864 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008865
8866 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008867 dpll |= DPLL_SDVO_HIGH_SPEED;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008868 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008869 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08008870
Eric Anholta07d6782011-03-30 13:01:08 -07008871 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008872 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008873 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008874 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008875
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008876 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07008877 case 5:
8878 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8879 break;
8880 case 7:
8881 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8882 break;
8883 case 10:
8884 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8885 break;
8886 case 14:
8887 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8888 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08008889 }
8890
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02008891 if (is_lvds && intel_panel_use_ssc(dev_priv))
Kristian Høgsberg43565a02009-02-13 20:56:52 -05008892 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08008893 else
8894 dpll |= PLL_REF_INPUT_DREFCLK;
8895
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008896 dpll |= DPLL_VCO_ENABLE;
8897
8898 crtc_state->dpll_hw_state.dpll = dpll;
8899 crtc_state->dpll_hw_state.fp0 = fp;
8900 crtc_state->dpll_hw_state.fp1 = fp2;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008901}
8902
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008903static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8904 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008905{
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008906 struct drm_device *dev = crtc->base.dev;
8907 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02008908 intel_clock_t reduced_clock;
Ander Conselvan de Oliveira7ed9f892016-03-21 18:00:07 +02008909 bool has_reduced_clock = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008910 struct intel_shared_dpll *pll;
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008911 const intel_limit_t *limit;
8912 int refclk = 120000;
Jesse Barnes79e53942008-11-07 14:24:08 -08008913
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03008914 memset(&crtc_state->dpll_hw_state, 0,
8915 sizeof(crtc_state->dpll_hw_state));
8916
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02008917 crtc->lowfreq_avail = false;
8918
8919 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
8920 if (!crtc_state->has_pch_encoder)
8921 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008922
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008923 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
8924 if (intel_panel_use_ssc(dev_priv)) {
8925 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
8926 dev_priv->vbt.lvds_ssc_freq);
8927 refclk = dev_priv->vbt.lvds_ssc_freq;
8928 }
8929
8930 if (intel_is_dual_link_lvds(dev)) {
8931 if (refclk == 100000)
8932 limit = &intel_limits_ironlake_dual_lvds_100m;
8933 else
8934 limit = &intel_limits_ironlake_dual_lvds;
8935 } else {
8936 if (refclk == 100000)
8937 limit = &intel_limits_ironlake_single_lvds_100m;
8938 else
8939 limit = &intel_limits_ironlake_single_lvds;
8940 }
8941 } else {
8942 limit = &intel_limits_ironlake_dac;
8943 }
8944
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02008945 if (!crtc_state->clock_set &&
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008946 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8947 refclk, NULL, &crtc_state->dpll)) {
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02008948 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8949 return -EINVAL;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008950 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008951
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008952 ironlake_compute_dpll(crtc, crtc_state,
8953 has_reduced_clock ? &reduced_clock : NULL);
Daniel Vetter66e985c2013-06-05 13:34:20 +02008954
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02008955 pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
8956 if (pll == NULL) {
8957 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
8958 pipe_name(crtc->pipe));
8959 return -EINVAL;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02008960 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008961
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02008962 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
8963 has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008964 crtc->lowfreq_avail = true;
Daniel Vettere2b78262013-06-07 23:10:03 +02008965
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008966 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008967}
8968
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008969static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8970 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02008971{
8972 struct drm_device *dev = crtc->base.dev;
8973 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008974 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02008975
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008976 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8977 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8978 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8979 & ~TU_SIZE_MASK;
8980 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8981 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8982 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8983}
8984
8985static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8986 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008987 struct intel_link_m_n *m_n,
8988 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008989{
8990 struct drm_device *dev = crtc->base.dev;
8991 struct drm_i915_private *dev_priv = dev->dev_private;
8992 enum pipe pipe = crtc->pipe;
8993
8994 if (INTEL_INFO(dev)->gen >= 5) {
8995 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8996 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8997 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8998 & ~TU_SIZE_MASK;
8999 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
9000 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
9001 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009002 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
9003 * gen < 8) and if DRRS is supported (to make sure the
9004 * registers are not unnecessarily read).
9005 */
9006 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009007 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009008 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
9009 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
9010 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
9011 & ~TU_SIZE_MASK;
9012 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
9013 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
9014 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9015 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009016 } else {
9017 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
9018 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
9019 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
9020 & ~TU_SIZE_MASK;
9021 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
9022 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
9023 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9024 }
9025}
9026
9027void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009028 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009029{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02009030 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009031 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
9032 else
9033 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009034 &pipe_config->dp_m_n,
9035 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009036}
9037
Daniel Vetter72419202013-04-04 13:28:53 +02009038static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009039 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02009040{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009041 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009042 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02009043}
9044
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009045static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009046 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009047{
9048 struct drm_device *dev = crtc->base.dev;
9049 struct drm_i915_private *dev_priv = dev->dev_private;
Chandra Kondurua1b22782015-04-07 15:28:45 -07009050 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
9051 uint32_t ps_ctrl = 0;
9052 int id = -1;
9053 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009054
Chandra Kondurua1b22782015-04-07 15:28:45 -07009055 /* find scaler attached to this pipe */
9056 for (i = 0; i < crtc->num_scalers; i++) {
9057 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
9058 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
9059 id = i;
9060 pipe_config->pch_pfit.enabled = true;
9061 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9062 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9063 break;
9064 }
9065 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009066
Chandra Kondurua1b22782015-04-07 15:28:45 -07009067 scaler_state->scaler_id = id;
9068 if (id >= 0) {
9069 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9070 } else {
9071 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009072 }
9073}
9074
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009075static void
9076skylake_get_initial_plane_config(struct intel_crtc *crtc,
9077 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009078{
9079 struct drm_device *dev = crtc->base.dev;
9080 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau40f46282015-02-27 11:15:21 +00009081 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009082 int pipe = crtc->pipe;
9083 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009084 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009085 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009086 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009087
Damien Lespiaud9806c92015-01-21 14:07:19 +00009088 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009089 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009090 DRM_DEBUG_KMS("failed to alloc fb\n");
9091 return;
9092 }
9093
Damien Lespiau1b842c82015-01-21 13:50:54 +00009094 fb = &intel_fb->base;
9095
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009096 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00009097 if (!(val & PLANE_CTL_ENABLE))
9098 goto error;
9099
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009100 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9101 fourcc = skl_format_to_fourcc(pixel_format,
9102 val & PLANE_CTL_ORDER_RGBX,
9103 val & PLANE_CTL_ALPHA_MASK);
9104 fb->pixel_format = fourcc;
9105 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9106
Damien Lespiau40f46282015-02-27 11:15:21 +00009107 tiling = val & PLANE_CTL_TILED_MASK;
9108 switch (tiling) {
9109 case PLANE_CTL_TILED_LINEAR:
9110 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9111 break;
9112 case PLANE_CTL_TILED_X:
9113 plane_config->tiling = I915_TILING_X;
9114 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9115 break;
9116 case PLANE_CTL_TILED_Y:
9117 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9118 break;
9119 case PLANE_CTL_TILED_YF:
9120 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9121 break;
9122 default:
9123 MISSING_CASE(tiling);
9124 goto error;
9125 }
9126
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009127 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9128 plane_config->base = base;
9129
9130 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9131
9132 val = I915_READ(PLANE_SIZE(pipe, 0));
9133 fb->height = ((val >> 16) & 0xfff) + 1;
9134 fb->width = ((val >> 0) & 0x1fff) + 1;
9135
9136 val = I915_READ(PLANE_STRIDE(pipe, 0));
Ville Syrjälä7b49f942016-01-12 21:08:32 +02009137 stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
Damien Lespiau40f46282015-02-27 11:15:21 +00009138 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009139 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9140
9141 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009142 fb->pixel_format,
9143 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009144
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009145 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009146
9147 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9148 pipe_name(pipe), fb->width, fb->height,
9149 fb->bits_per_pixel, base, fb->pitches[0],
9150 plane_config->size);
9151
Damien Lespiau2d140302015-02-05 17:22:18 +00009152 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009153 return;
9154
9155error:
9156 kfree(fb);
9157}
9158
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009159static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009160 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009161{
9162 struct drm_device *dev = crtc->base.dev;
9163 struct drm_i915_private *dev_priv = dev->dev_private;
9164 uint32_t tmp;
9165
9166 tmp = I915_READ(PF_CTL(crtc->pipe));
9167
9168 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009169 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009170 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9171 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02009172
9173 /* We currently do not free assignements of panel fitters on
9174 * ivb/hsw (since we don't use the higher upscaling modes which
9175 * differentiates them) so just WARN about this case for now. */
9176 if (IS_GEN7(dev)) {
9177 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9178 PF_PIPE_SEL_IVB(crtc->pipe));
9179 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009180 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009181}
9182
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009183static void
9184ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9185 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009186{
9187 struct drm_device *dev = crtc->base.dev;
9188 struct drm_i915_private *dev_priv = dev->dev_private;
9189 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009190 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009191 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009192 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009193 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009194 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009195
Damien Lespiau42a7b082015-02-05 19:35:13 +00009196 val = I915_READ(DSPCNTR(pipe));
9197 if (!(val & DISPLAY_PLANE_ENABLE))
9198 return;
9199
Damien Lespiaud9806c92015-01-21 14:07:19 +00009200 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009201 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009202 DRM_DEBUG_KMS("failed to alloc fb\n");
9203 return;
9204 }
9205
Damien Lespiau1b842c82015-01-21 13:50:54 +00009206 fb = &intel_fb->base;
9207
Daniel Vetter18c52472015-02-10 17:16:09 +00009208 if (INTEL_INFO(dev)->gen >= 4) {
9209 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00009210 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00009211 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9212 }
9213 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009214
9215 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00009216 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009217 fb->pixel_format = fourcc;
9218 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009219
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009220 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009221 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009222 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009223 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00009224 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009225 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009226 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009227 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009228 }
9229 plane_config->base = base;
9230
9231 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009232 fb->width = ((val >> 16) & 0xfff) + 1;
9233 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009234
9235 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009236 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009237
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009238 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009239 fb->pixel_format,
9240 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009241
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009242 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009243
Damien Lespiau2844a922015-01-20 12:51:48 +00009244 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9245 pipe_name(pipe), fb->width, fb->height,
9246 fb->bits_per_pixel, base, fb->pitches[0],
9247 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009248
Damien Lespiau2d140302015-02-05 17:22:18 +00009249 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009250}
9251
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009252static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009253 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009254{
9255 struct drm_device *dev = crtc->base.dev;
9256 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak17290502016-02-12 18:55:11 +02009257 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009258 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02009259 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009260
Imre Deak17290502016-02-12 18:55:11 +02009261 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9262 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03009263 return false;
9264
Daniel Vettere143a212013-07-04 12:01:15 +02009265 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009266 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02009267
Imre Deak17290502016-02-12 18:55:11 +02009268 ret = false;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009269 tmp = I915_READ(PIPECONF(crtc->pipe));
9270 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02009271 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009272
Ville Syrjälä42571ae2013-09-06 23:29:00 +03009273 switch (tmp & PIPECONF_BPC_MASK) {
9274 case PIPECONF_6BPC:
9275 pipe_config->pipe_bpp = 18;
9276 break;
9277 case PIPECONF_8BPC:
9278 pipe_config->pipe_bpp = 24;
9279 break;
9280 case PIPECONF_10BPC:
9281 pipe_config->pipe_bpp = 30;
9282 break;
9283 case PIPECONF_12BPC:
9284 pipe_config->pipe_bpp = 36;
9285 break;
9286 default:
9287 break;
9288 }
9289
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02009290 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9291 pipe_config->limited_color_range = true;
9292
Daniel Vetterab9412b2013-05-03 11:49:46 +02009293 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02009294 struct intel_shared_dpll *pll;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009295 enum intel_dpll_id pll_id;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009296
Daniel Vetter88adfff2013-03-28 10:42:01 +01009297 pipe_config->has_pch_encoder = true;
9298
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009299 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9300 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9301 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02009302
9303 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009304
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009305 if (HAS_PCH_IBX(dev_priv)) {
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009306 pll_id = (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009307 } else {
9308 tmp = I915_READ(PCH_DPLL_SEL);
9309 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009310 pll_id = DPLL_ID_PCH_PLL_B;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009311 else
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009312 pll_id= DPLL_ID_PCH_PLL_A;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009313 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02009314
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009315 pipe_config->shared_dpll =
9316 intel_get_shared_dpll_by_id(dev_priv, pll_id);
9317 pll = pipe_config->shared_dpll;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009318
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +02009319 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9320 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02009321
9322 tmp = pipe_config->dpll_hw_state.dpll;
9323 pipe_config->pixel_multiplier =
9324 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9325 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03009326
9327 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009328 } else {
9329 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009330 }
9331
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009332 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +02009333 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009334
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009335 ironlake_get_pfit_config(crtc, pipe_config);
9336
Imre Deak17290502016-02-12 18:55:11 +02009337 ret = true;
9338
9339out:
9340 intel_display_power_put(dev_priv, power_domain);
9341
9342 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009343}
9344
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009345static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9346{
9347 struct drm_device *dev = dev_priv->dev;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009348 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009349
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009350 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05009351 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009352 pipe_name(crtc->pipe));
9353
Rob Clarke2c719b2014-12-15 13:56:32 -05009354 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9355 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
Ville Syrjälä01403de2015-09-18 20:03:33 +03009356 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9357 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009358 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9359 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009360 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03009361 if (IS_HASWELL(dev))
Rob Clarke2c719b2014-12-15 13:56:32 -05009362 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03009363 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009364 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009365 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009366 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009367 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009368 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009369
Paulo Zanoni9926ada2014-04-01 19:39:47 -03009370 /*
9371 * In theory we can still leave IRQs enabled, as long as only the HPD
9372 * interrupts remain enabled. We used to check for that, but since it's
9373 * gen-specific and since we only disable LCPLL after we fully disable
9374 * the interrupts, the check below should be enough.
9375 */
Rob Clarke2c719b2014-12-15 13:56:32 -05009376 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009377}
9378
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009379static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9380{
9381 struct drm_device *dev = dev_priv->dev;
9382
9383 if (IS_HASWELL(dev))
9384 return I915_READ(D_COMP_HSW);
9385 else
9386 return I915_READ(D_COMP_BDW);
9387}
9388
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009389static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9390{
9391 struct drm_device *dev = dev_priv->dev;
9392
9393 if (IS_HASWELL(dev)) {
9394 mutex_lock(&dev_priv->rps.hw_lock);
9395 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9396 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03009397 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009398 mutex_unlock(&dev_priv->rps.hw_lock);
9399 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009400 I915_WRITE(D_COMP_BDW, val);
9401 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009402 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009403}
9404
9405/*
9406 * This function implements pieces of two sequences from BSpec:
9407 * - Sequence for display software to disable LCPLL
9408 * - Sequence for display software to allow package C8+
9409 * The steps implemented here are just the steps that actually touch the LCPLL
9410 * register. Callers should take care of disabling all the display engine
9411 * functions, doing the mode unset, fixing interrupts, etc.
9412 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009413static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9414 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009415{
9416 uint32_t val;
9417
9418 assert_can_disable_lcpll(dev_priv);
9419
9420 val = I915_READ(LCPLL_CTL);
9421
9422 if (switch_to_fclk) {
9423 val |= LCPLL_CD_SOURCE_FCLK;
9424 I915_WRITE(LCPLL_CTL, val);
9425
9426 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9427 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9428 DRM_ERROR("Switching to FCLK failed\n");
9429
9430 val = I915_READ(LCPLL_CTL);
9431 }
9432
9433 val |= LCPLL_PLL_DISABLE;
9434 I915_WRITE(LCPLL_CTL, val);
9435 POSTING_READ(LCPLL_CTL);
9436
9437 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9438 DRM_ERROR("LCPLL still locked\n");
9439
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009440 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009441 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009442 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009443 ndelay(100);
9444
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009445 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9446 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009447 DRM_ERROR("D_COMP RCOMP still in progress\n");
9448
9449 if (allow_power_down) {
9450 val = I915_READ(LCPLL_CTL);
9451 val |= LCPLL_POWER_DOWN_ALLOW;
9452 I915_WRITE(LCPLL_CTL, val);
9453 POSTING_READ(LCPLL_CTL);
9454 }
9455}
9456
9457/*
9458 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9459 * source.
9460 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009461static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009462{
9463 uint32_t val;
9464
9465 val = I915_READ(LCPLL_CTL);
9466
9467 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9468 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9469 return;
9470
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009471 /*
9472 * Make sure we're not on PC8 state before disabling PC8, otherwise
9473 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009474 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02009475 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03009476
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009477 if (val & LCPLL_POWER_DOWN_ALLOW) {
9478 val &= ~LCPLL_POWER_DOWN_ALLOW;
9479 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02009480 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009481 }
9482
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009483 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009484 val |= D_COMP_COMP_FORCE;
9485 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009486 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009487
9488 val = I915_READ(LCPLL_CTL);
9489 val &= ~LCPLL_PLL_DISABLE;
9490 I915_WRITE(LCPLL_CTL, val);
9491
9492 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9493 DRM_ERROR("LCPLL not locked yet\n");
9494
9495 if (val & LCPLL_CD_SOURCE_FCLK) {
9496 val = I915_READ(LCPLL_CTL);
9497 val &= ~LCPLL_CD_SOURCE_FCLK;
9498 I915_WRITE(LCPLL_CTL, val);
9499
9500 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9501 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9502 DRM_ERROR("Switching back to LCPLL failed\n");
9503 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03009504
Mika Kuoppala59bad942015-01-16 11:34:40 +02009505 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ville Syrjäläb6283052015-06-03 15:45:07 +03009506 intel_update_cdclk(dev_priv->dev);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009507}
9508
Paulo Zanoni765dab672014-03-07 20:08:18 -03009509/*
9510 * Package states C8 and deeper are really deep PC states that can only be
9511 * reached when all the devices on the system allow it, so even if the graphics
9512 * device allows PC8+, it doesn't mean the system will actually get to these
9513 * states. Our driver only allows PC8+ when going into runtime PM.
9514 *
9515 * The requirements for PC8+ are that all the outputs are disabled, the power
9516 * well is disabled and most interrupts are disabled, and these are also
9517 * requirements for runtime PM. When these conditions are met, we manually do
9518 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9519 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9520 * hang the machine.
9521 *
9522 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9523 * the state of some registers, so when we come back from PC8+ we need to
9524 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9525 * need to take care of the registers kept by RC6. Notice that this happens even
9526 * if we don't put the device in PCI D3 state (which is what currently happens
9527 * because of the runtime PM support).
9528 *
9529 * For more, read "Display Sequences for Package C8" on the hardware
9530 * documentation.
9531 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009532void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009533{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009534 struct drm_device *dev = dev_priv->dev;
9535 uint32_t val;
9536
Paulo Zanonic67a4702013-08-19 13:18:09 -03009537 DRM_DEBUG_KMS("Enabling package C8+\n");
9538
Ville Syrjäläc2699522015-08-27 23:55:59 +03009539 if (HAS_PCH_LPT_LP(dev)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009540 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9541 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9542 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9543 }
9544
9545 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009546 hsw_disable_lcpll(dev_priv, true, true);
9547}
9548
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009549void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009550{
9551 struct drm_device *dev = dev_priv->dev;
9552 uint32_t val;
9553
Paulo Zanonic67a4702013-08-19 13:18:09 -03009554 DRM_DEBUG_KMS("Disabling package C8+\n");
9555
9556 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009557 lpt_init_pch_refclk(dev);
9558
Ville Syrjäläc2699522015-08-27 23:55:59 +03009559 if (HAS_PCH_LPT_LP(dev)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009560 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9561 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9562 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9563 }
Paulo Zanonic67a4702013-08-19 13:18:09 -03009564}
9565
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009566static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309567{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009568 struct drm_device *dev = old_state->dev;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009569 struct intel_atomic_state *old_intel_state =
9570 to_intel_atomic_state(old_state);
9571 unsigned int req_cdclk = old_intel_state->dev_cdclk;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309572
Imre Deakc6c46962016-04-01 16:02:40 +03009573 broxton_set_cdclk(to_i915(dev), req_cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309574}
9575
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009576/* compute the max rate for new configuration */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009577static int ilk_max_pixel_rate(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009578{
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009579 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
9580 struct drm_i915_private *dev_priv = state->dev->dev_private;
9581 struct drm_crtc *crtc;
9582 struct drm_crtc_state *cstate;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009583 struct intel_crtc_state *crtc_state;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009584 unsigned max_pixel_rate = 0, i;
9585 enum pipe pipe;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009586
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009587 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
9588 sizeof(intel_state->min_pixclk));
9589
9590 for_each_crtc_in_state(state, crtc, cstate, i) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009591 int pixel_rate;
9592
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009593 crtc_state = to_intel_crtc_state(cstate);
9594 if (!crtc_state->base.enable) {
9595 intel_state->min_pixclk[i] = 0;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009596 continue;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009597 }
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009598
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009599 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009600
9601 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009602 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009603 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9604
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009605 intel_state->min_pixclk[i] = pixel_rate;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009606 }
9607
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009608 for_each_pipe(dev_priv, pipe)
9609 max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
9610
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009611 return max_pixel_rate;
9612}
9613
9614static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9615{
9616 struct drm_i915_private *dev_priv = dev->dev_private;
9617 uint32_t val, data;
9618 int ret;
9619
9620 if (WARN((I915_READ(LCPLL_CTL) &
9621 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9622 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9623 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9624 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9625 "trying to change cdclk frequency with cdclk not enabled\n"))
9626 return;
9627
9628 mutex_lock(&dev_priv->rps.hw_lock);
9629 ret = sandybridge_pcode_write(dev_priv,
9630 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9631 mutex_unlock(&dev_priv->rps.hw_lock);
9632 if (ret) {
9633 DRM_ERROR("failed to inform pcode about cdclk change\n");
9634 return;
9635 }
9636
9637 val = I915_READ(LCPLL_CTL);
9638 val |= LCPLL_CD_SOURCE_FCLK;
9639 I915_WRITE(LCPLL_CTL, val);
9640
Tvrtko Ursulin5ba00172016-03-03 14:36:45 +00009641 if (wait_for_us(I915_READ(LCPLL_CTL) &
9642 LCPLL_CD_SOURCE_FCLK_DONE, 1))
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009643 DRM_ERROR("Switching to FCLK failed\n");
9644
9645 val = I915_READ(LCPLL_CTL);
9646 val &= ~LCPLL_CLK_FREQ_MASK;
9647
9648 switch (cdclk) {
9649 case 450000:
9650 val |= LCPLL_CLK_FREQ_450;
9651 data = 0;
9652 break;
9653 case 540000:
9654 val |= LCPLL_CLK_FREQ_54O_BDW;
9655 data = 1;
9656 break;
9657 case 337500:
9658 val |= LCPLL_CLK_FREQ_337_5_BDW;
9659 data = 2;
9660 break;
9661 case 675000:
9662 val |= LCPLL_CLK_FREQ_675_BDW;
9663 data = 3;
9664 break;
9665 default:
9666 WARN(1, "invalid cdclk frequency\n");
9667 return;
9668 }
9669
9670 I915_WRITE(LCPLL_CTL, val);
9671
9672 val = I915_READ(LCPLL_CTL);
9673 val &= ~LCPLL_CD_SOURCE_FCLK;
9674 I915_WRITE(LCPLL_CTL, val);
9675
Tvrtko Ursulin5ba00172016-03-03 14:36:45 +00009676 if (wait_for_us((I915_READ(LCPLL_CTL) &
9677 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009678 DRM_ERROR("Switching back to LCPLL failed\n");
9679
9680 mutex_lock(&dev_priv->rps.hw_lock);
9681 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9682 mutex_unlock(&dev_priv->rps.hw_lock);
9683
9684 intel_update_cdclk(dev);
9685
9686 WARN(cdclk != dev_priv->cdclk_freq,
9687 "cdclk requested %d kHz but got %d kHz\n",
9688 cdclk, dev_priv->cdclk_freq);
9689}
9690
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009691static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009692{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009693 struct drm_i915_private *dev_priv = to_i915(state->dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009694 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009695 int max_pixclk = ilk_max_pixel_rate(state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009696 int cdclk;
9697
9698 /*
9699 * FIXME should also account for plane ratio
9700 * once 64bpp pixel formats are supported.
9701 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009702 if (max_pixclk > 540000)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009703 cdclk = 675000;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009704 else if (max_pixclk > 450000)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009705 cdclk = 540000;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009706 else if (max_pixclk > 337500)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009707 cdclk = 450000;
9708 else
9709 cdclk = 337500;
9710
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009711 if (cdclk > dev_priv->max_cdclk_freq) {
Maarten Lankhorst63ba5342015-11-24 11:29:03 +01009712 DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9713 cdclk, dev_priv->max_cdclk_freq);
9714 return -EINVAL;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009715 }
9716
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009717 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
9718 if (!intel_state->active_crtcs)
9719 intel_state->dev_cdclk = 337500;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009720
9721 return 0;
9722}
9723
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009724static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009725{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009726 struct drm_device *dev = old_state->dev;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009727 struct intel_atomic_state *old_intel_state =
9728 to_intel_atomic_state(old_state);
9729 unsigned req_cdclk = old_intel_state->dev_cdclk;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009730
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009731 broadwell_set_cdclk(dev, req_cdclk);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009732}
9733
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009734static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9735 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009736{
Mika Kaholaaf3997b2016-02-05 13:29:28 +02009737 struct intel_encoder *intel_encoder =
9738 intel_ddi_get_crtc_new_encoder(crtc_state);
9739
9740 if (intel_encoder->type != INTEL_OUTPUT_DSI) {
9741 if (!intel_ddi_pll_select(crtc, crtc_state))
9742 return -EINVAL;
9743 }
Daniel Vetter716c2e52014-06-25 22:02:02 +03009744
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009745 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02009746
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009747 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009748}
9749
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309750static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9751 enum port port,
9752 struct intel_crtc_state *pipe_config)
9753{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009754 enum intel_dpll_id id;
9755
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309756 switch (port) {
9757 case PORT_A:
9758 pipe_config->ddi_pll_sel = SKL_DPLL0;
Imre Deak08250c42016-03-14 19:55:34 +02009759 id = DPLL_ID_SKL_DPLL0;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309760 break;
9761 case PORT_B:
9762 pipe_config->ddi_pll_sel = SKL_DPLL1;
Imre Deak08250c42016-03-14 19:55:34 +02009763 id = DPLL_ID_SKL_DPLL1;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309764 break;
9765 case PORT_C:
9766 pipe_config->ddi_pll_sel = SKL_DPLL2;
Imre Deak08250c42016-03-14 19:55:34 +02009767 id = DPLL_ID_SKL_DPLL2;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309768 break;
9769 default:
9770 DRM_ERROR("Incorrect port type\n");
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009771 return;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309772 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009773
9774 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309775}
9776
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009777static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9778 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009779 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009780{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009781 enum intel_dpll_id id;
Ander Conselvan de Oliveiraa3c988e2016-03-08 17:46:27 +02009782 u32 temp;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009783
9784 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9785 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9786
9787 switch (pipe_config->ddi_pll_sel) {
Damien Lespiau3148ade2014-11-21 16:14:56 +00009788 case SKL_DPLL0:
Ander Conselvan de Oliveiraa3c988e2016-03-08 17:46:27 +02009789 id = DPLL_ID_SKL_DPLL0;
9790 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009791 case SKL_DPLL1:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009792 id = DPLL_ID_SKL_DPLL1;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009793 break;
9794 case SKL_DPLL2:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009795 id = DPLL_ID_SKL_DPLL2;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009796 break;
9797 case SKL_DPLL3:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009798 id = DPLL_ID_SKL_DPLL3;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009799 break;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009800 default:
9801 MISSING_CASE(pipe_config->ddi_pll_sel);
9802 return;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009803 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009804
9805 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009806}
9807
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009808static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9809 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009810 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009811{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009812 enum intel_dpll_id id;
9813
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009814 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9815
9816 switch (pipe_config->ddi_pll_sel) {
9817 case PORT_CLK_SEL_WRPLL1:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009818 id = DPLL_ID_WRPLL1;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009819 break;
9820 case PORT_CLK_SEL_WRPLL2:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009821 id = DPLL_ID_WRPLL2;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009822 break;
Maarten Lankhorst00490c22015-11-16 14:42:12 +01009823 case PORT_CLK_SEL_SPLL:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009824 id = DPLL_ID_SPLL;
Ville Syrjälä79bd23d2015-12-01 23:32:07 +02009825 break;
Ander Conselvan de Oliveira9d16da62016-03-08 17:46:26 +02009826 case PORT_CLK_SEL_LCPLL_810:
9827 id = DPLL_ID_LCPLL_810;
9828 break;
9829 case PORT_CLK_SEL_LCPLL_1350:
9830 id = DPLL_ID_LCPLL_1350;
9831 break;
9832 case PORT_CLK_SEL_LCPLL_2700:
9833 id = DPLL_ID_LCPLL_2700;
9834 break;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009835 default:
9836 MISSING_CASE(pipe_config->ddi_pll_sel);
9837 /* fall through */
9838 case PORT_CLK_SEL_NONE:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009839 return;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009840 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009841
9842 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009843}
9844
Jani Nikulacf304292016-03-18 17:05:41 +02009845static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
9846 struct intel_crtc_state *pipe_config,
9847 unsigned long *power_domain_mask)
9848{
9849 struct drm_device *dev = crtc->base.dev;
9850 struct drm_i915_private *dev_priv = dev->dev_private;
9851 enum intel_display_power_domain power_domain;
9852 u32 tmp;
9853
9854 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
9855
9856 /*
9857 * XXX: Do intel_display_power_get_if_enabled before reading this (for
9858 * consistency and less surprising code; it's in always on power).
9859 */
9860 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9861 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9862 enum pipe trans_edp_pipe;
9863 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9864 default:
9865 WARN(1, "unknown pipe linked to edp transcoder\n");
9866 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9867 case TRANS_DDI_EDP_INPUT_A_ON:
9868 trans_edp_pipe = PIPE_A;
9869 break;
9870 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9871 trans_edp_pipe = PIPE_B;
9872 break;
9873 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9874 trans_edp_pipe = PIPE_C;
9875 break;
9876 }
9877
9878 if (trans_edp_pipe == crtc->pipe)
9879 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9880 }
9881
9882 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
9883 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9884 return false;
9885 *power_domain_mask |= BIT(power_domain);
9886
9887 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
9888
9889 return tmp & PIPECONF_ENABLE;
9890}
9891
Jani Nikula4d1de972016-03-18 17:05:42 +02009892static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
9893 struct intel_crtc_state *pipe_config,
9894 unsigned long *power_domain_mask)
9895{
9896 struct drm_device *dev = crtc->base.dev;
9897 struct drm_i915_private *dev_priv = dev->dev_private;
9898 enum intel_display_power_domain power_domain;
9899 enum port port;
9900 enum transcoder cpu_transcoder;
9901 u32 tmp;
9902
9903 pipe_config->has_dsi_encoder = false;
9904
9905 for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
9906 if (port == PORT_A)
9907 cpu_transcoder = TRANSCODER_DSI_A;
9908 else
9909 cpu_transcoder = TRANSCODER_DSI_C;
9910
9911 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
9912 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9913 continue;
9914 *power_domain_mask |= BIT(power_domain);
9915
Imre Deakdb18b6a2016-03-24 12:41:40 +02009916 /*
9917 * The PLL needs to be enabled with a valid divider
9918 * configuration, otherwise accessing DSI registers will hang
9919 * the machine. See BSpec North Display Engine
9920 * registers/MIPI[BXT]. We can break out here early, since we
9921 * need the same DSI PLL to be enabled for both DSI ports.
9922 */
9923 if (!intel_dsi_pll_is_enabled(dev_priv))
9924 break;
9925
Jani Nikula4d1de972016-03-18 17:05:42 +02009926 /* XXX: this works for video mode only */
9927 tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
9928 if (!(tmp & DPI_ENABLE))
9929 continue;
9930
9931 tmp = I915_READ(MIPI_CTRL(port));
9932 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
9933 continue;
9934
9935 pipe_config->cpu_transcoder = cpu_transcoder;
9936 pipe_config->has_dsi_encoder = true;
9937 break;
9938 }
9939
9940 return pipe_config->has_dsi_encoder;
9941}
9942
Daniel Vetter26804af2014-06-25 22:01:55 +03009943static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009944 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +03009945{
9946 struct drm_device *dev = crtc->base.dev;
9947 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009948 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +03009949 enum port port;
9950 uint32_t tmp;
9951
9952 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9953
9954 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9955
Rodrigo Vivief11bdb2015-10-28 04:16:45 -07009956 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009957 skylake_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309958 else if (IS_BROXTON(dev))
9959 bxt_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009960 else
9961 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +03009962
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009963 pll = pipe_config->shared_dpll;
9964 if (pll) {
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +02009965 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9966 &pipe_config->dpll_hw_state));
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009967 }
9968
Daniel Vetter26804af2014-06-25 22:01:55 +03009969 /*
9970 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9971 * DDI E. So just check whether this pipe is wired to DDI E and whether
9972 * the PCH transcoder is on.
9973 */
Damien Lespiauca370452013-12-03 13:56:24 +00009974 if (INTEL_INFO(dev)->gen < 9 &&
9975 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +03009976 pipe_config->has_pch_encoder = true;
9977
9978 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9979 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9980 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9981
9982 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9983 }
9984}
9985
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009986static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009987 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009988{
9989 struct drm_device *dev = crtc->base.dev;
9990 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak17290502016-02-12 18:55:11 +02009991 enum intel_display_power_domain power_domain;
9992 unsigned long power_domain_mask;
Jani Nikulacf304292016-03-18 17:05:41 +02009993 bool active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009994
Imre Deak17290502016-02-12 18:55:11 +02009995 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9996 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +02009997 return false;
Imre Deak17290502016-02-12 18:55:11 +02009998 power_domain_mask = BIT(power_domain);
9999
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010000 pipe_config->shared_dpll = NULL;
Daniel Vetterc0d43d62013-06-07 23:11:08 +020010001
Jani Nikulacf304292016-03-18 17:05:41 +020010002 active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
Daniel Vettereccb1402013-05-22 00:50:22 +020010003
Jani Nikula4d1de972016-03-18 17:05:42 +020010004 if (IS_BROXTON(dev_priv)) {
10005 bxt_get_dsi_transcoder_state(crtc, pipe_config,
10006 &power_domain_mask);
10007 WARN_ON(active && pipe_config->has_dsi_encoder);
10008 if (pipe_config->has_dsi_encoder)
10009 active = true;
10010 }
10011
Jani Nikulacf304292016-03-18 17:05:41 +020010012 if (!active)
Imre Deak17290502016-02-12 18:55:11 +020010013 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010014
Jani Nikula4d1de972016-03-18 17:05:42 +020010015 if (!pipe_config->has_dsi_encoder) {
10016 haswell_get_ddi_port_state(crtc, pipe_config);
10017 intel_get_pipe_timings(crtc, pipe_config);
10018 }
Daniel Vetter627eb5a2013-04-29 19:33:42 +020010019
Jani Nikulabc58be62016-03-18 17:05:39 +020010020 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010021
Lionel Landwerlin05dc6982016-03-16 10:57:15 +000010022 pipe_config->gamma_mode =
10023 I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
10024
Chandra Kondurua1b22782015-04-07 15:28:45 -070010025 if (INTEL_INFO(dev)->gen >= 9) {
10026 skl_init_scalers(dev, crtc, pipe_config);
10027 }
10028
Chandra Konduruaf99ced2015-05-11 14:35:47 -070010029 if (INTEL_INFO(dev)->gen >= 9) {
10030 pipe_config->scaler_state.scaler_id = -1;
10031 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
10032 }
10033
Imre Deak17290502016-02-12 18:55:11 +020010034 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
10035 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
10036 power_domain_mask |= BIT(power_domain);
Rodrigo Vivi1c132b42015-09-02 15:19:26 -070010037 if (INTEL_INFO(dev)->gen >= 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +000010038 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -080010039 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -070010040 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesbd2e2442014-11-13 17:51:47 +000010041 }
Daniel Vetter88adfff2013-03-28 10:42:01 +010010042
Jesse Barnese59150d2014-01-07 13:30:45 -080010043 if (IS_HASWELL(dev))
10044 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
10045 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030010046
Jani Nikula4d1de972016-03-18 17:05:42 +020010047 if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
10048 !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
Clint Taylorebb69c92014-09-30 10:30:22 -070010049 pipe_config->pixel_multiplier =
10050 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
10051 } else {
10052 pipe_config->pixel_multiplier = 1;
10053 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020010054
Imre Deak17290502016-02-12 18:55:11 +020010055out:
10056 for_each_power_domain(power_domain, power_domain_mask)
10057 intel_display_power_put(dev_priv, power_domain);
10058
Jani Nikulacf304292016-03-18 17:05:41 +020010059 return active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010060}
10061
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010062static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
10063 const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +010010064{
10065 struct drm_device *dev = crtc->dev;
10066 struct drm_i915_private *dev_priv = dev->dev_private;
10067 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +030010068 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +010010069
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010070 if (plane_state && plane_state->visible) {
10071 unsigned int width = plane_state->base.crtc_w;
10072 unsigned int height = plane_state->base.crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +030010073 unsigned int stride = roundup_pow_of_two(width) * 4;
10074
10075 switch (stride) {
10076 default:
10077 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
10078 width, stride);
10079 stride = 256;
10080 /* fallthrough */
10081 case 256:
10082 case 512:
10083 case 1024:
10084 case 2048:
10085 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010086 }
10087
Ville Syrjälädc41c152014-08-13 11:57:05 +030010088 cntl |= CURSOR_ENABLE |
10089 CURSOR_GAMMA_ENABLE |
10090 CURSOR_FORMAT_ARGB |
10091 CURSOR_STRIDE(stride);
10092
10093 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010094 }
Chris Wilson560b85b2010-08-07 11:01:38 +010010095
Ville Syrjälädc41c152014-08-13 11:57:05 +030010096 if (intel_crtc->cursor_cntl != 0 &&
10097 (intel_crtc->cursor_base != base ||
10098 intel_crtc->cursor_size != size ||
10099 intel_crtc->cursor_cntl != cntl)) {
10100 /* On these chipsets we can only modify the base/size/stride
10101 * whilst the cursor is disabled.
10102 */
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010103 I915_WRITE(CURCNTR(PIPE_A), 0);
10104 POSTING_READ(CURCNTR(PIPE_A));
Ville Syrjälädc41c152014-08-13 11:57:05 +030010105 intel_crtc->cursor_cntl = 0;
10106 }
10107
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010108 if (intel_crtc->cursor_base != base) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010109 I915_WRITE(CURBASE(PIPE_A), base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010110 intel_crtc->cursor_base = base;
10111 }
Ville Syrjälädc41c152014-08-13 11:57:05 +030010112
10113 if (intel_crtc->cursor_size != size) {
10114 I915_WRITE(CURSIZE, size);
10115 intel_crtc->cursor_size = size;
10116 }
10117
Chris Wilson4b0e3332014-05-30 16:35:26 +030010118 if (intel_crtc->cursor_cntl != cntl) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010119 I915_WRITE(CURCNTR(PIPE_A), cntl);
10120 POSTING_READ(CURCNTR(PIPE_A));
Chris Wilson4b0e3332014-05-30 16:35:26 +030010121 intel_crtc->cursor_cntl = cntl;
10122 }
Chris Wilson560b85b2010-08-07 11:01:38 +010010123}
10124
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010125static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
10126 const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +010010127{
10128 struct drm_device *dev = crtc->dev;
10129 struct drm_i915_private *dev_priv = dev->dev_private;
10130 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10131 int pipe = intel_crtc->pipe;
Ville Syrjälä663f3122015-12-14 13:16:48 +020010132 uint32_t cntl = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +010010133
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010134 if (plane_state && plane_state->visible) {
Chris Wilson4b0e3332014-05-30 16:35:26 +030010135 cntl = MCURSOR_GAMMA_ENABLE;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010136 switch (plane_state->base.crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010137 case 64:
10138 cntl |= CURSOR_MODE_64_ARGB_AX;
10139 break;
10140 case 128:
10141 cntl |= CURSOR_MODE_128_ARGB_AX;
10142 break;
10143 case 256:
10144 cntl |= CURSOR_MODE_256_ARGB_AX;
10145 break;
10146 default:
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010147 MISSING_CASE(plane_state->base.crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010148 return;
Chris Wilson560b85b2010-08-07 11:01:38 +010010149 }
Chris Wilson4b0e3332014-05-30 16:35:26 +030010150 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010151
Bob Paauwefc6f93b2015-08-31 14:03:30 -070010152 if (HAS_DDI(dev))
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010153 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010154
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010155 if (plane_state->base.rotation == BIT(DRM_ROTATE_180))
10156 cntl |= CURSOR_ROTATE_180;
10157 }
Ville Syrjälä4398ad42014-10-23 07:41:34 -070010158
Chris Wilson4b0e3332014-05-30 16:35:26 +030010159 if (intel_crtc->cursor_cntl != cntl) {
10160 I915_WRITE(CURCNTR(pipe), cntl);
10161 POSTING_READ(CURCNTR(pipe));
10162 intel_crtc->cursor_cntl = cntl;
10163 }
10164
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010165 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010166 I915_WRITE(CURBASE(pipe), base);
10167 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010168
10169 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010170}
10171
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010172/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +010010173static void intel_crtc_update_cursor(struct drm_crtc *crtc,
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010174 const struct intel_plane_state *plane_state)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010175{
10176 struct drm_device *dev = crtc->dev;
10177 struct drm_i915_private *dev_priv = dev->dev_private;
10178 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10179 int pipe = intel_crtc->pipe;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010180 u32 base = intel_crtc->cursor_addr;
10181 u32 pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010182
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010183 if (plane_state) {
10184 int x = plane_state->base.crtc_x;
10185 int y = plane_state->base.crtc_y;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010186
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010187 if (x < 0) {
10188 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10189 x = -x;
10190 }
10191 pos |= x << CURSOR_X_SHIFT;
10192
10193 if (y < 0) {
10194 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10195 y = -y;
10196 }
10197 pos |= y << CURSOR_Y_SHIFT;
10198
10199 /* ILK+ do this automagically */
10200 if (HAS_GMCH_DISPLAY(dev) &&
10201 plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
10202 base += (plane_state->base.crtc_h *
10203 plane_state->base.crtc_w - 1) * 4;
10204 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010205 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010206
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010207 I915_WRITE(CURPOS(pipe), pos);
10208
Ville Syrjälä8ac54662014-08-12 19:39:54 +030010209 if (IS_845G(dev) || IS_I865G(dev))
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010210 i845_update_cursor(crtc, base, plane_state);
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010211 else
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010212 i9xx_update_cursor(crtc, base, plane_state);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010213}
10214
Ville Syrjälädc41c152014-08-13 11:57:05 +030010215static bool cursor_size_ok(struct drm_device *dev,
10216 uint32_t width, uint32_t height)
10217{
10218 if (width == 0 || height == 0)
10219 return false;
10220
10221 /*
10222 * 845g/865g are special in that they are only limited by
10223 * the width of their cursors, the height is arbitrary up to
10224 * the precision of the register. Everything else requires
10225 * square cursors, limited to a few power-of-two sizes.
10226 */
10227 if (IS_845G(dev) || IS_I865G(dev)) {
10228 if ((width & 63) != 0)
10229 return false;
10230
10231 if (width > (IS_845G(dev) ? 64 : 512))
10232 return false;
10233
10234 if (height > 1023)
10235 return false;
10236 } else {
10237 switch (width | height) {
10238 case 256:
10239 case 128:
10240 if (IS_GEN2(dev))
10241 return false;
10242 case 64:
10243 break;
10244 default:
10245 return false;
10246 }
10247 }
10248
10249 return true;
10250}
10251
Jesse Barnes79e53942008-11-07 14:24:08 -080010252/* VESA 640x480x72Hz mode to set on the pipe */
10253static struct drm_display_mode load_detect_mode = {
10254 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10255 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10256};
10257
Daniel Vettera8bb6812014-02-10 18:00:39 +010010258struct drm_framebuffer *
10259__intel_framebuffer_create(struct drm_device *dev,
10260 struct drm_mode_fb_cmd2 *mode_cmd,
10261 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +010010262{
10263 struct intel_framebuffer *intel_fb;
10264 int ret;
10265
10266 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010267 if (!intel_fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010268 return ERR_PTR(-ENOMEM);
Chris Wilsond2dff872011-04-19 08:36:26 +010010269
10270 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010271 if (ret)
10272 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +010010273
10274 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010275
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010276err:
10277 kfree(intel_fb);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010278 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +010010279}
10280
Daniel Vetterb5ea6422014-03-02 21:18:00 +010010281static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +010010282intel_framebuffer_create(struct drm_device *dev,
10283 struct drm_mode_fb_cmd2 *mode_cmd,
10284 struct drm_i915_gem_object *obj)
10285{
10286 struct drm_framebuffer *fb;
10287 int ret;
10288
10289 ret = i915_mutex_lock_interruptible(dev);
10290 if (ret)
10291 return ERR_PTR(ret);
10292 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10293 mutex_unlock(&dev->struct_mutex);
10294
10295 return fb;
10296}
10297
Chris Wilsond2dff872011-04-19 08:36:26 +010010298static u32
10299intel_framebuffer_pitch_for_width(int width, int bpp)
10300{
10301 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10302 return ALIGN(pitch, 64);
10303}
10304
10305static u32
10306intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10307{
10308 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +020010309 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +010010310}
10311
10312static struct drm_framebuffer *
10313intel_framebuffer_create_for_mode(struct drm_device *dev,
10314 struct drm_display_mode *mode,
10315 int depth, int bpp)
10316{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010317 struct drm_framebuffer *fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010318 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +000010319 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +010010320
10321 obj = i915_gem_alloc_object(dev,
10322 intel_framebuffer_size_for_mode(mode, bpp));
10323 if (obj == NULL)
10324 return ERR_PTR(-ENOMEM);
10325
10326 mode_cmd.width = mode->hdisplay;
10327 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010328 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10329 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +000010330 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +010010331
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010332 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
10333 if (IS_ERR(fb))
10334 drm_gem_object_unreference_unlocked(&obj->base);
10335
10336 return fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010337}
10338
10339static struct drm_framebuffer *
10340mode_fits_in_fbdev(struct drm_device *dev,
10341 struct drm_display_mode *mode)
10342{
Daniel Vetter06957262015-08-10 13:34:08 +020010343#ifdef CONFIG_DRM_FBDEV_EMULATION
Chris Wilsond2dff872011-04-19 08:36:26 +010010344 struct drm_i915_private *dev_priv = dev->dev_private;
10345 struct drm_i915_gem_object *obj;
10346 struct drm_framebuffer *fb;
10347
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010348 if (!dev_priv->fbdev)
10349 return NULL;
10350
10351 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010352 return NULL;
10353
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010354 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010355 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +010010356
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010357 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010358 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10359 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +010010360 return NULL;
10361
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010362 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +010010363 return NULL;
10364
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010365 drm_framebuffer_reference(fb);
Chris Wilsond2dff872011-04-19 08:36:26 +010010366 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +020010367#else
10368 return NULL;
10369#endif
Chris Wilsond2dff872011-04-19 08:36:26 +010010370}
10371
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010372static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10373 struct drm_crtc *crtc,
10374 struct drm_display_mode *mode,
10375 struct drm_framebuffer *fb,
10376 int x, int y)
10377{
10378 struct drm_plane_state *plane_state;
10379 int hdisplay, vdisplay;
10380 int ret;
10381
10382 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10383 if (IS_ERR(plane_state))
10384 return PTR_ERR(plane_state);
10385
10386 if (mode)
10387 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10388 else
10389 hdisplay = vdisplay = 0;
10390
10391 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10392 if (ret)
10393 return ret;
10394 drm_atomic_set_fb_for_plane(plane_state, fb);
10395 plane_state->crtc_x = 0;
10396 plane_state->crtc_y = 0;
10397 plane_state->crtc_w = hdisplay;
10398 plane_state->crtc_h = vdisplay;
10399 plane_state->src_x = x << 16;
10400 plane_state->src_y = y << 16;
10401 plane_state->src_w = hdisplay << 16;
10402 plane_state->src_h = vdisplay << 16;
10403
10404 return 0;
10405}
10406
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010407bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +010010408 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -050010409 struct intel_load_detect_pipe *old,
10410 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010411{
10412 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010413 struct intel_encoder *intel_encoder =
10414 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -080010415 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +010010416 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -080010417 struct drm_crtc *crtc = NULL;
10418 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +020010419 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -050010420 struct drm_mode_config *config = &dev->mode_config;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010421 struct drm_atomic_state *state = NULL, *restore_state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010422 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010423 struct intel_crtc_state *crtc_state;
Rob Clark51fd3712013-11-19 12:10:12 -050010424 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010425
Chris Wilsond2dff872011-04-19 08:36:26 +010010426 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010427 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010428 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010429
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010430 old->restore_state = NULL;
10431
Rob Clark51fd3712013-11-19 12:10:12 -050010432retry:
10433 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10434 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010435 goto fail;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020010436
Jesse Barnes79e53942008-11-07 14:24:08 -080010437 /*
10438 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +010010439 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010440 * - if the connector already has an assigned crtc, use it (but make
10441 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +010010442 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010443 * - try to find the first unused crtc that can drive this connector,
10444 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -080010445 */
10446
10447 /* See if we already have a CRTC for this connector */
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010448 if (connector->state->crtc) {
10449 crtc = connector->state->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +010010450
Rob Clark51fd3712013-11-19 12:10:12 -050010451 ret = drm_modeset_lock(&crtc->mutex, ctx);
10452 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010453 goto fail;
Chris Wilson8261b192011-04-19 23:18:09 +010010454
10455 /* Make sure the crtc and connector are running */
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010456 goto found;
Jesse Barnes79e53942008-11-07 14:24:08 -080010457 }
10458
10459 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010460 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010461 i++;
10462 if (!(encoder->possible_crtcs & (1 << i)))
10463 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010464
10465 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
10466 if (ret)
10467 goto fail;
10468
10469 if (possible_crtc->state->enable) {
10470 drm_modeset_unlock(&possible_crtc->mutex);
Ville Syrjäläa4592492014-08-11 13:15:36 +030010471 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010472 }
Ville Syrjäläa4592492014-08-11 13:15:36 +030010473
10474 crtc = possible_crtc;
10475 break;
Jesse Barnes79e53942008-11-07 14:24:08 -080010476 }
10477
10478 /*
10479 * If we didn't find an unused CRTC, don't use any.
10480 */
10481 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +010010482 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010483 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010484 }
10485
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010486found:
10487 intel_crtc = to_intel_crtc(crtc);
10488
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010489 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10490 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010491 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010492
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010493 state = drm_atomic_state_alloc(dev);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010494 restore_state = drm_atomic_state_alloc(dev);
10495 if (!state || !restore_state) {
10496 ret = -ENOMEM;
10497 goto fail;
10498 }
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010499
10500 state->acquire_ctx = ctx;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010501 restore_state->acquire_ctx = ctx;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010502
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010503 connector_state = drm_atomic_get_connector_state(state, connector);
10504 if (IS_ERR(connector_state)) {
10505 ret = PTR_ERR(connector_state);
10506 goto fail;
10507 }
10508
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010509 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
10510 if (ret)
10511 goto fail;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010512
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010513 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10514 if (IS_ERR(crtc_state)) {
10515 ret = PTR_ERR(crtc_state);
10516 goto fail;
10517 }
10518
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010519 crtc_state->base.active = crtc_state->base.enable = true;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010520
Chris Wilson64927112011-04-20 07:25:26 +010010521 if (!mode)
10522 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -080010523
Chris Wilsond2dff872011-04-19 08:36:26 +010010524 /* We need a framebuffer large enough to accommodate all accesses
10525 * that the plane may generate whilst we perform load detection.
10526 * We can not rely on the fbcon either being present (we get called
10527 * during its initialisation to detect all boot displays, or it may
10528 * not even exist) or that it is large enough to satisfy the
10529 * requested mode.
10530 */
Daniel Vetter94352cf2012-07-05 22:51:56 +020010531 fb = mode_fits_in_fbdev(dev, mode);
10532 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010533 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010534 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
Chris Wilsond2dff872011-04-19 08:36:26 +010010535 } else
10536 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010537 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010538 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010539 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010540 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010541
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010542 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10543 if (ret)
10544 goto fail;
10545
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010546 drm_framebuffer_unreference(fb);
10547
10548 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
10549 if (ret)
10550 goto fail;
10551
10552 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
10553 if (!ret)
10554 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
10555 if (!ret)
10556 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
10557 if (ret) {
10558 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
10559 goto fail;
10560 }
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030010561
Maarten Lankhorst3ba86072016-02-29 09:18:57 +010010562 ret = drm_atomic_commit(state);
10563 if (ret) {
Chris Wilson64927112011-04-20 07:25:26 +010010564 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010565 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010566 }
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010567
10568 old->restore_state = restore_state;
Chris Wilson71731882011-04-19 23:10:58 +010010569
Jesse Barnes79e53942008-11-07 14:24:08 -080010570 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010571 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +010010572 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010573
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010574fail:
Ander Conselvan de Oliveirae5d958e2015-04-21 17:12:57 +030010575 drm_atomic_state_free(state);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010576 drm_atomic_state_free(restore_state);
10577 restore_state = state = NULL;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010578
Rob Clark51fd3712013-11-19 12:10:12 -050010579 if (ret == -EDEADLK) {
10580 drm_modeset_backoff(ctx);
10581 goto retry;
10582 }
10583
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010584 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -080010585}
10586
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010587void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020010588 struct intel_load_detect_pipe *old,
10589 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010590{
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010591 struct intel_encoder *intel_encoder =
10592 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +010010593 struct drm_encoder *encoder = &intel_encoder->base;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010594 struct drm_atomic_state *state = old->restore_state;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010595 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080010596
Chris Wilsond2dff872011-04-19 08:36:26 +010010597 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010598 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010599 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010600
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010601 if (!state)
Chris Wilson0622a532011-04-21 09:32:11 +010010602 return;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010603
10604 ret = drm_atomic_commit(state);
10605 if (ret) {
10606 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
10607 drm_atomic_state_free(state);
Jesse Barnes79e53942008-11-07 14:24:08 -080010608 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010609}
10610
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010611static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010612 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010613{
10614 struct drm_i915_private *dev_priv = dev->dev_private;
10615 u32 dpll = pipe_config->dpll_hw_state.dpll;
10616
10617 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +020010618 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010619 else if (HAS_PCH_SPLIT(dev))
10620 return 120000;
10621 else if (!IS_GEN2(dev))
10622 return 96000;
10623 else
10624 return 48000;
10625}
10626
Jesse Barnes79e53942008-11-07 14:24:08 -080010627/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010628static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010629 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -080010630{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010631 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080010632 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010633 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010634 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -080010635 u32 fp;
10636 intel_clock_t clock;
Imre Deakdccbea32015-06-22 23:35:51 +030010637 int port_clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010638 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -080010639
10640 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +030010641 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010642 else
Ville Syrjälä293623f2013-09-13 16:18:46 +030010643 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010644
10645 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010646 if (IS_PINEVIEW(dev)) {
10647 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10648 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +080010649 } else {
10650 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10651 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10652 }
10653
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010654 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010655 if (IS_PINEVIEW(dev))
10656 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10657 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +080010658 else
10659 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -080010660 DPLL_FPA01_P1_POST_DIV_SHIFT);
10661
10662 switch (dpll & DPLL_MODE_MASK) {
10663 case DPLLB_MODE_DAC_SERIAL:
10664 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10665 5 : 10;
10666 break;
10667 case DPLLB_MODE_LVDS:
10668 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10669 7 : 14;
10670 break;
10671 default:
Zhao Yakui28c97732009-10-09 11:39:41 +080010672 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -080010673 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010674 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010675 }
10676
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010677 if (IS_PINEVIEW(dev))
Imre Deakdccbea32015-06-22 23:35:51 +030010678 port_clock = pnv_calc_dpll_params(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010679 else
Imre Deakdccbea32015-06-22 23:35:51 +030010680 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010681 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +020010682 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010683 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -080010684
10685 if (is_lvds) {
10686 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10687 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010688
10689 if (lvds & LVDS_CLKB_POWER_UP)
10690 clock.p2 = 7;
10691 else
10692 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -080010693 } else {
10694 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10695 clock.p1 = 2;
10696 else {
10697 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10698 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10699 }
10700 if (dpll & PLL_P2_DIVIDE_BY_4)
10701 clock.p2 = 4;
10702 else
10703 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -080010704 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010705
Imre Deakdccbea32015-06-22 23:35:51 +030010706 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010707 }
10708
Ville Syrjälä18442d02013-09-13 16:00:08 +030010709 /*
10710 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +010010711 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +030010712 * encoder's get_config() function.
10713 */
Imre Deakdccbea32015-06-22 23:35:51 +030010714 pipe_config->port_clock = port_clock;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010715}
10716
Ville Syrjälä6878da02013-09-13 15:59:11 +030010717int intel_dotclock_calculate(int link_freq,
10718 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010719{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010720 /*
10721 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010722 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010723 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010724 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010725 *
10726 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010727 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -080010728 */
10729
Ville Syrjälä6878da02013-09-13 15:59:11 +030010730 if (!m_n->link_n)
10731 return 0;
10732
10733 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10734}
10735
Ville Syrjälä18442d02013-09-13 16:00:08 +030010736static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010737 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +030010738{
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020010739 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä18442d02013-09-13 16:00:08 +030010740
10741 /* read out port_clock from the DPLL */
10742 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +030010743
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010744 /*
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020010745 * In case there is an active pipe without active ports,
10746 * we may need some idea for the dotclock anyway.
10747 * Calculate one based on the FDI configuration.
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010748 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010749 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä21a727b2016-02-17 21:41:10 +020010750 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjälä18442d02013-09-13 16:00:08 +030010751 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -080010752}
10753
10754/** Returns the currently programmed mode of the given pipe. */
10755struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10756 struct drm_crtc *crtc)
10757{
Jesse Barnes548f2452011-02-17 10:40:53 -080010758 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080010759 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010760 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010761 struct drm_display_mode *mode;
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010762 struct intel_crtc_state *pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -020010763 int htot = I915_READ(HTOTAL(cpu_transcoder));
10764 int hsync = I915_READ(HSYNC(cpu_transcoder));
10765 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10766 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +030010767 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -080010768
10769 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10770 if (!mode)
10771 return NULL;
10772
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010773 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
10774 if (!pipe_config) {
10775 kfree(mode);
10776 return NULL;
10777 }
10778
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010779 /*
10780 * Construct a pipe_config sufficient for getting the clock info
10781 * back out of crtc_clock_get.
10782 *
10783 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10784 * to use a real value here instead.
10785 */
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010786 pipe_config->cpu_transcoder = (enum transcoder) pipe;
10787 pipe_config->pixel_multiplier = 1;
10788 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10789 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10790 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
10791 i9xx_crtc_clock_get(intel_crtc, pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010792
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010793 mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -080010794 mode->hdisplay = (htot & 0xffff) + 1;
10795 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10796 mode->hsync_start = (hsync & 0xffff) + 1;
10797 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10798 mode->vdisplay = (vtot & 0xffff) + 1;
10799 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10800 mode->vsync_start = (vsync & 0xffff) + 1;
10801 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10802
10803 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -080010804
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010805 kfree(pipe_config);
10806
Jesse Barnes79e53942008-11-07 14:24:08 -080010807 return mode;
10808}
10809
Chris Wilsonf047e392012-07-21 12:31:41 +010010810void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -070010811{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010812 struct drm_i915_private *dev_priv = dev->dev_private;
10813
Chris Wilsonf62a0072014-02-21 17:55:39 +000010814 if (dev_priv->mm.busy)
10815 return;
10816
Paulo Zanoni43694d62014-03-07 20:08:08 -030010817 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -030010818 i915_update_gfx_val(dev_priv);
Chris Wilson43cf3bf2015-03-18 09:48:22 +000010819 if (INTEL_INFO(dev)->gen >= 6)
10820 gen6_rps_busy(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +000010821 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +010010822}
10823
10824void intel_mark_idle(struct drm_device *dev)
10825{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010826 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +000010827
Chris Wilsonf62a0072014-02-21 17:55:39 +000010828 if (!dev_priv->mm.busy)
10829 return;
10830
10831 dev_priv->mm.busy = false;
10832
Damien Lespiau3d13ef22014-02-07 19:12:47 +000010833 if (INTEL_INFO(dev)->gen >= 6)
Chris Wilsonb29c19b2013-09-25 17:34:56 +010010834 gen6_rps_idle(dev->dev_private);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -030010835
Paulo Zanoni43694d62014-03-07 20:08:08 -030010836 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +010010837}
10838
Jesse Barnes79e53942008-11-07 14:24:08 -080010839static void intel_crtc_destroy(struct drm_crtc *crtc)
10840{
10841 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010842 struct drm_device *dev = crtc->dev;
10843 struct intel_unpin_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +020010844
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010845 spin_lock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010846 work = intel_crtc->unpin_work;
10847 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010848 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010849
10850 if (work) {
10851 cancel_work_sync(&work->work);
10852 kfree(work);
10853 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010854
10855 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010856
Jesse Barnes79e53942008-11-07 14:24:08 -080010857 kfree(intel_crtc);
10858}
10859
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010860static void intel_unpin_work_fn(struct work_struct *__work)
10861{
10862 struct intel_unpin_work *work =
10863 container_of(__work, struct intel_unpin_work, work);
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010864 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
10865 struct drm_device *dev = crtc->base.dev;
10866 struct drm_plane *primary = crtc->base.primary;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010867
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010868 mutex_lock(&dev->struct_mutex);
Ville Syrjälä3465c582016-02-15 22:54:43 +020010869 intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
Chris Wilson05394f32010-11-08 19:18:58 +000010870 drm_gem_object_unreference(&work->pending_flip_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +000010871
John Harrisonf06cc1b2014-11-24 18:49:37 +000010872 if (work->flip_queued_req)
John Harrison146d84f2014-12-05 13:49:33 +000010873 i915_gem_request_assign(&work->flip_queued_req, NULL);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010874 mutex_unlock(&dev->struct_mutex);
10875
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010876 intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
Paulo Zanoni1eb52232016-01-19 11:35:44 -020010877 intel_fbc_post_update(crtc);
Chris Wilson89ed88b2015-02-16 14:31:49 +000010878 drm_framebuffer_unreference(work->old_fb);
Daniel Vetterf99d7062014-06-19 16:01:59 +020010879
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010880 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
10881 atomic_dec(&crtc->unpin_work_count);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010882
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010883 kfree(work);
10884}
10885
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010886static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +010010887 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010888{
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010889 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10890 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010891 unsigned long flags;
10892
10893 /* Ignore early vblank irqs */
10894 if (intel_crtc == NULL)
10895 return;
10896
Daniel Vetterf3260382014-09-15 14:55:23 +020010897 /*
10898 * This is called both by irq handlers and the reset code (to complete
10899 * lost pageflips) so needs the full irqsave spinlocks.
10900 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010901 spin_lock_irqsave(&dev->event_lock, flags);
10902 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +000010903
10904 /* Ensure we don't miss a work->pending update ... */
10905 smp_rmb();
10906
10907 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010908 spin_unlock_irqrestore(&dev->event_lock, flags);
10909 return;
10910 }
10911
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010912 page_flip_completed(intel_crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +010010913
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010914 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010915}
10916
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010917void intel_finish_page_flip(struct drm_device *dev, int pipe)
10918{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010919 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010920 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10921
Mario Kleiner49b14a52010-12-09 07:00:07 +010010922 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010923}
10924
10925void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
10926{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010927 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010928 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
10929
Mario Kleiner49b14a52010-12-09 07:00:07 +010010930 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010931}
10932
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010933/* Is 'a' after or equal to 'b'? */
10934static bool g4x_flip_count_after_eq(u32 a, u32 b)
10935{
10936 return !((a - b) & 0x80000000);
10937}
10938
10939static bool page_flip_finished(struct intel_crtc *crtc)
10940{
10941 struct drm_device *dev = crtc->base.dev;
10942 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc19ae982016-04-13 17:35:03 +010010943 unsigned reset_counter;
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010944
Chris Wilsonc19ae982016-04-13 17:35:03 +010010945 reset_counter = i915_reset_counter(&dev_priv->gpu_error);
Chris Wilson7f1847e2016-04-13 17:35:04 +010010946 if (crtc->reset_counter != reset_counter)
Ville Syrjäläbdfa7542014-05-27 21:33:09 +030010947 return true;
10948
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010949 /*
10950 * The relevant registers doen't exist on pre-ctg.
10951 * As the flip done interrupt doesn't trigger for mmio
10952 * flips on gmch platforms, a flip count check isn't
10953 * really needed there. But since ctg has the registers,
10954 * include it in the check anyway.
10955 */
10956 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10957 return true;
10958
10959 /*
Maarten Lankhorste8861672016-02-24 11:24:26 +010010960 * BDW signals flip done immediately if the plane
10961 * is disabled, even if the plane enable is already
10962 * armed to occur at the next vblank :(
10963 */
10964
10965 /*
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010966 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10967 * used the same base address. In that case the mmio flip might
10968 * have completed, but the CS hasn't even executed the flip yet.
10969 *
10970 * A flip count check isn't enough as the CS might have updated
10971 * the base address just after start of vblank, but before we
10972 * managed to process the interrupt. This means we'd complete the
10973 * CS flip too soon.
10974 *
10975 * Combining both checks should get us a good enough result. It may
10976 * still happen that the CS flip has been executed, but has not
10977 * yet actually completed. But in case the base address is the same
10978 * anyway, we don't really care.
10979 */
10980 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
10981 crtc->unpin_work->gtt_offset &&
Ville Syrjäläfd8f507c2015-09-18 20:03:42 +030010982 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010983 crtc->unpin_work->flip_count);
10984}
10985
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010986void intel_prepare_page_flip(struct drm_device *dev, int plane)
10987{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010988 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010989 struct intel_crtc *intel_crtc =
10990 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
10991 unsigned long flags;
10992
Daniel Vetterf3260382014-09-15 14:55:23 +020010993
10994 /*
10995 * This is called both by irq handlers and the reset code (to complete
10996 * lost pageflips) so needs the full irqsave spinlocks.
10997 *
10998 * NB: An MMIO update of the plane base pointer will also
Chris Wilsone7d841c2012-12-03 11:36:30 +000010999 * generate a page-flip completion irq, i.e. every modeset
11000 * is also accompanied by a spurious intel_prepare_page_flip().
11001 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011002 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011003 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
Chris Wilsone7d841c2012-12-03 11:36:30 +000011004 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011005 spin_unlock_irqrestore(&dev->event_lock, flags);
11006}
11007
Chris Wilson60426392015-10-10 10:44:32 +010011008static inline void intel_mark_page_flip_active(struct intel_unpin_work *work)
Chris Wilsone7d841c2012-12-03 11:36:30 +000011009{
11010 /* Ensure that the work item is consistent when activating it ... */
11011 smp_wmb();
Chris Wilson60426392015-10-10 10:44:32 +010011012 atomic_set(&work->pending, INTEL_FLIP_PENDING);
Chris Wilsone7d841c2012-12-03 11:36:30 +000011013 /* and that it is marked active as soon as the irq could fire. */
11014 smp_wmb();
11015}
11016
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011017static int intel_gen2_queue_flip(struct drm_device *dev,
11018 struct drm_crtc *crtc,
11019 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011020 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011021 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011022 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011023{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011024 struct intel_engine_cs *engine = req->engine;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011025 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011026 u32 flip_mask;
11027 int ret;
11028
John Harrison5fb9de12015-05-29 17:44:07 +010011029 ret = intel_ring_begin(req, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011030 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011031 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011032
11033 /* Can't queue multiple flips, so wait for the previous
11034 * one to finish before executing the next.
11035 */
11036 if (intel_crtc->plane)
11037 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11038 else
11039 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011040 intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
11041 intel_ring_emit(engine, MI_NOOP);
11042 intel_ring_emit(engine, MI_DISPLAY_FLIP |
Daniel Vetter6d90c952012-04-26 23:28:05 +020011043 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011044 intel_ring_emit(engine, fb->pitches[0]);
11045 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
11046 intel_ring_emit(engine, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +000011047
Chris Wilson60426392015-10-10 10:44:32 +010011048 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011049 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011050}
11051
11052static int intel_gen3_queue_flip(struct drm_device *dev,
11053 struct drm_crtc *crtc,
11054 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011055 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011056 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011057 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011058{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011059 struct intel_engine_cs *engine = req->engine;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011060 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011061 u32 flip_mask;
11062 int ret;
11063
John Harrison5fb9de12015-05-29 17:44:07 +010011064 ret = intel_ring_begin(req, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011065 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011066 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011067
11068 if (intel_crtc->plane)
11069 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11070 else
11071 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011072 intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
11073 intel_ring_emit(engine, MI_NOOP);
11074 intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 |
Daniel Vetter6d90c952012-04-26 23:28:05 +020011075 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011076 intel_ring_emit(engine, fb->pitches[0]);
11077 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
11078 intel_ring_emit(engine, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011079
Chris Wilson60426392015-10-10 10:44:32 +010011080 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011081 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011082}
11083
11084static int intel_gen4_queue_flip(struct drm_device *dev,
11085 struct drm_crtc *crtc,
11086 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011087 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011088 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011089 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011090{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011091 struct intel_engine_cs *engine = req->engine;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011092 struct drm_i915_private *dev_priv = dev->dev_private;
11093 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11094 uint32_t pf, pipesrc;
11095 int ret;
11096
John Harrison5fb9de12015-05-29 17:44:07 +010011097 ret = intel_ring_begin(req, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011098 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011099 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011100
11101 /* i965+ uses the linear or tiled offsets from the
11102 * Display Registers (which do not change across a page-flip)
11103 * so we need only reprogram the base address.
11104 */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011105 intel_ring_emit(engine, MI_DISPLAY_FLIP |
Daniel Vetter6d90c952012-04-26 23:28:05 +020011106 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011107 intel_ring_emit(engine, fb->pitches[0]);
11108 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset |
Daniel Vetterc2c75132012-07-05 12:17:30 +020011109 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011110
11111 /* XXX Enabling the panel-fitter across page-flip is so far
11112 * untested on non-native modes, so ignore it for now.
11113 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
11114 */
11115 pf = 0;
11116 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011117 intel_ring_emit(engine, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000011118
Chris Wilson60426392015-10-10 10:44:32 +010011119 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011120 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011121}
11122
11123static int intel_gen6_queue_flip(struct drm_device *dev,
11124 struct drm_crtc *crtc,
11125 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011126 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011127 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011128 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011129{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011130 struct intel_engine_cs *engine = req->engine;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011131 struct drm_i915_private *dev_priv = dev->dev_private;
11132 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11133 uint32_t pf, pipesrc;
11134 int ret;
11135
John Harrison5fb9de12015-05-29 17:44:07 +010011136 ret = intel_ring_begin(req, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011137 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011138 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011139
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011140 intel_ring_emit(engine, MI_DISPLAY_FLIP |
Daniel Vetter6d90c952012-04-26 23:28:05 +020011141 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011142 intel_ring_emit(engine, fb->pitches[0] | obj->tiling_mode);
11143 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011144
Chris Wilson99d9acd2012-04-17 20:37:00 +010011145 /* Contrary to the suggestions in the documentation,
11146 * "Enable Panel Fitter" does not seem to be required when page
11147 * flipping with a non-native mode, and worse causes a normal
11148 * modeset to fail.
11149 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11150 */
11151 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011152 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011153 intel_ring_emit(engine, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000011154
Chris Wilson60426392015-10-10 10:44:32 +010011155 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011156 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011157}
11158
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011159static int intel_gen7_queue_flip(struct drm_device *dev,
11160 struct drm_crtc *crtc,
11161 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011162 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011163 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011164 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011165{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011166 struct intel_engine_cs *engine = req->engine;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011167 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011168 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +010011169 int len, ret;
11170
Robin Schroereba905b2014-05-18 02:24:50 +020011171 switch (intel_crtc->plane) {
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011172 case PLANE_A:
11173 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11174 break;
11175 case PLANE_B:
11176 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11177 break;
11178 case PLANE_C:
11179 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11180 break;
11181 default:
11182 WARN_ONCE(1, "unknown plane in flip command\n");
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011183 return -ENODEV;
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011184 }
11185
Chris Wilsonffe74d72013-08-26 20:58:12 +010011186 len = 4;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011187 if (engine->id == RCS) {
Chris Wilsonffe74d72013-08-26 20:58:12 +010011188 len += 6;
Damien Lespiauf4768282014-04-07 20:24:34 +010011189 /*
11190 * On Gen 8, SRM is now taking an extra dword to accommodate
11191 * 48bits addresses, and we need a NOOP for the batch size to
11192 * stay even.
11193 */
11194 if (IS_GEN8(dev))
11195 len += 2;
11196 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010011197
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011198 /*
11199 * BSpec MI_DISPLAY_FLIP for IVB:
11200 * "The full packet must be contained within the same cache line."
11201 *
11202 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11203 * cacheline, if we ever start emitting more commands before
11204 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11205 * then do the cacheline alignment, and finally emit the
11206 * MI_DISPLAY_FLIP.
11207 */
John Harrisonbba09b12015-05-29 17:44:06 +010011208 ret = intel_ring_cacheline_align(req);
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011209 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011210 return ret;
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011211
John Harrison5fb9de12015-05-29 17:44:07 +010011212 ret = intel_ring_begin(req, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011213 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011214 return ret;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011215
Chris Wilsonffe74d72013-08-26 20:58:12 +010011216 /* Unmask the flip-done completion message. Note that the bspec says that
11217 * we should do this for both the BCS and RCS, and that we must not unmask
11218 * more than one flip event at any time (or ensure that one flip message
11219 * can be sent by waiting for flip-done prior to queueing new flips).
11220 * Experimentation says that BCS works despite DERRMR masking all
11221 * flip-done completion events and that unmasking all planes at once
11222 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11223 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11224 */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011225 if (engine->id == RCS) {
11226 intel_ring_emit(engine, MI_LOAD_REGISTER_IMM(1));
11227 intel_ring_emit_reg(engine, DERRMR);
11228 intel_ring_emit(engine, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
11229 DERRMR_PIPEB_PRI_FLIP_DONE |
11230 DERRMR_PIPEC_PRI_FLIP_DONE));
Damien Lespiauf4768282014-04-07 20:24:34 +010011231 if (IS_GEN8(dev))
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011232 intel_ring_emit(engine, MI_STORE_REGISTER_MEM_GEN8 |
Damien Lespiauf4768282014-04-07 20:24:34 +010011233 MI_SRM_LRM_GLOBAL_GTT);
11234 else
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011235 intel_ring_emit(engine, MI_STORE_REGISTER_MEM |
Damien Lespiauf4768282014-04-07 20:24:34 +010011236 MI_SRM_LRM_GLOBAL_GTT);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011237 intel_ring_emit_reg(engine, DERRMR);
11238 intel_ring_emit(engine, engine->scratch.gtt_offset + 256);
Damien Lespiauf4768282014-04-07 20:24:34 +010011239 if (IS_GEN8(dev)) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011240 intel_ring_emit(engine, 0);
11241 intel_ring_emit(engine, MI_NOOP);
Damien Lespiauf4768282014-04-07 20:24:34 +010011242 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010011243 }
11244
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011245 intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 | plane_bit);
11246 intel_ring_emit(engine, (fb->pitches[0] | obj->tiling_mode));
11247 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
11248 intel_ring_emit(engine, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +000011249
Chris Wilson60426392015-10-10 10:44:32 +010011250 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011251 return 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011252}
11253
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +000011254static bool use_mmio_flip(struct intel_engine_cs *engine,
Sourab Gupta84c33a62014-06-02 16:47:17 +053011255 struct drm_i915_gem_object *obj)
11256{
11257 /*
11258 * This is not being used for older platforms, because
11259 * non-availability of flip done interrupt forces us to use
11260 * CS flips. Older platforms derive flip done using some clever
11261 * tricks involving the flip_pending status bits and vblank irqs.
11262 * So using MMIO flips there would disrupt this mechanism.
11263 */
11264
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +000011265 if (engine == NULL)
Chris Wilson8e09bf82014-07-08 10:40:30 +010011266 return true;
11267
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +000011268 if (INTEL_INFO(engine->dev)->gen < 5)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011269 return false;
11270
11271 if (i915.use_mmio_flip < 0)
11272 return false;
11273 else if (i915.use_mmio_flip > 0)
11274 return true;
Oscar Mateo14bf9932014-07-24 17:04:34 +010011275 else if (i915.enable_execlists)
11276 return true;
Alex Goinsfd8e0582015-11-25 18:43:38 -080011277 else if (obj->base.dma_buf &&
11278 !reservation_object_test_signaled_rcu(obj->base.dma_buf->resv,
11279 false))
11280 return true;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011281 else
Tvrtko Ursulin666796d2016-03-16 11:00:39 +000011282 return engine != i915_gem_request_get_engine(obj->last_write_req);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011283}
11284
Chris Wilson60426392015-10-10 10:44:32 +010011285static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011286 unsigned int rotation,
Chris Wilson60426392015-10-10 10:44:32 +010011287 struct intel_unpin_work *work)
Damien Lespiauff944562014-11-20 14:58:16 +000011288{
11289 struct drm_device *dev = intel_crtc->base.dev;
11290 struct drm_i915_private *dev_priv = dev->dev_private;
11291 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
Damien Lespiauff944562014-11-20 14:58:16 +000011292 const enum pipe pipe = intel_crtc->pipe;
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011293 u32 ctl, stride, tile_height;
Damien Lespiauff944562014-11-20 14:58:16 +000011294
11295 ctl = I915_READ(PLANE_CTL(pipe, 0));
11296 ctl &= ~PLANE_CTL_TILED_MASK;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011297 switch (fb->modifier[0]) {
11298 case DRM_FORMAT_MOD_NONE:
11299 break;
11300 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauff944562014-11-20 14:58:16 +000011301 ctl |= PLANE_CTL_TILED_X;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011302 break;
11303 case I915_FORMAT_MOD_Y_TILED:
11304 ctl |= PLANE_CTL_TILED_Y;
11305 break;
11306 case I915_FORMAT_MOD_Yf_TILED:
11307 ctl |= PLANE_CTL_TILED_YF;
11308 break;
11309 default:
11310 MISSING_CASE(fb->modifier[0]);
11311 }
Damien Lespiauff944562014-11-20 14:58:16 +000011312
11313 /*
11314 * The stride is either expressed as a multiple of 64 bytes chunks for
11315 * linear buffers or in number of tiles for tiled buffers.
11316 */
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011317 if (intel_rotation_90_or_270(rotation)) {
11318 /* stride = Surface height in tiles */
Ville Syrjälä832be822016-01-12 21:08:33 +020011319 tile_height = intel_tile_height(dev_priv, fb->modifier[0], 0);
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011320 stride = DIV_ROUND_UP(fb->height, tile_height);
11321 } else {
11322 stride = fb->pitches[0] /
Ville Syrjälä7b49f942016-01-12 21:08:32 +020011323 intel_fb_stride_alignment(dev_priv, fb->modifier[0],
11324 fb->pixel_format);
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011325 }
Damien Lespiauff944562014-11-20 14:58:16 +000011326
11327 /*
11328 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11329 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11330 */
11331 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11332 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11333
Chris Wilson60426392015-10-10 10:44:32 +010011334 I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
Damien Lespiauff944562014-11-20 14:58:16 +000011335 POSTING_READ(PLANE_SURF(pipe, 0));
11336}
11337
Chris Wilson60426392015-10-10 10:44:32 +010011338static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
11339 struct intel_unpin_work *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011340{
11341 struct drm_device *dev = intel_crtc->base.dev;
11342 struct drm_i915_private *dev_priv = dev->dev_private;
11343 struct intel_framebuffer *intel_fb =
11344 to_intel_framebuffer(intel_crtc->base.primary->fb);
11345 struct drm_i915_gem_object *obj = intel_fb->obj;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020011346 i915_reg_t reg = DSPCNTR(intel_crtc->plane);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011347 u32 dspcntr;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011348
Sourab Gupta84c33a62014-06-02 16:47:17 +053011349 dspcntr = I915_READ(reg);
11350
Damien Lespiauc5d97472014-10-25 00:11:11 +010011351 if (obj->tiling_mode != I915_TILING_NONE)
11352 dspcntr |= DISPPLANE_TILED;
11353 else
11354 dspcntr &= ~DISPPLANE_TILED;
11355
Sourab Gupta84c33a62014-06-02 16:47:17 +053011356 I915_WRITE(reg, dspcntr);
11357
Chris Wilson60426392015-10-10 10:44:32 +010011358 I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011359 POSTING_READ(DSPSURF(intel_crtc->plane));
Damien Lespiauff944562014-11-20 14:58:16 +000011360}
11361
11362/*
11363 * XXX: This is the temporary way to update the plane registers until we get
11364 * around to using the usual plane update functions for MMIO flips
11365 */
Chris Wilson60426392015-10-10 10:44:32 +010011366static void intel_do_mmio_flip(struct intel_mmio_flip *mmio_flip)
Damien Lespiauff944562014-11-20 14:58:16 +000011367{
Chris Wilson60426392015-10-10 10:44:32 +010011368 struct intel_crtc *crtc = mmio_flip->crtc;
11369 struct intel_unpin_work *work;
Damien Lespiauff944562014-11-20 14:58:16 +000011370
Chris Wilson60426392015-10-10 10:44:32 +010011371 spin_lock_irq(&crtc->base.dev->event_lock);
11372 work = crtc->unpin_work;
11373 spin_unlock_irq(&crtc->base.dev->event_lock);
11374 if (work == NULL)
11375 return;
Damien Lespiauff944562014-11-20 14:58:16 +000011376
Chris Wilson60426392015-10-10 10:44:32 +010011377 intel_mark_page_flip_active(work);
Damien Lespiauff944562014-11-20 14:58:16 +000011378
Chris Wilson60426392015-10-10 10:44:32 +010011379 intel_pipe_update_start(crtc);
11380
11381 if (INTEL_INFO(mmio_flip->i915)->gen >= 9)
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011382 skl_do_mmio_flip(crtc, mmio_flip->rotation, work);
Damien Lespiauff944562014-11-20 14:58:16 +000011383 else
11384 /* use_mmio_flip() retricts MMIO flips to ilk+ */
Chris Wilson60426392015-10-10 10:44:32 +010011385 ilk_do_mmio_flip(crtc, work);
Damien Lespiauff944562014-11-20 14:58:16 +000011386
Chris Wilson60426392015-10-10 10:44:32 +010011387 intel_pipe_update_end(crtc);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011388}
11389
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020011390static void intel_mmio_flip_work_func(struct work_struct *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011391{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011392 struct intel_mmio_flip *mmio_flip =
11393 container_of(work, struct intel_mmio_flip, work);
Alex Goinsfd8e0582015-11-25 18:43:38 -080011394 struct intel_framebuffer *intel_fb =
11395 to_intel_framebuffer(mmio_flip->crtc->base.primary->fb);
11396 struct drm_i915_gem_object *obj = intel_fb->obj;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011397
Chris Wilson60426392015-10-10 10:44:32 +010011398 if (mmio_flip->req) {
Daniel Vettereed29a52015-05-21 14:21:25 +020011399 WARN_ON(__i915_wait_request(mmio_flip->req,
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011400 false, NULL,
11401 &mmio_flip->i915->rps.mmioflips));
Chris Wilson60426392015-10-10 10:44:32 +010011402 i915_gem_request_unreference__unlocked(mmio_flip->req);
11403 }
Sourab Gupta84c33a62014-06-02 16:47:17 +053011404
Alex Goinsfd8e0582015-11-25 18:43:38 -080011405 /* For framebuffer backed by dmabuf, wait for fence */
11406 if (obj->base.dma_buf)
11407 WARN_ON(reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
11408 false, false,
11409 MAX_SCHEDULE_TIMEOUT) < 0);
11410
Chris Wilson60426392015-10-10 10:44:32 +010011411 intel_do_mmio_flip(mmio_flip);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011412 kfree(mmio_flip);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011413}
11414
11415static int intel_queue_mmio_flip(struct drm_device *dev,
11416 struct drm_crtc *crtc,
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011417 struct drm_i915_gem_object *obj)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011418{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011419 struct intel_mmio_flip *mmio_flip;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011420
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011421 mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
11422 if (mmio_flip == NULL)
11423 return -ENOMEM;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011424
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011425 mmio_flip->i915 = to_i915(dev);
Daniel Vettereed29a52015-05-21 14:21:25 +020011426 mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011427 mmio_flip->crtc = to_intel_crtc(crtc);
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011428 mmio_flip->rotation = crtc->primary->state->rotation;
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011429
11430 INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
11431 schedule_work(&mmio_flip->work);
Ander Conselvan de Oliveira536f5b52014-11-06 11:03:40 +020011432
Sourab Gupta84c33a62014-06-02 16:47:17 +053011433 return 0;
11434}
11435
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011436static int intel_default_queue_flip(struct drm_device *dev,
11437 struct drm_crtc *crtc,
11438 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011439 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011440 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011441 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011442{
11443 return -ENODEV;
11444}
11445
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011446static bool __intel_pageflip_stall_check(struct drm_device *dev,
11447 struct drm_crtc *crtc)
11448{
11449 struct drm_i915_private *dev_priv = dev->dev_private;
11450 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11451 struct intel_unpin_work *work = intel_crtc->unpin_work;
11452 u32 addr;
11453
11454 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
11455 return true;
11456
Chris Wilson908565c2015-08-12 13:08:22 +010011457 if (atomic_read(&work->pending) < INTEL_FLIP_PENDING)
11458 return false;
11459
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011460 if (!work->enable_stall_check)
11461 return false;
11462
11463 if (work->flip_ready_vblank == 0) {
Daniel Vetter3a8a9462014-11-26 14:39:48 +010011464 if (work->flip_queued_req &&
11465 !i915_gem_request_completed(work->flip_queued_req, true))
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011466 return false;
11467
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011468 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011469 }
11470
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011471 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011472 return false;
11473
11474 /* Potential stall - if we see that the flip has happened,
11475 * assume a missed interrupt. */
11476 if (INTEL_INFO(dev)->gen >= 4)
11477 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11478 else
11479 addr = I915_READ(DSPADDR(intel_crtc->plane));
11480
11481 /* There is a potential issue here with a false positive after a flip
11482 * to the same address. We could address this by checking for a
11483 * non-incrementing frame counter.
11484 */
11485 return addr == work->gtt_offset;
11486}
11487
11488void intel_check_page_flip(struct drm_device *dev, int pipe)
11489{
11490 struct drm_i915_private *dev_priv = dev->dev_private;
11491 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11492 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011493 struct intel_unpin_work *work;
Daniel Vetterf3260382014-09-15 14:55:23 +020011494
Dave Gordon6c51d462015-03-06 15:34:26 +000011495 WARN_ON(!in_interrupt());
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011496
11497 if (crtc == NULL)
11498 return;
11499
Daniel Vetterf3260382014-09-15 14:55:23 +020011500 spin_lock(&dev->event_lock);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011501 work = intel_crtc->unpin_work;
11502 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011503 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
Chris Wilson6ad790c2015-04-07 16:20:31 +010011504 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011505 page_flip_completed(intel_crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011506 work = NULL;
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011507 }
Chris Wilson6ad790c2015-04-07 16:20:31 +010011508 if (work != NULL &&
11509 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
11510 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
Daniel Vetterf3260382014-09-15 14:55:23 +020011511 spin_unlock(&dev->event_lock);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011512}
11513
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011514static int intel_crtc_page_flip(struct drm_crtc *crtc,
11515 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011516 struct drm_pending_vblank_event *event,
11517 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011518{
11519 struct drm_device *dev = crtc->dev;
11520 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -070011521 struct drm_framebuffer *old_fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -070011522 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011523 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gustavo Padovan455a6802014-12-01 15:40:11 -080011524 struct drm_plane *primary = crtc->primary;
Daniel Vettera071fa02014-06-18 23:28:09 +020011525 enum pipe pipe = intel_crtc->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011526 struct intel_unpin_work *work;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011527 struct intel_engine_cs *engine;
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011528 bool mmio_flip;
John Harrison91af1272015-06-18 13:14:56 +010011529 struct drm_i915_gem_request *request = NULL;
Chris Wilson52e68632010-08-08 10:15:59 +010011530 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011531
Matt Roper2ff8fde2014-07-08 07:50:07 -070011532 /*
11533 * drm_mode_page_flip_ioctl() should already catch this, but double
11534 * check to be safe. In the future we may enable pageflipping from
11535 * a disabled primary plane.
11536 */
11537 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11538 return -EBUSY;
11539
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011540 /* Can't change pixel format via MI display flips. */
Matt Roperf4510a22014-04-01 15:22:40 -070011541 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011542 return -EINVAL;
11543
11544 /*
11545 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11546 * Note that pitch changes could also affect these register.
11547 */
11548 if (INTEL_INFO(dev)->gen > 3 &&
Matt Roperf4510a22014-04-01 15:22:40 -070011549 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11550 fb->pitches[0] != crtc->primary->fb->pitches[0]))
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011551 return -EINVAL;
11552
Chris Wilsonf900db42014-02-20 09:26:13 +000011553 if (i915_terminally_wedged(&dev_priv->gpu_error))
11554 goto out_hang;
11555
Daniel Vetterb14c5672013-09-19 12:18:32 +020011556 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011557 if (work == NULL)
11558 return -ENOMEM;
11559
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011560 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011561 work->crtc = crtc;
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011562 work->old_fb = old_fb;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011563 INIT_WORK(&work->work, intel_unpin_work_fn);
11564
Daniel Vetter87b6b102014-05-15 15:33:46 +020011565 ret = drm_crtc_vblank_get(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011566 if (ret)
11567 goto free_work;
11568
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011569 /* We borrow the event spin lock for protecting unpin_work */
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011570 spin_lock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011571 if (intel_crtc->unpin_work) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011572 /* Before declaring the flip queue wedged, check if
11573 * the hardware completed the operation behind our backs.
11574 */
11575 if (__intel_pageflip_stall_check(dev, crtc)) {
11576 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11577 page_flip_completed(intel_crtc);
11578 } else {
11579 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011580 spin_unlock_irq(&dev->event_lock);
Chris Wilson468f0b42010-05-27 13:18:13 +010011581
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011582 drm_crtc_vblank_put(crtc);
11583 kfree(work);
11584 return -EBUSY;
11585 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011586 }
11587 intel_crtc->unpin_work = work;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011588 spin_unlock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011589
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011590 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11591 flush_workqueue(dev_priv->wq);
11592
Jesse Barnes75dfca82010-02-10 15:09:44 -080011593 /* Reference the objects for the scheduled work. */
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011594 drm_framebuffer_reference(work->old_fb);
Chris Wilson05394f32010-11-08 19:18:58 +000011595 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011596
Matt Roperf4510a22014-04-01 15:22:40 -070011597 crtc->primary->fb = fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011598 update_state_fb(crtc->primary);
Paulo Zanonie8216e52016-01-19 11:35:56 -020011599 intel_fbc_pre_update(intel_crtc);
Matt Roper1ed1f962015-01-30 16:22:36 -080011600
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011601 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011602
Chris Wilson89ed88b2015-02-16 14:31:49 +000011603 ret = i915_mutex_lock_interruptible(dev);
11604 if (ret)
11605 goto cleanup;
11606
Chris Wilsonc19ae982016-04-13 17:35:03 +010011607 intel_crtc->reset_counter = i915_reset_counter(&dev_priv->gpu_error);
Chris Wilson7f1847e2016-04-13 17:35:04 +010011608 if (__i915_reset_in_progress_or_wedged(intel_crtc->reset_counter)) {
11609 ret = -EIO;
11610 goto cleanup;
11611 }
11612
11613 atomic_inc(&intel_crtc->unpin_work_count);
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011614
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011615 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
Ville Syrjäläfd8f507c2015-09-18 20:03:42 +030011616 work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011617
Wayne Boyer666a4532015-12-09 12:29:35 -080011618 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011619 engine = &dev_priv->engine[BCS];
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011620 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
Chris Wilson8e09bf82014-07-08 10:40:30 +010011621 /* vlv: DISPLAY_FLIP fails to change tiling */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011622 engine = NULL;
Chris Wilson48bf5b22014-12-27 09:48:28 +000011623 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011624 engine = &dev_priv->engine[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011625 } else if (INTEL_INFO(dev)->gen >= 7) {
Tvrtko Ursulin666796d2016-03-16 11:00:39 +000011626 engine = i915_gem_request_get_engine(obj->last_write_req);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011627 if (engine == NULL || engine->id != RCS)
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011628 engine = &dev_priv->engine[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011629 } else {
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011630 engine = &dev_priv->engine[RCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011631 }
11632
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011633 mmio_flip = use_mmio_flip(engine, obj);
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011634
11635 /* When using CS flips, we want to emit semaphores between rings.
11636 * However, when using mmio flips we will create a task to do the
11637 * synchronisation, so all we want here is to pin the framebuffer
11638 * into the display plane and skip any waits.
11639 */
Maarten Lankhorst7580d772015-08-18 13:40:06 +020011640 if (!mmio_flip) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011641 ret = i915_gem_object_sync(obj, engine, &request);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020011642 if (ret)
11643 goto cleanup_pending;
11644 }
11645
Ville Syrjälä3465c582016-02-15 22:54:43 +020011646 ret = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011647 if (ret)
11648 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011649
Tvrtko Ursulindedf2782015-09-21 10:45:35 +010011650 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary),
11651 obj, 0);
11652 work->gtt_offset += intel_crtc->dspaddr_offset;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011653
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011654 if (mmio_flip) {
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011655 ret = intel_queue_mmio_flip(dev, crtc, obj);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011656 if (ret)
11657 goto cleanup_unpin;
11658
John Harrisonf06cc1b2014-11-24 18:49:37 +000011659 i915_gem_request_assign(&work->flip_queued_req,
11660 obj->last_write_req);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011661 } else {
John Harrison6258fbe2015-05-29 17:43:48 +010011662 if (!request) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011663 request = i915_gem_request_alloc(engine, NULL);
Dave Gordon26827082016-01-19 19:02:53 +000011664 if (IS_ERR(request)) {
11665 ret = PTR_ERR(request);
John Harrison6258fbe2015-05-29 17:43:48 +010011666 goto cleanup_unpin;
Dave Gordon26827082016-01-19 19:02:53 +000011667 }
John Harrison6258fbe2015-05-29 17:43:48 +010011668 }
11669
11670 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011671 page_flip_flags);
11672 if (ret)
11673 goto cleanup_unpin;
11674
John Harrison6258fbe2015-05-29 17:43:48 +010011675 i915_gem_request_assign(&work->flip_queued_req, request);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011676 }
11677
John Harrison91af1272015-06-18 13:14:56 +010011678 if (request)
John Harrison75289872015-05-29 17:43:49 +010011679 i915_add_request_no_flush(request);
John Harrison91af1272015-06-18 13:14:56 +010011680
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011681 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011682 work->enable_stall_check = true;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011683
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011684 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011685 to_intel_plane(primary)->frontbuffer_bit);
Paulo Zanonic80ac852015-07-02 19:25:13 -030011686 mutex_unlock(&dev->struct_mutex);
Daniel Vettera071fa02014-06-18 23:28:09 +020011687
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011688 intel_frontbuffer_flip_prepare(dev,
11689 to_intel_plane(primary)->frontbuffer_bit);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011690
Jesse Barnese5510fa2010-07-01 16:48:37 -070011691 trace_i915_flip_request(intel_crtc->plane, obj);
11692
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011693 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +010011694
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011695cleanup_unpin:
Ville Syrjälä3465c582016-02-15 22:54:43 +020011696 intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011697cleanup_pending:
Dave Gordon0aa498d2016-01-28 10:48:09 +000011698 if (!IS_ERR_OR_NULL(request))
Chris Wilsonaa9b7812016-04-13 17:35:15 +010011699 i915_add_request_no_flush(request);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011700 atomic_dec(&intel_crtc->unpin_work_count);
Chris Wilson89ed88b2015-02-16 14:31:49 +000011701 mutex_unlock(&dev->struct_mutex);
11702cleanup:
Matt Roperf4510a22014-04-01 15:22:40 -070011703 crtc->primary->fb = old_fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011704 update_state_fb(crtc->primary);
Chris Wilson96b099f2010-06-07 14:03:04 +010011705
Chris Wilson89ed88b2015-02-16 14:31:49 +000011706 drm_gem_object_unreference_unlocked(&obj->base);
11707 drm_framebuffer_unreference(work->old_fb);
11708
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011709 spin_lock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011710 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011711 spin_unlock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011712
Daniel Vetter87b6b102014-05-15 15:33:46 +020011713 drm_crtc_vblank_put(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011714free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +010011715 kfree(work);
11716
Chris Wilsonf900db42014-02-20 09:26:13 +000011717 if (ret == -EIO) {
Maarten Lankhorst02e0efb2015-06-12 11:15:40 +020011718 struct drm_atomic_state *state;
11719 struct drm_plane_state *plane_state;
11720
Chris Wilsonf900db42014-02-20 09:26:13 +000011721out_hang:
Maarten Lankhorst02e0efb2015-06-12 11:15:40 +020011722 state = drm_atomic_state_alloc(dev);
11723 if (!state)
11724 return -ENOMEM;
11725 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
11726
11727retry:
11728 plane_state = drm_atomic_get_plane_state(state, primary);
11729 ret = PTR_ERR_OR_ZERO(plane_state);
11730 if (!ret) {
11731 drm_atomic_set_fb_for_plane(plane_state, fb);
11732
11733 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
11734 if (!ret)
11735 ret = drm_atomic_commit(state);
11736 }
11737
11738 if (ret == -EDEADLK) {
11739 drm_modeset_backoff(state->acquire_ctx);
11740 drm_atomic_state_clear(state);
11741 goto retry;
11742 }
11743
11744 if (ret)
11745 drm_atomic_state_free(state);
11746
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011747 if (ret == 0 && event) {
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011748 spin_lock_irq(&dev->event_lock);
Gustavo Padovan560ce1d2016-04-14 10:48:15 -070011749 drm_crtc_send_vblank_event(crtc, event);
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011750 spin_unlock_irq(&dev->event_lock);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011751 }
Chris Wilsonf900db42014-02-20 09:26:13 +000011752 }
Chris Wilson96b099f2010-06-07 14:03:04 +010011753 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011754}
11755
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011756
11757/**
11758 * intel_wm_need_update - Check whether watermarks need updating
11759 * @plane: drm plane
11760 * @state: new plane state
11761 *
11762 * Check current plane state versus the new one to determine whether
11763 * watermarks need to be recalculated.
11764 *
11765 * Returns true or false.
11766 */
11767static bool intel_wm_need_update(struct drm_plane *plane,
11768 struct drm_plane_state *state)
11769{
Matt Roperd21fbe82015-09-24 15:53:12 -070011770 struct intel_plane_state *new = to_intel_plane_state(state);
11771 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
11772
11773 /* Update watermarks on tiling or size changes. */
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010011774 if (new->visible != cur->visible)
11775 return true;
11776
11777 if (!cur->base.fb || !new->base.fb)
11778 return false;
11779
11780 if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
11781 cur->base.rotation != new->base.rotation ||
Matt Roperd21fbe82015-09-24 15:53:12 -070011782 drm_rect_width(&new->src) != drm_rect_width(&cur->src) ||
11783 drm_rect_height(&new->src) != drm_rect_height(&cur->src) ||
11784 drm_rect_width(&new->dst) != drm_rect_width(&cur->dst) ||
11785 drm_rect_height(&new->dst) != drm_rect_height(&cur->dst))
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011786 return true;
11787
11788 return false;
11789}
11790
Matt Roperd21fbe82015-09-24 15:53:12 -070011791static bool needs_scaling(struct intel_plane_state *state)
11792{
11793 int src_w = drm_rect_width(&state->src) >> 16;
11794 int src_h = drm_rect_height(&state->src) >> 16;
11795 int dst_w = drm_rect_width(&state->dst);
11796 int dst_h = drm_rect_height(&state->dst);
11797
11798 return (src_w != dst_w || src_h != dst_h);
11799}
11800
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011801int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
11802 struct drm_plane_state *plane_state)
11803{
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010011804 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011805 struct drm_crtc *crtc = crtc_state->crtc;
11806 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11807 struct drm_plane *plane = plane_state->plane;
11808 struct drm_device *dev = crtc->dev;
Matt Ropered4a6a72016-02-23 17:20:13 -080011809 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011810 struct intel_plane_state *old_plane_state =
11811 to_intel_plane_state(plane->state);
11812 int idx = intel_crtc->base.base.id, ret;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011813 bool mode_changed = needs_modeset(crtc_state);
11814 bool was_crtc_enabled = crtc->state->active;
11815 bool is_crtc_enabled = crtc_state->active;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011816 bool turn_off, turn_on, visible, was_visible;
11817 struct drm_framebuffer *fb = plane_state->fb;
11818
11819 if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
11820 plane->type != DRM_PLANE_TYPE_CURSOR) {
11821 ret = skl_update_scaler_plane(
11822 to_intel_crtc_state(crtc_state),
11823 to_intel_plane_state(plane_state));
11824 if (ret)
11825 return ret;
11826 }
11827
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011828 was_visible = old_plane_state->visible;
11829 visible = to_intel_plane_state(plane_state)->visible;
11830
11831 if (!was_crtc_enabled && WARN_ON(was_visible))
11832 was_visible = false;
11833
Maarten Lankhorst35c08f42015-12-03 14:31:07 +010011834 /*
11835 * Visibility is calculated as if the crtc was on, but
11836 * after scaler setup everything depends on it being off
11837 * when the crtc isn't active.
11838 */
11839 if (!is_crtc_enabled)
11840 to_intel_plane_state(plane_state)->visible = visible = false;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011841
11842 if (!was_visible && !visible)
11843 return 0;
11844
Maarten Lankhorste8861672016-02-24 11:24:26 +010011845 if (fb != old_plane_state->base.fb)
11846 pipe_config->fb_changed = true;
11847
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011848 turn_off = was_visible && (!visible || mode_changed);
11849 turn_on = visible && (!was_visible || mode_changed);
11850
11851 DRM_DEBUG_ATOMIC("[CRTC:%i] has [PLANE:%i] with fb %i\n", idx,
11852 plane->base.id, fb ? fb->base.id : -1);
11853
11854 DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
11855 plane->base.id, was_visible, visible,
11856 turn_off, turn_on, mode_changed);
11857
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011858 if (turn_on) {
11859 pipe_config->update_wm_pre = true;
11860
11861 /* must disable cxsr around plane enable/disable */
11862 if (plane->type != DRM_PLANE_TYPE_CURSOR)
11863 pipe_config->disable_cxsr = true;
11864 } else if (turn_off) {
11865 pipe_config->update_wm_post = true;
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010011866
Ville Syrjälä852eb002015-06-24 22:00:07 +030011867 /* must disable cxsr around plane enable/disable */
Maarten Lankhorste8861672016-02-24 11:24:26 +010011868 if (plane->type != DRM_PLANE_TYPE_CURSOR)
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010011869 pipe_config->disable_cxsr = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011870 } else if (intel_wm_need_update(plane, plane_state)) {
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011871 /* FIXME bollocks */
11872 pipe_config->update_wm_pre = true;
11873 pipe_config->update_wm_post = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011874 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011875
Matt Ropered4a6a72016-02-23 17:20:13 -080011876 /* Pre-gen9 platforms need two-step watermark updates */
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011877 if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
11878 INTEL_INFO(dev)->gen < 9 && dev_priv->display.optimize_watermarks)
Matt Ropered4a6a72016-02-23 17:20:13 -080011879 to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
11880
Rodrigo Vivi8be6ca82015-08-24 16:38:23 -070011881 if (visible || was_visible)
Maarten Lankhorstcd202f62016-03-09 10:35:44 +010011882 pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011883
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +010011884 /*
11885 * WaCxSRDisabledForSpriteScaling:ivb
11886 *
11887 * cstate->update_wm was already set above, so this flag will
11888 * take effect when we commit and program watermarks.
11889 */
11890 if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev) &&
11891 needs_scaling(to_intel_plane_state(plane_state)) &&
11892 !needs_scaling(old_plane_state))
11893 pipe_config->disable_lp_wm = true;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011894
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011895 return 0;
11896}
11897
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011898static bool encoders_cloneable(const struct intel_encoder *a,
11899 const struct intel_encoder *b)
11900{
11901 /* masks could be asymmetric, so check both ways */
11902 return a == b || (a->cloneable & (1 << b->type) &&
11903 b->cloneable & (1 << a->type));
11904}
11905
11906static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11907 struct intel_crtc *crtc,
11908 struct intel_encoder *encoder)
11909{
11910 struct intel_encoder *source_encoder;
11911 struct drm_connector *connector;
11912 struct drm_connector_state *connector_state;
11913 int i;
11914
11915 for_each_connector_in_state(state, connector, connector_state, i) {
11916 if (connector_state->crtc != &crtc->base)
11917 continue;
11918
11919 source_encoder =
11920 to_intel_encoder(connector_state->best_encoder);
11921 if (!encoders_cloneable(encoder, source_encoder))
11922 return false;
11923 }
11924
11925 return true;
11926}
11927
11928static bool check_encoder_cloning(struct drm_atomic_state *state,
11929 struct intel_crtc *crtc)
11930{
11931 struct intel_encoder *encoder;
11932 struct drm_connector *connector;
11933 struct drm_connector_state *connector_state;
11934 int i;
11935
11936 for_each_connector_in_state(state, connector, connector_state, i) {
11937 if (connector_state->crtc != &crtc->base)
11938 continue;
11939
11940 encoder = to_intel_encoder(connector_state->best_encoder);
11941 if (!check_single_encoder_cloning(state, crtc, encoder))
11942 return false;
11943 }
11944
11945 return true;
11946}
11947
11948static int intel_crtc_atomic_check(struct drm_crtc *crtc,
11949 struct drm_crtc_state *crtc_state)
11950{
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020011951 struct drm_device *dev = crtc->dev;
Maarten Lankhorstad421372015-06-15 12:33:42 +020011952 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011953 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020011954 struct intel_crtc_state *pipe_config =
11955 to_intel_crtc_state(crtc_state);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011956 struct drm_atomic_state *state = crtc_state->state;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020011957 int ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011958 bool mode_changed = needs_modeset(crtc_state);
11959
11960 if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
11961 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
11962 return -EINVAL;
11963 }
11964
Ville Syrjälä852eb002015-06-24 22:00:07 +030011965 if (mode_changed && !crtc_state->active)
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011966 pipe_config->update_wm_post = true;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020011967
Maarten Lankhorstad421372015-06-15 12:33:42 +020011968 if (mode_changed && crtc_state->enable &&
11969 dev_priv->display.crtc_compute_clock &&
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020011970 !WARN_ON(pipe_config->shared_dpll)) {
Maarten Lankhorstad421372015-06-15 12:33:42 +020011971 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
11972 pipe_config);
11973 if (ret)
11974 return ret;
11975 }
11976
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000011977 if (crtc_state->color_mgmt_changed) {
11978 ret = intel_color_check(crtc, crtc_state);
11979 if (ret)
11980 return ret;
11981 }
11982
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020011983 ret = 0;
Matt Roper86c8bbb2015-09-24 15:53:16 -070011984 if (dev_priv->display.compute_pipe_wm) {
Maarten Lankhorste3bddde2016-03-01 11:07:22 +010011985 ret = dev_priv->display.compute_pipe_wm(pipe_config);
Matt Ropered4a6a72016-02-23 17:20:13 -080011986 if (ret) {
11987 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
Matt Roper86c8bbb2015-09-24 15:53:16 -070011988 return ret;
Matt Ropered4a6a72016-02-23 17:20:13 -080011989 }
11990 }
11991
11992 if (dev_priv->display.compute_intermediate_wm &&
11993 !to_intel_atomic_state(state)->skip_intermediate_wm) {
11994 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
11995 return 0;
11996
11997 /*
11998 * Calculate 'intermediate' watermarks that satisfy both the
11999 * old state and the new state. We can program these
12000 * immediately.
12001 */
12002 ret = dev_priv->display.compute_intermediate_wm(crtc->dev,
12003 intel_crtc,
12004 pipe_config);
12005 if (ret) {
12006 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
12007 return ret;
12008 }
Matt Roper86c8bbb2015-09-24 15:53:16 -070012009 }
12010
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020012011 if (INTEL_INFO(dev)->gen >= 9) {
12012 if (mode_changed)
12013 ret = skl_update_scaler_crtc(pipe_config);
12014
12015 if (!ret)
12016 ret = intel_atomic_setup_scalers(dev, intel_crtc,
12017 pipe_config);
12018 }
12019
12020 return ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012021}
12022
Jani Nikula65b38e02015-04-13 11:26:56 +030012023static const struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012024 .mode_set_base_atomic = intel_pipe_set_base_atomic,
Matt Roperea2c67b2014-12-23 10:41:52 -080012025 .atomic_begin = intel_begin_crtc_commit,
12026 .atomic_flush = intel_finish_crtc_commit,
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012027 .atomic_check = intel_crtc_atomic_check,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012028};
12029
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020012030static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
12031{
12032 struct intel_connector *connector;
12033
12034 for_each_intel_connector(dev, connector) {
Daniel Vetter8863dc72016-05-06 15:39:03 +020012035 if (connector->base.state->crtc)
12036 drm_connector_unreference(&connector->base);
12037
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020012038 if (connector->base.encoder) {
12039 connector->base.state->best_encoder =
12040 connector->base.encoder;
12041 connector->base.state->crtc =
12042 connector->base.encoder->crtc;
Daniel Vetter8863dc72016-05-06 15:39:03 +020012043
12044 drm_connector_reference(&connector->base);
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020012045 } else {
12046 connector->base.state->best_encoder = NULL;
12047 connector->base.state->crtc = NULL;
12048 }
12049 }
12050}
12051
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012052static void
Robin Schroereba905b2014-05-18 02:24:50 +020012053connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012054 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012055{
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012056 int bpp = pipe_config->pipe_bpp;
12057
12058 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
12059 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030012060 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012061
12062 /* Don't use an invalid EDID bpc value */
12063 if (connector->base.display_info.bpc &&
12064 connector->base.display_info.bpc * 3 < bpp) {
12065 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
12066 bpp, connector->base.display_info.bpc*3);
12067 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
12068 }
12069
Jani Nikula013dd9e2016-01-13 16:35:20 +020012070 /* Clamp bpp to default limit on screens without EDID 1.4 */
12071 if (connector->base.display_info.bpc == 0) {
12072 int type = connector->base.connector_type;
12073 int clamp_bpp = 24;
12074
12075 /* Fall back to 18 bpp when DP sink capability is unknown. */
12076 if (type == DRM_MODE_CONNECTOR_DisplayPort ||
12077 type == DRM_MODE_CONNECTOR_eDP)
12078 clamp_bpp = 18;
12079
12080 if (bpp > clamp_bpp) {
12081 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of %d\n",
12082 bpp, clamp_bpp);
12083 pipe_config->pipe_bpp = clamp_bpp;
12084 }
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012085 }
12086}
12087
12088static int
12089compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012090 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012091{
12092 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012093 struct drm_atomic_state *state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012094 struct drm_connector *connector;
12095 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012096 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012097
Wayne Boyer666a4532015-12-09 12:29:35 -080012098 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012099 bpp = 10*3;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012100 else if (INTEL_INFO(dev)->gen >= 5)
12101 bpp = 12*3;
12102 else
12103 bpp = 8*3;
12104
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012105
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012106 pipe_config->pipe_bpp = bpp;
12107
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012108 state = pipe_config->base.state;
12109
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012110 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012111 for_each_connector_in_state(state, connector, connector_state, i) {
12112 if (connector_state->crtc != &crtc->base)
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012113 continue;
12114
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012115 connected_sink_compute_bpp(to_intel_connector(connector),
12116 pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012117 }
12118
12119 return bpp;
12120}
12121
Daniel Vetter644db712013-09-19 14:53:58 +020012122static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
12123{
12124 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
12125 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010012126 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020012127 mode->crtc_hdisplay, mode->crtc_hsync_start,
12128 mode->crtc_hsync_end, mode->crtc_htotal,
12129 mode->crtc_vdisplay, mode->crtc_vsync_start,
12130 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
12131}
12132
Daniel Vetterc0b03412013-05-28 12:05:54 +020012133static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012134 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012135 const char *context)
12136{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012137 struct drm_device *dev = crtc->base.dev;
12138 struct drm_plane *plane;
12139 struct intel_plane *intel_plane;
12140 struct intel_plane_state *state;
12141 struct drm_framebuffer *fb;
12142
12143 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
12144 context, pipe_config, pipe_name(crtc->pipe));
Daniel Vetterc0b03412013-05-28 12:05:54 +020012145
Jani Nikulada205632016-03-15 21:51:10 +020012146 DRM_DEBUG_KMS("cpu_transcoder: %s\n", transcoder_name(pipe_config->cpu_transcoder));
Daniel Vetterc0b03412013-05-28 12:05:54 +020012147 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
12148 pipe_config->pipe_bpp, pipe_config->dither);
12149 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
12150 pipe_config->has_pch_encoder,
12151 pipe_config->fdi_lanes,
12152 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
12153 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
12154 pipe_config->fdi_m_n.tu);
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012155 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 +030012156 pipe_config->has_dp_encoder,
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012157 pipe_config->lane_count,
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012158 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
12159 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
12160 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012161
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012162 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 -070012163 pipe_config->has_dp_encoder,
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012164 pipe_config->lane_count,
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012165 pipe_config->dp_m2_n2.gmch_m,
12166 pipe_config->dp_m2_n2.gmch_n,
12167 pipe_config->dp_m2_n2.link_m,
12168 pipe_config->dp_m2_n2.link_n,
12169 pipe_config->dp_m2_n2.tu);
12170
Daniel Vetter55072d12014-11-20 16:10:28 +010012171 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
12172 pipe_config->has_audio,
12173 pipe_config->has_infoframe);
12174
Daniel Vetterc0b03412013-05-28 12:05:54 +020012175 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012176 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012177 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012178 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
12179 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030012180 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030012181 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
12182 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Tvrtko Ursulin0ec463d2015-05-13 16:51:08 +010012183 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
12184 crtc->num_scalers,
12185 pipe_config->scaler_state.scaler_users,
12186 pipe_config->scaler_state.scaler_id);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012187 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
12188 pipe_config->gmch_pfit.control,
12189 pipe_config->gmch_pfit.pgm_ratios,
12190 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012191 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020012192 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012193 pipe_config->pch_pfit.size,
12194 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012195 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030012196 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012197
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012198 if (IS_BROXTON(dev)) {
Imre Deak05712c12015-06-18 17:25:54 +030012199 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012200 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
Imre Deakc8453332015-06-18 17:25:55 +030012201 "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012202 pipe_config->ddi_pll_sel,
12203 pipe_config->dpll_hw_state.ebb0,
Imre Deak05712c12015-06-18 17:25:54 +030012204 pipe_config->dpll_hw_state.ebb4,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012205 pipe_config->dpll_hw_state.pll0,
12206 pipe_config->dpll_hw_state.pll1,
12207 pipe_config->dpll_hw_state.pll2,
12208 pipe_config->dpll_hw_state.pll3,
12209 pipe_config->dpll_hw_state.pll6,
12210 pipe_config->dpll_hw_state.pll8,
Imre Deak05712c12015-06-18 17:25:54 +030012211 pipe_config->dpll_hw_state.pll9,
Imre Deakc8453332015-06-18 17:25:55 +030012212 pipe_config->dpll_hw_state.pll10,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012213 pipe_config->dpll_hw_state.pcsdw12);
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070012214 } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012215 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
12216 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
12217 pipe_config->ddi_pll_sel,
12218 pipe_config->dpll_hw_state.ctrl1,
12219 pipe_config->dpll_hw_state.cfgcr1,
12220 pipe_config->dpll_hw_state.cfgcr2);
12221 } else if (HAS_DDI(dev)) {
Ville Syrjälä1260f072016-02-17 21:41:08 +020012222 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 +010012223 pipe_config->ddi_pll_sel,
Maarten Lankhorst00490c22015-11-16 14:42:12 +010012224 pipe_config->dpll_hw_state.wrpll,
12225 pipe_config->dpll_hw_state.spll);
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012226 } else {
12227 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
12228 "fp0: 0x%x, fp1: 0x%x\n",
12229 pipe_config->dpll_hw_state.dpll,
12230 pipe_config->dpll_hw_state.dpll_md,
12231 pipe_config->dpll_hw_state.fp0,
12232 pipe_config->dpll_hw_state.fp1);
12233 }
12234
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012235 DRM_DEBUG_KMS("planes on this crtc\n");
12236 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
12237 intel_plane = to_intel_plane(plane);
12238 if (intel_plane->pipe != crtc->pipe)
12239 continue;
12240
12241 state = to_intel_plane_state(plane->state);
12242 fb = state->base.fb;
12243 if (!fb) {
12244 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
12245 "disabled, scaler_id = %d\n",
12246 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12247 plane->base.id, intel_plane->pipe,
12248 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
12249 drm_plane_index(plane), state->scaler_id);
12250 continue;
12251 }
12252
12253 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
12254 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12255 plane->base.id, intel_plane->pipe,
12256 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
12257 drm_plane_index(plane));
12258 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
12259 fb->base.id, fb->width, fb->height, fb->pixel_format);
12260 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
12261 state->scaler_id,
12262 state->src.x1 >> 16, state->src.y1 >> 16,
12263 drm_rect_width(&state->src) >> 16,
12264 drm_rect_height(&state->src) >> 16,
12265 state->dst.x1, state->dst.y1,
12266 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
12267 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020012268}
12269
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012270static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012271{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012272 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012273 struct drm_connector *connector;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012274 unsigned int used_ports = 0;
12275
12276 /*
12277 * Walk the connector list instead of the encoder
12278 * list to detect the problem on ddi platforms
12279 * where there's just one encoder per digital port.
12280 */
Ville Syrjälä0bff4852015-12-10 18:22:31 +020012281 drm_for_each_connector(connector, dev) {
12282 struct drm_connector_state *connector_state;
12283 struct intel_encoder *encoder;
12284
12285 connector_state = drm_atomic_get_existing_connector_state(state, connector);
12286 if (!connector_state)
12287 connector_state = connector->state;
12288
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012289 if (!connector_state->best_encoder)
12290 continue;
12291
12292 encoder = to_intel_encoder(connector_state->best_encoder);
12293
12294 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012295
12296 switch (encoder->type) {
12297 unsigned int port_mask;
12298 case INTEL_OUTPUT_UNKNOWN:
12299 if (WARN_ON(!HAS_DDI(dev)))
12300 break;
12301 case INTEL_OUTPUT_DISPLAYPORT:
12302 case INTEL_OUTPUT_HDMI:
12303 case INTEL_OUTPUT_EDP:
12304 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12305
12306 /* the same port mustn't appear more than once */
12307 if (used_ports & port_mask)
12308 return false;
12309
12310 used_ports |= port_mask;
12311 default:
12312 break;
12313 }
12314 }
12315
12316 return true;
12317}
12318
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012319static void
12320clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12321{
12322 struct drm_crtc_state tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012323 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012324 struct intel_dpll_hw_state dpll_hw_state;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012325 struct intel_shared_dpll *shared_dpll;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012326 uint32_t ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012327 bool force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012328
Ander Conselvan de Oliveira7546a382015-05-20 09:03:27 +030012329 /* FIXME: before the switch to atomic started, a new pipe_config was
12330 * kzalloc'd. Code that depends on any field being zero should be
12331 * fixed, so that the crtc_state can be safely duplicated. For now,
12332 * only fields that are know to not cause problems are preserved. */
12333
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012334 tmp_state = crtc_state->base;
Chandra Konduru663a3642015-04-07 15:28:41 -070012335 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012336 shared_dpll = crtc_state->shared_dpll;
12337 dpll_hw_state = crtc_state->dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012338 ddi_pll_sel = crtc_state->ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012339 force_thru = crtc_state->pch_pfit.force_thru;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012340
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012341 memset(crtc_state, 0, sizeof *crtc_state);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012342
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012343 crtc_state->base = tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012344 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012345 crtc_state->shared_dpll = shared_dpll;
12346 crtc_state->dpll_hw_state = dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012347 crtc_state->ddi_pll_sel = ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012348 crtc_state->pch_pfit.force_thru = force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012349}
12350
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012351static int
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012352intel_modeset_pipe_config(struct drm_crtc *crtc,
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012353 struct intel_crtc_state *pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +020012354{
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012355 struct drm_atomic_state *state = pipe_config->base.state;
Daniel Vetter7758a112012-07-08 19:40:39 +020012356 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012357 struct drm_connector *connector;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012358 struct drm_connector_state *connector_state;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012359 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012360 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010012361 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020012362
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012363 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020012364
Daniel Vettere143a212013-07-04 12:01:15 +020012365 pipe_config->cpu_transcoder =
12366 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012367
Imre Deak2960bc92013-07-30 13:36:32 +030012368 /*
12369 * Sanitize sync polarity flags based on requested ones. If neither
12370 * positive or negative polarity is requested, treat this as meaning
12371 * negative polarity.
12372 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012373 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012374 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012375 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012376
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012377 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012378 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012379 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012380
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012381 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12382 pipe_config);
12383 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012384 goto fail;
12385
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012386 /*
12387 * Determine the real pipe dimensions. Note that stereo modes can
12388 * increase the actual pipe size due to the frame doubling and
12389 * insertion of additional space for blanks between the frame. This
12390 * is stored in the crtc timings. We use the requested mode to do this
12391 * computation to clearly distinguish it from the adjusted mode, which
12392 * can be changed by the connectors in the below retry loop.
12393 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012394 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080012395 &pipe_config->pipe_src_w,
12396 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012397
Daniel Vettere29c22c2013-02-21 00:00:16 +010012398encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020012399 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020012400 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020012401 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012402
Daniel Vetter135c81b2013-07-21 21:37:09 +020012403 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012404 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12405 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020012406
Daniel Vetter7758a112012-07-08 19:40:39 +020012407 /* Pass our mode to the connectors and the CRTC to give them a chance to
12408 * adjust it according to limitations or connector properties, and also
12409 * a chance to reject the mode entirely.
12410 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012411 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012412 if (connector_state->crtc != crtc)
12413 continue;
12414
12415 encoder = to_intel_encoder(connector_state->best_encoder);
12416
Daniel Vetterefea6e82013-07-21 21:36:59 +020012417 if (!(encoder->compute_config(encoder, pipe_config))) {
12418 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020012419 goto fail;
12420 }
12421 }
12422
Daniel Vetterff9a6752013-06-01 17:16:21 +020012423 /* Set default port clock if not overwritten by the encoder. Needs to be
12424 * done afterwards in case the encoder adjusts the mode. */
12425 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012426 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010012427 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012428
Daniel Vettera43f6e02013-06-07 23:10:32 +020012429 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010012430 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020012431 DRM_DEBUG_KMS("CRTC fixup failed\n");
12432 goto fail;
12433 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010012434
12435 if (ret == RETRY) {
12436 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12437 ret = -EINVAL;
12438 goto fail;
12439 }
12440
12441 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12442 retry = false;
12443 goto encoder_retry;
12444 }
12445
Daniel Vettere8fa4272015-08-12 11:43:34 +020012446 /* Dithering seems to not pass-through bits correctly when it should, so
12447 * only enable it on 6bpc panels. */
12448 pipe_config->dither = pipe_config->pipe_bpp == 6*3;
Daniel Vetter62f0ace2015-08-26 18:57:26 +020012449 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012450 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012451
Daniel Vetter7758a112012-07-08 19:40:39 +020012452fail:
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012453 return ret;
Daniel Vetter7758a112012-07-08 19:40:39 +020012454}
12455
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012456static void
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020012457intel_modeset_update_crtc_state(struct drm_atomic_state *state)
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012458{
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012459 struct drm_crtc *crtc;
12460 struct drm_crtc_state *crtc_state;
Maarten Lankhorst8a75d152015-07-13 16:30:14 +020012461 int i;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012462
Ville Syrjälä76688512014-01-10 11:28:06 +020012463 /* Double check state. */
Maarten Lankhorst8a75d152015-07-13 16:30:14 +020012464 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst3cb480b2015-06-01 12:49:49 +020012465 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
Maarten Lankhorstfc467a222015-06-01 12:50:07 +020012466
12467 /* Update hwmode for vblank functions */
12468 if (crtc->state->active)
12469 crtc->hwmode = crtc->state->adjusted_mode;
12470 else
12471 crtc->hwmode.crtc_clock = 0;
Maarten Lankhorst61067a52015-09-23 16:29:36 +020012472
12473 /*
12474 * Update legacy state to satisfy fbc code. This can
12475 * be removed when fbc uses the atomic state.
12476 */
12477 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
12478 struct drm_plane_state *plane_state = crtc->primary->state;
12479
12480 crtc->primary->fb = plane_state->fb;
12481 crtc->x = plane_state->src_x >> 16;
12482 crtc->y = plane_state->src_y >> 16;
12483 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012484 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012485}
12486
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012487static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012488{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012489 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012490
12491 if (clock1 == clock2)
12492 return true;
12493
12494 if (!clock1 || !clock2)
12495 return false;
12496
12497 diff = abs(clock1 - clock2);
12498
12499 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12500 return true;
12501
12502 return false;
12503}
12504
Daniel Vetter25c5b262012-07-08 22:08:04 +020012505#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12506 list_for_each_entry((intel_crtc), \
12507 &(dev)->mode_config.crtc_list, \
12508 base.head) \
Jani Nikula95150bd2015-11-24 21:21:56 +020012509 for_each_if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020012510
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012511static bool
12512intel_compare_m_n(unsigned int m, unsigned int n,
12513 unsigned int m2, unsigned int n2,
12514 bool exact)
12515{
12516 if (m == m2 && n == n2)
12517 return true;
12518
12519 if (exact || !m || !n || !m2 || !n2)
12520 return false;
12521
12522 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
12523
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012524 if (n > n2) {
12525 while (n > n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012526 m2 <<= 1;
12527 n2 <<= 1;
12528 }
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012529 } else if (n < n2) {
12530 while (n < n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012531 m <<= 1;
12532 n <<= 1;
12533 }
12534 }
12535
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012536 if (n != n2)
12537 return false;
12538
12539 return intel_fuzzy_clock_check(m, m2);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012540}
12541
12542static bool
12543intel_compare_link_m_n(const struct intel_link_m_n *m_n,
12544 struct intel_link_m_n *m2_n2,
12545 bool adjust)
12546{
12547 if (m_n->tu == m2_n2->tu &&
12548 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
12549 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
12550 intel_compare_m_n(m_n->link_m, m_n->link_n,
12551 m2_n2->link_m, m2_n2->link_n, !adjust)) {
12552 if (adjust)
12553 *m2_n2 = *m_n;
12554
12555 return true;
12556 }
12557
12558 return false;
12559}
12560
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012561static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012562intel_pipe_config_compare(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012563 struct intel_crtc_state *current_config,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012564 struct intel_crtc_state *pipe_config,
12565 bool adjust)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012566{
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012567 bool ret = true;
12568
12569#define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
12570 do { \
12571 if (!adjust) \
12572 DRM_ERROR(fmt, ##__VA_ARGS__); \
12573 else \
12574 DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
12575 } while (0)
12576
Daniel Vetter66e985c2013-06-05 13:34:20 +020012577#define PIPE_CONF_CHECK_X(name) \
12578 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012579 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012580 "(expected 0x%08x, found 0x%08x)\n", \
12581 current_config->name, \
12582 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012583 ret = false; \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012584 }
12585
Daniel Vetter08a24032013-04-19 11:25:34 +020012586#define PIPE_CONF_CHECK_I(name) \
12587 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012588 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter08a24032013-04-19 11:25:34 +020012589 "(expected %i, found %i)\n", \
12590 current_config->name, \
12591 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012592 ret = false; \
12593 }
12594
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012595#define PIPE_CONF_CHECK_P(name) \
12596 if (current_config->name != pipe_config->name) { \
12597 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12598 "(expected %p, found %p)\n", \
12599 current_config->name, \
12600 pipe_config->name); \
12601 ret = false; \
12602 }
12603
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012604#define PIPE_CONF_CHECK_M_N(name) \
12605 if (!intel_compare_link_m_n(&current_config->name, \
12606 &pipe_config->name,\
12607 adjust)) { \
12608 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12609 "(expected tu %i gmch %i/%i link %i/%i, " \
12610 "found tu %i, gmch %i/%i link %i/%i)\n", \
12611 current_config->name.tu, \
12612 current_config->name.gmch_m, \
12613 current_config->name.gmch_n, \
12614 current_config->name.link_m, \
12615 current_config->name.link_n, \
12616 pipe_config->name.tu, \
12617 pipe_config->name.gmch_m, \
12618 pipe_config->name.gmch_n, \
12619 pipe_config->name.link_m, \
12620 pipe_config->name.link_n); \
12621 ret = false; \
12622 }
12623
Daniel Vetter55c561a2016-03-30 11:34:36 +020012624/* This is required for BDW+ where there is only one set of registers for
12625 * switching between high and low RR.
12626 * This macro can be used whenever a comparison has to be made between one
12627 * hw state and multiple sw state variables.
12628 */
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012629#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
12630 if (!intel_compare_link_m_n(&current_config->name, \
12631 &pipe_config->name, adjust) && \
12632 !intel_compare_link_m_n(&current_config->alt_name, \
12633 &pipe_config->name, adjust)) { \
12634 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12635 "(expected tu %i gmch %i/%i link %i/%i, " \
12636 "or tu %i gmch %i/%i link %i/%i, " \
12637 "found tu %i, gmch %i/%i link %i/%i)\n", \
12638 current_config->name.tu, \
12639 current_config->name.gmch_m, \
12640 current_config->name.gmch_n, \
12641 current_config->name.link_m, \
12642 current_config->name.link_n, \
12643 current_config->alt_name.tu, \
12644 current_config->alt_name.gmch_m, \
12645 current_config->alt_name.gmch_n, \
12646 current_config->alt_name.link_m, \
12647 current_config->alt_name.link_n, \
12648 pipe_config->name.tu, \
12649 pipe_config->name.gmch_m, \
12650 pipe_config->name.gmch_n, \
12651 pipe_config->name.link_m, \
12652 pipe_config->name.link_n); \
12653 ret = false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010012654 }
12655
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012656#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12657 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012658 INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012659 "(expected %i, found %i)\n", \
12660 current_config->name & (mask), \
12661 pipe_config->name & (mask)); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012662 ret = false; \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012663 }
12664
Ville Syrjälä5e550652013-09-06 23:29:07 +030012665#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12666 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012667 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012668 "(expected %i, found %i)\n", \
12669 current_config->name, \
12670 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012671 ret = false; \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012672 }
12673
Daniel Vetterbb760062013-06-06 14:55:52 +020012674#define PIPE_CONF_QUIRK(quirk) \
12675 ((current_config->quirks | pipe_config->quirks) & (quirk))
12676
Daniel Vettereccb1402013-05-22 00:50:22 +020012677 PIPE_CONF_CHECK_I(cpu_transcoder);
12678
Daniel Vetter08a24032013-04-19 11:25:34 +020012679 PIPE_CONF_CHECK_I(has_pch_encoder);
12680 PIPE_CONF_CHECK_I(fdi_lanes);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012681 PIPE_CONF_CHECK_M_N(fdi_m_n);
Daniel Vetter08a24032013-04-19 11:25:34 +020012682
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012683 PIPE_CONF_CHECK_I(has_dp_encoder);
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012684 PIPE_CONF_CHECK_I(lane_count);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012685
12686 if (INTEL_INFO(dev)->gen < 8) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012687 PIPE_CONF_CHECK_M_N(dp_m_n);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012688
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012689 if (current_config->has_drrs)
12690 PIPE_CONF_CHECK_M_N(dp_m2_n2);
12691 } else
12692 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012693
Jani Nikulaa65347b2015-11-27 12:21:46 +020012694 PIPE_CONF_CHECK_I(has_dsi_encoder);
12695
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012696 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12697 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12698 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12699 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12700 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12701 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012702
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012703 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12704 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12705 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12706 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12707 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12708 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012709
Daniel Vetterc93f54c2013-06-27 19:47:19 +020012710 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b52014-04-24 23:54:47 +020012711 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012712 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
Wayne Boyer666a4532015-12-09 12:29:35 -080012713 IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012714 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080012715 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020012716
Daniel Vetter9ed109a2014-04-24 23:54:52 +020012717 PIPE_CONF_CHECK_I(has_audio);
12718
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012719 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012720 DRM_MODE_FLAG_INTERLACE);
12721
Daniel Vetterbb760062013-06-06 14:55:52 +020012722 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012723 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012724 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012725 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012726 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012727 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012728 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012729 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012730 DRM_MODE_FLAG_NVSYNC);
12731 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012732
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030012733 PIPE_CONF_CHECK_X(gmch_pfit.control);
Daniel Vettere2ff2d42015-07-15 14:15:50 +020012734 /* pfit ratios are autocomputed by the hw on gen4+ */
12735 if (INTEL_INFO(dev)->gen < 4)
Ville Syrjälä7f7d8dd2016-03-15 16:40:07 +020012736 PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030012737 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
Daniel Vetter99535992014-04-13 12:00:33 +020012738
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020012739 if (!adjust) {
12740 PIPE_CONF_CHECK_I(pipe_src_w);
12741 PIPE_CONF_CHECK_I(pipe_src_h);
12742
12743 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12744 if (current_config->pch_pfit.enabled) {
12745 PIPE_CONF_CHECK_X(pch_pfit.pos);
12746 PIPE_CONF_CHECK_X(pch_pfit.size);
12747 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012748
Maarten Lankhorst7aefe2b2015-09-14 11:30:10 +020012749 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12750 }
Chandra Kondurua1b22782015-04-07 15:28:45 -070012751
Jesse Barnese59150d2014-01-07 13:30:45 -080012752 /* BDW+ don't expose a synchronous way to read the state */
12753 if (IS_HASWELL(dev))
12754 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012755
Ville Syrjälä282740f2013-09-04 18:30:03 +030012756 PIPE_CONF_CHECK_I(double_wide);
12757
Daniel Vetter26804af2014-06-25 22:01:55 +030012758 PIPE_CONF_CHECK_X(ddi_pll_sel);
12759
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012760 PIPE_CONF_CHECK_P(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012761 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020012762 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012763 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12764 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030012765 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Maarten Lankhorst00490c22015-11-16 14:42:12 +010012766 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000012767 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12768 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12769 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012770
Ville Syrjälä47eacba2016-04-12 22:14:35 +030012771 PIPE_CONF_CHECK_X(dsi_pll.ctrl);
12772 PIPE_CONF_CHECK_X(dsi_pll.div);
12773
Ville Syrjälä42571ae2013-09-06 23:29:00 +030012774 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12775 PIPE_CONF_CHECK_I(pipe_bpp);
12776
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012777 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080012778 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030012779
Daniel Vetter66e985c2013-06-05 13:34:20 +020012780#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020012781#undef PIPE_CONF_CHECK_I
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012782#undef PIPE_CONF_CHECK_P
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012783#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030012784#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020012785#undef PIPE_CONF_QUIRK
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012786#undef INTEL_ERR_OR_DBG_KMS
Daniel Vetter627eb5a2013-04-29 19:33:42 +020012787
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012788 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012789}
12790
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020012791static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
12792 const struct intel_crtc_state *pipe_config)
12793{
12794 if (pipe_config->has_pch_encoder) {
Ville Syrjälä21a727b2016-02-17 21:41:10 +020012795 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020012796 &pipe_config->fdi_m_n);
12797 int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
12798
12799 /*
12800 * FDI already provided one idea for the dotclock.
12801 * Yell if the encoder disagrees.
12802 */
12803 WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
12804 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
12805 fdi_dotclock, dotclock);
12806 }
12807}
12808
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012809static void verify_wm_state(struct drm_crtc *crtc,
12810 struct drm_crtc_state *new_state)
Damien Lespiau08db6652014-11-04 17:06:52 +000012811{
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012812 struct drm_device *dev = crtc->dev;
Damien Lespiau08db6652014-11-04 17:06:52 +000012813 struct drm_i915_private *dev_priv = dev->dev_private;
12814 struct skl_ddb_allocation hw_ddb, *sw_ddb;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012815 struct skl_ddb_entry *hw_entry, *sw_entry;
12816 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12817 const enum pipe pipe = intel_crtc->pipe;
Damien Lespiau08db6652014-11-04 17:06:52 +000012818 int plane;
12819
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012820 if (INTEL_INFO(dev)->gen < 9 || !new_state->active)
Damien Lespiau08db6652014-11-04 17:06:52 +000012821 return;
12822
12823 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12824 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12825
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012826 /* planes */
12827 for_each_plane(dev_priv, pipe, plane) {
12828 hw_entry = &hw_ddb.plane[pipe][plane];
12829 sw_entry = &sw_ddb->plane[pipe][plane];
Damien Lespiau08db6652014-11-04 17:06:52 +000012830
12831 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12832 continue;
12833
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012834 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12835 "(expected (%u,%u), found (%u,%u))\n",
12836 pipe_name(pipe), plane + 1,
12837 sw_entry->start, sw_entry->end,
12838 hw_entry->start, hw_entry->end);
12839 }
12840
12841 /* cursor */
12842 hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
12843 sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
12844
12845 if (!skl_ddb_entry_equal(hw_entry, sw_entry)) {
Damien Lespiau08db6652014-11-04 17:06:52 +000012846 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12847 "(expected (%u,%u), found (%u,%u))\n",
12848 pipe_name(pipe),
12849 sw_entry->start, sw_entry->end,
12850 hw_entry->start, hw_entry->end);
12851 }
12852}
12853
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012854static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012855verify_connector_state(struct drm_device *dev, struct drm_crtc *crtc)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012856{
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012857 struct drm_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012858
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012859 drm_for_each_connector(connector, dev) {
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012860 struct drm_encoder *encoder = connector->encoder;
12861 struct drm_connector_state *state = connector->state;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012862
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012863 if (state->crtc != crtc)
12864 continue;
12865
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012866 intel_connector_verify_state(to_intel_connector(connector));
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012867
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012868 I915_STATE_WARN(state->best_encoder != encoder,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012869 "connector's atomic encoder doesn't match legacy encoder\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012870 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012871}
12872
12873static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012874verify_encoder_state(struct drm_device *dev)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012875{
12876 struct intel_encoder *encoder;
12877 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012878
Damien Lespiaub2784e12014-08-05 11:29:37 +010012879 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012880 bool enabled = false;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012881 enum pipe pipe;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012882
12883 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
12884 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030012885 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012886
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012887 for_each_intel_connector(dev, connector) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012888 if (connector->base.state->best_encoder != &encoder->base)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012889 continue;
12890 enabled = true;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012891
12892 I915_STATE_WARN(connector->base.state->crtc !=
12893 encoder->base.crtc,
12894 "connector's crtc doesn't match encoder crtc\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012895 }
Dave Airlie0e32b392014-05-02 14:02:48 +100012896
Rob Clarke2c719b2014-12-15 13:56:32 -050012897 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012898 "encoder's enabled state mismatch "
12899 "(expected %i, found %i)\n",
12900 !!encoder->base.crtc, enabled);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012901
12902 if (!encoder->base.crtc) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012903 bool active;
12904
12905 active = encoder->get_hw_state(encoder, &pipe);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012906 I915_STATE_WARN(active,
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012907 "encoder detached but still enabled on pipe %c.\n",
12908 pipe_name(pipe));
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012909 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012910 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012911}
12912
12913static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012914verify_crtc_state(struct drm_crtc *crtc,
12915 struct drm_crtc_state *old_crtc_state,
12916 struct drm_crtc_state *new_crtc_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012917{
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012918 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030012919 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012920 struct intel_encoder *encoder;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012921 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12922 struct intel_crtc_state *pipe_config, *sw_config;
12923 struct drm_atomic_state *old_state;
12924 bool active;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012925
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012926 old_state = old_crtc_state->state;
12927 __drm_atomic_helper_crtc_destroy_state(crtc, old_crtc_state);
12928 pipe_config = to_intel_crtc_state(old_crtc_state);
12929 memset(pipe_config, 0, sizeof(*pipe_config));
12930 pipe_config->base.crtc = crtc;
12931 pipe_config->base.state = old_state;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012932
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012933 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012934
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012935 active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012936
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012937 /* hw state is inconsistent with the pipe quirk */
12938 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12939 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
12940 active = new_crtc_state->active;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012941
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012942 I915_STATE_WARN(new_crtc_state->active != active,
12943 "crtc active state doesn't match with hw state "
12944 "(expected %i, found %i)\n", new_crtc_state->active, active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012945
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012946 I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
12947 "transitional active state does not match atomic hw state "
12948 "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012949
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012950 for_each_encoder_on_crtc(dev, crtc, encoder) {
12951 enum pipe pipe;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012952
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012953 active = encoder->get_hw_state(encoder, &pipe);
12954 I915_STATE_WARN(active != new_crtc_state->active,
12955 "[ENCODER:%i] active %i with crtc active %i\n",
12956 encoder->base.base.id, active, new_crtc_state->active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012957
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012958 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
12959 "Encoder connected to wrong pipe %c\n",
12960 pipe_name(pipe));
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012961
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012962 if (active)
12963 encoder->get_config(encoder, pipe_config);
12964 }
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012965
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012966 if (!new_crtc_state->active)
12967 return;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012968
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012969 intel_pipe_config_sanity_check(dev_priv, pipe_config);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012970
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012971 sw_config = to_intel_crtc_state(crtc->state);
12972 if (!intel_pipe_config_compare(dev, sw_config,
12973 pipe_config, false)) {
12974 I915_STATE_WARN(1, "pipe state doesn't match!\n");
12975 intel_dump_pipe_config(intel_crtc, pipe_config,
12976 "[hw state]");
12977 intel_dump_pipe_config(intel_crtc, sw_config,
12978 "[sw state]");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012979 }
12980}
12981
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012982static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012983verify_single_dpll_state(struct drm_i915_private *dev_priv,
12984 struct intel_shared_dpll *pll,
12985 struct drm_crtc *crtc,
12986 struct drm_crtc_state *new_state)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012987{
12988 struct intel_dpll_hw_state dpll_hw_state;
12989 unsigned crtc_mask;
12990 bool active;
12991
12992 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
12993
12994 DRM_DEBUG_KMS("%s\n", pll->name);
12995
12996 active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
12997
12998 if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
12999 I915_STATE_WARN(!pll->on && pll->active_mask,
13000 "pll in active use but not on in sw tracking\n");
13001 I915_STATE_WARN(pll->on && !pll->active_mask,
13002 "pll is on but not used by any active crtc\n");
13003 I915_STATE_WARN(pll->on != active,
13004 "pll on state mismatch (expected %i, found %i)\n",
13005 pll->on, active);
13006 }
13007
13008 if (!crtc) {
13009 I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
13010 "more active pll users than references: %x vs %x\n",
13011 pll->active_mask, pll->config.crtc_mask);
13012
13013 return;
13014 }
13015
13016 crtc_mask = 1 << drm_crtc_index(crtc);
13017
13018 if (new_state->active)
13019 I915_STATE_WARN(!(pll->active_mask & crtc_mask),
13020 "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
13021 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
13022 else
13023 I915_STATE_WARN(pll->active_mask & crtc_mask,
13024 "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
13025 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
13026
13027 I915_STATE_WARN(!(pll->config.crtc_mask & crtc_mask),
13028 "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
13029 crtc_mask, pll->config.crtc_mask);
13030
13031 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state,
13032 &dpll_hw_state,
13033 sizeof(dpll_hw_state)),
13034 "pll hw state mismatch\n");
13035}
13036
13037static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013038verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
13039 struct drm_crtc_state *old_crtc_state,
13040 struct drm_crtc_state *new_crtc_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013041{
Jani Nikulafbee40d2014-03-31 14:27:18 +030013042 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013043 struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
13044 struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
13045
13046 if (new_state->shared_dpll)
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013047 verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013048
13049 if (old_state->shared_dpll &&
13050 old_state->shared_dpll != new_state->shared_dpll) {
13051 unsigned crtc_mask = 1 << drm_crtc_index(crtc);
13052 struct intel_shared_dpll *pll = old_state->shared_dpll;
13053
13054 I915_STATE_WARN(pll->active_mask & crtc_mask,
13055 "pll active mismatch (didn't expect pipe %c in active mask)\n",
13056 pipe_name(drm_crtc_index(crtc)));
13057 I915_STATE_WARN(pll->config.crtc_mask & crtc_mask,
13058 "pll enabled crtcs mismatch (found %x in enabled mask)\n",
13059 pipe_name(drm_crtc_index(crtc)));
13060 }
13061}
13062
13063static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013064intel_modeset_verify_crtc(struct drm_crtc *crtc,
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013065 struct drm_crtc_state *old_state,
13066 struct drm_crtc_state *new_state)
13067{
13068 if (!needs_modeset(new_state) &&
13069 !to_intel_crtc_state(new_state)->update_pipe)
13070 return;
13071
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013072 verify_wm_state(crtc, new_state);
13073 verify_connector_state(crtc->dev, crtc);
13074 verify_crtc_state(crtc, old_state, new_state);
13075 verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013076}
13077
13078static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013079verify_disabled_dpll_state(struct drm_device *dev)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013080{
13081 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013082 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020013083
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013084 for (i = 0; i < dev_priv->num_shared_dpll; i++)
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013085 verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013086}
Daniel Vetter53589012013-06-05 13:34:16 +020013087
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013088static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013089intel_modeset_verify_disabled(struct drm_device *dev)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013090{
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013091 verify_encoder_state(dev);
13092 verify_connector_state(dev, NULL);
13093 verify_disabled_dpll_state(dev);
Daniel Vettere2e1ed42012-07-08 21:14:38 +020013094}
13095
Ville Syrjälä80715b22014-05-15 20:23:23 +030013096static void update_scanline_offset(struct intel_crtc *crtc)
13097{
13098 struct drm_device *dev = crtc->base.dev;
13099
13100 /*
13101 * The scanline counter increments at the leading edge of hsync.
13102 *
13103 * On most platforms it starts counting from vtotal-1 on the
13104 * first active line. That means the scanline counter value is
13105 * always one less than what we would expect. Ie. just after
13106 * start of vblank, which also occurs at start of hsync (on the
13107 * last active line), the scanline counter will read vblank_start-1.
13108 *
13109 * On gen2 the scanline counter starts counting from 1 instead
13110 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
13111 * to keep the value positive), instead of adding one.
13112 *
13113 * On HSW+ the behaviour of the scanline counter depends on the output
13114 * type. For DP ports it behaves like most other platforms, but on HDMI
13115 * there's an extra 1 line difference. So we need to add two instead of
13116 * one to the value.
13117 */
13118 if (IS_GEN2(dev)) {
Ville Syrjälä124abe02015-09-08 13:40:45 +030013119 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030013120 int vtotal;
13121
Ville Syrjälä124abe02015-09-08 13:40:45 +030013122 vtotal = adjusted_mode->crtc_vtotal;
13123 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
Ville Syrjälä80715b22014-05-15 20:23:23 +030013124 vtotal /= 2;
13125
13126 crtc->scanline_offset = vtotal - 1;
13127 } else if (HAS_DDI(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +030013128 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030013129 crtc->scanline_offset = 2;
13130 } else
13131 crtc->scanline_offset = 1;
13132}
13133
Maarten Lankhorstad421372015-06-15 12:33:42 +020013134static void intel_modeset_clear_plls(struct drm_atomic_state *state)
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013135{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030013136 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013137 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstad421372015-06-15 12:33:42 +020013138 struct intel_shared_dpll_config *shared_dpll = NULL;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013139 struct drm_crtc *crtc;
13140 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013141 int i;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013142
13143 if (!dev_priv->display.crtc_compute_clock)
Maarten Lankhorstad421372015-06-15 12:33:42 +020013144 return;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013145
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013146 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013147 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013148 struct intel_shared_dpll *old_dpll =
13149 to_intel_crtc_state(crtc->state)->shared_dpll;
Maarten Lankhorstad421372015-06-15 12:33:42 +020013150
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013151 if (!needs_modeset(crtc_state))
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030013152 continue;
13153
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013154 to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013155
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013156 if (!old_dpll)
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013157 continue;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013158
Maarten Lankhorstad421372015-06-15 12:33:42 +020013159 if (!shared_dpll)
13160 shared_dpll = intel_atomic_get_shared_dpll_state(state);
13161
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013162 intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013163 }
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013164}
13165
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013166/*
13167 * This implements the workaround described in the "notes" section of the mode
13168 * set sequence documentation. When going from no pipes or single pipe to
13169 * multiple pipes, and planes are enabled after the pipe, we need to wait at
13170 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
13171 */
13172static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
13173{
13174 struct drm_crtc_state *crtc_state;
13175 struct intel_crtc *intel_crtc;
13176 struct drm_crtc *crtc;
13177 struct intel_crtc_state *first_crtc_state = NULL;
13178 struct intel_crtc_state *other_crtc_state = NULL;
13179 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
13180 int i;
13181
13182 /* look at all crtc's that are going to be enabled in during modeset */
13183 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13184 intel_crtc = to_intel_crtc(crtc);
13185
13186 if (!crtc_state->active || !needs_modeset(crtc_state))
13187 continue;
13188
13189 if (first_crtc_state) {
13190 other_crtc_state = to_intel_crtc_state(crtc_state);
13191 break;
13192 } else {
13193 first_crtc_state = to_intel_crtc_state(crtc_state);
13194 first_pipe = intel_crtc->pipe;
13195 }
13196 }
13197
13198 /* No workaround needed? */
13199 if (!first_crtc_state)
13200 return 0;
13201
13202 /* w/a possibly needed, check how many crtc's are already enabled. */
13203 for_each_intel_crtc(state->dev, intel_crtc) {
13204 struct intel_crtc_state *pipe_config;
13205
13206 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
13207 if (IS_ERR(pipe_config))
13208 return PTR_ERR(pipe_config);
13209
13210 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
13211
13212 if (!pipe_config->base.active ||
13213 needs_modeset(&pipe_config->base))
13214 continue;
13215
13216 /* 2 or more enabled crtcs means no need for w/a */
13217 if (enabled_pipe != INVALID_PIPE)
13218 return 0;
13219
13220 enabled_pipe = intel_crtc->pipe;
13221 }
13222
13223 if (enabled_pipe != INVALID_PIPE)
13224 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
13225 else if (other_crtc_state)
13226 other_crtc_state->hsw_workaround_pipe = first_pipe;
13227
13228 return 0;
13229}
13230
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013231static int intel_modeset_all_pipes(struct drm_atomic_state *state)
13232{
13233 struct drm_crtc *crtc;
13234 struct drm_crtc_state *crtc_state;
13235 int ret = 0;
13236
13237 /* add all active pipes to the state */
13238 for_each_crtc(state->dev, crtc) {
13239 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13240 if (IS_ERR(crtc_state))
13241 return PTR_ERR(crtc_state);
13242
13243 if (!crtc_state->active || needs_modeset(crtc_state))
13244 continue;
13245
13246 crtc_state->mode_changed = true;
13247
13248 ret = drm_atomic_add_affected_connectors(state, crtc);
13249 if (ret)
13250 break;
13251
13252 ret = drm_atomic_add_affected_planes(state, crtc);
13253 if (ret)
13254 break;
13255 }
13256
13257 return ret;
13258}
13259
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013260static int intel_modeset_checks(struct drm_atomic_state *state)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013261{
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013262 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
13263 struct drm_i915_private *dev_priv = state->dev->dev_private;
13264 struct drm_crtc *crtc;
13265 struct drm_crtc_state *crtc_state;
13266 int ret = 0, i;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013267
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013268 if (!check_digital_port_conflicts(state)) {
13269 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
13270 return -EINVAL;
13271 }
13272
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013273 intel_state->modeset = true;
13274 intel_state->active_crtcs = dev_priv->active_crtcs;
13275
13276 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13277 if (crtc_state->active)
13278 intel_state->active_crtcs |= 1 << i;
13279 else
13280 intel_state->active_crtcs &= ~(1 << i);
13281 }
13282
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013283 /*
13284 * See if the config requires any additional preparation, e.g.
13285 * to adjust global state with pipes off. We need to do this
13286 * here so we can get the modeset_pipe updated config for the new
13287 * mode set on this crtc. For other crtcs we need to use the
13288 * adjusted_mode bits in the crtc directly.
13289 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013290 if (dev_priv->display.modeset_calc_cdclk) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013291 ret = dev_priv->display.modeset_calc_cdclk(state);
13292
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013293 if (!ret && intel_state->dev_cdclk != dev_priv->cdclk_freq)
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013294 ret = intel_modeset_all_pipes(state);
13295
13296 if (ret < 0)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013297 return ret;
Maarten Lankhorste8788cb2016-02-16 10:25:11 +010013298
13299 DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
13300 intel_state->cdclk, intel_state->dev_cdclk);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013301 } else
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013302 to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013303
Maarten Lankhorstad421372015-06-15 12:33:42 +020013304 intel_modeset_clear_plls(state);
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013305
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013306 if (IS_HASWELL(dev_priv))
Maarten Lankhorstad421372015-06-15 12:33:42 +020013307 return haswell_mode_set_planes_workaround(state);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013308
Maarten Lankhorstad421372015-06-15 12:33:42 +020013309 return 0;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013310}
13311
Matt Roperaa363132015-09-24 15:53:18 -070013312/*
13313 * Handle calculation of various watermark data at the end of the atomic check
13314 * phase. The code here should be run after the per-crtc and per-plane 'check'
13315 * handlers to ensure that all derived state has been updated.
13316 */
13317static void calc_watermark_data(struct drm_atomic_state *state)
13318{
13319 struct drm_device *dev = state->dev;
13320 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
13321 struct drm_crtc *crtc;
13322 struct drm_crtc_state *cstate;
13323 struct drm_plane *plane;
13324 struct drm_plane_state *pstate;
13325
13326 /*
13327 * Calculate watermark configuration details now that derived
13328 * plane/crtc state is all properly updated.
13329 */
13330 drm_for_each_crtc(crtc, dev) {
13331 cstate = drm_atomic_get_existing_crtc_state(state, crtc) ?:
13332 crtc->state;
13333
13334 if (cstate->active)
13335 intel_state->wm_config.num_pipes_active++;
13336 }
13337 drm_for_each_legacy_plane(plane, dev) {
13338 pstate = drm_atomic_get_existing_plane_state(state, plane) ?:
13339 plane->state;
13340
13341 if (!to_intel_plane_state(pstate)->visible)
13342 continue;
13343
13344 intel_state->wm_config.sprites_enabled = true;
13345 if (pstate->crtc_w != pstate->src_w >> 16 ||
13346 pstate->crtc_h != pstate->src_h >> 16)
13347 intel_state->wm_config.sprites_scaled = true;
13348 }
13349}
13350
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013351/**
13352 * intel_atomic_check - validate state object
13353 * @dev: drm device
13354 * @state: state to validate
13355 */
13356static int intel_atomic_check(struct drm_device *dev,
13357 struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020013358{
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013359 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roperaa363132015-09-24 15:53:18 -070013360 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013361 struct drm_crtc *crtc;
13362 struct drm_crtc_state *crtc_state;
13363 int ret, i;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013364 bool any_ms = false;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013365
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013366 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020013367 if (ret)
13368 return ret;
13369
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013370 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013371 struct intel_crtc_state *pipe_config =
13372 to_intel_crtc_state(crtc_state);
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013373
13374 /* Catch I915_MODE_FLAG_INHERITED */
13375 if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
13376 crtc_state->mode_changed = true;
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013377
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013378 if (!crtc_state->enable) {
13379 if (needs_modeset(crtc_state))
13380 any_ms = true;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013381 continue;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013382 }
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013383
Daniel Vetter26495482015-07-15 14:15:52 +020013384 if (!needs_modeset(crtc_state))
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013385 continue;
13386
Daniel Vetter26495482015-07-15 14:15:52 +020013387 /* FIXME: For only active_changed we shouldn't need to do any
13388 * state recomputation at all. */
13389
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013390 ret = drm_atomic_add_affected_connectors(state, crtc);
13391 if (ret)
13392 return ret;
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013393
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013394 ret = intel_modeset_pipe_config(crtc, pipe_config);
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013395 if (ret)
13396 return ret;
13397
Jani Nikula73831232015-11-19 10:26:30 +020013398 if (i915.fastboot &&
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013399 intel_pipe_config_compare(dev,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013400 to_intel_crtc_state(crtc->state),
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013401 pipe_config, true)) {
Daniel Vetter26495482015-07-15 14:15:52 +020013402 crtc_state->mode_changed = false;
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013403 to_intel_crtc_state(crtc_state)->update_pipe = true;
Daniel Vetter26495482015-07-15 14:15:52 +020013404 }
13405
13406 if (needs_modeset(crtc_state)) {
13407 any_ms = true;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013408
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013409 ret = drm_atomic_add_affected_planes(state, crtc);
13410 if (ret)
13411 return ret;
13412 }
13413
Daniel Vetter26495482015-07-15 14:15:52 +020013414 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
13415 needs_modeset(crtc_state) ?
13416 "[modeset]" : "[fastset]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013417 }
13418
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013419 if (any_ms) {
13420 ret = intel_modeset_checks(state);
13421
13422 if (ret)
13423 return ret;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013424 } else
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013425 intel_state->cdclk = dev_priv->cdclk_freq;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013426
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013427 ret = drm_atomic_helper_check_planes(dev, state);
Matt Roperaa363132015-09-24 15:53:18 -070013428 if (ret)
13429 return ret;
13430
Paulo Zanonif51be2e2016-01-19 11:35:50 -020013431 intel_fbc_choose_crtc(dev_priv, state);
Matt Roperaa363132015-09-24 15:53:18 -070013432 calc_watermark_data(state);
13433
13434 return 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020013435}
13436
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013437static int intel_atomic_prepare_commit(struct drm_device *dev,
13438 struct drm_atomic_state *state,
Maarten Lankhorst81072bf2016-04-26 16:11:45 +020013439 bool nonblock)
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013440{
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013441 struct drm_i915_private *dev_priv = dev->dev_private;
13442 struct drm_plane_state *plane_state;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013443 struct drm_crtc_state *crtc_state;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013444 struct drm_plane *plane;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013445 struct drm_crtc *crtc;
13446 int i, ret;
13447
Maarten Lankhorst81072bf2016-04-26 16:11:45 +020013448 if (nonblock) {
13449 DRM_DEBUG_KMS("i915 does not yet support nonblocking commit\n");
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013450 return -EINVAL;
13451 }
13452
13453 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Chris Wilsonacf4e842016-04-17 20:42:46 +010013454 if (state->legacy_cursor_update)
13455 continue;
13456
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013457 ret = intel_crtc_wait_for_pending_flips(crtc);
13458 if (ret)
13459 return ret;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013460
13461 if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
13462 flush_workqueue(dev_priv->wq);
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013463 }
13464
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013465 ret = mutex_lock_interruptible(&dev->struct_mutex);
13466 if (ret)
13467 return ret;
13468
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013469 ret = drm_atomic_helper_prepare_planes(dev, state);
Chris Wilsonf7e58382016-04-13 17:35:07 +010013470 mutex_unlock(&dev->struct_mutex);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013471
Dave Airlie21daaee2016-05-05 09:56:30 +100013472 if (!ret && !nonblock) {
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013473 for_each_plane_in_state(state, plane, plane_state, i) {
13474 struct intel_plane_state *intel_plane_state =
13475 to_intel_plane_state(plane_state);
13476
13477 if (!intel_plane_state->wait_req)
13478 continue;
13479
13480 ret = __i915_wait_request(intel_plane_state->wait_req,
Chris Wilson299259a2016-04-13 17:35:06 +010013481 true, NULL, NULL);
Chris Wilsonf7e58382016-04-13 17:35:07 +010013482 if (ret) {
Chris Wilsonf4457ae2016-04-13 17:35:08 +010013483 /* Any hang should be swallowed by the wait */
13484 WARN_ON(ret == -EIO);
Chris Wilsonf7e58382016-04-13 17:35:07 +010013485 mutex_lock(&dev->struct_mutex);
13486 drm_atomic_helper_cleanup_planes(dev, state);
13487 mutex_unlock(&dev->struct_mutex);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013488 break;
Chris Wilsonf7e58382016-04-13 17:35:07 +010013489 }
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013490 }
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013491 }
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013492
13493 return ret;
13494}
13495
Maarten Lankhorste8861672016-02-24 11:24:26 +010013496static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
13497 struct drm_i915_private *dev_priv,
13498 unsigned crtc_mask)
13499{
13500 unsigned last_vblank_count[I915_MAX_PIPES];
13501 enum pipe pipe;
13502 int ret;
13503
13504 if (!crtc_mask)
13505 return;
13506
13507 for_each_pipe(dev_priv, pipe) {
13508 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13509
13510 if (!((1 << pipe) & crtc_mask))
13511 continue;
13512
13513 ret = drm_crtc_vblank_get(crtc);
13514 if (WARN_ON(ret != 0)) {
13515 crtc_mask &= ~(1 << pipe);
13516 continue;
13517 }
13518
13519 last_vblank_count[pipe] = drm_crtc_vblank_count(crtc);
13520 }
13521
13522 for_each_pipe(dev_priv, pipe) {
13523 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13524 long lret;
13525
13526 if (!((1 << pipe) & crtc_mask))
13527 continue;
13528
13529 lret = wait_event_timeout(dev->vblank[pipe].queue,
13530 last_vblank_count[pipe] !=
13531 drm_crtc_vblank_count(crtc),
13532 msecs_to_jiffies(50));
13533
Ville Syrjälä8a8dae22016-04-18 14:29:32 +030013534 WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
Maarten Lankhorste8861672016-02-24 11:24:26 +010013535
13536 drm_crtc_vblank_put(crtc);
13537 }
13538}
13539
13540static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
13541{
13542 /* fb updated, need to unpin old fb */
13543 if (crtc_state->fb_changed)
13544 return true;
13545
13546 /* wm changes, need vblank before final wm's */
Ville Syrjäläcaed3612016-03-09 19:07:25 +020013547 if (crtc_state->update_wm_post)
Maarten Lankhorste8861672016-02-24 11:24:26 +010013548 return true;
13549
13550 /*
13551 * cxsr is re-enabled after vblank.
Ville Syrjäläcaed3612016-03-09 19:07:25 +020013552 * This is already handled by crtc_state->update_wm_post,
Maarten Lankhorste8861672016-02-24 11:24:26 +010013553 * but added for clarity.
13554 */
13555 if (crtc_state->disable_cxsr)
13556 return true;
13557
13558 return false;
13559}
13560
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013561/**
13562 * intel_atomic_commit - commit validated state object
13563 * @dev: DRM device
13564 * @state: the top-level driver state object
Maarten Lankhorst81072bf2016-04-26 16:11:45 +020013565 * @nonblock: nonblocking commit
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013566 *
13567 * This function commits a top-level state object that has been validated
13568 * with drm_atomic_helper_check().
13569 *
13570 * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
13571 * we can only handle plane-related operations and do not yet support
Maarten Lankhorst81072bf2016-04-26 16:11:45 +020013572 * nonblocking commit.
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013573 *
13574 * RETURNS
13575 * Zero for success or -errno.
13576 */
13577static int intel_atomic_commit(struct drm_device *dev,
13578 struct drm_atomic_state *state,
Maarten Lankhorst81072bf2016-04-26 16:11:45 +020013579 bool nonblock)
Daniel Vettera6778b32012-07-02 09:56:42 +020013580{
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013581 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Jani Nikulafbee40d2014-03-31 14:27:18 +030013582 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013583 struct drm_crtc_state *old_crtc_state;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013584 struct drm_crtc *crtc;
Matt Ropered4a6a72016-02-23 17:20:13 -080013585 struct intel_crtc_state *intel_cstate;
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013586 int ret = 0, i;
13587 bool hw_check = intel_state->modeset;
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013588 unsigned long put_domains[I915_MAX_PIPES] = {};
Maarten Lankhorste8861672016-02-24 11:24:26 +010013589 unsigned crtc_vblank_mask = 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020013590
Maarten Lankhorst81072bf2016-04-26 16:11:45 +020013591 ret = intel_atomic_prepare_commit(dev, state, nonblock);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013592 if (ret) {
13593 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013594 return ret;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013595 }
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013596
Maarten Lankhorst1c5e19f2015-06-01 12:50:06 +020013597 drm_atomic_helper_swap_state(dev, state);
Maarten Lankhorsta1475e72016-03-14 09:27:53 +010013598 dev_priv->wm.config = intel_state->wm_config;
13599 intel_shared_dpll_commit(state);
Maarten Lankhorst1c5e19f2015-06-01 12:50:06 +020013600
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013601 if (intel_state->modeset) {
13602 memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
13603 sizeof(intel_state->min_pixclk));
13604 dev_priv->active_crtcs = intel_state->active_crtcs;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013605 dev_priv->atomic_cdclk_freq = intel_state->cdclk;
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013606
13607 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013608 }
13609
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013610 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013611 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13612
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013613 if (needs_modeset(crtc->state) ||
13614 to_intel_crtc_state(crtc->state)->update_pipe) {
13615 hw_check = true;
13616
13617 put_domains[to_intel_crtc(crtc)->pipe] =
13618 modeset_get_crtc_power_domains(crtc,
13619 to_intel_crtc_state(crtc->state));
13620 }
13621
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013622 if (!needs_modeset(crtc->state))
13623 continue;
13624
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013625 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
Daniel Vetter460da9162013-03-27 00:44:51 +010013626
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013627 if (old_crtc_state->active) {
13628 intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013629 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013630 intel_crtc->active = false;
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -020013631 intel_fbc_disable(intel_crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013632 intel_disable_shared_dpll(intel_crtc);
Ville Syrjälä9bbc8258a2015-11-20 22:09:20 +020013633
13634 /*
13635 * Underruns don't always raise
13636 * interrupts, so check manually.
13637 */
13638 intel_check_cpu_fifo_underruns(dev_priv);
13639 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorstb9001112015-11-19 16:07:16 +010013640
13641 if (!crtc->state->active)
13642 intel_update_watermarks(crtc);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013643 }
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010013644 }
Daniel Vetter7758a112012-07-08 19:40:39 +020013645
Daniel Vetterea9d7582012-07-10 10:42:52 +020013646 /* Only after disabling all output pipelines that will be changed can we
13647 * update the the output configuration. */
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013648 intel_modeset_update_crtc_state(state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020013649
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013650 if (intel_state->modeset) {
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013651 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013652
13653 if (dev_priv->display.modeset_commit_cdclk &&
13654 intel_state->dev_cdclk != dev_priv->cdclk_freq)
13655 dev_priv->display.modeset_commit_cdclk(state);
Maarten Lankhorstf6d19732016-03-23 14:58:07 +010013656
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013657 intel_modeset_verify_disabled(dev);
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013658 }
Daniel Vetter47fab732012-10-26 10:58:18 +020013659
Daniel Vettera6778b32012-07-02 09:56:42 +020013660 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013661 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013662 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13663 bool modeset = needs_modeset(crtc->state);
Maarten Lankhorste8861672016-02-24 11:24:26 +010013664 struct intel_crtc_state *pipe_config =
13665 to_intel_crtc_state(crtc->state);
13666 bool update_pipe = !modeset && pipe_config->update_pipe;
Patrik Jakobsson9f836f92015-11-16 16:20:01 +010013667
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013668 if (modeset && crtc->state->active) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013669 update_scanline_offset(to_intel_crtc(crtc));
13670 dev_priv->display.crtc_enable(crtc);
13671 }
13672
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013673 if (!modeset)
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013674 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013675
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +010013676 if (crtc->state->active &&
13677 drm_atomic_get_existing_plane_state(state, crtc->primary))
Paulo Zanoni49227c42016-01-19 11:35:52 -020013678 intel_fbc_enable(intel_crtc);
13679
Maarten Lankhorst6173ee22015-09-23 16:29:39 +020013680 if (crtc->state->active &&
13681 (crtc->state->planes_changed || update_pipe))
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013682 drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013683
Maarten Lankhorste8861672016-02-24 11:24:26 +010013684 if (pipe_config->base.active && needs_vblank_wait(pipe_config))
13685 crtc_vblank_mask |= 1 << i;
Ville Syrjälä80715b22014-05-15 20:23:23 +030013686 }
Daniel Vettera6778b32012-07-02 09:56:42 +020013687
Daniel Vettera6778b32012-07-02 09:56:42 +020013688 /* FIXME: add subpixel order */
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013689
Maarten Lankhorste8861672016-02-24 11:24:26 +010013690 if (!state->legacy_cursor_update)
13691 intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013692
Matt Ropered4a6a72016-02-23 17:20:13 -080013693 /*
13694 * Now that the vblank has passed, we can go ahead and program the
13695 * optimal watermarks on platforms that need two-step watermark
13696 * programming.
13697 *
13698 * TODO: Move this (and other cleanup) to an async worker eventually.
13699 */
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013700 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Matt Ropered4a6a72016-02-23 17:20:13 -080013701 intel_cstate = to_intel_crtc_state(crtc->state);
13702
13703 if (dev_priv->display.optimize_watermarks)
13704 dev_priv->display.optimize_watermarks(intel_cstate);
13705 }
13706
Matt Roper177246a2016-03-04 15:59:39 -080013707 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
13708 intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
13709
13710 if (put_domains[i])
13711 modeset_put_power_domains(dev_priv, put_domains[i]);
Maarten Lankhorstf6d19732016-03-23 14:58:07 +010013712
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013713 intel_modeset_verify_crtc(crtc, old_crtc_state, crtc->state);
Matt Roper177246a2016-03-04 15:59:39 -080013714 }
13715
13716 if (intel_state->modeset)
13717 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
13718
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013719 mutex_lock(&dev->struct_mutex);
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013720 drm_atomic_helper_cleanup_planes(dev, state);
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013721 mutex_unlock(&dev->struct_mutex);
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013722
Maarten Lankhorstee165b12015-08-05 12:37:00 +020013723 drm_atomic_state_free(state);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013724
Mika Kuoppala75714942015-12-16 09:26:48 +020013725 /* As one of the primary mmio accessors, KMS has a high likelihood
13726 * of triggering bugs in unclaimed access. After we finish
13727 * modesetting, see if an error has been flagged, and if so
13728 * enable debugging for the next modeset - and hope we catch
13729 * the culprit.
13730 *
13731 * XXX note that we assume display power is on at this point.
13732 * This might hold true now but we need to add pm helper to check
13733 * unclaimed only when the hardware is on, as atomic commits
13734 * can happen also when the device is completely off.
13735 */
13736 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
13737
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013738 return 0;
Daniel Vetterf30da182013-04-11 20:22:50 +020013739}
13740
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013741void intel_crtc_restore_mode(struct drm_crtc *crtc)
13742{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013743 struct drm_device *dev = crtc->dev;
13744 struct drm_atomic_state *state;
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013745 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013746 int ret;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013747
13748 state = drm_atomic_state_alloc(dev);
13749 if (!state) {
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013750 DRM_DEBUG_KMS("[CRTC:%d] crtc restore failed, out of memory",
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013751 crtc->base.id);
13752 return;
13753 }
13754
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013755 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013756
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013757retry:
13758 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13759 ret = PTR_ERR_OR_ZERO(crtc_state);
13760 if (!ret) {
13761 if (!crtc_state->active)
13762 goto out;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013763
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013764 crtc_state->mode_changed = true;
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013765 ret = drm_atomic_commit(state);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013766 }
13767
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013768 if (ret == -EDEADLK) {
13769 drm_atomic_state_clear(state);
13770 drm_modeset_backoff(state->acquire_ctx);
13771 goto retry;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013772 }
13773
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013774 if (ret)
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013775out:
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013776 drm_atomic_state_free(state);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013777}
13778
Daniel Vetter25c5b262012-07-08 22:08:04 +020013779#undef for_each_intel_crtc_masked
13780
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013781static const struct drm_crtc_funcs intel_crtc_funcs = {
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000013782 .gamma_set = drm_atomic_helper_legacy_gamma_set,
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013783 .set_config = drm_atomic_helper_set_config,
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000013784 .set_property = drm_atomic_helper_crtc_set_property,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013785 .destroy = intel_crtc_destroy,
13786 .page_flip = intel_crtc_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080013787 .atomic_duplicate_state = intel_crtc_duplicate_state,
13788 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013789};
13790
Matt Roper6beb8c232014-12-01 15:40:14 -080013791/**
13792 * intel_prepare_plane_fb - Prepare fb for usage on plane
13793 * @plane: drm plane to prepare for
13794 * @fb: framebuffer to prepare for presentation
13795 *
13796 * Prepares a framebuffer for usage on a display plane. Generally this
13797 * involves pinning the underlying object and updating the frontbuffer tracking
13798 * bits. Some older platforms need special physical address handling for
13799 * cursor planes.
13800 *
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013801 * Must be called with struct_mutex held.
13802 *
Matt Roper6beb8c232014-12-01 15:40:14 -080013803 * Returns 0 on success, negative error code on failure.
13804 */
13805int
13806intel_prepare_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013807 const struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070013808{
13809 struct drm_device *dev = plane->dev;
Maarten Lankhorst844f9112015-09-02 10:42:40 +020013810 struct drm_framebuffer *fb = new_state->fb;
Matt Roper6beb8c232014-12-01 15:40:14 -080013811 struct intel_plane *intel_plane = to_intel_plane(plane);
Matt Roper6beb8c232014-12-01 15:40:14 -080013812 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013813 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
Matt Roper6beb8c232014-12-01 15:40:14 -080013814 int ret = 0;
Matt Roper465c1202014-05-29 08:06:54 -070013815
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013816 if (!obj && !old_obj)
Matt Roper465c1202014-05-29 08:06:54 -070013817 return 0;
13818
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013819 if (old_obj) {
13820 struct drm_crtc_state *crtc_state =
13821 drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
13822
13823 /* Big Hammer, we also need to ensure that any pending
13824 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
13825 * current scanout is retired before unpinning the old
13826 * framebuffer. Note that we rely on userspace rendering
13827 * into the buffer attached to the pipe they are waiting
13828 * on. If not, userspace generates a GPU hang with IPEHR
13829 * point to the MI_WAIT_FOR_EVENT.
13830 *
13831 * This should only fail upon a hung GPU, in which case we
13832 * can safely continue.
13833 */
13834 if (needs_modeset(crtc_state))
13835 ret = i915_gem_object_wait_rendering(old_obj, true);
Chris Wilsonf4457ae2016-04-13 17:35:08 +010013836 if (ret) {
13837 /* GPU hangs should have been swallowed by the wait */
13838 WARN_ON(ret == -EIO);
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013839 return ret;
Chris Wilsonf4457ae2016-04-13 17:35:08 +010013840 }
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013841 }
13842
Alex Goins3c28ff22015-11-25 18:43:39 -080013843 /* For framebuffer backed by dmabuf, wait for fence */
13844 if (obj && obj->base.dma_buf) {
Maarten Lankhorstbcf8be22015-12-08 15:52:56 +010013845 long lret;
Alex Goins3c28ff22015-11-25 18:43:39 -080013846
Maarten Lankhorstbcf8be22015-12-08 15:52:56 +010013847 lret = reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
13848 false, true,
13849 MAX_SCHEDULE_TIMEOUT);
13850 if (lret == -ERESTARTSYS)
13851 return lret;
13852
13853 WARN(lret < 0, "waiting returns %li\n", lret);
Alex Goins3c28ff22015-11-25 18:43:39 -080013854 }
13855
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013856 if (!obj) {
13857 ret = 0;
13858 } else if (plane->type == DRM_PLANE_TYPE_CURSOR &&
Matt Roper6beb8c232014-12-01 15:40:14 -080013859 INTEL_INFO(dev)->cursor_needs_physical) {
13860 int align = IS_I830(dev) ? 16 * 1024 : 256;
13861 ret = i915_gem_object_attach_phys(obj, align);
13862 if (ret)
13863 DRM_DEBUG_KMS("failed to attach phys object\n");
13864 } else {
Ville Syrjälä3465c582016-02-15 22:54:43 +020013865 ret = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
Matt Roper6beb8c232014-12-01 15:40:14 -080013866 }
13867
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013868 if (ret == 0) {
13869 if (obj) {
13870 struct intel_plane_state *plane_state =
13871 to_intel_plane_state(new_state);
13872
13873 i915_gem_request_assign(&plane_state->wait_req,
13874 obj->last_write_req);
13875 }
13876
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030013877 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013878 }
Matt Roper6beb8c232014-12-01 15:40:14 -080013879
Matt Roper6beb8c232014-12-01 15:40:14 -080013880 return ret;
13881}
13882
Matt Roper38f3ce32014-12-02 07:45:25 -080013883/**
13884 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13885 * @plane: drm plane to clean up for
13886 * @fb: old framebuffer that was on plane
13887 *
13888 * Cleans up a framebuffer that has just been removed from a plane.
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013889 *
13890 * Must be called with struct_mutex held.
Matt Roper38f3ce32014-12-02 07:45:25 -080013891 */
13892void
13893intel_cleanup_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013894 const struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080013895{
13896 struct drm_device *dev = plane->dev;
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013897 struct intel_plane *intel_plane = to_intel_plane(plane);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013898 struct intel_plane_state *old_intel_state;
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013899 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
13900 struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
Matt Roper38f3ce32014-12-02 07:45:25 -080013901
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013902 old_intel_state = to_intel_plane_state(old_state);
13903
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013904 if (!obj && !old_obj)
Matt Roper38f3ce32014-12-02 07:45:25 -080013905 return;
13906
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013907 if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
13908 !INTEL_INFO(dev)->cursor_needs_physical))
Ville Syrjälä3465c582016-02-15 22:54:43 +020013909 intel_unpin_fb_obj(old_state->fb, old_state->rotation);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013910
13911 /* prepare_fb aborted? */
13912 if ((old_obj && (old_obj->frontbuffer_bits & intel_plane->frontbuffer_bit)) ||
13913 (obj && !(obj->frontbuffer_bits & intel_plane->frontbuffer_bit)))
13914 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013915
13916 i915_gem_request_assign(&old_intel_state->wait_req, NULL);
Matt Roper465c1202014-05-29 08:06:54 -070013917}
13918
Chandra Konduru6156a452015-04-27 13:48:39 -070013919int
13920skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13921{
13922 int max_scale;
13923 struct drm_device *dev;
13924 struct drm_i915_private *dev_priv;
13925 int crtc_clock, cdclk;
13926
Maarten Lankhorstbf8a0af2015-11-24 11:29:02 +010013927 if (!intel_crtc || !crtc_state->base.enable)
Chandra Konduru6156a452015-04-27 13:48:39 -070013928 return DRM_PLANE_HELPER_NO_SCALING;
13929
13930 dev = intel_crtc->base.dev;
13931 dev_priv = dev->dev_private;
13932 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013933 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
Chandra Konduru6156a452015-04-27 13:48:39 -070013934
Tvrtko Ursulin54bf1ce2015-10-20 17:17:07 +010013935 if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
Chandra Konduru6156a452015-04-27 13:48:39 -070013936 return DRM_PLANE_HELPER_NO_SCALING;
13937
13938 /*
13939 * skl max scale is lower of:
13940 * close to 3 but not 3, -1 is for that purpose
13941 * or
13942 * cdclk/crtc_clock
13943 */
13944 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
13945
13946 return max_scale;
13947}
13948
Matt Roper465c1202014-05-29 08:06:54 -070013949static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013950intel_check_primary_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013951 struct intel_crtc_state *crtc_state,
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013952 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070013953{
Matt Roper2b875c22014-12-01 15:40:13 -080013954 struct drm_crtc *crtc = state->base.crtc;
13955 struct drm_framebuffer *fb = state->base.fb;
Chandra Konduru6156a452015-04-27 13:48:39 -070013956 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013957 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13958 bool can_position = false;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013959
Ville Syrjälä693bdc22016-01-15 20:46:53 +020013960 if (INTEL_INFO(plane->dev)->gen >= 9) {
13961 /* use scaler when colorkey is not required */
13962 if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
13963 min_scale = 1;
13964 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
13965 }
Sonika Jindald8106362015-04-10 14:37:28 +053013966 can_position = true;
Chandra Konduru6156a452015-04-27 13:48:39 -070013967 }
Sonika Jindald8106362015-04-10 14:37:28 +053013968
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013969 return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
13970 &state->dst, &state->clip,
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020013971 min_scale, max_scale,
13972 can_position, true,
13973 &state->visible);
Matt Roper465c1202014-05-29 08:06:54 -070013974}
13975
Maarten Lankhorst613d2b22015-07-21 13:28:58 +020013976static void intel_begin_crtc_commit(struct drm_crtc *crtc,
13977 struct drm_crtc_state *old_crtc_state)
Matt Roper32b7eee2014-12-24 07:59:06 -080013978{
13979 struct drm_device *dev = crtc->dev;
Matt Roper32b7eee2014-12-24 07:59:06 -080013980 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013981 struct intel_crtc_state *old_intel_state =
13982 to_intel_crtc_state(old_crtc_state);
13983 bool modeset = needs_modeset(crtc->state);
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013984
Matt Roperc34c9ee2014-12-23 10:41:50 -080013985 /* Perform vblank evasion around commit operation */
Maarten Lankhorst62852622015-09-23 16:29:38 +020013986 intel_pipe_update_start(intel_crtc);
Maarten Lankhorst05832362015-06-15 12:33:48 +020013987
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013988 if (modeset)
13989 return;
13990
Maarten Lankhorst20a34e72016-03-30 17:16:36 +020013991 if (crtc->state->color_mgmt_changed || to_intel_crtc_state(crtc->state)->update_pipe) {
13992 intel_color_set_csc(crtc->state);
13993 intel_color_load_luts(crtc->state);
13994 }
13995
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013996 if (to_intel_crtc_state(crtc->state)->update_pipe)
13997 intel_update_pipe_config(intel_crtc, old_intel_state);
13998 else if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorst05832362015-06-15 12:33:48 +020013999 skl_detach_scalers(intel_crtc);
Matt Roper32b7eee2014-12-24 07:59:06 -080014000}
14001
Maarten Lankhorst613d2b22015-07-21 13:28:58 +020014002static void intel_finish_crtc_commit(struct drm_crtc *crtc,
14003 struct drm_crtc_state *old_crtc_state)
Matt Roper32b7eee2014-12-24 07:59:06 -080014004{
Matt Roper32b7eee2014-12-24 07:59:06 -080014005 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roper32b7eee2014-12-24 07:59:06 -080014006
Maarten Lankhorst62852622015-09-23 16:29:38 +020014007 intel_pipe_update_end(intel_crtc);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030014008}
14009
Matt Ropercf4c7c12014-12-04 10:27:42 -080014010/**
Matt Roper4a3b8762014-12-23 10:41:51 -080014011 * intel_plane_destroy - destroy a plane
14012 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080014013 *
Matt Roper4a3b8762014-12-23 10:41:51 -080014014 * Common destruction function for all types of planes (primary, cursor,
14015 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080014016 */
Matt Roper4a3b8762014-12-23 10:41:51 -080014017void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070014018{
14019 struct intel_plane *intel_plane = to_intel_plane(plane);
14020 drm_plane_cleanup(plane);
14021 kfree(intel_plane);
14022}
14023
Matt Roper65a3fea2015-01-21 16:35:42 -080014024const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070014025 .update_plane = drm_atomic_helper_update_plane,
14026 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070014027 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080014028 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080014029 .atomic_get_property = intel_plane_atomic_get_property,
14030 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080014031 .atomic_duplicate_state = intel_plane_duplicate_state,
14032 .atomic_destroy_state = intel_plane_destroy_state,
14033
Matt Roper465c1202014-05-29 08:06:54 -070014034};
14035
14036static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
14037 int pipe)
14038{
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014039 struct intel_plane *primary = NULL;
14040 struct intel_plane_state *state = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070014041 const uint32_t *intel_primary_formats;
Thierry Reding45e37432015-08-12 16:54:28 +020014042 unsigned int num_formats;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014043 int ret;
Matt Roper465c1202014-05-29 08:06:54 -070014044
14045 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014046 if (!primary)
14047 goto fail;
Matt Roper465c1202014-05-29 08:06:54 -070014048
Matt Roper8e7d6882015-01-21 16:35:41 -080014049 state = intel_create_plane_state(&primary->base);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014050 if (!state)
14051 goto fail;
Matt Roper8e7d6882015-01-21 16:35:41 -080014052 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080014053
Matt Roper465c1202014-05-29 08:06:54 -070014054 primary->can_scale = false;
14055 primary->max_downscale = 1;
Chandra Konduru6156a452015-04-27 13:48:39 -070014056 if (INTEL_INFO(dev)->gen >= 9) {
14057 primary->can_scale = true;
Chandra Konduruaf99ced2015-05-11 14:35:47 -070014058 state->scaler_id = -1;
Chandra Konduru6156a452015-04-27 13:48:39 -070014059 }
Matt Roper465c1202014-05-29 08:06:54 -070014060 primary->pipe = pipe;
14061 primary->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030014062 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080014063 primary->check_plane = intel_check_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070014064 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
14065 primary->plane = !pipe;
14066
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014067 if (INTEL_INFO(dev)->gen >= 9) {
14068 intel_primary_formats = skl_primary_formats;
14069 num_formats = ARRAY_SIZE(skl_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010014070
14071 primary->update_plane = skylake_update_primary_plane;
14072 primary->disable_plane = skylake_disable_primary_plane;
14073 } else if (HAS_PCH_SPLIT(dev)) {
14074 intel_primary_formats = i965_primary_formats;
14075 num_formats = ARRAY_SIZE(i965_primary_formats);
14076
14077 primary->update_plane = ironlake_update_primary_plane;
14078 primary->disable_plane = i9xx_disable_primary_plane;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014079 } else if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau568db4f2015-05-12 16:13:18 +010014080 intel_primary_formats = i965_primary_formats;
14081 num_formats = ARRAY_SIZE(i965_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010014082
14083 primary->update_plane = i9xx_update_primary_plane;
14084 primary->disable_plane = i9xx_disable_primary_plane;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014085 } else {
14086 intel_primary_formats = i8xx_primary_formats;
14087 num_formats = ARRAY_SIZE(i8xx_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010014088
14089 primary->update_plane = i9xx_update_primary_plane;
14090 primary->disable_plane = i9xx_disable_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070014091 }
14092
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014093 ret = drm_universal_plane_init(dev, &primary->base, 0,
14094 &intel_plane_funcs,
14095 intel_primary_formats, num_formats,
14096 DRM_PLANE_TYPE_PRIMARY, NULL);
14097 if (ret)
14098 goto fail;
Sonika Jindal48404c12014-08-22 14:06:04 +053014099
Sonika Jindal3b7a5112015-04-10 14:37:29 +053014100 if (INTEL_INFO(dev)->gen >= 4)
14101 intel_create_rotation_property(dev, primary);
Sonika Jindal48404c12014-08-22 14:06:04 +053014102
Matt Roperea2c67b2014-12-23 10:41:52 -080014103 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
14104
Matt Roper465c1202014-05-29 08:06:54 -070014105 return &primary->base;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014106
14107fail:
14108 kfree(state);
14109 kfree(primary);
14110
14111 return NULL;
Matt Roper465c1202014-05-29 08:06:54 -070014112}
14113
Sonika Jindal3b7a5112015-04-10 14:37:29 +053014114void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
14115{
14116 if (!dev->mode_config.rotation_property) {
14117 unsigned long flags = BIT(DRM_ROTATE_0) |
14118 BIT(DRM_ROTATE_180);
14119
14120 if (INTEL_INFO(dev)->gen >= 9)
14121 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
14122
14123 dev->mode_config.rotation_property =
14124 drm_mode_create_rotation_property(dev, flags);
14125 }
14126 if (dev->mode_config.rotation_property)
14127 drm_object_attach_property(&plane->base.base,
14128 dev->mode_config.rotation_property,
14129 plane->base.state->rotation);
14130}
14131
Matt Roper3d7d6512014-06-10 08:28:13 -070014132static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030014133intel_check_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014134 struct intel_crtc_state *crtc_state,
Gustavo Padovan852e7872014-09-05 17:22:31 -030014135 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070014136{
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014137 struct drm_crtc *crtc = crtc_state->base.crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080014138 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014139 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Ville Syrjäläb29ec922015-12-18 19:24:39 +020014140 enum pipe pipe = to_intel_plane(plane)->pipe;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014141 unsigned stride;
14142 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030014143
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014144 ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
14145 &state->dst, &state->clip,
Gustavo Padovan852e7872014-09-05 17:22:31 -030014146 DRM_PLANE_HELPER_NO_SCALING,
14147 DRM_PLANE_HELPER_NO_SCALING,
14148 true, true, &state->visible);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014149 if (ret)
14150 return ret;
14151
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014152 /* if we want to turn off the cursor ignore width and height */
14153 if (!obj)
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014154 return 0;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014155
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014156 /* Check for which cursor types we support */
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014157 if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
Matt Roperea2c67b2014-12-23 10:41:52 -080014158 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
14159 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014160 return -EINVAL;
14161 }
14162
Matt Roperea2c67b2014-12-23 10:41:52 -080014163 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
14164 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014165 DRM_DEBUG_KMS("buffer is too small\n");
14166 return -ENOMEM;
14167 }
14168
Ville Syrjälä3a656b52015-03-09 21:08:37 +020014169 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014170 DRM_DEBUG_KMS("cursor cannot be tiled\n");
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014171 return -EINVAL;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014172 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014173
Ville Syrjäläb29ec922015-12-18 19:24:39 +020014174 /*
14175 * There's something wrong with the cursor on CHV pipe C.
14176 * If it straddles the left edge of the screen then
14177 * moving it away from the edge or disabling it often
14178 * results in a pipe underrun, and often that can lead to
14179 * dead pipe (constant underrun reported, and it scans
14180 * out just a solid color). To recover from that, the
14181 * display power well must be turned off and on again.
14182 * Refuse the put the cursor into that compromised position.
14183 */
14184 if (IS_CHERRYVIEW(plane->dev) && pipe == PIPE_C &&
14185 state->visible && state->base.crtc_x < 0) {
14186 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
14187 return -EINVAL;
14188 }
14189
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014190 return 0;
Gustavo Padovan852e7872014-09-05 17:22:31 -030014191}
14192
Matt Roperf4a2cf22014-12-01 15:40:12 -080014193static void
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014194intel_disable_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst7fabf5e2015-06-15 12:33:47 +020014195 struct drm_crtc *crtc)
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014196{
Maarten Lankhorstf2858022016-01-07 11:54:09 +010014197 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14198
14199 intel_crtc->cursor_addr = 0;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014200 intel_crtc_update_cursor(crtc, NULL);
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014201}
14202
14203static void
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014204intel_update_cursor_plane(struct drm_plane *plane,
14205 const struct intel_crtc_state *crtc_state,
14206 const struct intel_plane_state *state)
Gustavo Padovan852e7872014-09-05 17:22:31 -030014207{
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014208 struct drm_crtc *crtc = crtc_state->base.crtc;
14209 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roperea2c67b2014-12-23 10:41:52 -080014210 struct drm_device *dev = plane->dev;
Matt Roper2b875c22014-12-01 15:40:13 -080014211 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080014212 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070014213
Matt Roperf4a2cf22014-12-01 15:40:12 -080014214 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080014215 addr = 0;
Matt Roperf4a2cf22014-12-01 15:40:12 -080014216 else if (!INTEL_INFO(dev)->cursor_needs_physical)
Gustavo Padovana912f122014-12-01 15:40:10 -080014217 addr = i915_gem_obj_ggtt_offset(obj);
Matt Roperf4a2cf22014-12-01 15:40:12 -080014218 else
Gustavo Padovana912f122014-12-01 15:40:10 -080014219 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080014220
Gustavo Padovana912f122014-12-01 15:40:10 -080014221 intel_crtc->cursor_addr = addr;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014222 intel_crtc_update_cursor(crtc, state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014223}
Gustavo Padovan852e7872014-09-05 17:22:31 -030014224
Matt Roper3d7d6512014-06-10 08:28:13 -070014225static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
14226 int pipe)
14227{
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014228 struct intel_plane *cursor = NULL;
14229 struct intel_plane_state *state = NULL;
14230 int ret;
Matt Roper3d7d6512014-06-10 08:28:13 -070014231
14232 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014233 if (!cursor)
14234 goto fail;
Matt Roper3d7d6512014-06-10 08:28:13 -070014235
Matt Roper8e7d6882015-01-21 16:35:41 -080014236 state = intel_create_plane_state(&cursor->base);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014237 if (!state)
14238 goto fail;
Matt Roper8e7d6882015-01-21 16:35:41 -080014239 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080014240
Matt Roper3d7d6512014-06-10 08:28:13 -070014241 cursor->can_scale = false;
14242 cursor->max_downscale = 1;
14243 cursor->pipe = pipe;
14244 cursor->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030014245 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080014246 cursor->check_plane = intel_check_cursor_plane;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014247 cursor->update_plane = intel_update_cursor_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014248 cursor->disable_plane = intel_disable_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070014249
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014250 ret = drm_universal_plane_init(dev, &cursor->base, 0,
14251 &intel_plane_funcs,
14252 intel_cursor_formats,
14253 ARRAY_SIZE(intel_cursor_formats),
14254 DRM_PLANE_TYPE_CURSOR, NULL);
14255 if (ret)
14256 goto fail;
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014257
14258 if (INTEL_INFO(dev)->gen >= 4) {
14259 if (!dev->mode_config.rotation_property)
14260 dev->mode_config.rotation_property =
14261 drm_mode_create_rotation_property(dev,
14262 BIT(DRM_ROTATE_0) |
14263 BIT(DRM_ROTATE_180));
14264 if (dev->mode_config.rotation_property)
14265 drm_object_attach_property(&cursor->base.base,
14266 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080014267 state->base.rotation);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014268 }
14269
Chandra Konduruaf99ced2015-05-11 14:35:47 -070014270 if (INTEL_INFO(dev)->gen >=9)
14271 state->scaler_id = -1;
14272
Matt Roperea2c67b2014-12-23 10:41:52 -080014273 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
14274
Matt Roper3d7d6512014-06-10 08:28:13 -070014275 return &cursor->base;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014276
14277fail:
14278 kfree(state);
14279 kfree(cursor);
14280
14281 return NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070014282}
14283
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014284static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
14285 struct intel_crtc_state *crtc_state)
14286{
14287 int i;
14288 struct intel_scaler *intel_scaler;
14289 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
14290
14291 for (i = 0; i < intel_crtc->num_scalers; i++) {
14292 intel_scaler = &scaler_state->scalers[i];
14293 intel_scaler->in_use = 0;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014294 intel_scaler->mode = PS_SCALER_MODE_DYN;
14295 }
14296
14297 scaler_state->scaler_id = -1;
14298}
14299
Hannes Ederb358d0a2008-12-18 21:18:47 +010014300static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080014301{
Jani Nikulafbee40d2014-03-31 14:27:18 +030014302 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080014303 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014304 struct intel_crtc_state *crtc_state = NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070014305 struct drm_plane *primary = NULL;
14306 struct drm_plane *cursor = NULL;
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +000014307 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080014308
Daniel Vetter955382f2013-09-19 14:05:45 +020014309 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080014310 if (intel_crtc == NULL)
14311 return;
14312
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014313 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
14314 if (!crtc_state)
14315 goto fail;
Ander Conselvan de Oliveira550acef2015-04-21 17:13:24 +030014316 intel_crtc->config = crtc_state;
14317 intel_crtc->base.state = &crtc_state->base;
Matt Roper07878242015-02-25 11:43:26 -080014318 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014319
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014320 /* initialize shared scalers */
14321 if (INTEL_INFO(dev)->gen >= 9) {
14322 if (pipe == PIPE_C)
14323 intel_crtc->num_scalers = 1;
14324 else
14325 intel_crtc->num_scalers = SKL_NUM_SCALERS;
14326
14327 skl_init_scalers(dev, intel_crtc, crtc_state);
14328 }
14329
Matt Roper465c1202014-05-29 08:06:54 -070014330 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014331 if (!primary)
14332 goto fail;
14333
14334 cursor = intel_cursor_plane_create(dev, pipe);
14335 if (!cursor)
14336 goto fail;
14337
Matt Roper465c1202014-05-29 08:06:54 -070014338 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Ville Syrjäläf9882872015-12-09 16:19:31 +020014339 cursor, &intel_crtc_funcs, NULL);
Matt Roper3d7d6512014-06-10 08:28:13 -070014340 if (ret)
14341 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080014342
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014343 /*
14344 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020014345 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014346 */
Jesse Barnes80824002009-09-10 15:28:06 -070014347 intel_crtc->pipe = pipe;
14348 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010014349 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080014350 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010014351 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070014352 }
14353
Chris Wilson4b0e3332014-05-30 16:35:26 +030014354 intel_crtc->cursor_base = ~0;
14355 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030014356 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030014357
Ville Syrjälä852eb002015-06-24 22:00:07 +030014358 intel_crtc->wm.cxsr_allowed = true;
14359
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080014360 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
14361 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
14362 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
14363 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
14364
Jesse Barnes79e53942008-11-07 14:24:08 -080014365 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020014366
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +000014367 intel_color_init(&intel_crtc->base);
14368
Daniel Vetter87b6b102014-05-15 15:33:46 +020014369 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014370 return;
14371
14372fail:
14373 if (primary)
14374 drm_plane_cleanup(primary);
14375 if (cursor)
14376 drm_plane_cleanup(cursor);
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014377 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014378 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080014379}
14380
Jesse Barnes752aa882013-10-31 18:55:49 +020014381enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14382{
14383 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014384 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020014385
Rob Clark51fd3712013-11-19 12:10:12 -050014386 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020014387
Ville Syrjäläd3babd32014-11-07 11:16:01 +020014388 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020014389 return INVALID_PIPE;
14390
14391 return to_intel_crtc(encoder->crtc)->pipe;
14392}
14393
Carl Worth08d7b3d2009-04-29 14:43:54 -070014394int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000014395 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070014396{
Carl Worth08d7b3d2009-04-29 14:43:54 -070014397 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040014398 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020014399 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014400
Rob Clark7707e652014-07-17 23:30:04 -040014401 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Carl Worth08d7b3d2009-04-29 14:43:54 -070014402
Rob Clark7707e652014-07-17 23:30:04 -040014403 if (!drmmode_crtc) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070014404 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030014405 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014406 }
14407
Rob Clark7707e652014-07-17 23:30:04 -040014408 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020014409 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014410
Daniel Vetterc05422d2009-08-11 16:05:30 +020014411 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014412}
14413
Daniel Vetter66a92782012-07-12 20:08:18 +020014414static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080014415{
Daniel Vetter66a92782012-07-12 20:08:18 +020014416 struct drm_device *dev = encoder->base.dev;
14417 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080014418 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080014419 int entry = 0;
14420
Damien Lespiaub2784e12014-08-05 11:29:37 +010014421 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020014422 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020014423 index_mask |= (1 << entry);
14424
Jesse Barnes79e53942008-11-07 14:24:08 -080014425 entry++;
14426 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010014427
Jesse Barnes79e53942008-11-07 14:24:08 -080014428 return index_mask;
14429}
14430
Chris Wilson4d302442010-12-14 19:21:29 +000014431static bool has_edp_a(struct drm_device *dev)
14432{
14433 struct drm_i915_private *dev_priv = dev->dev_private;
14434
14435 if (!IS_MOBILE(dev))
14436 return false;
14437
14438 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14439 return false;
14440
Damien Lespiaue3589902014-02-07 19:12:50 +000014441 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000014442 return false;
14443
14444 return true;
14445}
14446
Jesse Barnes84b4e042014-06-25 08:24:29 -070014447static bool intel_crt_present(struct drm_device *dev)
14448{
14449 struct drm_i915_private *dev_priv = dev->dev_private;
14450
Damien Lespiau884497e2013-12-03 13:56:23 +000014451 if (INTEL_INFO(dev)->gen >= 9)
14452 return false;
14453
Damien Lespiaucf404ce2014-10-01 20:04:15 +010014454 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
Jesse Barnes84b4e042014-06-25 08:24:29 -070014455 return false;
14456
14457 if (IS_CHERRYVIEW(dev))
14458 return false;
14459
Ville Syrjälä65e472e2015-12-01 23:28:55 +020014460 if (HAS_PCH_LPT_H(dev) && I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
14461 return false;
14462
Ville Syrjälä70ac54d2015-12-01 23:29:56 +020014463 /* DDI E can't be used if DDI A requires 4 lanes */
14464 if (HAS_DDI(dev) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
14465 return false;
14466
Ville Syrjäläe4abb732015-12-01 23:31:33 +020014467 if (!dev_priv->vbt.int_crt_support)
Jesse Barnes84b4e042014-06-25 08:24:29 -070014468 return false;
14469
14470 return true;
14471}
14472
Jesse Barnes79e53942008-11-07 14:24:08 -080014473static void intel_setup_outputs(struct drm_device *dev)
14474{
Eric Anholt725e30a2009-01-22 13:01:02 -080014475 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010014476 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014477 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080014478
Daniel Vetterc9093352013-06-06 22:22:47 +020014479 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014480
Jesse Barnes84b4e042014-06-25 08:24:29 -070014481 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020014482 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014483
Vandana Kannanc776eb22014-08-19 12:05:01 +053014484 if (IS_BROXTON(dev)) {
14485 /*
14486 * FIXME: Broxton doesn't support port detection via the
14487 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14488 * detect the ports.
14489 */
14490 intel_ddi_init(dev, PORT_A);
14491 intel_ddi_init(dev, PORT_B);
14492 intel_ddi_init(dev, PORT_C);
Shashank Sharmac6c794a2016-03-22 12:01:50 +020014493
14494 intel_dsi_init(dev);
Vandana Kannanc776eb22014-08-19 12:05:01 +053014495 } else if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014496 int found;
14497
Jesse Barnesde31fac2015-03-06 15:53:32 -080014498 /*
14499 * Haswell uses DDI functions to detect digital outputs.
14500 * On SKL pre-D0 the strap isn't connected, so we assume
14501 * it's there.
14502 */
Ville Syrjälä77179402015-09-18 20:03:35 +030014503 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080014504 /* WaIgnoreDDIAStrap: skl */
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070014505 if (found || IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014506 intel_ddi_init(dev, PORT_A);
14507
14508 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14509 * register */
14510 found = I915_READ(SFUSE_STRAP);
14511
14512 if (found & SFUSE_STRAP_DDIB_DETECTED)
14513 intel_ddi_init(dev, PORT_B);
14514 if (found & SFUSE_STRAP_DDIC_DETECTED)
14515 intel_ddi_init(dev, PORT_C);
14516 if (found & SFUSE_STRAP_DDID_DETECTED)
14517 intel_ddi_init(dev, PORT_D);
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014518 /*
14519 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
14520 */
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070014521 if ((IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) &&
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014522 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
14523 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
14524 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
14525 intel_ddi_init(dev, PORT_E);
14526
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014527 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014528 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020014529 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020014530
14531 if (has_edp_a(dev))
14532 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014533
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014534 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080014535 /* PCH SDVOB multiplex with HDMIB */
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014536 found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014537 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014538 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014539 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014540 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014541 }
14542
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014543 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014544 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014545
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014546 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014547 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014548
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014549 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014550 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014551
Daniel Vetter270b3042012-10-27 15:52:05 +020014552 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014553 intel_dp_init(dev, PCH_DP_D, PORT_D);
Wayne Boyer666a4532015-12-09 12:29:35 -080014554 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014555 /*
14556 * The DP_DETECTED bit is the latched state of the DDC
14557 * SDA pin at boot. However since eDP doesn't require DDC
14558 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14559 * eDP ports may have been muxed to an alternate function.
14560 * Thus we can't rely on the DP_DETECTED bit alone to detect
14561 * eDP ports. Consult the VBT as well as DP_DETECTED to
14562 * detect eDP ports.
14563 */
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014564 if (I915_READ(VLV_HDMIB) & SDVO_DETECTED &&
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014565 !intel_dp_is_edp(dev, PORT_B))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014566 intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
14567 if (I915_READ(VLV_DP_B) & DP_DETECTED ||
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014568 intel_dp_is_edp(dev, PORT_B))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014569 intel_dp_init(dev, VLV_DP_B, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014570
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014571 if (I915_READ(VLV_HDMIC) & SDVO_DETECTED &&
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014572 !intel_dp_is_edp(dev, PORT_C))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014573 intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
14574 if (I915_READ(VLV_DP_C) & DP_DETECTED ||
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014575 intel_dp_is_edp(dev, PORT_C))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014576 intel_dp_init(dev, VLV_DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053014577
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014578 if (IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014579 /* eDP not supported on port D, so don't check VBT */
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014580 if (I915_READ(CHV_HDMID) & SDVO_DETECTED)
14581 intel_hdmi_init(dev, CHV_HDMID, PORT_D);
14582 if (I915_READ(CHV_DP_D) & DP_DETECTED)
14583 intel_dp_init(dev, CHV_DP_D, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014584 }
14585
Jani Nikula3cfca972013-08-27 15:12:26 +030014586 intel_dsi_init(dev);
Daniel Vetter09da55d2015-07-07 11:44:32 +020014587 } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014588 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080014589
Paulo Zanonie2debe92013-02-18 19:00:27 -030014590 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014591 DRM_DEBUG_KMS("probing SDVOB\n");
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014592 found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014593 if (!found && IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014594 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014595 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014596 }
Ma Ling27185ae2009-08-24 13:50:23 +080014597
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014598 if (!found && IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014599 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080014600 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014601
14602 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014603
Paulo Zanonie2debe92013-02-18 19:00:27 -030014604 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014605 DRM_DEBUG_KMS("probing SDVOC\n");
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014606 found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014607 }
Ma Ling27185ae2009-08-24 13:50:23 +080014608
Paulo Zanonie2debe92013-02-18 19:00:27 -030014609 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014610
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014611 if (IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014612 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014613 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014614 }
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014615 if (IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014616 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080014617 }
Ma Ling27185ae2009-08-24 13:50:23 +080014618
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014619 if (IS_G4X(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030014620 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014621 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070014622 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014623 intel_dvo_init(dev);
14624
Zhenyu Wang103a1962009-11-27 11:44:36 +080014625 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014626 intel_tv_init(dev);
14627
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -080014628 intel_psr_init(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070014629
Damien Lespiaub2784e12014-08-05 11:29:37 +010014630 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010014631 encoder->base.possible_crtcs = encoder->crtc_mask;
14632 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020014633 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080014634 }
Chris Wilson47356eb2011-01-11 17:06:04 +000014635
Paulo Zanonidde86e22012-12-01 12:04:25 -020014636 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020014637
14638 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014639}
14640
14641static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14642{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014643 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080014644 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080014645
Daniel Vetteref2d6332014-02-10 18:00:38 +010014646 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014647 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010014648 WARN_ON(!intel_fb->obj->framebuffer_references--);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014649 drm_gem_object_unreference(&intel_fb->obj->base);
14650 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080014651 kfree(intel_fb);
14652}
14653
14654static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000014655 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080014656 unsigned int *handle)
14657{
14658 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000014659 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014660
Chris Wilsoncc917ab2015-10-13 14:22:26 +010014661 if (obj->userptr.mm) {
14662 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
14663 return -EINVAL;
14664 }
14665
Chris Wilson05394f32010-11-08 19:18:58 +000014666 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080014667}
14668
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014669static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
14670 struct drm_file *file,
14671 unsigned flags, unsigned color,
14672 struct drm_clip_rect *clips,
14673 unsigned num_clips)
14674{
14675 struct drm_device *dev = fb->dev;
14676 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
14677 struct drm_i915_gem_object *obj = intel_fb->obj;
14678
14679 mutex_lock(&dev->struct_mutex);
Paulo Zanoni74b4ea12015-07-14 16:29:14 -030014680 intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014681 mutex_unlock(&dev->struct_mutex);
14682
14683 return 0;
14684}
14685
Jesse Barnes79e53942008-11-07 14:24:08 -080014686static const struct drm_framebuffer_funcs intel_fb_funcs = {
14687 .destroy = intel_user_framebuffer_destroy,
14688 .create_handle = intel_user_framebuffer_create_handle,
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014689 .dirty = intel_user_framebuffer_dirty,
Jesse Barnes79e53942008-11-07 14:24:08 -080014690};
14691
Damien Lespiaub3218032015-02-27 11:15:18 +000014692static
14693u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14694 uint32_t pixel_format)
14695{
14696 u32 gen = INTEL_INFO(dev)->gen;
14697
14698 if (gen >= 9) {
Ville Syrjäläac484962016-01-20 21:05:26 +020014699 int cpp = drm_format_plane_cpp(pixel_format, 0);
14700
Damien Lespiaub3218032015-02-27 11:15:18 +000014701 /* "The stride in bytes must not exceed the of the size of 8K
14702 * pixels and 32K bytes."
14703 */
Ville Syrjäläac484962016-01-20 21:05:26 +020014704 return min(8192 * cpp, 32768);
Wayne Boyer666a4532015-12-09 12:29:35 -080014705 } else if (gen >= 5 && !IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014706 return 32*1024;
14707 } else if (gen >= 4) {
14708 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14709 return 16*1024;
14710 else
14711 return 32*1024;
14712 } else if (gen >= 3) {
14713 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14714 return 8*1024;
14715 else
14716 return 16*1024;
14717 } else {
14718 /* XXX DSPC is limited to 4k tiled */
14719 return 8*1024;
14720 }
14721}
14722
Daniel Vetterb5ea6422014-03-02 21:18:00 +010014723static int intel_framebuffer_init(struct drm_device *dev,
14724 struct intel_framebuffer *intel_fb,
14725 struct drm_mode_fb_cmd2 *mode_cmd,
14726 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080014727{
Ville Syrjälä7b49f942016-01-12 21:08:32 +020014728 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +000014729 unsigned int aligned_height;
Jesse Barnes79e53942008-11-07 14:24:08 -080014730 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000014731 u32 pitch_limit, stride_alignment;
Jesse Barnes79e53942008-11-07 14:24:08 -080014732
Daniel Vetterdd4916c2013-10-09 21:23:51 +020014733 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14734
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014735 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14736 /* Enforce that fb modifier and tiling mode match, but only for
14737 * X-tiled. This is needed for FBC. */
14738 if (!!(obj->tiling_mode == I915_TILING_X) !=
14739 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14740 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14741 return -EINVAL;
14742 }
14743 } else {
14744 if (obj->tiling_mode == I915_TILING_X)
14745 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14746 else if (obj->tiling_mode == I915_TILING_Y) {
14747 DRM_DEBUG("No Y tiling for legacy addfb\n");
14748 return -EINVAL;
14749 }
14750 }
14751
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014752 /* Passed in modifier sanity checking. */
14753 switch (mode_cmd->modifier[0]) {
14754 case I915_FORMAT_MOD_Y_TILED:
14755 case I915_FORMAT_MOD_Yf_TILED:
14756 if (INTEL_INFO(dev)->gen < 9) {
14757 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14758 mode_cmd->modifier[0]);
14759 return -EINVAL;
14760 }
14761 case DRM_FORMAT_MOD_NONE:
14762 case I915_FORMAT_MOD_X_TILED:
14763 break;
14764 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070014765 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14766 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010014767 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014768 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014769
Ville Syrjälä7b49f942016-01-12 21:08:32 +020014770 stride_alignment = intel_fb_stride_alignment(dev_priv,
14771 mode_cmd->modifier[0],
Damien Lespiaub3218032015-02-27 11:15:18 +000014772 mode_cmd->pixel_format);
14773 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14774 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14775 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010014776 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014777 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014778
Damien Lespiaub3218032015-02-27 11:15:18 +000014779 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14780 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014781 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014782 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14783 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014784 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014785 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014786 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014787 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014788
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014789 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014790 mode_cmd->pitches[0] != obj->stride) {
14791 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14792 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014793 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014794 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014795
Ville Syrjälä57779d02012-10-31 17:50:14 +020014796 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014797 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020014798 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014799 case DRM_FORMAT_RGB565:
14800 case DRM_FORMAT_XRGB8888:
14801 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014802 break;
14803 case DRM_FORMAT_XRGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014804 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014805 DRM_DEBUG("unsupported pixel format: %s\n",
14806 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014807 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014808 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020014809 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +020014810 case DRM_FORMAT_ABGR8888:
Wayne Boyer666a4532015-12-09 12:29:35 -080014811 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
14812 INTEL_INFO(dev)->gen < 9) {
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014813 DRM_DEBUG("unsupported pixel format: %s\n",
14814 drm_get_format_name(mode_cmd->pixel_format));
14815 return -EINVAL;
14816 }
14817 break;
14818 case DRM_FORMAT_XBGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014819 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014820 case DRM_FORMAT_XBGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014821 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014822 DRM_DEBUG("unsupported pixel format: %s\n",
14823 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014824 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014825 }
Jesse Barnesb5626742011-06-24 12:19:27 -070014826 break;
Damien Lespiau75312082015-05-15 19:06:01 +010014827 case DRM_FORMAT_ABGR2101010:
Wayne Boyer666a4532015-12-09 12:29:35 -080014828 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Damien Lespiau75312082015-05-15 19:06:01 +010014829 DRM_DEBUG("unsupported pixel format: %s\n",
14830 drm_get_format_name(mode_cmd->pixel_format));
14831 return -EINVAL;
14832 }
14833 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020014834 case DRM_FORMAT_YUYV:
14835 case DRM_FORMAT_UYVY:
14836 case DRM_FORMAT_YVYU:
14837 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014838 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014839 DRM_DEBUG("unsupported pixel format: %s\n",
14840 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014841 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014842 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014843 break;
14844 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014845 DRM_DEBUG("unsupported pixel format: %s\n",
14846 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010014847 return -EINVAL;
14848 }
14849
Ville Syrjälä90f9a332012-10-31 17:50:19 +020014850 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14851 if (mode_cmd->offsets[0] != 0)
14852 return -EINVAL;
14853
Damien Lespiauec2c9812015-01-20 12:51:45 +000014854 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +000014855 mode_cmd->pixel_format,
14856 mode_cmd->modifier[0]);
Daniel Vetter53155c02013-10-09 21:55:33 +020014857 /* FIXME drm helper for size checks (especially planar formats)? */
14858 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14859 return -EINVAL;
14860
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014861 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14862 intel_fb->obj = obj;
14863
Ville Syrjälä2d7a2152016-02-15 22:54:47 +020014864 intel_fill_fb_info(dev_priv, &intel_fb->base);
14865
Jesse Barnes79e53942008-11-07 14:24:08 -080014866 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14867 if (ret) {
14868 DRM_ERROR("framebuffer init failed %d\n", ret);
14869 return ret;
14870 }
14871
Ville Syrjälä0b05e1e2016-01-14 15:22:09 +020014872 intel_fb->obj->framebuffer_references++;
14873
Jesse Barnes79e53942008-11-07 14:24:08 -080014874 return 0;
14875}
14876
Jesse Barnes79e53942008-11-07 14:24:08 -080014877static struct drm_framebuffer *
14878intel_user_framebuffer_create(struct drm_device *dev,
14879 struct drm_file *filp,
Ville Syrjälä1eb834512015-11-11 19:11:29 +020014880 const struct drm_mode_fb_cmd2 *user_mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080014881{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020014882 struct drm_framebuffer *fb;
Chris Wilson05394f32010-11-08 19:18:58 +000014883 struct drm_i915_gem_object *obj;
Ville Syrjälä76dc3762015-11-11 19:11:28 +020014884 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
Jesse Barnes79e53942008-11-07 14:24:08 -080014885
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014886 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
Ville Syrjälä76dc3762015-11-11 19:11:28 +020014887 mode_cmd.handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000014888 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010014889 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080014890
Daniel Vetter92907cb2015-11-23 09:04:05 +010014891 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020014892 if (IS_ERR(fb))
14893 drm_gem_object_unreference_unlocked(&obj->base);
14894
14895 return fb;
Jesse Barnes79e53942008-11-07 14:24:08 -080014896}
14897
Daniel Vetter06957262015-08-10 13:34:08 +020014898#ifndef CONFIG_DRM_FBDEV_EMULATION
Daniel Vetter0632fef2013-10-08 17:44:49 +020014899static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020014900{
14901}
14902#endif
14903
Jesse Barnes79e53942008-11-07 14:24:08 -080014904static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080014905 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020014906 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080014907 .atomic_check = intel_atomic_check,
14908 .atomic_commit = intel_atomic_commit,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +020014909 .atomic_state_alloc = intel_atomic_state_alloc,
14910 .atomic_state_clear = intel_atomic_state_clear,
Jesse Barnes79e53942008-11-07 14:24:08 -080014911};
14912
Imre Deak88212942016-03-16 13:38:53 +020014913/**
14914 * intel_init_display_hooks - initialize the display modesetting hooks
14915 * @dev_priv: device private
14916 */
14917void intel_init_display_hooks(struct drm_i915_private *dev_priv)
Jesse Barnese70236a2009-09-21 10:42:27 -070014918{
Imre Deak88212942016-03-16 13:38:53 +020014919 if (INTEL_INFO(dev_priv)->gen >= 9) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014920 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014921 dev_priv->display.get_initial_plane_config =
14922 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014923 dev_priv->display.crtc_compute_clock =
14924 haswell_crtc_compute_clock;
14925 dev_priv->display.crtc_enable = haswell_crtc_enable;
14926 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020014927 } else if (HAS_DDI(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014928 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014929 dev_priv->display.get_initial_plane_config =
14930 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020014931 dev_priv->display.crtc_compute_clock =
14932 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020014933 dev_priv->display.crtc_enable = haswell_crtc_enable;
14934 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020014935 } else if (HAS_PCH_SPLIT(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014936 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014937 dev_priv->display.get_initial_plane_config =
14938 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020014939 dev_priv->display.crtc_compute_clock =
14940 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014941 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14942 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +020014943 } else if (IS_CHERRYVIEW(dev_priv)) {
Jesse Barnes89b667f2013-04-18 14:51:36 -070014944 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014945 dev_priv->display.get_initial_plane_config =
14946 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +020014947 dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
14948 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14949 dev_priv->display.crtc_disable = i9xx_crtc_disable;
14950 } else if (IS_VALLEYVIEW(dev_priv)) {
14951 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14952 dev_priv->display.get_initial_plane_config =
14953 i9xx_get_initial_plane_config;
14954 dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014955 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14956 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +020014957 } else if (IS_G4X(dev_priv)) {
14958 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14959 dev_priv->display.get_initial_plane_config =
14960 i9xx_get_initial_plane_config;
14961 dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
14962 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14963 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +020014964 } else if (IS_PINEVIEW(dev_priv)) {
14965 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14966 dev_priv->display.get_initial_plane_config =
14967 i9xx_get_initial_plane_config;
14968 dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
14969 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14970 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +020014971 } else if (!IS_GEN2(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014972 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014973 dev_priv->display.get_initial_plane_config =
14974 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014975 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014976 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14977 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +020014978 } else {
14979 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14980 dev_priv->display.get_initial_plane_config =
14981 i9xx_get_initial_plane_config;
14982 dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
14983 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14984 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Eric Anholtf564048e2011-03-30 13:01:02 -070014985 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014986
Jesse Barnese70236a2009-09-21 10:42:27 -070014987 /* Returns the core display clock speed */
Imre Deak88212942016-03-16 13:38:53 +020014988 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030014989 dev_priv->display.get_display_clock_speed =
14990 skylake_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014991 else if (IS_BROXTON(dev_priv))
Bob Paauweacd3f3d2015-06-23 14:14:26 -070014992 dev_priv->display.get_display_clock_speed =
14993 broxton_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014994 else if (IS_BROADWELL(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030014995 dev_priv->display.get_display_clock_speed =
14996 broadwell_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014997 else if (IS_HASWELL(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030014998 dev_priv->display.get_display_clock_speed =
14999 haswell_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015000 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070015001 dev_priv->display.get_display_clock_speed =
15002 valleyview_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015003 else if (IS_GEN5(dev_priv))
Ville Syrjäläb37a6432015-03-31 14:11:54 +030015004 dev_priv->display.get_display_clock_speed =
15005 ilk_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015006 else if (IS_I945G(dev_priv) || IS_BROADWATER(dev_priv) ||
15007 IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015008 dev_priv->display.get_display_clock_speed =
15009 i945_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015010 else if (IS_GM45(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030015011 dev_priv->display.get_display_clock_speed =
15012 gm45_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015013 else if (IS_CRESTLINE(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030015014 dev_priv->display.get_display_clock_speed =
15015 i965gm_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015016 else if (IS_PINEVIEW(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030015017 dev_priv->display.get_display_clock_speed =
15018 pnv_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015019 else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030015020 dev_priv->display.get_display_clock_speed =
15021 g33_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015022 else if (IS_I915G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015023 dev_priv->display.get_display_clock_speed =
15024 i915_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015025 else if (IS_I945GM(dev_priv) || IS_845G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015026 dev_priv->display.get_display_clock_speed =
15027 i9xx_misc_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015028 else if (IS_I915GM(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015029 dev_priv->display.get_display_clock_speed =
15030 i915gm_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015031 else if (IS_I865G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015032 dev_priv->display.get_display_clock_speed =
15033 i865_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015034 else if (IS_I85X(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015035 dev_priv->display.get_display_clock_speed =
Ville Syrjälä1b1d2712015-05-22 11:22:31 +030015036 i85x_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030015037 else { /* 830 */
Imre Deak88212942016-03-16 13:38:53 +020015038 WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
Jesse Barnese70236a2009-09-21 10:42:27 -070015039 dev_priv->display.get_display_clock_speed =
15040 i830_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030015041 }
Jesse Barnese70236a2009-09-21 10:42:27 -070015042
Imre Deak88212942016-03-16 13:38:53 +020015043 if (IS_GEN5(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015044 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020015045 } else if (IS_GEN6(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015046 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020015047 } else if (IS_IVYBRIDGE(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015048 /* FIXME: detect B0+ stepping and use auto training */
15049 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020015050 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015051 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020015052 if (IS_BROADWELL(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020015053 dev_priv->display.modeset_commit_cdclk =
15054 broadwell_modeset_commit_cdclk;
15055 dev_priv->display.modeset_calc_cdclk =
15056 broadwell_modeset_calc_cdclk;
15057 }
Imre Deak88212942016-03-16 13:38:53 +020015058 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020015059 dev_priv->display.modeset_commit_cdclk =
15060 valleyview_modeset_commit_cdclk;
15061 dev_priv->display.modeset_calc_cdclk =
15062 valleyview_modeset_calc_cdclk;
Imre Deak88212942016-03-16 13:38:53 +020015063 } else if (IS_BROXTON(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020015064 dev_priv->display.modeset_commit_cdclk =
15065 broxton_modeset_commit_cdclk;
15066 dev_priv->display.modeset_calc_cdclk =
15067 broxton_modeset_calc_cdclk;
Jesse Barnese70236a2009-09-21 10:42:27 -070015068 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070015069
Imre Deak88212942016-03-16 13:38:53 +020015070 switch (INTEL_INFO(dev_priv)->gen) {
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070015071 case 2:
15072 dev_priv->display.queue_flip = intel_gen2_queue_flip;
15073 break;
15074
15075 case 3:
15076 dev_priv->display.queue_flip = intel_gen3_queue_flip;
15077 break;
15078
15079 case 4:
15080 case 5:
15081 dev_priv->display.queue_flip = intel_gen4_queue_flip;
15082 break;
15083
15084 case 6:
15085 dev_priv->display.queue_flip = intel_gen6_queue_flip;
15086 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070015087 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070015088 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070015089 dev_priv->display.queue_flip = intel_gen7_queue_flip;
15090 break;
Damien Lespiau830c81d2014-11-13 17:51:46 +000015091 case 9:
Tvrtko Ursulinba343e02015-02-10 17:16:12 +000015092 /* Drop through - unsupported since execlist only. */
15093 default:
15094 /* Default just returns -ENODEV to indicate unsupported */
15095 dev_priv->display.queue_flip = intel_default_queue_flip;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070015096 }
Jesse Barnese70236a2009-09-21 10:42:27 -070015097}
15098
Jesse Barnesb690e962010-07-19 13:53:12 -070015099/*
15100 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
15101 * resume, or other times. This quirk makes sure that's the case for
15102 * affected systems.
15103 */
Akshay Joshi0206e352011-08-16 15:34:10 -040015104static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070015105{
15106 struct drm_i915_private *dev_priv = dev->dev_private;
15107
15108 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015109 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070015110}
15111
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030015112static void quirk_pipeb_force(struct drm_device *dev)
15113{
15114 struct drm_i915_private *dev_priv = dev->dev_private;
15115
15116 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
15117 DRM_INFO("applying pipe b force quirk\n");
15118}
15119
Keith Packard435793d2011-07-12 14:56:22 -070015120/*
15121 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
15122 */
15123static void quirk_ssc_force_disable(struct drm_device *dev)
15124{
15125 struct drm_i915_private *dev_priv = dev->dev_private;
15126 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015127 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070015128}
15129
Carsten Emde4dca20e2012-03-15 15:56:26 +010015130/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010015131 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
15132 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010015133 */
15134static void quirk_invert_brightness(struct drm_device *dev)
15135{
15136 struct drm_i915_private *dev_priv = dev->dev_private;
15137 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015138 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070015139}
15140
Scot Doyle9c72cc62014-07-03 23:27:50 +000015141/* Some VBT's incorrectly indicate no backlight is present */
15142static void quirk_backlight_present(struct drm_device *dev)
15143{
15144 struct drm_i915_private *dev_priv = dev->dev_private;
15145 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
15146 DRM_INFO("applying backlight present quirk\n");
15147}
15148
Jesse Barnesb690e962010-07-19 13:53:12 -070015149struct intel_quirk {
15150 int device;
15151 int subsystem_vendor;
15152 int subsystem_device;
15153 void (*hook)(struct drm_device *dev);
15154};
15155
Egbert Eich5f85f172012-10-14 15:46:38 +020015156/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
15157struct intel_dmi_quirk {
15158 void (*hook)(struct drm_device *dev);
15159 const struct dmi_system_id (*dmi_id_list)[];
15160};
15161
15162static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
15163{
15164 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
15165 return 1;
15166}
15167
15168static const struct intel_dmi_quirk intel_dmi_quirks[] = {
15169 {
15170 .dmi_id_list = &(const struct dmi_system_id[]) {
15171 {
15172 .callback = intel_dmi_reverse_brightness,
15173 .ident = "NCR Corporation",
15174 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
15175 DMI_MATCH(DMI_PRODUCT_NAME, ""),
15176 },
15177 },
15178 { } /* terminating entry */
15179 },
15180 .hook = quirk_invert_brightness,
15181 },
15182};
15183
Ben Widawskyc43b5632012-04-16 14:07:40 -070015184static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070015185 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
15186 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
15187
Jesse Barnesb690e962010-07-19 13:53:12 -070015188 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
15189 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
15190
Ville Syrjälä5f080c02014-08-15 01:22:06 +030015191 /* 830 needs to leave pipe A & dpll A up */
15192 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
15193
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030015194 /* 830 needs to leave pipe B & dpll B up */
15195 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
15196
Keith Packard435793d2011-07-12 14:56:22 -070015197 /* Lenovo U160 cannot use SSC on LVDS */
15198 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020015199
15200 /* Sony Vaio Y cannot use SSC on LVDS */
15201 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010015202
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010015203 /* Acer Aspire 5734Z must invert backlight brightness */
15204 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
15205
15206 /* Acer/eMachines G725 */
15207 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
15208
15209 /* Acer/eMachines e725 */
15210 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
15211
15212 /* Acer/Packard Bell NCL20 */
15213 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
15214
15215 /* Acer Aspire 4736Z */
15216 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020015217
15218 /* Acer Aspire 5336 */
15219 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000015220
15221 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
15222 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000015223
Scot Doyledfb3d47b2014-08-21 16:08:02 +000015224 /* Acer C720 Chromebook (Core i3 4005U) */
15225 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
15226
jens steinb2a96012014-10-28 20:25:53 +010015227 /* Apple Macbook 2,1 (Core 2 T7400) */
15228 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
15229
Jani Nikula1b9448b02015-11-05 11:49:59 +020015230 /* Apple Macbook 4,1 */
15231 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
15232
Scot Doyled4967d82014-07-03 23:27:52 +000015233 /* Toshiba CB35 Chromebook (Celeron 2955U) */
15234 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000015235
15236 /* HP Chromebook 14 (Celeron 2955U) */
15237 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020015238
15239 /* Dell Chromebook 11 */
15240 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jani Nikula9be64ee2015-10-30 14:50:24 +020015241
15242 /* Dell Chromebook 11 (2015 version) */
15243 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070015244};
15245
15246static void intel_init_quirks(struct drm_device *dev)
15247{
15248 struct pci_dev *d = dev->pdev;
15249 int i;
15250
15251 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
15252 struct intel_quirk *q = &intel_quirks[i];
15253
15254 if (d->device == q->device &&
15255 (d->subsystem_vendor == q->subsystem_vendor ||
15256 q->subsystem_vendor == PCI_ANY_ID) &&
15257 (d->subsystem_device == q->subsystem_device ||
15258 q->subsystem_device == PCI_ANY_ID))
15259 q->hook(dev);
15260 }
Egbert Eich5f85f172012-10-14 15:46:38 +020015261 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
15262 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
15263 intel_dmi_quirks[i].hook(dev);
15264 }
Jesse Barnesb690e962010-07-19 13:53:12 -070015265}
15266
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015267/* Disable the VGA plane that we never use */
15268static void i915_disable_vga(struct drm_device *dev)
15269{
15270 struct drm_i915_private *dev_priv = dev->dev_private;
15271 u8 sr1;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020015272 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015273
Ville Syrjälä2b37c612014-01-22 21:32:38 +020015274 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015275 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070015276 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015277 sr1 = inb(VGA_SR_DATA);
15278 outb(sr1 | 1<<5, VGA_SR_DATA);
15279 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
15280 udelay(300);
15281
Ville Syrjälä01f5a622014-12-16 18:38:37 +020015282 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015283 POSTING_READ(vga_reg);
15284}
15285
Daniel Vetterf8175862012-04-10 15:50:11 +020015286void intel_modeset_init_hw(struct drm_device *dev)
15287{
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010015288 struct drm_i915_private *dev_priv = dev->dev_private;
15289
Ville Syrjäläb6283052015-06-03 15:45:07 +030015290 intel_update_cdclk(dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010015291
15292 dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
15293
Daniel Vetterf8175862012-04-10 15:50:11 +020015294 intel_init_clock_gating(dev);
Daniel Vetter8090c6b2012-06-24 16:42:32 +020015295 intel_enable_gt_powersave(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020015296}
15297
Matt Roperd93c0372015-12-03 11:37:41 -080015298/*
15299 * Calculate what we think the watermarks should be for the state we've read
15300 * out of the hardware and then immediately program those watermarks so that
15301 * we ensure the hardware settings match our internal state.
15302 *
15303 * We can calculate what we think WM's should be by creating a duplicate of the
15304 * current state (which was constructed during hardware readout) and running it
15305 * through the atomic check code to calculate new watermark values in the
15306 * state object.
15307 */
15308static void sanitize_watermarks(struct drm_device *dev)
15309{
15310 struct drm_i915_private *dev_priv = to_i915(dev);
15311 struct drm_atomic_state *state;
15312 struct drm_crtc *crtc;
15313 struct drm_crtc_state *cstate;
15314 struct drm_modeset_acquire_ctx ctx;
15315 int ret;
15316 int i;
15317
15318 /* Only supported on platforms that use atomic watermark design */
Matt Ropered4a6a72016-02-23 17:20:13 -080015319 if (!dev_priv->display.optimize_watermarks)
Matt Roperd93c0372015-12-03 11:37:41 -080015320 return;
15321
15322 /*
15323 * We need to hold connection_mutex before calling duplicate_state so
15324 * that the connector loop is protected.
15325 */
15326 drm_modeset_acquire_init(&ctx, 0);
15327retry:
Matt Roper0cd12622016-01-12 07:13:37 -080015328 ret = drm_modeset_lock_all_ctx(dev, &ctx);
Matt Roperd93c0372015-12-03 11:37:41 -080015329 if (ret == -EDEADLK) {
15330 drm_modeset_backoff(&ctx);
15331 goto retry;
15332 } else if (WARN_ON(ret)) {
Matt Roper0cd12622016-01-12 07:13:37 -080015333 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015334 }
15335
15336 state = drm_atomic_helper_duplicate_state(dev, &ctx);
15337 if (WARN_ON(IS_ERR(state)))
Matt Roper0cd12622016-01-12 07:13:37 -080015338 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015339
Matt Ropered4a6a72016-02-23 17:20:13 -080015340 /*
15341 * Hardware readout is the only time we don't want to calculate
15342 * intermediate watermarks (since we don't trust the current
15343 * watermarks).
15344 */
15345 to_intel_atomic_state(state)->skip_intermediate_wm = true;
15346
Matt Roperd93c0372015-12-03 11:37:41 -080015347 ret = intel_atomic_check(dev, state);
15348 if (ret) {
15349 /*
15350 * If we fail here, it means that the hardware appears to be
15351 * programmed in a way that shouldn't be possible, given our
15352 * understanding of watermark requirements. This might mean a
15353 * mistake in the hardware readout code or a mistake in the
15354 * watermark calculations for a given platform. Raise a WARN
15355 * so that this is noticeable.
15356 *
15357 * If this actually happens, we'll have to just leave the
15358 * BIOS-programmed watermarks untouched and hope for the best.
15359 */
15360 WARN(true, "Could not determine valid watermarks for inherited state\n");
Matt Roper0cd12622016-01-12 07:13:37 -080015361 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015362 }
15363
15364 /* Write calculated watermark values back */
15365 to_i915(dev)->wm.config = to_intel_atomic_state(state)->wm_config;
15366 for_each_crtc_in_state(state, crtc, cstate, i) {
15367 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
15368
Matt Ropered4a6a72016-02-23 17:20:13 -080015369 cs->wm.need_postvbl_update = true;
15370 dev_priv->display.optimize_watermarks(cs);
Matt Roperd93c0372015-12-03 11:37:41 -080015371 }
15372
15373 drm_atomic_state_free(state);
Matt Roper0cd12622016-01-12 07:13:37 -080015374fail:
Matt Roperd93c0372015-12-03 11:37:41 -080015375 drm_modeset_drop_locks(&ctx);
15376 drm_modeset_acquire_fini(&ctx);
15377}
15378
Jesse Barnes79e53942008-11-07 14:24:08 -080015379void intel_modeset_init(struct drm_device *dev)
15380{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +030015381 struct drm_i915_private *dev_priv = to_i915(dev);
15382 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Damien Lespiau1fe47782014-03-03 17:31:47 +000015383 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015384 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080015385 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080015386
15387 drm_mode_config_init(dev);
15388
15389 dev->mode_config.min_width = 0;
15390 dev->mode_config.min_height = 0;
15391
Dave Airlie019d96c2011-09-29 16:20:42 +010015392 dev->mode_config.preferred_depth = 24;
15393 dev->mode_config.prefer_shadow = 1;
15394
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000015395 dev->mode_config.allow_fb_modifiers = true;
15396
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020015397 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080015398
Jesse Barnesb690e962010-07-19 13:53:12 -070015399 intel_init_quirks(dev);
15400
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030015401 intel_init_pm(dev);
15402
Ben Widawskye3c74752013-04-05 13:12:39 -070015403 if (INTEL_INFO(dev)->num_pipes == 0)
15404 return;
15405
Lukas Wunner69f92f62015-07-15 13:57:35 +020015406 /*
15407 * There may be no VBT; and if the BIOS enabled SSC we can
15408 * just keep using it to avoid unnecessary flicker. Whereas if the
15409 * BIOS isn't using it, don't assume it will work even if the VBT
15410 * indicates as much.
15411 */
15412 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
15413 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15414 DREF_SSC1_ENABLE);
15415
15416 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
15417 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
15418 bios_lvds_use_ssc ? "en" : "dis",
15419 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
15420 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
15421 }
15422 }
15423
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015424 if (IS_GEN2(dev)) {
15425 dev->mode_config.max_width = 2048;
15426 dev->mode_config.max_height = 2048;
15427 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070015428 dev->mode_config.max_width = 4096;
15429 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080015430 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015431 dev->mode_config.max_width = 8192;
15432 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080015433 }
Damien Lespiau068be562014-03-28 14:17:49 +000015434
Ville Syrjälädc41c152014-08-13 11:57:05 +030015435 if (IS_845G(dev) || IS_I865G(dev)) {
15436 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
15437 dev->mode_config.cursor_height = 1023;
15438 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000015439 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
15440 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
15441 } else {
15442 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
15443 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
15444 }
15445
Joonas Lahtinen72e96d62016-03-30 16:57:10 +030015446 dev->mode_config.fb_base = ggtt->mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080015447
Zhao Yakui28c97732009-10-09 11:39:41 +080015448 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015449 INTEL_INFO(dev)->num_pipes,
15450 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080015451
Damien Lespiau055e3932014-08-18 13:49:10 +010015452 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015453 intel_crtc_init(dev, pipe);
Damien Lespiau3bdcfc02015-02-28 14:54:09 +000015454 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +000015455 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015456 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030015457 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000015458 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015459 }
Jesse Barnes79e53942008-11-07 14:24:08 -080015460 }
15461
Ville Syrjäläbfa7df02015-09-24 23:29:18 +030015462 intel_update_czclk(dev_priv);
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +020015463 intel_update_rawclk(dev_priv);
Ville Syrjäläbfa7df02015-09-24 23:29:18 +030015464 intel_update_cdclk(dev);
15465
Daniel Vettere72f9fb2013-06-05 13:34:06 +020015466 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010015467
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015468 /* Just disable it once at startup */
15469 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015470 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000015471
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015472 drm_modeset_lock_all(dev);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015473 intel_modeset_setup_hw_state(dev);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015474 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015475
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015476 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015477 struct intel_initial_plane_config plane_config = {};
15478
Jesse Barnes46f297f2014-03-07 08:57:48 -080015479 if (!crtc->active)
15480 continue;
15481
Jesse Barnes46f297f2014-03-07 08:57:48 -080015482 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080015483 * Note that reserving the BIOS fb up front prevents us
15484 * from stuffing other stolen allocations like the ring
15485 * on top. This prevents some ugliness at boot time, and
15486 * can even allow for smooth boot transitions if the BIOS
15487 * fb is large enough for the active pipe configuration.
15488 */
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015489 dev_priv->display.get_initial_plane_config(crtc,
15490 &plane_config);
15491
15492 /*
15493 * If the fb is shared between multiple heads, we'll
15494 * just get the first one.
15495 */
15496 intel_find_initial_plane_obj(crtc, &plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015497 }
Matt Roperd93c0372015-12-03 11:37:41 -080015498
15499 /*
15500 * Make sure hardware watermarks really match the state we read out.
15501 * Note that we need to do this after reconstructing the BIOS fb's
15502 * since the watermark calculation done here will use pstate->fb.
15503 */
15504 sanitize_watermarks(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010015505}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080015506
Daniel Vetter7fad7982012-07-04 17:51:47 +020015507static void intel_enable_pipe_a(struct drm_device *dev)
15508{
15509 struct intel_connector *connector;
15510 struct drm_connector *crt = NULL;
15511 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015512 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020015513
15514 /* We can't just switch on the pipe A, we need to set things up with a
15515 * proper mode and output configuration. As a gross hack, enable pipe A
15516 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015517 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020015518 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15519 crt = &connector->base;
15520 break;
15521 }
15522 }
15523
15524 if (!crt)
15525 return;
15526
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015527 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020015528 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020015529}
15530
Daniel Vetterfa555832012-10-10 23:14:00 +020015531static bool
15532intel_check_plane_mapping(struct intel_crtc *crtc)
15533{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015534 struct drm_device *dev = crtc->base.dev;
15535 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä649636e2015-09-22 19:50:01 +030015536 u32 val;
Daniel Vetterfa555832012-10-10 23:14:00 +020015537
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015538 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020015539 return true;
15540
Ville Syrjälä649636e2015-09-22 19:50:01 +030015541 val = I915_READ(DSPCNTR(!crtc->plane));
Daniel Vetterfa555832012-10-10 23:14:00 +020015542
15543 if ((val & DISPLAY_PLANE_ENABLE) &&
15544 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15545 return false;
15546
15547 return true;
15548}
15549
Ville Syrjälä02e93c32015-08-26 19:39:19 +030015550static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
15551{
15552 struct drm_device *dev = crtc->base.dev;
15553 struct intel_encoder *encoder;
15554
15555 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15556 return true;
15557
15558 return false;
15559}
15560
Ville Syrjälädd756192016-02-17 21:28:45 +020015561static bool intel_encoder_has_connectors(struct intel_encoder *encoder)
15562{
15563 struct drm_device *dev = encoder->base.dev;
15564 struct intel_connector *connector;
15565
15566 for_each_connector_on_encoder(dev, &encoder->base, connector)
15567 return true;
15568
15569 return false;
15570}
15571
Daniel Vetter24929352012-07-02 20:28:59 +020015572static void intel_sanitize_crtc(struct intel_crtc *crtc)
15573{
15574 struct drm_device *dev = crtc->base.dev;
15575 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula4d1de972016-03-18 17:05:42 +020015576 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter24929352012-07-02 20:28:59 +020015577
Daniel Vetter24929352012-07-02 20:28:59 +020015578 /* Clear any frame start delays used for debugging left by the BIOS */
Jani Nikula4d1de972016-03-18 17:05:42 +020015579 if (!transcoder_is_dsi(cpu_transcoder)) {
15580 i915_reg_t reg = PIPECONF(cpu_transcoder);
15581
15582 I915_WRITE(reg,
15583 I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15584 }
Daniel Vetter24929352012-07-02 20:28:59 +020015585
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015586 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010015587 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030015588 if (crtc->active) {
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015589 struct intel_plane *plane;
15590
Daniel Vetter96256042015-02-13 21:03:42 +010015591 drm_crtc_vblank_on(&crtc->base);
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015592
15593 /* Disable everything but the primary plane */
15594 for_each_intel_plane_on_crtc(dev, crtc, plane) {
15595 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
15596 continue;
15597
15598 plane->disable_plane(&plane->base, &crtc->base);
15599 }
Daniel Vetter96256042015-02-13 21:03:42 +010015600 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015601
Daniel Vetter24929352012-07-02 20:28:59 +020015602 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020015603 * disable the crtc (and hence change the state) if it is wrong. Note
15604 * that gen4+ has a fixed plane -> pipe mapping. */
15605 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020015606 bool plane;
15607
Daniel Vetter24929352012-07-02 20:28:59 +020015608 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
15609 crtc->base.base.id);
15610
15611 /* Pipe has the wrong plane attached and the plane is active.
15612 * Temporarily change the plane mapping and disable everything
15613 * ... */
15614 plane = crtc->plane;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015615 to_intel_plane_state(crtc->base.primary->state)->visible = true;
Daniel Vetter24929352012-07-02 20:28:59 +020015616 crtc->plane = !plane;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015617 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015618 crtc->plane = plane;
Daniel Vetter24929352012-07-02 20:28:59 +020015619 }
Daniel Vetter24929352012-07-02 20:28:59 +020015620
Daniel Vetter7fad7982012-07-04 17:51:47 +020015621 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15622 crtc->pipe == PIPE_A && !crtc->active) {
15623 /* BIOS forgot to enable pipe A, this mostly happens after
15624 * resume. Force-enable the pipe to fix this, the update_dpms
15625 * call below we restore the pipe to the right state, but leave
15626 * the required bits on. */
15627 intel_enable_pipe_a(dev);
15628 }
15629
Daniel Vetter24929352012-07-02 20:28:59 +020015630 /* Adjust the state of the output pipe according to whether we
15631 * have active connectors/encoders. */
Maarten Lankhorst842e0302016-03-02 15:48:01 +010015632 if (crtc->active && !intel_crtc_has_encoders(crtc))
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015633 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015634
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030015635 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010015636 /*
15637 * We start out with underrun reporting disabled to avoid races.
15638 * For correct bookkeeping mark this on active crtcs.
15639 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020015640 * Also on gmch platforms we dont have any hardware bits to
15641 * disable the underrun reporting. Which means we need to start
15642 * out with underrun reporting disabled also on inactive pipes,
15643 * since otherwise we'll complain about the garbage we read when
15644 * e.g. coming up after runtime pm.
15645 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010015646 * No protection against concurrent access is required - at
15647 * worst a fifo underrun happens which also sets this to false.
15648 */
15649 crtc->cpu_fifo_underrun_disabled = true;
15650 crtc->pch_fifo_underrun_disabled = true;
15651 }
Daniel Vetter24929352012-07-02 20:28:59 +020015652}
15653
15654static void intel_sanitize_encoder(struct intel_encoder *encoder)
15655{
15656 struct intel_connector *connector;
15657 struct drm_device *dev = encoder->base.dev;
15658
15659 /* We need to check both for a crtc link (meaning that the
15660 * encoder is active and trying to read from a pipe) and the
15661 * pipe itself being active. */
15662 bool has_active_crtc = encoder->base.crtc &&
15663 to_intel_crtc(encoder->base.crtc)->active;
15664
Ville Syrjälädd756192016-02-17 21:28:45 +020015665 if (intel_encoder_has_connectors(encoder) && !has_active_crtc) {
Daniel Vetter24929352012-07-02 20:28:59 +020015666 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15667 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015668 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015669
15670 /* Connector is active, but has no active pipe. This is
15671 * fallout from our resume register restoring. Disable
15672 * the encoder manually again. */
15673 if (encoder->base.crtc) {
15674 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15675 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015676 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015677 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030015678 if (encoder->post_disable)
15679 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020015680 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020015681 encoder->base.crtc = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015682
15683 /* Inconsistent output/port/pipe state happens presumably due to
15684 * a bug in one of the get_hw_state functions. Or someplace else
15685 * in our code, like the register restore mess on resume. Clamp
15686 * things to off as a safer default. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015687 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015688 if (connector->encoder != encoder)
15689 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020015690 connector->base.dpms = DRM_MODE_DPMS_OFF;
15691 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015692 }
15693 }
15694 /* Enabled encoders without active connectors will be fixed in
15695 * the crtc fixup. */
15696}
15697
Imre Deak04098752014-02-18 00:02:16 +020015698void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015699{
15700 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020015701 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015702
Imre Deak04098752014-02-18 00:02:16 +020015703 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15704 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15705 i915_disable_vga(dev);
15706 }
15707}
15708
15709void i915_redisable_vga(struct drm_device *dev)
15710{
15711 struct drm_i915_private *dev_priv = dev->dev_private;
15712
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015713 /* This function can be called both from intel_modeset_setup_hw_state or
15714 * at a very early point in our resume sequence, where the power well
15715 * structures are not yet restored. Since this function is at a very
15716 * paranoid "someone might have enabled VGA while we were not looking"
15717 * level, just check if the power well is enabled instead of trying to
15718 * follow the "don't touch the power well if we don't need it" policy
15719 * the rest of the driver uses. */
Imre Deak6392f842016-02-12 18:55:13 +020015720 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015721 return;
15722
Imre Deak04098752014-02-18 00:02:16 +020015723 i915_redisable_vga_power_on(dev);
Imre Deak6392f842016-02-12 18:55:13 +020015724
15725 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015726}
15727
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015728static bool primary_get_hw_state(struct intel_plane *plane)
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015729{
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015730 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015731
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015732 return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015733}
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015734
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015735/* FIXME read out full plane state for all planes */
15736static void readout_plane_state(struct intel_crtc *crtc)
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015737{
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015738 struct drm_plane *primary = crtc->base.primary;
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015739 struct intel_plane_state *plane_state =
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015740 to_intel_plane_state(primary->state);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015741
Matt Roper19b8d382015-09-24 15:53:17 -070015742 plane_state->visible = crtc->active &&
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015743 primary_get_hw_state(to_intel_plane(primary));
15744
15745 if (plane_state->visible)
15746 crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015747}
15748
Daniel Vetter30e984d2013-06-05 13:34:17 +020015749static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020015750{
15751 struct drm_i915_private *dev_priv = dev->dev_private;
15752 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020015753 struct intel_crtc *crtc;
15754 struct intel_encoder *encoder;
15755 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020015756 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020015757
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015758 dev_priv->active_crtcs = 0;
15759
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015760 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015761 struct intel_crtc_state *crtc_state = crtc->config;
15762 int pixclk = 0;
Daniel Vetter3b117c82013-04-17 20:15:07 +020015763
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015764 __drm_atomic_helper_crtc_destroy_state(&crtc->base, &crtc_state->base);
15765 memset(crtc_state, 0, sizeof(*crtc_state));
15766 crtc_state->base.crtc = &crtc->base;
Daniel Vetter24929352012-07-02 20:28:59 +020015767
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015768 crtc_state->base.active = crtc_state->base.enable =
15769 dev_priv->display.get_pipe_config(crtc, crtc_state);
15770
15771 crtc->base.enabled = crtc_state->base.enable;
15772 crtc->active = crtc_state->base.active;
15773
15774 if (crtc_state->base.active) {
15775 dev_priv->active_crtcs |= 1 << crtc->pipe;
15776
15777 if (IS_BROADWELL(dev_priv)) {
15778 pixclk = ilk_pipe_pixel_rate(crtc_state);
15779
15780 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
15781 if (crtc_state->ips_enabled)
15782 pixclk = DIV_ROUND_UP(pixclk * 100, 95);
15783 } else if (IS_VALLEYVIEW(dev_priv) ||
15784 IS_CHERRYVIEW(dev_priv) ||
15785 IS_BROXTON(dev_priv))
15786 pixclk = crtc_state->base.adjusted_mode.crtc_clock;
15787 else
15788 WARN_ON(dev_priv->display.modeset_calc_cdclk);
15789 }
15790
15791 dev_priv->min_pixclk[crtc->pipe] = pixclk;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015792
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015793 readout_plane_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020015794
15795 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
15796 crtc->base.base.id,
15797 crtc->active ? "enabled" : "disabled");
15798 }
15799
Daniel Vetter53589012013-06-05 13:34:16 +020015800 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15801 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15802
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020015803 pll->on = pll->funcs.get_hw_state(dev_priv, pll,
15804 &pll->config.hw_state);
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015805 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015806 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010015807 if (crtc->active && crtc->config->shared_dpll == pll)
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015808 pll->config.crtc_mask |= 1 << crtc->pipe;
Daniel Vetter53589012013-06-05 13:34:16 +020015809 }
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010015810 pll->active_mask = pll->config.crtc_mask;
Daniel Vetter53589012013-06-05 13:34:16 +020015811
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015812 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015813 pll->name, pll->config.crtc_mask, pll->on);
Daniel Vetter53589012013-06-05 13:34:16 +020015814 }
15815
Damien Lespiaub2784e12014-08-05 11:29:37 +010015816 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015817 pipe = 0;
15818
15819 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070015820 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15821 encoder->base.crtc = &crtc->base;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015822 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020015823 } else {
15824 encoder->base.crtc = NULL;
15825 }
15826
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015827 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020015828 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015829 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015830 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015831 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020015832 }
15833
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015834 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015835 if (connector->get_hw_state(connector)) {
15836 connector->base.dpms = DRM_MODE_DPMS_ON;
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010015837
15838 encoder = connector->encoder;
15839 connector->base.encoder = &encoder->base;
15840
15841 if (encoder->base.crtc &&
15842 encoder->base.crtc->state->active) {
15843 /*
15844 * This has to be done during hardware readout
15845 * because anything calling .crtc_disable may
15846 * rely on the connector_mask being accurate.
15847 */
15848 encoder->base.crtc->state->connector_mask |=
15849 1 << drm_connector_index(&connector->base);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +010015850 encoder->base.crtc->state->encoder_mask |=
15851 1 << drm_encoder_index(&encoder->base);
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010015852 }
15853
Daniel Vetter24929352012-07-02 20:28:59 +020015854 } else {
15855 connector->base.dpms = DRM_MODE_DPMS_OFF;
15856 connector->base.encoder = NULL;
15857 }
15858 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
15859 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030015860 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015861 connector->base.encoder ? "enabled" : "disabled");
15862 }
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015863
15864 for_each_intel_crtc(dev, crtc) {
15865 crtc->base.hwmode = crtc->config->base.adjusted_mode;
15866
15867 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
15868 if (crtc->base.state->active) {
15869 intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
15870 intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
15871 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
15872
15873 /*
15874 * The initial mode needs to be set in order to keep
15875 * the atomic core happy. It wants a valid mode if the
15876 * crtc's enabled, so we do the above call.
15877 *
15878 * At this point some state updated by the connectors
15879 * in their ->detect() callback has not run yet, so
15880 * no recalculation can be done yet.
15881 *
15882 * Even if we could do a recalculation and modeset
15883 * right now it would cause a double modeset if
15884 * fbdev or userspace chooses a different initial mode.
15885 *
15886 * If that happens, someone indicated they wanted a
15887 * mode change, which means it's safe to do a full
15888 * recalculation.
15889 */
15890 crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
Ville Syrjälä9eca68322015-09-10 18:59:10 +030015891
15892 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
15893 update_scanline_offset(crtc);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015894 }
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020015895
15896 intel_pipe_config_sanity_check(dev_priv, crtc->config);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015897 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020015898}
15899
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015900/* Scan out the current hw modeset state,
15901 * and sanitizes it to the current state
15902 */
15903static void
15904intel_modeset_setup_hw_state(struct drm_device *dev)
Daniel Vetter30e984d2013-06-05 13:34:17 +020015905{
15906 struct drm_i915_private *dev_priv = dev->dev_private;
15907 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015908 struct intel_crtc *crtc;
15909 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020015910 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015911
15912 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020015913
15914 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010015915 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015916 intel_sanitize_encoder(encoder);
15917 }
15918
Damien Lespiau055e3932014-08-18 13:49:10 +010015919 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020015920 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15921 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015922 intel_dump_pipe_config(crtc, crtc->config,
15923 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020015924 }
Daniel Vetter9a935852012-07-05 22:34:27 +020015925
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020015926 intel_modeset_update_connector_atomic_state(dev);
15927
Daniel Vetter35c95372013-07-17 06:55:04 +020015928 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15929 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15930
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010015931 if (!pll->on || pll->active_mask)
Daniel Vetter35c95372013-07-17 06:55:04 +020015932 continue;
15933
15934 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15935
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020015936 pll->funcs.disable(dev_priv, pll);
Daniel Vetter35c95372013-07-17 06:55:04 +020015937 pll->on = false;
15938 }
15939
Wayne Boyer666a4532015-12-09 12:29:35 -080015940 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Ville Syrjälä6eb1a682015-06-24 22:00:03 +030015941 vlv_wm_get_hw_state(dev);
15942 else if (IS_GEN9(dev))
Pradeep Bhat30789992014-11-04 17:06:45 +000015943 skl_wm_get_hw_state(dev);
15944 else if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030015945 ilk_wm_get_hw_state(dev);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020015946
15947 for_each_intel_crtc(dev, crtc) {
15948 unsigned long put_domains;
15949
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +010015950 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020015951 if (WARN_ON(put_domains))
15952 modeset_put_power_domains(dev_priv, put_domains);
15953 }
15954 intel_display_set_init_power(dev_priv, false);
Paulo Zanoni010cf732016-01-19 11:35:48 -020015955
15956 intel_fbc_init_pipe_state(dev_priv);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015957}
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030015958
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015959void intel_display_resume(struct drm_device *dev)
15960{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015961 struct drm_i915_private *dev_priv = to_i915(dev);
15962 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
15963 struct drm_modeset_acquire_ctx ctx;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015964 int ret;
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015965 bool setup = false;
Daniel Vetterf30da182013-04-11 20:22:50 +020015966
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015967 dev_priv->modeset_restore_state = NULL;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015968
Maarten Lankhorstea49c9a2016-02-16 15:27:42 +010015969 /*
15970 * This is a cludge because with real atomic modeset mode_config.mutex
15971 * won't be taken. Unfortunately some probed state like
15972 * audio_codec_enable is still protected by mode_config.mutex, so lock
15973 * it here for now.
15974 */
15975 mutex_lock(&dev->mode_config.mutex);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015976 drm_modeset_acquire_init(&ctx, 0);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015977
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015978retry:
15979 ret = drm_modeset_lock_all_ctx(dev, &ctx);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015980
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015981 if (ret == 0 && !setup) {
15982 setup = true;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015983
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015984 intel_modeset_setup_hw_state(dev);
15985 i915_redisable_vga(dev);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010015986 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020015987
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015988 if (ret == 0 && state) {
15989 struct drm_crtc_state *crtc_state;
15990 struct drm_crtc *crtc;
15991 int i;
15992
15993 state->acquire_ctx = &ctx;
15994
15995 for_each_crtc_in_state(state, crtc, crtc_state, i) {
15996 /*
15997 * Force recalculation even if we restore
15998 * current state. With fast modeset this may not result
15999 * in a modeset when the state is compatible.
16000 */
16001 crtc_state->mode_changed = true;
16002 }
16003
16004 ret = drm_atomic_commit(state);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016005 }
16006
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016007 if (ret == -EDEADLK) {
16008 drm_modeset_backoff(&ctx);
16009 goto retry;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016010 }
16011
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016012 drm_modeset_drop_locks(&ctx);
16013 drm_modeset_acquire_fini(&ctx);
Maarten Lankhorstea49c9a2016-02-16 15:27:42 +010016014 mutex_unlock(&dev->mode_config.mutex);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016015
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016016 if (ret) {
16017 DRM_ERROR("Restoring old state failed with %i\n", ret);
16018 drm_atomic_state_free(state);
16019 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010016020}
16021
16022void intel_modeset_gem_init(struct drm_device *dev)
16023{
Jesse Barnes484b41d2014-03-07 08:57:55 -080016024 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070016025 struct drm_i915_gem_object *obj;
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010016026 int ret;
Jesse Barnes484b41d2014-03-07 08:57:55 -080016027
Imre Deakae484342014-03-31 15:10:44 +030016028 intel_init_gt_powersave(dev);
Imre Deakae484342014-03-31 15:10:44 +030016029
Chris Wilson1833b132012-05-09 11:56:28 +010016030 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020016031
16032 intel_setup_overlay(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080016033
16034 /*
16035 * Make sure any fbs we allocated at startup are properly
16036 * pinned & fenced. When we do the allocation it's too early
16037 * for this.
16038 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010016039 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070016040 obj = intel_fb_obj(c->primary->fb);
16041 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080016042 continue;
16043
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010016044 mutex_lock(&dev->struct_mutex);
Ville Syrjälä3465c582016-02-15 22:54:43 +020016045 ret = intel_pin_and_fence_fb_obj(c->primary->fb,
16046 c->primary->state->rotation);
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010016047 mutex_unlock(&dev->struct_mutex);
16048 if (ret) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080016049 DRM_ERROR("failed to pin boot fb on pipe %d\n",
16050 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100016051 drm_framebuffer_unreference(c->primary->fb);
16052 c->primary->fb = NULL;
Maarten Lankhorst36750f22015-06-01 12:49:54 +020016053 c->primary->crtc = c->primary->state->crtc = NULL;
Matt Roperafd65eb2015-02-03 13:10:04 -080016054 update_state_fb(c->primary);
Maarten Lankhorst36750f22015-06-01 12:49:54 +020016055 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
Jesse Barnes484b41d2014-03-07 08:57:55 -080016056 }
16057 }
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020016058
16059 intel_backlight_register(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080016060}
16061
Imre Deak4932e2c2014-02-11 17:12:48 +020016062void intel_connector_unregister(struct intel_connector *intel_connector)
16063{
16064 struct drm_connector *connector = &intel_connector->base;
16065
16066 intel_panel_destroy_backlight(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +010016067 drm_connector_unregister(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020016068}
16069
Jesse Barnes79e53942008-11-07 14:24:08 -080016070void intel_modeset_cleanup(struct drm_device *dev)
16071{
Jesse Barnes652c3932009-08-17 13:31:43 -070016072 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula19c80542015-12-16 12:48:16 +020016073 struct intel_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070016074
Imre Deak2eb52522014-11-19 15:30:05 +020016075 intel_disable_gt_powersave(dev);
16076
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020016077 intel_backlight_unregister(dev);
16078
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016079 /*
16080 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020016081 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016082 * experience fancy races otherwise.
16083 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020016084 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070016085
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016086 /*
16087 * Due to the hpd irq storm handling the hotplug work can re-arm the
16088 * poll handlers. Hence disable polling after hpd handling is shut down.
16089 */
Keith Packardf87ea762010-10-03 19:36:26 -070016090 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016091
Jesse Barnes723bfd72010-10-07 16:01:13 -070016092 intel_unregister_dsm_handler();
16093
Paulo Zanonic937ab3e52016-01-19 11:35:46 -020016094 intel_fbc_global_disable(dev_priv);
Kristian Høgsberg69341a52009-11-11 12:19:17 -050016095
Chris Wilson1630fe72011-07-08 12:22:42 +010016096 /* flush any delayed tasks or pending work */
16097 flush_scheduled_work();
16098
Jani Nikuladb31af1d2013-11-08 16:48:53 +020016099 /* destroy the backlight and sysfs files before encoders/connectors */
Jani Nikula19c80542015-12-16 12:48:16 +020016100 for_each_intel_connector(dev, connector)
16101 connector->unregister(connector);
Paulo Zanonid9255d52013-09-26 20:05:59 -030016102
Jesse Barnes79e53942008-11-07 14:24:08 -080016103 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010016104
16105 intel_cleanup_overlay(dev);
Imre Deakae484342014-03-31 15:10:44 +030016106
Imre Deakae484342014-03-31 15:10:44 +030016107 intel_cleanup_gt_powersave(dev);
Daniel Vetterf5949142016-01-13 11:55:28 +010016108
16109 intel_teardown_gmbus(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080016110}
16111
Dave Airlie28d52042009-09-21 14:33:58 +100016112/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080016113 * Return which encoder is currently attached for connector.
16114 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010016115struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080016116{
Chris Wilsondf0e9242010-09-09 16:20:55 +010016117 return &intel_attached_encoder(connector)->base;
16118}
Jesse Barnes79e53942008-11-07 14:24:08 -080016119
Chris Wilsondf0e9242010-09-09 16:20:55 +010016120void intel_connector_attach_encoder(struct intel_connector *connector,
16121 struct intel_encoder *encoder)
16122{
16123 connector->encoder = encoder;
16124 drm_mode_connector_attach_encoder(&connector->base,
16125 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080016126}
Dave Airlie28d52042009-09-21 14:33:58 +100016127
16128/*
16129 * set vga decode state - true == enable VGA decode
16130 */
16131int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
16132{
16133 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000016134 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100016135 u16 gmch_ctrl;
16136
Chris Wilson75fa0412014-02-07 18:37:02 -020016137 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
16138 DRM_ERROR("failed to read control word\n");
16139 return -EIO;
16140 }
16141
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020016142 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
16143 return 0;
16144
Dave Airlie28d52042009-09-21 14:33:58 +100016145 if (state)
16146 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
16147 else
16148 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020016149
16150 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
16151 DRM_ERROR("failed to write control word\n");
16152 return -EIO;
16153 }
16154
Dave Airlie28d52042009-09-21 14:33:58 +100016155 return 0;
16156}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016157
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016158struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016159
16160 u32 power_well_driver;
16161
Chris Wilson63b66e52013-08-08 15:12:06 +020016162 int num_transcoders;
16163
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016164 struct intel_cursor_error_state {
16165 u32 control;
16166 u32 position;
16167 u32 base;
16168 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010016169 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016170
16171 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020016172 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016173 u32 source;
Imre Deakf301b1e12014-04-18 15:55:04 +030016174 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010016175 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016176
16177 struct intel_plane_error_state {
16178 u32 control;
16179 u32 stride;
16180 u32 size;
16181 u32 pos;
16182 u32 addr;
16183 u32 surface;
16184 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010016185 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020016186
16187 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020016188 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020016189 enum transcoder cpu_transcoder;
16190
16191 u32 conf;
16192
16193 u32 htotal;
16194 u32 hblank;
16195 u32 hsync;
16196 u32 vtotal;
16197 u32 vblank;
16198 u32 vsync;
16199 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016200};
16201
16202struct intel_display_error_state *
16203intel_display_capture_error_state(struct drm_device *dev)
16204{
Jani Nikulafbee40d2014-03-31 14:27:18 +030016205 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016206 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020016207 int transcoders[] = {
16208 TRANSCODER_A,
16209 TRANSCODER_B,
16210 TRANSCODER_C,
16211 TRANSCODER_EDP,
16212 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016213 int i;
16214
Chris Wilson63b66e52013-08-08 15:12:06 +020016215 if (INTEL_INFO(dev)->num_pipes == 0)
16216 return NULL;
16217
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016218 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016219 if (error == NULL)
16220 return NULL;
16221
Imre Deak190be112013-11-25 17:15:31 +020016222 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016223 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
16224
Damien Lespiau055e3932014-08-18 13:49:10 +010016225 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020016226 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020016227 __intel_display_power_is_enabled(dev_priv,
16228 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020016229 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016230 continue;
16231
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030016232 error->cursor[i].control = I915_READ(CURCNTR(i));
16233 error->cursor[i].position = I915_READ(CURPOS(i));
16234 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016235
16236 error->plane[i].control = I915_READ(DSPCNTR(i));
16237 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016238 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030016239 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016240 error->plane[i].pos = I915_READ(DSPPOS(i));
16241 }
Paulo Zanonica291362013-03-06 20:03:14 -030016242 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
16243 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016244 if (INTEL_INFO(dev)->gen >= 4) {
16245 error->plane[i].surface = I915_READ(DSPSURF(i));
16246 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
16247 }
16248
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016249 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e12014-04-18 15:55:04 +030016250
Sonika Jindal3abfce72014-07-21 15:23:43 +053016251 if (HAS_GMCH_DISPLAY(dev))
Imre Deakf301b1e12014-04-18 15:55:04 +030016252 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020016253 }
16254
Jani Nikula4d1de972016-03-18 17:05:42 +020016255 /* Note: this does not include DSI transcoders. */
Chris Wilson63b66e52013-08-08 15:12:06 +020016256 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +030016257 if (HAS_DDI(dev_priv))
Chris Wilson63b66e52013-08-08 15:12:06 +020016258 error->num_transcoders++; /* Account for eDP. */
16259
16260 for (i = 0; i < error->num_transcoders; i++) {
16261 enum transcoder cpu_transcoder = transcoders[i];
16262
Imre Deakddf9c532013-11-27 22:02:02 +020016263 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020016264 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020016265 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020016266 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016267 continue;
16268
Chris Wilson63b66e52013-08-08 15:12:06 +020016269 error->transcoder[i].cpu_transcoder = cpu_transcoder;
16270
16271 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
16272 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
16273 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
16274 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
16275 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
16276 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
16277 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016278 }
16279
16280 return error;
16281}
16282
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016283#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
16284
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016285void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016286intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016287 struct drm_device *dev,
16288 struct intel_display_error_state *error)
16289{
Damien Lespiau055e3932014-08-18 13:49:10 +010016290 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016291 int i;
16292
Chris Wilson63b66e52013-08-08 15:12:06 +020016293 if (!error)
16294 return;
16295
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016296 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020016297 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016298 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016299 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010016300 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016301 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020016302 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020016303 onoff(error->pipe[i].power_domain_on));
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016304 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e12014-04-18 15:55:04 +030016305 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016306
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016307 err_printf(m, "Plane [%d]:\n", i);
16308 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
16309 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016310 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016311 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
16312 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016313 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030016314 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016315 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016316 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016317 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
16318 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016319 }
16320
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016321 err_printf(m, "Cursor [%d]:\n", i);
16322 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
16323 err_printf(m, " POS: %08x\n", error->cursor[i].position);
16324 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016325 }
Chris Wilson63b66e52013-08-08 15:12:06 +020016326
16327 for (i = 0; i < error->num_transcoders; i++) {
Jani Nikulada205632016-03-15 21:51:10 +020016328 err_printf(m, "CPU transcoder: %s\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020016329 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020016330 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020016331 onoff(error->transcoder[i].power_domain_on));
Chris Wilson63b66e52013-08-08 15:12:06 +020016332 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
16333 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
16334 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
16335 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
16336 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
16337 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
16338 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
16339 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016340}