blob: 6382241cb7870ec68db9aa6f8eabca3487eeb762 [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);
Chandra Konduru549e2bf2015-04-07 15:28:38 -0700111static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
112 struct intel_crtc_state *crtc_state);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +0200113static void skylake_pfit_enable(struct intel_crtc *crtc);
114static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
115static void ironlake_pfit_enable(struct intel_crtc *crtc);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +0200116static void intel_modeset_setup_hw_state(struct drm_device *dev);
Ville Syrjälä2622a082016-03-09 19:07:26 +0200117static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
Ville Syrjälä4e5ca602016-05-11 22:44:44 +0300118static int ilk_max_pixel_rate(struct drm_atomic_state *state);
Maarten Lankhorst143f73b32016-05-17 15:07:54 +0200119static void intel_modeset_verify_crtc(struct drm_crtc *crtc,
120 struct drm_crtc_state *old_state,
121 struct drm_crtc_state *new_state);
Damien Lespiaue7457a92013-08-08 22:28:59 +0100122
Ma Lingd4906092009-03-18 20:13:27 +0800123struct intel_limit {
Ander Conselvan de Oliveira4c5def92016-05-04 12:11:58 +0300124 struct {
125 int min, max;
126 } dot, vco, n, m, m1, m2, p, p1;
127
128 struct {
129 int dot_limit;
130 int p2_slow, p2_fast;
131 } p2;
Ma Lingd4906092009-03-18 20:13:27 +0800132};
Jesse Barnes79e53942008-11-07 14:24:08 -0800133
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300134/* returns HPLL frequency in kHz */
135static int valleyview_get_vco(struct drm_i915_private *dev_priv)
136{
137 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
138
139 /* Obtain SKU information */
140 mutex_lock(&dev_priv->sb_lock);
141 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
142 CCK_FUSE_HPLL_FREQ_MASK;
143 mutex_unlock(&dev_priv->sb_lock);
144
145 return vco_freq[hpll_freq] * 1000;
146}
147
Ville Syrjäläc30fec62016-03-04 21:43:02 +0200148int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
149 const char *name, u32 reg, int ref_freq)
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300150{
151 u32 val;
152 int divider;
153
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300154 mutex_lock(&dev_priv->sb_lock);
155 val = vlv_cck_read(dev_priv, reg);
156 mutex_unlock(&dev_priv->sb_lock);
157
158 divider = val & CCK_FREQUENCY_VALUES;
159
160 WARN((val & CCK_FREQUENCY_STATUS) !=
161 (divider << CCK_FREQUENCY_STATUS_SHIFT),
162 "%s change in progress\n", name);
163
Ville Syrjäläc30fec62016-03-04 21:43:02 +0200164 return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
165}
166
167static int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
168 const char *name, u32 reg)
169{
170 if (dev_priv->hpll_freq == 0)
171 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
172
173 return vlv_get_cck_clock(dev_priv, name, reg,
174 dev_priv->hpll_freq);
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300175}
176
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200177static int
178intel_pch_rawclk(struct drm_i915_private *dev_priv)
Daniel Vetterd2acd212012-10-20 20:57:43 +0200179{
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200180 return (I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK) * 1000;
Daniel Vetterd2acd212012-10-20 20:57:43 +0200181}
182
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200183static int
184intel_vlv_hrawclk(struct drm_i915_private *dev_priv)
Jani Nikula79e50a42015-08-26 10:58:20 +0300185{
Ville Syrjälä19ab4ed2016-04-27 17:43:22 +0300186 /* RAWCLK_FREQ_VLV register updated from power well code */
Ville Syrjälä35d38d12016-03-02 17:22:16 +0200187 return vlv_get_cck_clock_hpll(dev_priv, "hrawclk",
188 CCK_DISPLAY_REF_CLOCK_CONTROL);
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200189}
190
191static int
192intel_g4x_hrawclk(struct drm_i915_private *dev_priv)
193{
Jani Nikula79e50a42015-08-26 10:58:20 +0300194 uint32_t clkcfg;
195
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200196 /* hrawclock is 1/4 the FSB frequency */
Jani Nikula79e50a42015-08-26 10:58:20 +0300197 clkcfg = I915_READ(CLKCFG);
198 switch (clkcfg & CLKCFG_FSB_MASK) {
199 case CLKCFG_FSB_400:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200200 return 100000;
Jani Nikula79e50a42015-08-26 10:58:20 +0300201 case CLKCFG_FSB_533:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200202 return 133333;
Jani Nikula79e50a42015-08-26 10:58:20 +0300203 case CLKCFG_FSB_667:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200204 return 166667;
Jani Nikula79e50a42015-08-26 10:58:20 +0300205 case CLKCFG_FSB_800:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200206 return 200000;
Jani Nikula79e50a42015-08-26 10:58:20 +0300207 case CLKCFG_FSB_1067:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200208 return 266667;
Jani Nikula79e50a42015-08-26 10:58:20 +0300209 case CLKCFG_FSB_1333:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200210 return 333333;
Jani Nikula79e50a42015-08-26 10:58:20 +0300211 /* these two are just a guess; one of them might be right */
212 case CLKCFG_FSB_1600:
213 case CLKCFG_FSB_1600_ALT:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200214 return 400000;
Jani Nikula79e50a42015-08-26 10:58:20 +0300215 default:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200216 return 133333;
Jani Nikula79e50a42015-08-26 10:58:20 +0300217 }
218}
219
Ville Syrjälä19ab4ed2016-04-27 17:43:22 +0300220void intel_update_rawclk(struct drm_i915_private *dev_priv)
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200221{
222 if (HAS_PCH_SPLIT(dev_priv))
223 dev_priv->rawclk_freq = intel_pch_rawclk(dev_priv);
224 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
225 dev_priv->rawclk_freq = intel_vlv_hrawclk(dev_priv);
226 else if (IS_G4X(dev_priv) || IS_PINEVIEW(dev_priv))
227 dev_priv->rawclk_freq = intel_g4x_hrawclk(dev_priv);
228 else
229 return; /* no rawclk on other platforms, or no need to know it */
230
231 DRM_DEBUG_DRIVER("rawclk rate: %d kHz\n", dev_priv->rawclk_freq);
232}
233
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300234static void intel_update_czclk(struct drm_i915_private *dev_priv)
235{
Wayne Boyer666a4532015-12-09 12:29:35 -0800236 if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300237 return;
238
239 dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
240 CCK_CZ_CLOCK_CONTROL);
241
242 DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
243}
244
Chris Wilson021357a2010-09-07 20:54:59 +0100245static inline u32 /* units of 100MHz */
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200246intel_fdi_link_freq(struct drm_i915_private *dev_priv,
247 const struct intel_crtc_state *pipe_config)
Chris Wilson021357a2010-09-07 20:54:59 +0100248{
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200249 if (HAS_DDI(dev_priv))
250 return pipe_config->port_clock; /* SPLL */
251 else if (IS_GEN5(dev_priv))
252 return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
Ville Syrjäläe3b247d2016-02-17 21:41:09 +0200253 else
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200254 return 270000;
Chris Wilson021357a2010-09-07 20:54:59 +0100255}
256
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300257static const struct intel_limit intel_limits_i8xx_dac = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400258 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200259 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200260 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400261 .m = { .min = 96, .max = 140 },
262 .m1 = { .min = 18, .max = 26 },
263 .m2 = { .min = 6, .max = 16 },
264 .p = { .min = 4, .max = 128 },
265 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700266 .p2 = { .dot_limit = 165000,
267 .p2_slow = 4, .p2_fast = 2 },
Keith Packarde4b36692009-06-05 19:22:17 -0700268};
269
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300270static const struct intel_limit intel_limits_i8xx_dvo = {
Daniel Vetter5d536e22013-07-06 12:52:06 +0200271 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200272 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200273 .n = { .min = 2, .max = 16 },
Daniel Vetter5d536e22013-07-06 12:52:06 +0200274 .m = { .min = 96, .max = 140 },
275 .m1 = { .min = 18, .max = 26 },
276 .m2 = { .min = 6, .max = 16 },
277 .p = { .min = 4, .max = 128 },
278 .p1 = { .min = 2, .max = 33 },
279 .p2 = { .dot_limit = 165000,
280 .p2_slow = 4, .p2_fast = 4 },
281};
282
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300283static const struct intel_limit intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400284 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200285 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200286 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400287 .m = { .min = 96, .max = 140 },
288 .m1 = { .min = 18, .max = 26 },
289 .m2 = { .min = 6, .max = 16 },
290 .p = { .min = 4, .max = 128 },
291 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700292 .p2 = { .dot_limit = 165000,
293 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700294};
Eric Anholt273e27c2011-03-30 13:01:10 -0700295
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300296static const struct intel_limit intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400297 .dot = { .min = 20000, .max = 400000 },
298 .vco = { .min = 1400000, .max = 2800000 },
299 .n = { .min = 1, .max = 6 },
300 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100301 .m1 = { .min = 8, .max = 18 },
302 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400303 .p = { .min = 5, .max = 80 },
304 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700305 .p2 = { .dot_limit = 200000,
306 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700307};
308
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300309static const struct intel_limit intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400310 .dot = { .min = 20000, .max = 400000 },
311 .vco = { .min = 1400000, .max = 2800000 },
312 .n = { .min = 1, .max = 6 },
313 .m = { .min = 70, .max = 120 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100314 .m1 = { .min = 8, .max = 18 },
315 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400316 .p = { .min = 7, .max = 98 },
317 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700318 .p2 = { .dot_limit = 112000,
319 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700320};
321
Eric Anholt273e27c2011-03-30 13:01:10 -0700322
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300323static const struct intel_limit intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700324 .dot = { .min = 25000, .max = 270000 },
325 .vco = { .min = 1750000, .max = 3500000},
326 .n = { .min = 1, .max = 4 },
327 .m = { .min = 104, .max = 138 },
328 .m1 = { .min = 17, .max = 23 },
329 .m2 = { .min = 5, .max = 11 },
330 .p = { .min = 10, .max = 30 },
331 .p1 = { .min = 1, .max = 3},
332 .p2 = { .dot_limit = 270000,
333 .p2_slow = 10,
334 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800335 },
Keith Packarde4b36692009-06-05 19:22:17 -0700336};
337
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300338static const struct intel_limit intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700339 .dot = { .min = 22000, .max = 400000 },
340 .vco = { .min = 1750000, .max = 3500000},
341 .n = { .min = 1, .max = 4 },
342 .m = { .min = 104, .max = 138 },
343 .m1 = { .min = 16, .max = 23 },
344 .m2 = { .min = 5, .max = 11 },
345 .p = { .min = 5, .max = 80 },
346 .p1 = { .min = 1, .max = 8},
347 .p2 = { .dot_limit = 165000,
348 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700349};
350
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300351static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700352 .dot = { .min = 20000, .max = 115000 },
353 .vco = { .min = 1750000, .max = 3500000 },
354 .n = { .min = 1, .max = 3 },
355 .m = { .min = 104, .max = 138 },
356 .m1 = { .min = 17, .max = 23 },
357 .m2 = { .min = 5, .max = 11 },
358 .p = { .min = 28, .max = 112 },
359 .p1 = { .min = 2, .max = 8 },
360 .p2 = { .dot_limit = 0,
361 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800362 },
Keith Packarde4b36692009-06-05 19:22:17 -0700363};
364
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300365static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700366 .dot = { .min = 80000, .max = 224000 },
367 .vco = { .min = 1750000, .max = 3500000 },
368 .n = { .min = 1, .max = 3 },
369 .m = { .min = 104, .max = 138 },
370 .m1 = { .min = 17, .max = 23 },
371 .m2 = { .min = 5, .max = 11 },
372 .p = { .min = 14, .max = 42 },
373 .p1 = { .min = 2, .max = 6 },
374 .p2 = { .dot_limit = 0,
375 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800376 },
Keith Packarde4b36692009-06-05 19:22:17 -0700377};
378
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300379static const struct intel_limit intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400380 .dot = { .min = 20000, .max = 400000},
381 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700382 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400383 .n = { .min = 3, .max = 6 },
384 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700385 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400386 .m1 = { .min = 0, .max = 0 },
387 .m2 = { .min = 0, .max = 254 },
388 .p = { .min = 5, .max = 80 },
389 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700390 .p2 = { .dot_limit = 200000,
391 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700392};
393
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300394static const struct intel_limit intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400395 .dot = { .min = 20000, .max = 400000 },
396 .vco = { .min = 1700000, .max = 3500000 },
397 .n = { .min = 3, .max = 6 },
398 .m = { .min = 2, .max = 256 },
399 .m1 = { .min = 0, .max = 0 },
400 .m2 = { .min = 0, .max = 254 },
401 .p = { .min = 7, .max = 112 },
402 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700403 .p2 = { .dot_limit = 112000,
404 .p2_slow = 14, .p2_fast = 14 },
Keith Packarde4b36692009-06-05 19:22:17 -0700405};
406
Eric Anholt273e27c2011-03-30 13:01:10 -0700407/* Ironlake / Sandybridge
408 *
409 * We calculate clock using (register_value + 2) for N/M1/M2, so here
410 * the range value for them is (actual_value - 2).
411 */
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300412static const struct intel_limit intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700413 .dot = { .min = 25000, .max = 350000 },
414 .vco = { .min = 1760000, .max = 3510000 },
415 .n = { .min = 1, .max = 5 },
416 .m = { .min = 79, .max = 127 },
417 .m1 = { .min = 12, .max = 22 },
418 .m2 = { .min = 5, .max = 9 },
419 .p = { .min = 5, .max = 80 },
420 .p1 = { .min = 1, .max = 8 },
421 .p2 = { .dot_limit = 225000,
422 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700423};
424
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300425static const struct intel_limit intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700426 .dot = { .min = 25000, .max = 350000 },
427 .vco = { .min = 1760000, .max = 3510000 },
428 .n = { .min = 1, .max = 3 },
429 .m = { .min = 79, .max = 118 },
430 .m1 = { .min = 12, .max = 22 },
431 .m2 = { .min = 5, .max = 9 },
432 .p = { .min = 28, .max = 112 },
433 .p1 = { .min = 2, .max = 8 },
434 .p2 = { .dot_limit = 225000,
435 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800436};
437
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300438static const struct intel_limit intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700439 .dot = { .min = 25000, .max = 350000 },
440 .vco = { .min = 1760000, .max = 3510000 },
441 .n = { .min = 1, .max = 3 },
442 .m = { .min = 79, .max = 127 },
443 .m1 = { .min = 12, .max = 22 },
444 .m2 = { .min = 5, .max = 9 },
445 .p = { .min = 14, .max = 56 },
446 .p1 = { .min = 2, .max = 8 },
447 .p2 = { .dot_limit = 225000,
448 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800449};
450
Eric Anholt273e27c2011-03-30 13:01:10 -0700451/* LVDS 100mhz refclk limits. */
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300452static const struct intel_limit intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700453 .dot = { .min = 25000, .max = 350000 },
454 .vco = { .min = 1760000, .max = 3510000 },
455 .n = { .min = 1, .max = 2 },
456 .m = { .min = 79, .max = 126 },
457 .m1 = { .min = 12, .max = 22 },
458 .m2 = { .min = 5, .max = 9 },
459 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400460 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700461 .p2 = { .dot_limit = 225000,
462 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800463};
464
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300465static const struct intel_limit intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700466 .dot = { .min = 25000, .max = 350000 },
467 .vco = { .min = 1760000, .max = 3510000 },
468 .n = { .min = 1, .max = 3 },
469 .m = { .min = 79, .max = 126 },
470 .m1 = { .min = 12, .max = 22 },
471 .m2 = { .min = 5, .max = 9 },
472 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400473 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700474 .p2 = { .dot_limit = 225000,
475 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800476};
477
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300478static const struct intel_limit intel_limits_vlv = {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300479 /*
480 * These are the data rate limits (measured in fast clocks)
481 * since those are the strictest limits we have. The fast
482 * clock and actual rate limits are more relaxed, so checking
483 * them would make no difference.
484 */
485 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200486 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700487 .n = { .min = 1, .max = 7 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700488 .m1 = { .min = 2, .max = 3 },
489 .m2 = { .min = 11, .max = 156 },
Ville Syrjäläb99ab662013-09-24 21:26:26 +0300490 .p1 = { .min = 2, .max = 3 },
Ville Syrjälä5fdc9c492013-09-24 21:26:29 +0300491 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700492};
493
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300494static const struct intel_limit intel_limits_chv = {
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300495 /*
496 * These are the data rate limits (measured in fast clocks)
497 * since those are the strictest limits we have. The fast
498 * clock and actual rate limits are more relaxed, so checking
499 * them would make no difference.
500 */
501 .dot = { .min = 25000 * 5, .max = 540000 * 5},
Ville Syrjälä17fe1022015-02-26 21:01:52 +0200502 .vco = { .min = 4800000, .max = 6480000 },
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300503 .n = { .min = 1, .max = 1 },
504 .m1 = { .min = 2, .max = 2 },
505 .m2 = { .min = 24 << 22, .max = 175 << 22 },
506 .p1 = { .min = 2, .max = 4 },
507 .p2 = { .p2_slow = 1, .p2_fast = 14 },
508};
509
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300510static const struct intel_limit intel_limits_bxt = {
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200511 /* FIXME: find real dot limits */
512 .dot = { .min = 0, .max = INT_MAX },
Vandana Kannane6292552015-07-01 17:02:57 +0530513 .vco = { .min = 4800000, .max = 6700000 },
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200514 .n = { .min = 1, .max = 1 },
515 .m1 = { .min = 2, .max = 2 },
516 /* FIXME: find real m2 limits */
517 .m2 = { .min = 2 << 22, .max = 255 << 22 },
518 .p1 = { .min = 2, .max = 4 },
519 .p2 = { .p2_slow = 1, .p2_fast = 20 },
520};
521
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200522static bool
523needs_modeset(struct drm_crtc_state *state)
524{
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200525 return drm_atomic_crtc_needs_modeset(state);
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200526}
527
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300528/**
529 * Returns whether any output on the specified pipe is of the specified type
530 */
Damien Lespiau40935612014-10-29 11:16:59 +0000531bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300532{
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300533 struct drm_device *dev = crtc->base.dev;
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300534 struct intel_encoder *encoder;
535
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300536 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300537 if (encoder->type == type)
538 return true;
539
540 return false;
541}
542
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200543/**
544 * Returns whether any output on the specified pipe will have the specified
545 * type after a staged modeset is complete, i.e., the same as
546 * intel_pipe_has_type() but looking at encoder->new_crtc instead of
547 * encoder->crtc.
548 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200549static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
550 int type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200551{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200552 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +0300553 struct drm_connector *connector;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200554 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200555 struct intel_encoder *encoder;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200556 int i, num_connectors = 0;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200557
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +0300558 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200559 if (connector_state->crtc != crtc_state->base.crtc)
560 continue;
561
562 num_connectors++;
563
564 encoder = to_intel_encoder(connector_state->best_encoder);
565 if (encoder->type == type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200566 return true;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200567 }
568
569 WARN_ON(num_connectors == 0);
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200570
571 return false;
572}
573
Imre Deakdccbea32015-06-22 23:35:51 +0300574/*
575 * Platform specific helpers to calculate the port PLL loopback- (clock.m),
576 * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
577 * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
578 * The helpers' return value is the rate of the clock that is fed to the
579 * display engine's pipe which can be the above fast dot clock rate or a
580 * divided-down version of it.
581 */
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500582/* m1 is reserved as 0 in Pineview, n is a ring counter */
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300583static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800584{
Shaohua Li21778322009-02-23 15:19:16 +0800585 clock->m = clock->m2 + 2;
586 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200587 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300588 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300589 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
590 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300591
592 return clock->dot;
Shaohua Li21778322009-02-23 15:19:16 +0800593}
594
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200595static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
596{
597 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
598}
599
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300600static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800601{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200602 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800603 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200604 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300605 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300606 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
607 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300608
609 return clock->dot;
Jesse Barnes79e53942008-11-07 14:24:08 -0800610}
611
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300612static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
Imre Deak589eca62015-06-22 23:35:50 +0300613{
614 clock->m = clock->m1 * clock->m2;
615 clock->p = clock->p1 * clock->p2;
616 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300617 return 0;
Imre Deak589eca62015-06-22 23:35:50 +0300618 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
619 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300620
621 return clock->dot / 5;
Imre Deak589eca62015-06-22 23:35:50 +0300622}
623
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300624int chv_calc_dpll_params(int refclk, struct dpll *clock)
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300625{
626 clock->m = clock->m1 * clock->m2;
627 clock->p = clock->p1 * clock->p2;
628 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300629 return 0;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300630 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
631 clock->n << 22);
632 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300633
634 return clock->dot / 5;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300635}
636
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800637#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800638/**
639 * Returns whether the given set of divisors are valid for a given refclk with
640 * the given connectors.
641 */
642
Chris Wilson1b894b52010-12-14 20:04:54 +0000643static bool intel_PLL_is_valid(struct drm_device *dev,
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300644 const struct intel_limit *limit,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300645 const struct dpll *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800646{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300647 if (clock->n < limit->n.min || limit->n.max < clock->n)
648 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800649 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400650 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800651 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400652 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800653 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400654 INTELPllInvalid("m1 out of range\n");
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300655
Wayne Boyer666a4532015-12-09 12:29:35 -0800656 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) &&
657 !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev))
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300658 if (clock->m1 <= clock->m2)
659 INTELPllInvalid("m1 <= m2\n");
660
Wayne Boyer666a4532015-12-09 12:29:35 -0800661 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev)) {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300662 if (clock->p < limit->p.min || limit->p.max < clock->p)
663 INTELPllInvalid("p out of range\n");
664 if (clock->m < limit->m.min || limit->m.max < clock->m)
665 INTELPllInvalid("m out of range\n");
666 }
667
Jesse Barnes79e53942008-11-07 14:24:08 -0800668 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400669 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800670 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
671 * connector, etc., rather than just a single range.
672 */
673 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400674 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800675
676 return true;
677}
678
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300679static int
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300680i9xx_select_p2_div(const struct intel_limit *limit,
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300681 const struct intel_crtc_state *crtc_state,
682 int target)
Jesse Barnes79e53942008-11-07 14:24:08 -0800683{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300684 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800685
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200686 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800687 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100688 * For LVDS just rely on its current settings for dual-channel.
689 * We haven't figured out how to reliably set up different
690 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800691 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100692 if (intel_is_dual_link_lvds(dev))
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300693 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800694 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300695 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800696 } else {
697 if (target < limit->p2.dot_limit)
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300698 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800699 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300700 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800701 }
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300702}
703
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200704/*
705 * Returns a set of divisors for the desired target clock with the given
706 * refclk, or FALSE. The returned values represent the clock equation:
707 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
708 *
709 * Target and reference clocks are specified in kHz.
710 *
711 * If match_clock is provided, then best_clock P divider must match the P
712 * divider from @match_clock used for LVDS downclocking.
713 */
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300714static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300715i9xx_find_best_dpll(const struct intel_limit *limit,
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300716 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300717 int target, int refclk, struct dpll *match_clock,
718 struct dpll *best_clock)
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300719{
720 struct drm_device *dev = crtc_state->base.crtc->dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300721 struct dpll clock;
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300722 int err = target;
Jesse Barnes79e53942008-11-07 14:24:08 -0800723
Akshay Joshi0206e352011-08-16 15:34:10 -0400724 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800725
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300726 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
727
Zhao Yakui42158662009-11-20 11:24:18 +0800728 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
729 clock.m1++) {
730 for (clock.m2 = limit->m2.min;
731 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200732 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800733 break;
734 for (clock.n = limit->n.min;
735 clock.n <= limit->n.max; clock.n++) {
736 for (clock.p1 = limit->p1.min;
737 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800738 int this_err;
739
Imre Deakdccbea32015-06-22 23:35:51 +0300740 i9xx_calc_dpll_params(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000741 if (!intel_PLL_is_valid(dev, limit,
742 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800743 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800744 if (match_clock &&
745 clock.p != match_clock->p)
746 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800747
748 this_err = abs(clock.dot - target);
749 if (this_err < err) {
750 *best_clock = clock;
751 err = this_err;
752 }
753 }
754 }
755 }
756 }
757
758 return (err != target);
759}
760
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200761/*
762 * Returns a set of divisors for the desired target clock with the given
763 * refclk, or FALSE. The returned values represent the clock equation:
764 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
765 *
766 * Target and reference clocks are specified in kHz.
767 *
768 * If match_clock is provided, then best_clock P divider must match the P
769 * divider from @match_clock used for LVDS downclocking.
770 */
Ma Lingd4906092009-03-18 20:13:27 +0800771static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300772pnv_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200773 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300774 int target, int refclk, struct dpll *match_clock,
775 struct dpll *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200776{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300777 struct drm_device *dev = crtc_state->base.crtc->dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300778 struct dpll clock;
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200779 int err = target;
780
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200781 memset(best_clock, 0, sizeof(*best_clock));
782
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300783 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
784
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200785 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
786 clock.m1++) {
787 for (clock.m2 = limit->m2.min;
788 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200789 for (clock.n = limit->n.min;
790 clock.n <= limit->n.max; clock.n++) {
791 for (clock.p1 = limit->p1.min;
792 clock.p1 <= limit->p1.max; clock.p1++) {
793 int this_err;
794
Imre Deakdccbea32015-06-22 23:35:51 +0300795 pnv_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800796 if (!intel_PLL_is_valid(dev, limit,
797 &clock))
798 continue;
799 if (match_clock &&
800 clock.p != match_clock->p)
801 continue;
802
803 this_err = abs(clock.dot - target);
804 if (this_err < err) {
805 *best_clock = clock;
806 err = this_err;
807 }
808 }
809 }
810 }
811 }
812
813 return (err != target);
814}
815
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +0200816/*
817 * Returns a set of divisors for the desired target clock with the given
818 * refclk, or FALSE. The returned values represent the clock equation:
819 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200820 *
821 * Target and reference clocks are specified in kHz.
822 *
823 * If match_clock is provided, then best_clock P divider must match the P
824 * divider from @match_clock used for LVDS downclocking.
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +0200825 */
Ma Lingd4906092009-03-18 20:13:27 +0800826static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300827g4x_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200828 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300829 int target, int refclk, struct dpll *match_clock,
830 struct dpll *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800831{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300832 struct drm_device *dev = crtc_state->base.crtc->dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300833 struct dpll clock;
Ma Lingd4906092009-03-18 20:13:27 +0800834 int max_n;
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300835 bool found = false;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400836 /* approximately equals target * 0.00585 */
837 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800838
839 memset(best_clock, 0, sizeof(*best_clock));
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300840
841 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
842
Ma Lingd4906092009-03-18 20:13:27 +0800843 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200844 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800845 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200846 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800847 for (clock.m1 = limit->m1.max;
848 clock.m1 >= limit->m1.min; clock.m1--) {
849 for (clock.m2 = limit->m2.max;
850 clock.m2 >= limit->m2.min; clock.m2--) {
851 for (clock.p1 = limit->p1.max;
852 clock.p1 >= limit->p1.min; clock.p1--) {
853 int this_err;
854
Imre Deakdccbea32015-06-22 23:35:51 +0300855 i9xx_calc_dpll_params(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000856 if (!intel_PLL_is_valid(dev, limit,
857 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800858 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000859
860 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800861 if (this_err < err_most) {
862 *best_clock = clock;
863 err_most = this_err;
864 max_n = clock.n;
865 found = true;
866 }
867 }
868 }
869 }
870 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800871 return found;
872}
Ma Lingd4906092009-03-18 20:13:27 +0800873
Imre Deakd5dd62b2015-03-17 11:40:03 +0200874/*
875 * Check if the calculated PLL configuration is more optimal compared to the
876 * best configuration and error found so far. Return the calculated error.
877 */
878static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300879 const struct dpll *calculated_clock,
880 const struct dpll *best_clock,
Imre Deakd5dd62b2015-03-17 11:40:03 +0200881 unsigned int best_error_ppm,
882 unsigned int *error_ppm)
883{
Imre Deak9ca3ba02015-03-17 11:40:05 +0200884 /*
885 * For CHV ignore the error and consider only the P value.
886 * Prefer a bigger P value based on HW requirements.
887 */
888 if (IS_CHERRYVIEW(dev)) {
889 *error_ppm = 0;
890
891 return calculated_clock->p > best_clock->p;
892 }
893
Imre Deak24be4e42015-03-17 11:40:04 +0200894 if (WARN_ON_ONCE(!target_freq))
895 return false;
896
Imre Deakd5dd62b2015-03-17 11:40:03 +0200897 *error_ppm = div_u64(1000000ULL *
898 abs(target_freq - calculated_clock->dot),
899 target_freq);
900 /*
901 * Prefer a better P value over a better (smaller) error if the error
902 * is small. Ensure this preference for future configurations too by
903 * setting the error to 0.
904 */
905 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
906 *error_ppm = 0;
907
908 return true;
909 }
910
911 return *error_ppm + 10 < best_error_ppm;
912}
913
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200914/*
915 * Returns a set of divisors for the desired target clock with the given
916 * refclk, or FALSE. The returned values represent the clock equation:
917 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
918 */
Zhenyu Wang2c072452009-06-05 15:38:42 +0800919static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300920vlv_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200921 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300922 int target, int refclk, struct dpll *match_clock,
923 struct dpll *best_clock)
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700924{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200925 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300926 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300927 struct dpll clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300928 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300929 /* min update 19.2 MHz */
930 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300931 bool found = false;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700932
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300933 target *= 5; /* fast clock */
934
935 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700936
937 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300938 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300939 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300940 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300941 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300942 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700943 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300944 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Imre Deakd5dd62b2015-03-17 11:40:03 +0200945 unsigned int ppm;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300946
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300947 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
948 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300949
Imre Deakdccbea32015-06-22 23:35:51 +0300950 vlv_calc_dpll_params(refclk, &clock);
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300951
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300952 if (!intel_PLL_is_valid(dev, limit,
953 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300954 continue;
955
Imre Deakd5dd62b2015-03-17 11:40:03 +0200956 if (!vlv_PLL_is_optimal(dev, target,
957 &clock,
958 best_clock,
959 bestppm, &ppm))
960 continue;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300961
Imre Deakd5dd62b2015-03-17 11:40:03 +0200962 *best_clock = clock;
963 bestppm = ppm;
964 found = true;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700965 }
966 }
967 }
968 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700969
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300970 return found;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700971}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700972
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200973/*
974 * Returns a set of divisors for the desired target clock with the given
975 * refclk, or FALSE. The returned values represent the clock equation:
976 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
977 */
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300978static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300979chv_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200980 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300981 int target, int refclk, struct dpll *match_clock,
982 struct dpll *best_clock)
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300983{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200984 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300985 struct drm_device *dev = crtc->base.dev;
Imre Deak9ca3ba02015-03-17 11:40:05 +0200986 unsigned int best_error_ppm;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300987 struct dpll clock;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300988 uint64_t m2;
989 int found = false;
990
991 memset(best_clock, 0, sizeof(*best_clock));
Imre Deak9ca3ba02015-03-17 11:40:05 +0200992 best_error_ppm = 1000000;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300993
994 /*
995 * Based on hardware doc, the n always set to 1, and m1 always
996 * set to 2. If requires to support 200Mhz refclk, we need to
997 * revisit this because n may not 1 anymore.
998 */
999 clock.n = 1, clock.m1 = 2;
1000 target *= 5; /* fast clock */
1001
1002 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
1003 for (clock.p2 = limit->p2.p2_fast;
1004 clock.p2 >= limit->p2.p2_slow;
1005 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Imre Deak9ca3ba02015-03-17 11:40:05 +02001006 unsigned int error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001007
1008 clock.p = clock.p1 * clock.p2;
1009
1010 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
1011 clock.n) << 22, refclk * clock.m1);
1012
1013 if (m2 > INT_MAX/clock.m1)
1014 continue;
1015
1016 clock.m2 = m2;
1017
Imre Deakdccbea32015-06-22 23:35:51 +03001018 chv_calc_dpll_params(refclk, &clock);
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001019
1020 if (!intel_PLL_is_valid(dev, limit, &clock))
1021 continue;
1022
Imre Deak9ca3ba02015-03-17 11:40:05 +02001023 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
1024 best_error_ppm, &error_ppm))
1025 continue;
1026
1027 *best_clock = clock;
1028 best_error_ppm = error_ppm;
1029 found = true;
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001030 }
1031 }
1032
1033 return found;
1034}
1035
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001036bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03001037 struct dpll *best_clock)
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001038{
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02001039 int refclk = 100000;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03001040 const struct intel_limit *limit = &intel_limits_bxt;
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001041
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02001042 return chv_find_best_dpll(limit, crtc_state,
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001043 target_clock, refclk, NULL, best_clock);
1044}
1045
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001046bool intel_crtc_active(struct drm_crtc *crtc)
1047{
1048 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1049
1050 /* Be paranoid as we can arrive here with only partial
1051 * state retrieved from the hardware during setup.
1052 *
Damien Lespiau241bfc32013-09-25 16:45:37 +01001053 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001054 * as Haswell has gained clock readout/fastboot support.
1055 *
Dave Airlie66e514c2014-04-03 07:51:54 +10001056 * We can ditch the crtc->primary->fb check as soon as we can
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001057 * properly reconstruct framebuffers.
Matt Roperc3d1f432015-03-09 10:19:23 -07001058 *
1059 * FIXME: The intel_crtc->active here should be switched to
1060 * crtc->state->active once we have proper CRTC states wired up
1061 * for atomic.
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001062 */
Matt Roperc3d1f432015-03-09 10:19:23 -07001063 return intel_crtc->active && crtc->primary->state->fb &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001064 intel_crtc->config->base.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001065}
1066
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001067enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1068 enum pipe pipe)
1069{
1070 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1071 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1072
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001073 return intel_crtc->config->cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001074}
1075
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001076static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1077{
1078 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001079 i915_reg_t reg = PIPEDSL(pipe);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001080 u32 line1, line2;
1081 u32 line_mask;
1082
1083 if (IS_GEN2(dev))
1084 line_mask = DSL_LINEMASK_GEN2;
1085 else
1086 line_mask = DSL_LINEMASK_GEN3;
1087
1088 line1 = I915_READ(reg) & line_mask;
Daniel Vetter6adfb1e2015-07-07 09:10:40 +02001089 msleep(5);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001090 line2 = I915_READ(reg) & line_mask;
1091
1092 return line1 == line2;
1093}
1094
Keith Packardab7ad7f2010-10-03 00:33:06 -07001095/*
1096 * intel_wait_for_pipe_off - wait for pipe to turn off
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001097 * @crtc: crtc whose pipe to wait for
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001098 *
1099 * After disabling a pipe, we can't wait for vblank in the usual way,
1100 * spinning on the vblank interrupt status bit, since we won't actually
1101 * see an interrupt when the pipe is disabled.
1102 *
Keith Packardab7ad7f2010-10-03 00:33:06 -07001103 * On Gen4 and above:
1104 * wait for the pipe register state bit to turn off
1105 *
1106 * Otherwise:
1107 * wait for the display line value to settle (it usually
1108 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +01001109 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001110 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001111static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001112{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001113 struct drm_device *dev = crtc->base.dev;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001114 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001115 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001116 enum pipe pipe = crtc->pipe;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001117
Keith Packardab7ad7f2010-10-03 00:33:06 -07001118 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001119 i915_reg_t reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001120
Keith Packardab7ad7f2010-10-03 00:33:06 -07001121 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +01001122 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
1123 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001124 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001125 } else {
Keith Packardab7ad7f2010-10-03 00:33:06 -07001126 /* Wait for the display line to settle */
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001127 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001128 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001129 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001130}
1131
Jesse Barnesb24e7172011-01-04 15:09:30 -08001132/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +02001133void assert_pll(struct drm_i915_private *dev_priv,
1134 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001135{
Jesse Barnesb24e7172011-01-04 15:09:30 -08001136 u32 val;
1137 bool cur_state;
1138
Ville Syrjälä649636e2015-09-22 19:50:01 +03001139 val = I915_READ(DPLL(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001140 cur_state = !!(val & DPLL_VCO_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001141 I915_STATE_WARN(cur_state != state,
Jesse Barnesb24e7172011-01-04 15:09:30 -08001142 "PLL state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001143 onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001144}
Jesse Barnesb24e7172011-01-04 15:09:30 -08001145
Jani Nikula23538ef2013-08-27 15:12:22 +03001146/* XXX: the dsi pll is shared between MIPI DSI ports */
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +00001147void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
Jani Nikula23538ef2013-08-27 15:12:22 +03001148{
1149 u32 val;
1150 bool cur_state;
1151
Ville Syrjäläa5805162015-05-26 20:42:30 +03001152 mutex_lock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001153 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
Ville Syrjäläa5805162015-05-26 20:42:30 +03001154 mutex_unlock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001155
1156 cur_state = val & DSI_PLL_VCO_EN;
Rob Clarke2c719b2014-12-15 13:56:32 -05001157 I915_STATE_WARN(cur_state != state,
Jani Nikula23538ef2013-08-27 15:12:22 +03001158 "DSI PLL state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001159 onoff(state), onoff(cur_state));
Jani Nikula23538ef2013-08-27 15:12:22 +03001160}
Jani Nikula23538ef2013-08-27 15:12:22 +03001161
Jesse Barnes040484a2011-01-03 12:14:26 -08001162static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1163 enum pipe pipe, bool state)
1164{
Jesse Barnes040484a2011-01-03 12:14:26 -08001165 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001166 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1167 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001168
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001169 if (HAS_DDI(dev_priv)) {
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001170 /* DDI does not have a specific FDI_TX register */
Ville Syrjälä649636e2015-09-22 19:50:01 +03001171 u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
Paulo Zanoniad80a812012-10-24 16:06:19 -02001172 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001173 } else {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001174 u32 val = I915_READ(FDI_TX_CTL(pipe));
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001175 cur_state = !!(val & FDI_TX_ENABLE);
1176 }
Rob Clarke2c719b2014-12-15 13:56:32 -05001177 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001178 "FDI TX state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001179 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001180}
1181#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1182#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1183
1184static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1185 enum pipe pipe, bool state)
1186{
Jesse Barnes040484a2011-01-03 12:14:26 -08001187 u32 val;
1188 bool cur_state;
1189
Ville Syrjälä649636e2015-09-22 19:50:01 +03001190 val = I915_READ(FDI_RX_CTL(pipe));
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001191 cur_state = !!(val & FDI_RX_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001192 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001193 "FDI RX state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001194 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001195}
1196#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1197#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1198
1199static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1200 enum pipe pipe)
1201{
Jesse Barnes040484a2011-01-03 12:14:26 -08001202 u32 val;
1203
1204 /* ILK FDI PLL is always enabled */
Tvrtko Ursulin7e22dbb2016-05-10 10:57:06 +01001205 if (IS_GEN5(dev_priv))
Jesse Barnes040484a2011-01-03 12:14:26 -08001206 return;
1207
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001208 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001209 if (HAS_DDI(dev_priv))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001210 return;
1211
Ville Syrjälä649636e2015-09-22 19:50:01 +03001212 val = I915_READ(FDI_TX_CTL(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001213 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 -08001214}
1215
Daniel Vetter55607e82013-06-16 21:42:39 +02001216void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1217 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001218{
Jesse Barnes040484a2011-01-03 12:14:26 -08001219 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001220 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001221
Ville Syrjälä649636e2015-09-22 19:50:01 +03001222 val = I915_READ(FDI_RX_CTL(pipe));
Daniel Vetter55607e82013-06-16 21:42:39 +02001223 cur_state = !!(val & FDI_RX_PLL_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001224 I915_STATE_WARN(cur_state != state,
Daniel Vetter55607e82013-06-16 21:42:39 +02001225 "FDI RX PLL assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001226 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001227}
1228
Daniel Vetterb680c372014-09-19 18:27:27 +02001229void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1230 enum pipe pipe)
Jesse Barnesea0760c2011-01-04 15:09:32 -08001231{
Jani Nikulabedd4db2014-08-22 15:04:13 +03001232 struct drm_device *dev = dev_priv->dev;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001233 i915_reg_t pp_reg;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001234 u32 val;
1235 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001236 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001237
Jani Nikulabedd4db2014-08-22 15:04:13 +03001238 if (WARN_ON(HAS_DDI(dev)))
1239 return;
1240
1241 if (HAS_PCH_SPLIT(dev)) {
1242 u32 port_sel;
1243
Jesse Barnesea0760c2011-01-04 15:09:32 -08001244 pp_reg = PCH_PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001245 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1246
1247 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1248 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1249 panel_pipe = PIPE_B;
1250 /* XXX: else fix for eDP */
Wayne Boyer666a4532015-12-09 12:29:35 -08001251 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Jani Nikulabedd4db2014-08-22 15:04:13 +03001252 /* presumably write lock depends on pipe, not port select */
1253 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1254 panel_pipe = pipe;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001255 } else {
1256 pp_reg = PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001257 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1258 panel_pipe = PIPE_B;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001259 }
1260
1261 val = I915_READ(pp_reg);
1262 if (!(val & PANEL_POWER_ON) ||
Jani Nikulaec49ba22014-08-21 15:06:25 +03001263 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
Jesse Barnesea0760c2011-01-04 15:09:32 -08001264 locked = false;
1265
Rob Clarke2c719b2014-12-15 13:56:32 -05001266 I915_STATE_WARN(panel_pipe == pipe && locked,
Jesse Barnesea0760c2011-01-04 15:09:32 -08001267 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001268 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001269}
1270
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001271static void assert_cursor(struct drm_i915_private *dev_priv,
1272 enum pipe pipe, bool state)
1273{
1274 struct drm_device *dev = dev_priv->dev;
1275 bool cur_state;
1276
Paulo Zanonid9d82082014-02-27 16:30:56 -03001277 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä0b87c242015-09-22 19:47:51 +03001278 cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
Paulo Zanonid9d82082014-02-27 16:30:56 -03001279 else
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03001280 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001281
Rob Clarke2c719b2014-12-15 13:56:32 -05001282 I915_STATE_WARN(cur_state != state,
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001283 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001284 pipe_name(pipe), onoff(state), onoff(cur_state));
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001285}
1286#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1287#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1288
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001289void assert_pipe(struct drm_i915_private *dev_priv,
1290 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001291{
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001292 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001293 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1294 pipe);
Imre Deak4feed0e2016-02-12 18:55:14 +02001295 enum intel_display_power_domain power_domain;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001296
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001297 /* if we need the pipe quirk it must be always on */
1298 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1299 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetter8e636782012-01-22 01:36:48 +01001300 state = true;
1301
Imre Deak4feed0e2016-02-12 18:55:14 +02001302 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
1303 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001304 u32 val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni69310162013-01-29 16:35:19 -02001305 cur_state = !!(val & PIPECONF_ENABLE);
Imre Deak4feed0e2016-02-12 18:55:14 +02001306
1307 intel_display_power_put(dev_priv, power_domain);
1308 } else {
1309 cur_state = false;
Paulo Zanoni69310162013-01-29 16:35:19 -02001310 }
1311
Rob Clarke2c719b2014-12-15 13:56:32 -05001312 I915_STATE_WARN(cur_state != state,
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001313 "pipe %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001314 pipe_name(pipe), onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001315}
1316
Chris Wilson931872f2012-01-16 23:01:13 +00001317static void assert_plane(struct drm_i915_private *dev_priv,
1318 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001319{
Jesse Barnesb24e7172011-01-04 15:09:30 -08001320 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001321 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001322
Ville Syrjälä649636e2015-09-22 19:50:01 +03001323 val = I915_READ(DSPCNTR(plane));
Chris Wilson931872f2012-01-16 23:01:13 +00001324 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001325 I915_STATE_WARN(cur_state != state,
Chris Wilson931872f2012-01-16 23:01:13 +00001326 "plane %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001327 plane_name(plane), onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001328}
1329
Chris Wilson931872f2012-01-16 23:01:13 +00001330#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1331#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1332
Jesse Barnesb24e7172011-01-04 15:09:30 -08001333static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1334 enum pipe pipe)
1335{
Ville Syrjälä653e1022013-06-04 13:49:05 +03001336 struct drm_device *dev = dev_priv->dev;
Ville Syrjälä649636e2015-09-22 19:50:01 +03001337 int i;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001338
Ville Syrjälä653e1022013-06-04 13:49:05 +03001339 /* Primary planes are fixed to pipes on gen4+ */
1340 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001341 u32 val = I915_READ(DSPCNTR(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001342 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
Adam Jackson28c057942011-10-07 14:38:42 -04001343 "plane %c assertion failure, should be disabled but not\n",
1344 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001345 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001346 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001347
Jesse Barnesb24e7172011-01-04 15:09:30 -08001348 /* Need to check both planes against the pipe */
Damien Lespiau055e3932014-08-18 13:49:10 +01001349 for_each_pipe(dev_priv, i) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001350 u32 val = I915_READ(DSPCNTR(i));
1351 enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
Jesse Barnesb24e7172011-01-04 15:09:30 -08001352 DISPPLANE_SEL_PIPE_SHIFT;
Rob Clarke2c719b2014-12-15 13:56:32 -05001353 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001354 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1355 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001356 }
1357}
1358
Jesse Barnes19332d72013-03-28 09:55:38 -07001359static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1360 enum pipe pipe)
1361{
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001362 struct drm_device *dev = dev_priv->dev;
Ville Syrjälä649636e2015-09-22 19:50:01 +03001363 int sprite;
Jesse Barnes19332d72013-03-28 09:55:38 -07001364
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001365 if (INTEL_INFO(dev)->gen >= 9) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001366 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001367 u32 val = I915_READ(PLANE_CTL(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001368 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001369 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1370 sprite, pipe_name(pipe));
1371 }
Wayne Boyer666a4532015-12-09 12:29:35 -08001372 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001373 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001374 u32 val = I915_READ(SPCNTR(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001375 I915_STATE_WARN(val & SP_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001376 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +00001377 sprite_name(pipe, sprite), pipe_name(pipe));
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001378 }
1379 } else if (INTEL_INFO(dev)->gen >= 7) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001380 u32 val = I915_READ(SPRCTL(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001381 I915_STATE_WARN(val & SPRITE_ENABLE,
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001382 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001383 plane_name(pipe), pipe_name(pipe));
1384 } else if (INTEL_INFO(dev)->gen >= 5) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001385 u32 val = I915_READ(DVSCNTR(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001386 I915_STATE_WARN(val & DVS_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001387 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1388 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001389 }
1390}
1391
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001392static void assert_vblank_disabled(struct drm_crtc *crtc)
1393{
Rob Clarke2c719b2014-12-15 13:56:32 -05001394 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001395 drm_crtc_vblank_put(crtc);
1396}
1397
Ander Conselvan de Oliveira7abd4b32016-03-08 17:46:15 +02001398void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1399 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001400{
Jesse Barnes92f25842011-01-04 15:09:34 -08001401 u32 val;
1402 bool enabled;
1403
Ville Syrjälä649636e2015-09-22 19:50:01 +03001404 val = I915_READ(PCH_TRANSCONF(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001405 enabled = !!(val & TRANS_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001406 I915_STATE_WARN(enabled,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001407 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1408 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001409}
1410
Keith Packard4e634382011-08-06 10:39:45 -07001411static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1412 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001413{
1414 if ((val & DP_PORT_EN) == 0)
1415 return false;
1416
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001417 if (HAS_PCH_CPT(dev_priv)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001418 u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
Keith Packardf0575e92011-07-25 22:12:43 -07001419 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1420 return false;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001421 } else if (IS_CHERRYVIEW(dev_priv)) {
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001422 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1423 return false;
Keith Packardf0575e92011-07-25 22:12:43 -07001424 } else {
1425 if ((val & DP_PIPE_MASK) != (pipe << 30))
1426 return false;
1427 }
1428 return true;
1429}
1430
Keith Packard1519b992011-08-06 10:35:34 -07001431static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1432 enum pipe pipe, u32 val)
1433{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001434 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001435 return false;
1436
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001437 if (HAS_PCH_CPT(dev_priv)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001438 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001439 return false;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001440 } else if (IS_CHERRYVIEW(dev_priv)) {
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001441 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1442 return false;
Keith Packard1519b992011-08-06 10:35:34 -07001443 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001444 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001445 return false;
1446 }
1447 return true;
1448}
1449
1450static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1451 enum pipe pipe, u32 val)
1452{
1453 if ((val & LVDS_PORT_EN) == 0)
1454 return false;
1455
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001456 if (HAS_PCH_CPT(dev_priv)) {
Keith Packard1519b992011-08-06 10:35:34 -07001457 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1458 return false;
1459 } else {
1460 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1461 return false;
1462 }
1463 return true;
1464}
1465
1466static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1467 enum pipe pipe, u32 val)
1468{
1469 if ((val & ADPA_DAC_ENABLE) == 0)
1470 return false;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001471 if (HAS_PCH_CPT(dev_priv)) {
Keith Packard1519b992011-08-06 10:35:34 -07001472 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1473 return false;
1474 } else {
1475 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1476 return false;
1477 }
1478 return true;
1479}
1480
Jesse Barnes291906f2011-02-02 12:28:03 -08001481static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001482 enum pipe pipe, i915_reg_t reg,
1483 u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001484{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001485 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001486 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001487 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001488 i915_mmio_reg_offset(reg), pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001489
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001490 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001491 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001492 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001493}
1494
1495static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001496 enum pipe pipe, i915_reg_t reg)
Jesse Barnes291906f2011-02-02 12:28:03 -08001497{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001498 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001499 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001500 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001501 i915_mmio_reg_offset(reg), pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001502
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001503 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001504 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001505 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001506}
1507
1508static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1509 enum pipe pipe)
1510{
Jesse Barnes291906f2011-02-02 12:28:03 -08001511 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001512
Keith Packardf0575e92011-07-25 22:12:43 -07001513 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1514 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1515 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001516
Ville Syrjälä649636e2015-09-22 19:50:01 +03001517 val = I915_READ(PCH_ADPA);
Rob Clarke2c719b2014-12-15 13:56:32 -05001518 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001519 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001520 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001521
Ville Syrjälä649636e2015-09-22 19:50:01 +03001522 val = I915_READ(PCH_LVDS);
Rob Clarke2c719b2014-12-15 13:56:32 -05001523 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001524 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001525 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001526
Paulo Zanonie2debe92013-02-18 19:00:27 -03001527 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1528 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1529 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001530}
1531
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001532static void _vlv_enable_pll(struct intel_crtc *crtc,
1533 const struct intel_crtc_state *pipe_config)
1534{
1535 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1536 enum pipe pipe = crtc->pipe;
1537
1538 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
1539 POSTING_READ(DPLL(pipe));
1540 udelay(150);
1541
1542 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1543 DRM_ERROR("DPLL %d failed to lock\n", pipe);
1544}
1545
Ville Syrjäläd288f652014-10-28 13:20:22 +02001546static void vlv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001547 const struct intel_crtc_state *pipe_config)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001548{
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001549 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001550 enum pipe pipe = crtc->pipe;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001551
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001552 assert_pipe_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001553
Daniel Vetter87442f72013-06-06 00:52:17 +02001554 /* PLL is protected by panel, make sure we can write it */
Ville Syrjälä7d1a83c2016-03-15 16:39:58 +02001555 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001556
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001557 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1558 _vlv_enable_pll(crtc, pipe_config);
Daniel Vetter426115c2013-07-11 22:13:42 +02001559
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001560 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1561 POSTING_READ(DPLL_MD(pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001562}
1563
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001564
1565static void _chv_enable_pll(struct intel_crtc *crtc,
1566 const struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001567{
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001568 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001569 enum pipe pipe = crtc->pipe;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001570 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001571 u32 tmp;
1572
Ville Syrjäläa5805162015-05-26 20:42:30 +03001573 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001574
1575 /* Enable back the 10bit clock to display controller */
1576 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1577 tmp |= DPIO_DCLKP_EN;
1578 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1579
Ville Syrjälä54433e92015-05-26 20:42:31 +03001580 mutex_unlock(&dev_priv->sb_lock);
1581
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001582 /*
1583 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1584 */
1585 udelay(1);
1586
1587 /* Enable PLL */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001588 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001589
1590 /* Check PLL is locked */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001591 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001592 DRM_ERROR("PLL %d failed to lock\n", pipe);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001593}
1594
1595static void chv_enable_pll(struct intel_crtc *crtc,
1596 const struct intel_crtc_state *pipe_config)
1597{
1598 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1599 enum pipe pipe = crtc->pipe;
1600
1601 assert_pipe_disabled(dev_priv, pipe);
1602
1603 /* PLL is protected by panel, make sure we can write it */
1604 assert_panel_unlocked(dev_priv, pipe);
1605
1606 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1607 _chv_enable_pll(crtc, pipe_config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001608
Ville Syrjäläc2317752016-03-15 16:39:56 +02001609 if (pipe != PIPE_A) {
1610 /*
1611 * WaPixelRepeatModeFixForC0:chv
1612 *
1613 * DPLLCMD is AWOL. Use chicken bits to propagate
1614 * the value from DPLLBMD to either pipe B or C.
1615 */
1616 I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
1617 I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
1618 I915_WRITE(CBR4_VLV, 0);
1619 dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
1620
1621 /*
1622 * DPLLB VGA mode also seems to cause problems.
1623 * We should always have it disabled.
1624 */
1625 WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
1626 } else {
1627 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1628 POSTING_READ(DPLL_MD(pipe));
1629 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001630}
1631
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001632static int intel_num_dvo_pipes(struct drm_device *dev)
1633{
1634 struct intel_crtc *crtc;
1635 int count = 0;
1636
1637 for_each_intel_crtc(dev, crtc)
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001638 count += crtc->base.state->active &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001639 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001640
1641 return count;
1642}
1643
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001644static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001645{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001646 struct drm_device *dev = crtc->base.dev;
1647 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001648 i915_reg_t reg = DPLL(crtc->pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001649 u32 dpll = crtc->config->dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001650
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001651 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001652
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001653 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001654 if (IS_MOBILE(dev) && !IS_I830(dev))
1655 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001656
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001657 /* Enable DVO 2x clock on both PLLs if necessary */
1658 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1659 /*
1660 * It appears to be important that we don't enable this
1661 * for the current pipe before otherwise configuring the
1662 * PLL. No idea how this should be handled if multiple
1663 * DVO outputs are enabled simultaneosly.
1664 */
1665 dpll |= DPLL_DVO_2X_MODE;
1666 I915_WRITE(DPLL(!crtc->pipe),
1667 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1668 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001669
Ville Syrjäläc2b63372015-10-07 22:08:25 +03001670 /*
1671 * Apparently we need to have VGA mode enabled prior to changing
1672 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1673 * dividers, even though the register value does change.
1674 */
1675 I915_WRITE(reg, 0);
1676
Ville Syrjälä8e7a65a2015-10-07 22:08:24 +03001677 I915_WRITE(reg, dpll);
1678
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001679 /* Wait for the clocks to stabilize. */
1680 POSTING_READ(reg);
1681 udelay(150);
1682
1683 if (INTEL_INFO(dev)->gen >= 4) {
1684 I915_WRITE(DPLL_MD(crtc->pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001685 crtc->config->dpll_hw_state.dpll_md);
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001686 } else {
1687 /* The pixel multiplier can only be updated once the
1688 * DPLL is enabled and the clocks are stable.
1689 *
1690 * So write it again.
1691 */
1692 I915_WRITE(reg, dpll);
1693 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001694
1695 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001696 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001697 POSTING_READ(reg);
1698 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001699 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001700 POSTING_READ(reg);
1701 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001702 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001703 POSTING_READ(reg);
1704 udelay(150); /* wait for warmup */
1705}
1706
1707/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001708 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001709 * @dev_priv: i915 private structure
1710 * @pipe: pipe PLL to disable
1711 *
1712 * Disable the PLL for @pipe, making sure the pipe is off first.
1713 *
1714 * Note! This is for pre-ILK only.
1715 */
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001716static void i9xx_disable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001717{
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001718 struct drm_device *dev = crtc->base.dev;
1719 struct drm_i915_private *dev_priv = dev->dev_private;
1720 enum pipe pipe = crtc->pipe;
1721
1722 /* Disable DVO 2x clock on both PLLs if necessary */
1723 if (IS_I830(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001724 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001725 !intel_num_dvo_pipes(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001726 I915_WRITE(DPLL(PIPE_B),
1727 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1728 I915_WRITE(DPLL(PIPE_A),
1729 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1730 }
1731
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001732 /* Don't disable pipe or pipe PLLs if needed */
1733 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1734 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001735 return;
1736
1737 /* Make sure the pipe isn't still relying on us */
1738 assert_pipe_disabled(dev_priv, pipe);
1739
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001740 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
Daniel Vetter50b44a42013-06-05 13:34:33 +02001741 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001742}
1743
Jesse Barnesf6071162013-10-01 10:41:38 -07001744static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1745{
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001746 u32 val;
Jesse Barnesf6071162013-10-01 10:41:38 -07001747
1748 /* Make sure the pipe isn't still relying on us */
1749 assert_pipe_disabled(dev_priv, pipe);
1750
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02001751 val = DPLL_INTEGRATED_REF_CLK_VLV |
1752 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1753 if (pipe != PIPE_A)
1754 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1755
Jesse Barnesf6071162013-10-01 10:41:38 -07001756 I915_WRITE(DPLL(pipe), val);
1757 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001758}
1759
1760static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1761{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001762 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001763 u32 val;
1764
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001765 /* Make sure the pipe isn't still relying on us */
1766 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001767
Ville Syrjälä60bfe442015-06-29 15:25:49 +03001768 val = DPLL_SSC_REF_CLK_CHV |
1769 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001770 if (pipe != PIPE_A)
1771 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02001772
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001773 I915_WRITE(DPLL(pipe), val);
1774 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001775
Ville Syrjäläa5805162015-05-26 20:42:30 +03001776 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd7520482014-04-09 13:28:59 +03001777
1778 /* Disable 10bit clock to display controller */
1779 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1780 val &= ~DPIO_DCLKP_EN;
1781 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1782
Ville Syrjäläa5805162015-05-26 20:42:30 +03001783 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001784}
1785
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001786void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001787 struct intel_digital_port *dport,
1788 unsigned int expected_mask)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001789{
1790 u32 port_mask;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001791 i915_reg_t dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001792
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001793 switch (dport->port) {
1794 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001795 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001796 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001797 break;
1798 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001799 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001800 dpll_reg = DPLL(0);
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001801 expected_mask <<= 4;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001802 break;
1803 case PORT_D:
1804 port_mask = DPLL_PORTD_READY_MASK;
1805 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001806 break;
1807 default:
1808 BUG();
1809 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001810
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001811 if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
1812 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1813 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
Jesse Barnes89b667f2013-04-18 14:51:36 -07001814}
1815
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001816static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1817 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001818{
Daniel Vetter23670b322012-11-01 09:15:30 +01001819 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001820 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001821 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001822 i915_reg_t reg;
1823 uint32_t val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001824
Jesse Barnes040484a2011-01-03 12:14:26 -08001825 /* Make sure PCH DPLL is enabled */
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02001826 assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
Jesse Barnes040484a2011-01-03 12:14:26 -08001827
1828 /* FDI must be feeding us bits for PCH ports */
1829 assert_fdi_tx_enabled(dev_priv, pipe);
1830 assert_fdi_rx_enabled(dev_priv, pipe);
1831
Daniel Vetter23670b322012-11-01 09:15:30 +01001832 if (HAS_PCH_CPT(dev)) {
1833 /* Workaround: Set the timing override bit before enabling the
1834 * pch transcoder. */
1835 reg = TRANS_CHICKEN2(pipe);
1836 val = I915_READ(reg);
1837 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1838 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001839 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001840
Daniel Vetterab9412b2013-05-03 11:49:46 +02001841 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001842 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001843 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001844
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001845 if (HAS_PCH_IBX(dev_priv)) {
Jesse Barnese9bcff52011-06-24 12:19:20 -07001846 /*
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001847 * Make the BPC in transcoder be consistent with
1848 * that in pipeconf reg. For HDMI we must use 8bpc
1849 * here for both 8bpc and 12bpc.
Jesse Barnese9bcff52011-06-24 12:19:20 -07001850 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001851 val &= ~PIPECONF_BPC_MASK;
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001852 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
1853 val |= PIPECONF_8BPC;
1854 else
1855 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001856 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001857
1858 val &= ~TRANS_INTERLACE_MASK;
1859 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001860 if (HAS_PCH_IBX(dev_priv) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001861 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001862 val |= TRANS_LEGACY_INTERLACED_ILK;
1863 else
1864 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001865 else
1866 val |= TRANS_PROGRESSIVE;
1867
Jesse Barnes040484a2011-01-03 12:14:26 -08001868 I915_WRITE(reg, val | TRANS_ENABLE);
1869 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001870 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08001871}
1872
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001873static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001874 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001875{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001876 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001877
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001878 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001879 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001880 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001881
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001882 /* Workaround: set timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001883 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01001884 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001885 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001886
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001887 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001888 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001889
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001890 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1891 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001892 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001893 else
1894 val |= TRANS_PROGRESSIVE;
1895
Daniel Vetterab9412b2013-05-03 11:49:46 +02001896 I915_WRITE(LPT_TRANSCONF, val);
1897 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02001898 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001899}
1900
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001901static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1902 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001903{
Daniel Vetter23670b322012-11-01 09:15:30 +01001904 struct drm_device *dev = dev_priv->dev;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001905 i915_reg_t reg;
1906 uint32_t val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001907
1908 /* FDI relies on the transcoder */
1909 assert_fdi_tx_disabled(dev_priv, pipe);
1910 assert_fdi_rx_disabled(dev_priv, pipe);
1911
Jesse Barnes291906f2011-02-02 12:28:03 -08001912 /* Ports must be off as well */
1913 assert_pch_ports_disabled(dev_priv, pipe);
1914
Daniel Vetterab9412b2013-05-03 11:49:46 +02001915 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001916 val = I915_READ(reg);
1917 val &= ~TRANS_ENABLE;
1918 I915_WRITE(reg, val);
1919 /* wait for PCH transcoder off, transcoder state */
1920 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001921 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01001922
Ville Syrjäläc4656132015-10-29 21:25:56 +02001923 if (HAS_PCH_CPT(dev)) {
Daniel Vetter23670b322012-11-01 09:15:30 +01001924 /* Workaround: Clear the timing override chicken bit again. */
1925 reg = TRANS_CHICKEN2(pipe);
1926 val = I915_READ(reg);
1927 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1928 I915_WRITE(reg, val);
1929 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001930}
1931
Paulo Zanoniab4d9662012-10-31 18:12:55 -02001932static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001933{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001934 u32 val;
1935
Daniel Vetterab9412b2013-05-03 11:49:46 +02001936 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001937 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02001938 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001939 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02001940 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001941 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001942
1943 /* Workaround: clear timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001944 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01001945 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001946 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Jesse Barnes92f25842011-01-04 15:09:34 -08001947}
1948
1949/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001950 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02001951 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08001952 *
Paulo Zanoni03722642014-01-17 13:51:09 -02001953 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001954 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08001955 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02001956static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001957{
Paulo Zanoni03722642014-01-17 13:51:09 -02001958 struct drm_device *dev = crtc->base.dev;
1959 struct drm_i915_private *dev_priv = dev->dev_private;
1960 enum pipe pipe = crtc->pipe;
Ville Syrjälä1a70a7282015-10-29 21:25:50 +02001961 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter1a240d42012-11-29 22:18:51 +01001962 enum pipe pch_transcoder;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001963 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001964 u32 val;
1965
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03001966 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
1967
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001968 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001969 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001970 assert_sprites_disabled(dev_priv, pipe);
1971
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001972 if (HAS_PCH_LPT(dev_priv))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001973 pch_transcoder = TRANSCODER_A;
1974 else
1975 pch_transcoder = pipe;
1976
Jesse Barnesb24e7172011-01-04 15:09:30 -08001977 /*
1978 * A pipe without a PLL won't actually be able to drive bits from
1979 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1980 * need the check.
1981 */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001982 if (HAS_GMCH_DISPLAY(dev_priv))
Jani Nikulaa65347b2015-11-27 12:21:46 +02001983 if (crtc->config->has_dsi_encoder)
Jani Nikula23538ef2013-08-27 15:12:22 +03001984 assert_dsi_pll_enabled(dev_priv);
1985 else
1986 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001987 else {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001988 if (crtc->config->has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08001989 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001990 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001991 assert_fdi_tx_pll_enabled(dev_priv,
1992 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08001993 }
1994 /* FIXME: assert CPU port conditions for SNB+ */
1995 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08001996
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001997 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001998 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02001999 if (val & PIPECONF_ENABLE) {
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002000 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2001 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
Chris Wilson00d70b12011-03-17 07:18:29 +00002002 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002003 }
Chris Wilson00d70b12011-03-17 07:18:29 +00002004
2005 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02002006 POSTING_READ(reg);
Ville Syrjäläb7792d82015-12-14 18:23:43 +02002007
2008 /*
2009 * Until the pipe starts DSL will read as 0, which would cause
2010 * an apparent vblank timestamp jump, which messes up also the
2011 * frame count when it's derived from the timestamps. So let's
2012 * wait for the pipe to start properly before we call
2013 * drm_crtc_vblank_on()
2014 */
2015 if (dev->max_vblank_count == 0 &&
2016 wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
2017 DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08002018}
2019
2020/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002021 * intel_disable_pipe - disable a pipe, asserting requirements
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002022 * @crtc: crtc whose pipes is to be disabled
Jesse Barnesb24e7172011-01-04 15:09:30 -08002023 *
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002024 * Disable the pipe of @crtc, making sure that various hardware
2025 * specific requirements are met, if applicable, e.g. plane
2026 * disabled, panel fitter off, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002027 *
2028 * Will wait until the pipe has shut down before returning.
2029 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002030static void intel_disable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002031{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002032 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002033 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002034 enum pipe pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002035 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002036 u32 val;
2037
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03002038 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
2039
Jesse Barnesb24e7172011-01-04 15:09:30 -08002040 /*
2041 * Make sure planes won't keep trying to pump pixels to us,
2042 * or we might hang the display.
2043 */
2044 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002045 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07002046 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002047
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002048 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002049 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00002050 if ((val & PIPECONF_ENABLE) == 0)
2051 return;
2052
Ville Syrjälä67adc642014-08-15 01:21:57 +03002053 /*
2054 * Double wide has implications for planes
2055 * so best keep it disabled when not needed.
2056 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002057 if (crtc->config->double_wide)
Ville Syrjälä67adc642014-08-15 01:21:57 +03002058 val &= ~PIPECONF_DOUBLE_WIDE;
2059
2060 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002061 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2062 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Ville Syrjälä67adc642014-08-15 01:21:57 +03002063 val &= ~PIPECONF_ENABLE;
2064
2065 I915_WRITE(reg, val);
2066 if ((val & PIPECONF_ENABLE) == 0)
2067 intel_wait_for_pipe_off(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002068}
2069
Chris Wilson693db182013-03-05 14:52:39 +00002070static bool need_vtd_wa(struct drm_device *dev)
2071{
2072#ifdef CONFIG_INTEL_IOMMU
2073 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2074 return true;
2075#endif
2076 return false;
2077}
2078
Ville Syrjälä832be822016-01-12 21:08:33 +02002079static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
2080{
2081 return IS_GEN2(dev_priv) ? 2048 : 4096;
2082}
2083
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002084static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
2085 uint64_t fb_modifier, unsigned int cpp)
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002086{
2087 switch (fb_modifier) {
2088 case DRM_FORMAT_MOD_NONE:
2089 return cpp;
2090 case I915_FORMAT_MOD_X_TILED:
2091 if (IS_GEN2(dev_priv))
2092 return 128;
2093 else
2094 return 512;
2095 case I915_FORMAT_MOD_Y_TILED:
2096 if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
2097 return 128;
2098 else
2099 return 512;
2100 case I915_FORMAT_MOD_Yf_TILED:
2101 switch (cpp) {
2102 case 1:
2103 return 64;
2104 case 2:
2105 case 4:
2106 return 128;
2107 case 8:
2108 case 16:
2109 return 256;
2110 default:
2111 MISSING_CASE(cpp);
2112 return cpp;
2113 }
2114 break;
2115 default:
2116 MISSING_CASE(fb_modifier);
2117 return cpp;
2118 }
2119}
2120
Ville Syrjälä832be822016-01-12 21:08:33 +02002121unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
2122 uint64_t fb_modifier, unsigned int cpp)
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002123{
Ville Syrjälä832be822016-01-12 21:08:33 +02002124 if (fb_modifier == DRM_FORMAT_MOD_NONE)
2125 return 1;
2126 else
2127 return intel_tile_size(dev_priv) /
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002128 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002129}
2130
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002131/* Return the tile dimensions in pixel units */
2132static void intel_tile_dims(const struct drm_i915_private *dev_priv,
2133 unsigned int *tile_width,
2134 unsigned int *tile_height,
2135 uint64_t fb_modifier,
2136 unsigned int cpp)
2137{
2138 unsigned int tile_width_bytes =
2139 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
2140
2141 *tile_width = tile_width_bytes / cpp;
2142 *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
2143}
2144
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002145unsigned int
2146intel_fb_align_height(struct drm_device *dev, unsigned int height,
Ville Syrjälä832be822016-01-12 21:08:33 +02002147 uint32_t pixel_format, uint64_t fb_modifier)
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002148{
Ville Syrjälä832be822016-01-12 21:08:33 +02002149 unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
2150 unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
2151
2152 return ALIGN(height, tile_height);
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002153}
2154
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002155unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
2156{
2157 unsigned int size = 0;
2158 int i;
2159
2160 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
2161 size += rot_info->plane[i].width * rot_info->plane[i].height;
2162
2163 return size;
2164}
2165
Daniel Vetter75c82a52015-10-14 16:51:04 +02002166static void
Ville Syrjälä3465c582016-02-15 22:54:43 +02002167intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
2168 const struct drm_framebuffer *fb,
2169 unsigned int rotation)
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002170{
Ville Syrjälä2d7a2152016-02-15 22:54:47 +02002171 if (intel_rotation_90_or_270(rotation)) {
2172 *view = i915_ggtt_view_rotated;
2173 view->params.rotated = to_intel_framebuffer(fb)->rot_info;
2174 } else {
2175 *view = i915_ggtt_view_normal;
2176 }
2177}
2178
2179static void
2180intel_fill_fb_info(struct drm_i915_private *dev_priv,
2181 struct drm_framebuffer *fb)
2182{
2183 struct intel_rotation_info *info = &to_intel_framebuffer(fb)->rot_info;
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002184 unsigned int tile_size, tile_width, tile_height, cpp;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002185
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002186 tile_size = intel_tile_size(dev_priv);
2187
2188 cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002189 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2190 fb->modifier[0], cpp);
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002191
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002192 info->plane[0].width = DIV_ROUND_UP(fb->pitches[0], tile_width * cpp);
2193 info->plane[0].height = DIV_ROUND_UP(fb->height, tile_height);
Tvrtko Ursulin84fe03f2015-06-23 14:26:46 +01002194
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01002195 if (info->pixel_format == DRM_FORMAT_NV12) {
Ville Syrjälä832be822016-01-12 21:08:33 +02002196 cpp = drm_format_plane_cpp(fb->pixel_format, 1);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002197 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2198 fb->modifier[1], cpp);
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002199
Ville Syrjälä2d7a2152016-02-15 22:54:47 +02002200 info->uv_offset = fb->offsets[1];
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002201 info->plane[1].width = DIV_ROUND_UP(fb->pitches[1], tile_width * cpp);
2202 info->plane[1].height = DIV_ROUND_UP(fb->height / 2, tile_height);
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01002203 }
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002204}
2205
Ville Syrjälä603525d2016-01-12 21:08:37 +02002206static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002207{
2208 if (INTEL_INFO(dev_priv)->gen >= 9)
2209 return 256 * 1024;
Ville Syrjälä985b8bb2015-06-11 16:31:15 +03002210 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
Wayne Boyer666a4532015-12-09 12:29:35 -08002211 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002212 return 128 * 1024;
2213 else if (INTEL_INFO(dev_priv)->gen >= 4)
2214 return 4 * 1024;
2215 else
Ville Syrjälä44c59052015-06-11 16:31:16 +03002216 return 0;
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002217}
2218
Ville Syrjälä603525d2016-01-12 21:08:37 +02002219static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
2220 uint64_t fb_modifier)
2221{
2222 switch (fb_modifier) {
2223 case DRM_FORMAT_MOD_NONE:
2224 return intel_linear_alignment(dev_priv);
2225 case I915_FORMAT_MOD_X_TILED:
2226 if (INTEL_INFO(dev_priv)->gen >= 9)
2227 return 256 * 1024;
2228 return 0;
2229 case I915_FORMAT_MOD_Y_TILED:
2230 case I915_FORMAT_MOD_Yf_TILED:
2231 return 1 * 1024 * 1024;
2232 default:
2233 MISSING_CASE(fb_modifier);
2234 return 0;
2235 }
2236}
2237
Chris Wilson127bd2a2010-07-23 23:32:05 +01002238int
Ville Syrjälä3465c582016-02-15 22:54:43 +02002239intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
2240 unsigned int rotation)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002241{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002242 struct drm_device *dev = fb->dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00002243 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002244 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002245 struct i915_ggtt_view view;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002246 u32 alignment;
2247 int ret;
2248
Matt Roperebcdd392014-07-09 16:22:11 -07002249 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2250
Ville Syrjälä603525d2016-01-12 21:08:37 +02002251 alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002252
Ville Syrjälä3465c582016-02-15 22:54:43 +02002253 intel_fill_fb_ggtt_view(&view, fb, rotation);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002254
Chris Wilson693db182013-03-05 14:52:39 +00002255 /* Note that the w/a also requires 64 PTE of padding following the
2256 * bo. We currently fill all unused PTE with the shadow page and so
2257 * we should always have valid PTE following the scanout preventing
2258 * the VT-d warning.
2259 */
2260 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2261 alignment = 256 * 1024;
2262
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002263 /*
2264 * Global gtt pte registers are special registers which actually forward
2265 * writes to a chunk of system memory. Which means that there is no risk
2266 * that the register values disappear as soon as we call
2267 * intel_runtime_pm_put(), so it is correct to wrap only the
2268 * pin/unpin/fence and not more.
2269 */
2270 intel_runtime_pm_get(dev_priv);
2271
Maarten Lankhorst7580d772015-08-18 13:40:06 +02002272 ret = i915_gem_object_pin_to_display_plane(obj, alignment,
2273 &view);
Chris Wilson48b956c2010-09-14 12:50:34 +01002274 if (ret)
Maarten Lankhorstb26a6b32015-09-23 13:27:09 +02002275 goto err_pm;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002276
2277 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2278 * fence, whereas 965+ only requires a fence if using
2279 * framebuffer compression. For simplicity, we always install
2280 * a fence as the cost is not that onerous.
2281 */
Vivek Kasireddy98072162015-10-29 18:54:38 -07002282 if (view.type == I915_GGTT_VIEW_NORMAL) {
2283 ret = i915_gem_object_get_fence(obj);
2284 if (ret == -EDEADLK) {
2285 /*
2286 * -EDEADLK means there are no free fences
2287 * no pending flips.
2288 *
2289 * This is propagated to atomic, but it uses
2290 * -EDEADLK to force a locking recovery, so
2291 * change the returned error to -EBUSY.
2292 */
2293 ret = -EBUSY;
2294 goto err_unpin;
2295 } else if (ret)
2296 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002297
Vivek Kasireddy98072162015-10-29 18:54:38 -07002298 i915_gem_object_pin_fence(obj);
2299 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002300
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002301 intel_runtime_pm_put(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002302 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002303
2304err_unpin:
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002305 i915_gem_object_unpin_from_display_plane(obj, &view);
Maarten Lankhorstb26a6b32015-09-23 13:27:09 +02002306err_pm:
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002307 intel_runtime_pm_put(dev_priv);
Chris Wilson48b956c2010-09-14 12:50:34 +01002308 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002309}
2310
Chris Wilsonfb4b8ce2016-04-28 09:56:35 +01002311void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
Chris Wilson1690e1e2011-12-14 13:57:08 +01002312{
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002313 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002314 struct i915_ggtt_view view;
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002315
Matt Roperebcdd392014-07-09 16:22:11 -07002316 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2317
Ville Syrjälä3465c582016-02-15 22:54:43 +02002318 intel_fill_fb_ggtt_view(&view, fb, rotation);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002319
Vivek Kasireddy98072162015-10-29 18:54:38 -07002320 if (view.type == I915_GGTT_VIEW_NORMAL)
2321 i915_gem_object_unpin_fence(obj);
2322
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002323 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002324}
2325
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002326/*
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002327 * Adjust the tile offset by moving the difference into
2328 * the x/y offsets.
2329 *
2330 * Input tile dimensions and pitch must already be
2331 * rotated to match x and y, and in pixel units.
2332 */
2333static u32 intel_adjust_tile_offset(int *x, int *y,
2334 unsigned int tile_width,
2335 unsigned int tile_height,
2336 unsigned int tile_size,
2337 unsigned int pitch_tiles,
2338 u32 old_offset,
2339 u32 new_offset)
2340{
2341 unsigned int tiles;
2342
2343 WARN_ON(old_offset & (tile_size - 1));
2344 WARN_ON(new_offset & (tile_size - 1));
2345 WARN_ON(new_offset > old_offset);
2346
2347 tiles = (old_offset - new_offset) / tile_size;
2348
2349 *y += tiles / pitch_tiles * tile_height;
2350 *x += tiles % pitch_tiles * tile_width;
2351
2352 return new_offset;
2353}
2354
2355/*
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002356 * Computes the linear offset to the base tile and adjusts
2357 * x, y. bytes per pixel is assumed to be a power-of-two.
2358 *
2359 * In the 90/270 rotated case, x and y are assumed
2360 * to be already rotated to match the rotated GTT view, and
2361 * pitch is the tile_height aligned framebuffer height.
2362 */
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002363u32 intel_compute_tile_offset(int *x, int *y,
2364 const struct drm_framebuffer *fb, int plane,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002365 unsigned int pitch,
2366 unsigned int rotation)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002367{
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002368 const struct drm_i915_private *dev_priv = to_i915(fb->dev);
2369 uint64_t fb_modifier = fb->modifier[plane];
2370 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002371 u32 offset, offset_aligned, alignment;
2372
2373 alignment = intel_surf_alignment(dev_priv, fb_modifier);
2374 if (alignment)
2375 alignment--;
2376
Ville Syrjäläb5c65332016-01-12 21:08:31 +02002377 if (fb_modifier != DRM_FORMAT_MOD_NONE) {
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002378 unsigned int tile_size, tile_width, tile_height;
2379 unsigned int tile_rows, tiles, pitch_tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002380
Ville Syrjäläd8433102016-01-12 21:08:35 +02002381 tile_size = intel_tile_size(dev_priv);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002382 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2383 fb_modifier, cpp);
2384
2385 if (intel_rotation_90_or_270(rotation)) {
2386 pitch_tiles = pitch / tile_height;
2387 swap(tile_width, tile_height);
2388 } else {
2389 pitch_tiles = pitch / (tile_width * cpp);
2390 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002391
Ville Syrjäläd8433102016-01-12 21:08:35 +02002392 tile_rows = *y / tile_height;
2393 *y %= tile_height;
Chris Wilsonbc752862013-02-21 20:04:31 +00002394
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002395 tiles = *x / tile_width;
2396 *x %= tile_width;
Ville Syrjäläd8433102016-01-12 21:08:35 +02002397
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002398 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2399 offset_aligned = offset & ~alignment;
Chris Wilsonbc752862013-02-21 20:04:31 +00002400
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002401 intel_adjust_tile_offset(x, y, tile_width, tile_height,
2402 tile_size, pitch_tiles,
2403 offset, offset_aligned);
2404 } else {
Chris Wilsonbc752862013-02-21 20:04:31 +00002405 offset = *y * pitch + *x * cpp;
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002406 offset_aligned = offset & ~alignment;
2407
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002408 *y = (offset & alignment) / pitch;
2409 *x = ((offset & alignment) - *y * pitch) / cpp;
Chris Wilsonbc752862013-02-21 20:04:31 +00002410 }
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002411
2412 return offset_aligned;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002413}
2414
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002415static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002416{
2417 switch (format) {
2418 case DISPPLANE_8BPP:
2419 return DRM_FORMAT_C8;
2420 case DISPPLANE_BGRX555:
2421 return DRM_FORMAT_XRGB1555;
2422 case DISPPLANE_BGRX565:
2423 return DRM_FORMAT_RGB565;
2424 default:
2425 case DISPPLANE_BGRX888:
2426 return DRM_FORMAT_XRGB8888;
2427 case DISPPLANE_RGBX888:
2428 return DRM_FORMAT_XBGR8888;
2429 case DISPPLANE_BGRX101010:
2430 return DRM_FORMAT_XRGB2101010;
2431 case DISPPLANE_RGBX101010:
2432 return DRM_FORMAT_XBGR2101010;
2433 }
2434}
2435
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002436static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2437{
2438 switch (format) {
2439 case PLANE_CTL_FORMAT_RGB_565:
2440 return DRM_FORMAT_RGB565;
2441 default:
2442 case PLANE_CTL_FORMAT_XRGB_8888:
2443 if (rgb_order) {
2444 if (alpha)
2445 return DRM_FORMAT_ABGR8888;
2446 else
2447 return DRM_FORMAT_XBGR8888;
2448 } else {
2449 if (alpha)
2450 return DRM_FORMAT_ARGB8888;
2451 else
2452 return DRM_FORMAT_XRGB8888;
2453 }
2454 case PLANE_CTL_FORMAT_XRGB_2101010:
2455 if (rgb_order)
2456 return DRM_FORMAT_XBGR2101010;
2457 else
2458 return DRM_FORMAT_XRGB2101010;
2459 }
2460}
2461
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002462static bool
Daniel Vetterf6936e22015-03-26 12:17:05 +01002463intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2464 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002465{
2466 struct drm_device *dev = crtc->base.dev;
Paulo Zanoni3badb492015-09-23 12:52:23 -03002467 struct drm_i915_private *dev_priv = to_i915(dev);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002468 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002469 struct drm_i915_gem_object *obj = NULL;
2470 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Damien Lespiau2d140302015-02-05 17:22:18 +00002471 struct drm_framebuffer *fb = &plane_config->fb->base;
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002472 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2473 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2474 PAGE_SIZE);
2475
2476 size_aligned -= base_aligned;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002477
Chris Wilsonff2652e2014-03-10 08:07:02 +00002478 if (plane_config->size == 0)
2479 return false;
2480
Paulo Zanoni3badb492015-09-23 12:52:23 -03002481 /* If the FB is too big, just don't use it since fbdev is not very
2482 * important and we should probably use that space with FBC or other
2483 * features. */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002484 if (size_aligned * 2 > ggtt->stolen_usable_size)
Paulo Zanoni3badb492015-09-23 12:52:23 -03002485 return false;
2486
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002487 mutex_lock(&dev->struct_mutex);
2488
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002489 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2490 base_aligned,
2491 base_aligned,
2492 size_aligned);
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002493 if (!obj) {
2494 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002495 return false;
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002496 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002497
Damien Lespiau49af4492015-01-20 12:51:44 +00002498 obj->tiling_mode = plane_config->tiling;
2499 if (obj->tiling_mode == I915_TILING_X)
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002500 obj->stride = fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002501
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002502 mode_cmd.pixel_format = fb->pixel_format;
2503 mode_cmd.width = fb->width;
2504 mode_cmd.height = fb->height;
2505 mode_cmd.pitches[0] = fb->pitches[0];
Daniel Vetter18c52472015-02-10 17:16:09 +00002506 mode_cmd.modifier[0] = fb->modifier[0];
2507 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002508
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002509 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002510 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002511 DRM_DEBUG_KMS("intel fb init failed\n");
2512 goto out_unref_obj;
2513 }
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002514
Jesse Barnes46f297f2014-03-07 08:57:48 -08002515 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002516
Daniel Vetterf6936e22015-03-26 12:17:05 +01002517 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002518 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002519
2520out_unref_obj:
2521 drm_gem_object_unreference(&obj->base);
2522 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002523 return false;
2524}
2525
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002526static void
Daniel Vetterf6936e22015-03-26 12:17:05 +01002527intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2528 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002529{
2530 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnesd9ceb812014-10-09 12:57:43 -07002531 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002532 struct drm_crtc *c;
2533 struct intel_crtc *i;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002534 struct drm_i915_gem_object *obj;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002535 struct drm_plane *primary = intel_crtc->base.primary;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002536 struct drm_plane_state *plane_state = primary->state;
Matt Roper200757f2015-12-03 11:37:36 -08002537 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
2538 struct intel_plane *intel_plane = to_intel_plane(primary);
Matt Roper0a8d8a82015-12-03 11:37:38 -08002539 struct intel_plane_state *intel_state =
2540 to_intel_plane_state(plane_state);
Daniel Vetter88595ac2015-03-26 12:42:24 +01002541 struct drm_framebuffer *fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002542
Damien Lespiau2d140302015-02-05 17:22:18 +00002543 if (!plane_config->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002544 return;
2545
Daniel Vetterf6936e22015-03-26 12:17:05 +01002546 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002547 fb = &plane_config->fb->base;
2548 goto valid_fb;
Damien Lespiauf55548b2015-02-05 18:30:20 +00002549 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002550
Damien Lespiau2d140302015-02-05 17:22:18 +00002551 kfree(plane_config->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002552
2553 /*
2554 * Failed to alloc the obj, check to see if we should share
2555 * an fb with another CRTC instead
2556 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002557 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002558 i = to_intel_crtc(c);
2559
2560 if (c == &intel_crtc->base)
2561 continue;
2562
Matt Roper2ff8fde2014-07-08 07:50:07 -07002563 if (!i->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002564 continue;
2565
Daniel Vetter88595ac2015-03-26 12:42:24 +01002566 fb = c->primary->fb;
2567 if (!fb)
Matt Roper2ff8fde2014-07-08 07:50:07 -07002568 continue;
2569
Daniel Vetter88595ac2015-03-26 12:42:24 +01002570 obj = intel_fb_obj(fb);
Matt Roper2ff8fde2014-07-08 07:50:07 -07002571 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002572 drm_framebuffer_reference(fb);
2573 goto valid_fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002574 }
2575 }
Daniel Vetter88595ac2015-03-26 12:42:24 +01002576
Matt Roper200757f2015-12-03 11:37:36 -08002577 /*
2578 * We've failed to reconstruct the BIOS FB. Current display state
2579 * indicates that the primary plane is visible, but has a NULL FB,
2580 * which will lead to problems later if we don't fix it up. The
2581 * simplest solution is to just disable the primary plane now and
2582 * pretend the BIOS never had it enabled.
2583 */
2584 to_intel_plane_state(plane_state)->visible = false;
2585 crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
Ville Syrjälä2622a082016-03-09 19:07:26 +02002586 intel_pre_disable_primary_noatomic(&intel_crtc->base);
Matt Roper200757f2015-12-03 11:37:36 -08002587 intel_plane->disable_plane(primary, &intel_crtc->base);
2588
Daniel Vetter88595ac2015-03-26 12:42:24 +01002589 return;
2590
2591valid_fb:
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002592 plane_state->src_x = 0;
2593 plane_state->src_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002594 plane_state->src_w = fb->width << 16;
2595 plane_state->src_h = fb->height << 16;
2596
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002597 plane_state->crtc_x = 0;
2598 plane_state->crtc_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002599 plane_state->crtc_w = fb->width;
2600 plane_state->crtc_h = fb->height;
2601
Matt Roper0a8d8a82015-12-03 11:37:38 -08002602 intel_state->src.x1 = plane_state->src_x;
2603 intel_state->src.y1 = plane_state->src_y;
2604 intel_state->src.x2 = plane_state->src_x + plane_state->src_w;
2605 intel_state->src.y2 = plane_state->src_y + plane_state->src_h;
2606 intel_state->dst.x1 = plane_state->crtc_x;
2607 intel_state->dst.y1 = plane_state->crtc_y;
2608 intel_state->dst.x2 = plane_state->crtc_x + plane_state->crtc_w;
2609 intel_state->dst.y2 = plane_state->crtc_y + plane_state->crtc_h;
2610
Daniel Vetter88595ac2015-03-26 12:42:24 +01002611 obj = intel_fb_obj(fb);
2612 if (obj->tiling_mode != I915_TILING_NONE)
2613 dev_priv->preserve_bios_swizzle = true;
2614
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002615 drm_framebuffer_reference(fb);
2616 primary->fb = primary->state->fb = fb;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002617 primary->crtc = primary->state->crtc = &intel_crtc->base;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002618 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
Ville Syrjäläa9ff8712015-06-24 21:59:34 +03002619 obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002620}
2621
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002622static void i9xx_update_primary_plane(struct drm_plane *primary,
2623 const struct intel_crtc_state *crtc_state,
2624 const struct intel_plane_state *plane_state)
Jesse Barnes81255562010-08-02 12:07:50 -07002625{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002626 struct drm_device *dev = primary->dev;
Jesse Barnes81255562010-08-02 12:07:50 -07002627 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002628 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2629 struct drm_framebuffer *fb = plane_state->base.fb;
2630 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Jesse Barnes81255562010-08-02 12:07:50 -07002631 int plane = intel_crtc->plane;
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002632 u32 linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002633 u32 dspcntr;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002634 i915_reg_t reg = DSPCNTR(plane);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002635 unsigned int rotation = plane_state->base.rotation;
Ville Syrjäläac484962016-01-20 21:05:26 +02002636 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002637 int x = plane_state->src.x1 >> 16;
2638 int y = plane_state->src.y1 >> 16;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002639
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002640 dspcntr = DISPPLANE_GAMMA_ENABLE;
2641
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002642 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002643
2644 if (INTEL_INFO(dev)->gen < 4) {
2645 if (intel_crtc->pipe == PIPE_B)
2646 dspcntr |= DISPPLANE_SEL_PIPE_B;
2647
2648 /* pipesrc and dspsize control the size that is scaled from,
2649 * which should always be the user's requested size.
2650 */
2651 I915_WRITE(DSPSIZE(plane),
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002652 ((crtc_state->pipe_src_h - 1) << 16) |
2653 (crtc_state->pipe_src_w - 1));
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002654 I915_WRITE(DSPPOS(plane), 0);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002655 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2656 I915_WRITE(PRIMSIZE(plane),
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002657 ((crtc_state->pipe_src_h - 1) << 16) |
2658 (crtc_state->pipe_src_w - 1));
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002659 I915_WRITE(PRIMPOS(plane), 0);
2660 I915_WRITE(PRIMCNSTALPHA(plane), 0);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002661 }
2662
Ville Syrjälä57779d02012-10-31 17:50:14 +02002663 switch (fb->pixel_format) {
2664 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002665 dspcntr |= DISPPLANE_8BPP;
2666 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002667 case DRM_FORMAT_XRGB1555:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002668 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002669 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002670 case DRM_FORMAT_RGB565:
2671 dspcntr |= DISPPLANE_BGRX565;
2672 break;
2673 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002674 dspcntr |= DISPPLANE_BGRX888;
2675 break;
2676 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002677 dspcntr |= DISPPLANE_RGBX888;
2678 break;
2679 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002680 dspcntr |= DISPPLANE_BGRX101010;
2681 break;
2682 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002683 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002684 break;
2685 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002686 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002687 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002688
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002689 if (INTEL_INFO(dev)->gen >= 4 &&
2690 obj->tiling_mode != I915_TILING_NONE)
2691 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07002692
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002693 if (IS_G4X(dev))
2694 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2695
Ville Syrjäläac484962016-01-20 21:05:26 +02002696 linear_offset = y * fb->pitches[0] + x * cpp;
Jesse Barnes81255562010-08-02 12:07:50 -07002697
Daniel Vetterc2c75132012-07-05 12:17:30 +02002698 if (INTEL_INFO(dev)->gen >= 4) {
2699 intel_crtc->dspaddr_offset =
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002700 intel_compute_tile_offset(&x, &y, fb, 0,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002701 fb->pitches[0], rotation);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002702 linear_offset -= intel_crtc->dspaddr_offset;
2703 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002704 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002705 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002706
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002707 if (rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302708 dspcntr |= DISPPLANE_ROTATE_180;
2709
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002710 x += (crtc_state->pipe_src_w - 1);
2711 y += (crtc_state->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302712
2713 /* Finding the last pixel of the last line of the display
2714 data and adding to linear_offset*/
2715 linear_offset +=
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002716 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
Ville Syrjäläac484962016-01-20 21:05:26 +02002717 (crtc_state->pipe_src_w - 1) * cpp;
Sonika Jindal48404c12014-08-22 14:06:04 +05302718 }
2719
Paulo Zanoni2db33662015-09-14 15:20:03 -03002720 intel_crtc->adjusted_x = x;
2721 intel_crtc->adjusted_y = y;
2722
Sonika Jindal48404c12014-08-22 14:06:04 +05302723 I915_WRITE(reg, dspcntr);
2724
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002725 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002726 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002727 I915_WRITE(DSPSURF(plane),
2728 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002729 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002730 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002731 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002732 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002733 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002734}
2735
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002736static void i9xx_disable_primary_plane(struct drm_plane *primary,
2737 struct drm_crtc *crtc)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002738{
2739 struct drm_device *dev = crtc->dev;
2740 struct drm_i915_private *dev_priv = dev->dev_private;
2741 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002742 int plane = intel_crtc->plane;
2743
2744 I915_WRITE(DSPCNTR(plane), 0);
2745 if (INTEL_INFO(dev_priv)->gen >= 4)
2746 I915_WRITE(DSPSURF(plane), 0);
2747 else
2748 I915_WRITE(DSPADDR(plane), 0);
2749 POSTING_READ(DSPCNTR(plane));
2750}
2751
2752static void ironlake_update_primary_plane(struct drm_plane *primary,
2753 const struct intel_crtc_state *crtc_state,
2754 const struct intel_plane_state *plane_state)
2755{
2756 struct drm_device *dev = primary->dev;
2757 struct drm_i915_private *dev_priv = dev->dev_private;
2758 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2759 struct drm_framebuffer *fb = plane_state->base.fb;
2760 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002761 int plane = intel_crtc->plane;
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002762 u32 linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002763 u32 dspcntr;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002764 i915_reg_t reg = DSPCNTR(plane);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002765 unsigned int rotation = plane_state->base.rotation;
Ville Syrjäläac484962016-01-20 21:05:26 +02002766 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002767 int x = plane_state->src.x1 >> 16;
2768 int y = plane_state->src.y1 >> 16;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002769
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002770 dspcntr = DISPPLANE_GAMMA_ENABLE;
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002771 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002772
2773 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2774 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
2775
Ville Syrjälä57779d02012-10-31 17:50:14 +02002776 switch (fb->pixel_format) {
2777 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002778 dspcntr |= DISPPLANE_8BPP;
2779 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002780 case DRM_FORMAT_RGB565:
2781 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002782 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002783 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002784 dspcntr |= DISPPLANE_BGRX888;
2785 break;
2786 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002787 dspcntr |= DISPPLANE_RGBX888;
2788 break;
2789 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002790 dspcntr |= DISPPLANE_BGRX101010;
2791 break;
2792 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002793 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002794 break;
2795 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002796 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002797 }
2798
2799 if (obj->tiling_mode != I915_TILING_NONE)
2800 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002801
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002802 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002803 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002804
Ville Syrjäläac484962016-01-20 21:05:26 +02002805 linear_offset = y * fb->pitches[0] + x * cpp;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002806 intel_crtc->dspaddr_offset =
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002807 intel_compute_tile_offset(&x, &y, fb, 0,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002808 fb->pitches[0], rotation);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002809 linear_offset -= intel_crtc->dspaddr_offset;
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002810 if (rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302811 dspcntr |= DISPPLANE_ROTATE_180;
2812
2813 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002814 x += (crtc_state->pipe_src_w - 1);
2815 y += (crtc_state->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302816
2817 /* Finding the last pixel of the last line of the display
2818 data and adding to linear_offset*/
2819 linear_offset +=
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002820 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
Ville Syrjäläac484962016-01-20 21:05:26 +02002821 (crtc_state->pipe_src_w - 1) * cpp;
Sonika Jindal48404c12014-08-22 14:06:04 +05302822 }
2823 }
2824
Paulo Zanoni2db33662015-09-14 15:20:03 -03002825 intel_crtc->adjusted_x = x;
2826 intel_crtc->adjusted_y = y;
2827
Sonika Jindal48404c12014-08-22 14:06:04 +05302828 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002829
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002830 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002831 I915_WRITE(DSPSURF(plane),
2832 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002833 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002834 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2835 } else {
2836 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2837 I915_WRITE(DSPLINOFF(plane), linear_offset);
2838 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002839 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002840}
2841
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002842u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
2843 uint64_t fb_modifier, uint32_t pixel_format)
Damien Lespiaub3218032015-02-27 11:15:18 +00002844{
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002845 if (fb_modifier == DRM_FORMAT_MOD_NONE) {
2846 return 64;
2847 } else {
2848 int cpp = drm_format_plane_cpp(pixel_format, 0);
Damien Lespiaub3218032015-02-27 11:15:18 +00002849
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002850 return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
Damien Lespiaub3218032015-02-27 11:15:18 +00002851 }
2852}
2853
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002854u32 intel_plane_obj_offset(struct intel_plane *intel_plane,
2855 struct drm_i915_gem_object *obj,
2856 unsigned int plane)
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002857{
Daniel Vetterce7f1722015-10-14 16:51:06 +02002858 struct i915_ggtt_view view;
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002859 struct i915_vma *vma;
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002860 u64 offset;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002861
Ville Syrjäläe7941292016-01-19 18:23:17 +02002862 intel_fill_fb_ggtt_view(&view, intel_plane->base.state->fb,
Ville Syrjälä3465c582016-02-15 22:54:43 +02002863 intel_plane->base.state->rotation);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002864
Daniel Vetterce7f1722015-10-14 16:51:06 +02002865 vma = i915_gem_obj_to_ggtt_view(obj, &view);
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002866 if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
Daniel Vetterce7f1722015-10-14 16:51:06 +02002867 view.type))
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002868 return -1;
2869
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002870 offset = vma->node.start;
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002871
2872 if (plane == 1) {
Ville Syrjälä7723f47d2016-01-20 21:05:22 +02002873 offset += vma->ggtt_view.params.rotated.uv_start_page *
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002874 PAGE_SIZE;
2875 }
2876
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002877 WARN_ON(upper_32_bits(offset));
2878
2879 return lower_32_bits(offset);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002880}
2881
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002882static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
2883{
2884 struct drm_device *dev = intel_crtc->base.dev;
2885 struct drm_i915_private *dev_priv = dev->dev_private;
2886
2887 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
2888 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
2889 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002890}
2891
Chandra Kondurua1b22782015-04-07 15:28:45 -07002892/*
2893 * This function detaches (aka. unbinds) unused scalers in hardware
2894 */
Maarten Lankhorst05832362015-06-15 12:33:48 +02002895static void skl_detach_scalers(struct intel_crtc *intel_crtc)
Chandra Kondurua1b22782015-04-07 15:28:45 -07002896{
Chandra Kondurua1b22782015-04-07 15:28:45 -07002897 struct intel_crtc_scaler_state *scaler_state;
2898 int i;
2899
Chandra Kondurua1b22782015-04-07 15:28:45 -07002900 scaler_state = &intel_crtc->config->scaler_state;
2901
2902 /* loop through and disable scalers that aren't in use */
2903 for (i = 0; i < intel_crtc->num_scalers; i++) {
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002904 if (!scaler_state->scalers[i].in_use)
2905 skl_detach_scaler(intel_crtc, i);
Chandra Kondurua1b22782015-04-07 15:28:45 -07002906 }
2907}
2908
Chandra Konduru6156a452015-04-27 13:48:39 -07002909u32 skl_plane_ctl_format(uint32_t pixel_format)
2910{
Chandra Konduru6156a452015-04-27 13:48:39 -07002911 switch (pixel_format) {
Damien Lespiaud161cf72015-05-12 16:13:17 +01002912 case DRM_FORMAT_C8:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002913 return PLANE_CTL_FORMAT_INDEXED;
Chandra Konduru6156a452015-04-27 13:48:39 -07002914 case DRM_FORMAT_RGB565:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002915 return PLANE_CTL_FORMAT_RGB_565;
Chandra Konduru6156a452015-04-27 13:48:39 -07002916 case DRM_FORMAT_XBGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002917 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
Chandra Konduru6156a452015-04-27 13:48:39 -07002918 case DRM_FORMAT_XRGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002919 return PLANE_CTL_FORMAT_XRGB_8888;
Chandra Konduru6156a452015-04-27 13:48:39 -07002920 /*
2921 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2922 * to be already pre-multiplied. We need to add a knob (or a different
2923 * DRM_FORMAT) for user-space to configure that.
2924 */
2925 case DRM_FORMAT_ABGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002926 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
Chandra Konduru6156a452015-04-27 13:48:39 -07002927 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002928 case DRM_FORMAT_ARGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002929 return PLANE_CTL_FORMAT_XRGB_8888 |
Chandra Konduru6156a452015-04-27 13:48:39 -07002930 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002931 case DRM_FORMAT_XRGB2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002932 return PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002933 case DRM_FORMAT_XBGR2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002934 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002935 case DRM_FORMAT_YUYV:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002936 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
Chandra Konduru6156a452015-04-27 13:48:39 -07002937 case DRM_FORMAT_YVYU:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002938 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
Chandra Konduru6156a452015-04-27 13:48:39 -07002939 case DRM_FORMAT_UYVY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002940 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002941 case DRM_FORMAT_VYUY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002942 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002943 default:
Damien Lespiau4249eee2015-05-12 16:13:16 +01002944 MISSING_CASE(pixel_format);
Chandra Konduru6156a452015-04-27 13:48:39 -07002945 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002946
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002947 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002948}
2949
2950u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
2951{
Chandra Konduru6156a452015-04-27 13:48:39 -07002952 switch (fb_modifier) {
2953 case DRM_FORMAT_MOD_NONE:
2954 break;
2955 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002956 return PLANE_CTL_TILED_X;
Chandra Konduru6156a452015-04-27 13:48:39 -07002957 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002958 return PLANE_CTL_TILED_Y;
Chandra Konduru6156a452015-04-27 13:48:39 -07002959 case I915_FORMAT_MOD_Yf_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002960 return PLANE_CTL_TILED_YF;
Chandra Konduru6156a452015-04-27 13:48:39 -07002961 default:
2962 MISSING_CASE(fb_modifier);
2963 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002964
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002965 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002966}
2967
2968u32 skl_plane_ctl_rotation(unsigned int rotation)
2969{
Chandra Konduru6156a452015-04-27 13:48:39 -07002970 switch (rotation) {
2971 case BIT(DRM_ROTATE_0):
2972 break;
Sonika Jindal1e8df162015-05-20 13:40:48 +05302973 /*
2974 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
2975 * while i915 HW rotation is clockwise, thats why this swapping.
2976 */
Chandra Konduru6156a452015-04-27 13:48:39 -07002977 case BIT(DRM_ROTATE_90):
Sonika Jindal1e8df162015-05-20 13:40:48 +05302978 return PLANE_CTL_ROTATE_270;
Chandra Konduru6156a452015-04-27 13:48:39 -07002979 case BIT(DRM_ROTATE_180):
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002980 return PLANE_CTL_ROTATE_180;
Chandra Konduru6156a452015-04-27 13:48:39 -07002981 case BIT(DRM_ROTATE_270):
Sonika Jindal1e8df162015-05-20 13:40:48 +05302982 return PLANE_CTL_ROTATE_90;
Chandra Konduru6156a452015-04-27 13:48:39 -07002983 default:
2984 MISSING_CASE(rotation);
2985 }
2986
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002987 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002988}
2989
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002990static void skylake_update_primary_plane(struct drm_plane *plane,
2991 const struct intel_crtc_state *crtc_state,
2992 const struct intel_plane_state *plane_state)
Damien Lespiau70d21f02013-07-03 21:06:04 +01002993{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002994 struct drm_device *dev = plane->dev;
Damien Lespiau70d21f02013-07-03 21:06:04 +01002995 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002996 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2997 struct drm_framebuffer *fb = plane_state->base.fb;
2998 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Damien Lespiau70d21f02013-07-03 21:06:04 +01002999 int pipe = intel_crtc->pipe;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303000 u32 plane_ctl, stride_div, stride;
3001 u32 tile_height, plane_offset, plane_size;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003002 unsigned int rotation = plane_state->base.rotation;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303003 int x_offset, y_offset;
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02003004 u32 surf_addr;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003005 int scaler_id = plane_state->scaler_id;
3006 int src_x = plane_state->src.x1 >> 16;
3007 int src_y = plane_state->src.y1 >> 16;
3008 int src_w = drm_rect_width(&plane_state->src) >> 16;
3009 int src_h = drm_rect_height(&plane_state->src) >> 16;
3010 int dst_x = plane_state->dst.x1;
3011 int dst_y = plane_state->dst.y1;
3012 int dst_w = drm_rect_width(&plane_state->dst);
3013 int dst_h = drm_rect_height(&plane_state->dst);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003014
3015 plane_ctl = PLANE_CTL_ENABLE |
3016 PLANE_CTL_PIPE_GAMMA_ENABLE |
3017 PLANE_CTL_PIPE_CSC_ENABLE;
3018
Chandra Konduru6156a452015-04-27 13:48:39 -07003019 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3020 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003021 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Chandra Konduru6156a452015-04-27 13:48:39 -07003022 plane_ctl |= skl_plane_ctl_rotation(rotation);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003023
Ville Syrjälä7b49f942016-01-12 21:08:32 +02003024 stride_div = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
Damien Lespiaub3218032015-02-27 11:15:18 +00003025 fb->pixel_format);
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01003026 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj, 0);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303027
Paulo Zanonia42e5a22015-09-30 17:05:43 -03003028 WARN_ON(drm_rect_width(&plane_state->src) == 0);
Chandra Konduru6156a452015-04-27 13:48:39 -07003029
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303030 if (intel_rotation_90_or_270(rotation)) {
Ville Syrjälä832be822016-01-12 21:08:33 +02003031 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
3032
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303033 /* stride = Surface height in tiles */
Ville Syrjälä832be822016-01-12 21:08:33 +02003034 tile_height = intel_tile_height(dev_priv, fb->modifier[0], cpp);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303035 stride = DIV_ROUND_UP(fb->height, tile_height);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003036 x_offset = stride * tile_height - src_y - src_h;
3037 y_offset = src_x;
Chandra Konduru6156a452015-04-27 13:48:39 -07003038 plane_size = (src_w - 1) << 16 | (src_h - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303039 } else {
3040 stride = fb->pitches[0] / stride_div;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003041 x_offset = src_x;
3042 y_offset = src_y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003043 plane_size = (src_h - 1) << 16 | (src_w - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303044 }
3045 plane_offset = y_offset << 16 | x_offset;
Damien Lespiaub3218032015-02-27 11:15:18 +00003046
Paulo Zanoni2db33662015-09-14 15:20:03 -03003047 intel_crtc->adjusted_x = x_offset;
3048 intel_crtc->adjusted_y = y_offset;
3049
Damien Lespiau70d21f02013-07-03 21:06:04 +01003050 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303051 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3052 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3053 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
Chandra Konduru6156a452015-04-27 13:48:39 -07003054
3055 if (scaler_id >= 0) {
3056 uint32_t ps_ctrl = 0;
3057
3058 WARN_ON(!dst_w || !dst_h);
3059 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3060 crtc_state->scaler_state.scalers[scaler_id].mode;
3061 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3062 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3063 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3064 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3065 I915_WRITE(PLANE_POS(pipe, 0), 0);
3066 } else {
3067 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3068 }
3069
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003070 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003071
3072 POSTING_READ(PLANE_SURF(pipe, 0));
3073}
3074
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003075static void skylake_disable_primary_plane(struct drm_plane *primary,
3076 struct drm_crtc *crtc)
3077{
3078 struct drm_device *dev = crtc->dev;
3079 struct drm_i915_private *dev_priv = dev->dev_private;
3080 int pipe = to_intel_crtc(crtc)->pipe;
3081
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003082 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3083 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3084 POSTING_READ(PLANE_SURF(pipe, 0));
3085}
3086
Jesse Barnes17638cd2011-06-24 12:19:23 -07003087/* Assume fb object is pinned & idle & fenced and just update base pointers */
3088static int
3089intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3090 int x, int y, enum mode_set_atomic state)
3091{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003092 /* Support for kgdboc is disabled, this needs a major rework. */
3093 DRM_ERROR("legacy panic handler not supported any more.\n");
Jesse Barnes17638cd2011-06-24 12:19:23 -07003094
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003095 return -ENODEV;
Jesse Barnes81255562010-08-02 12:07:50 -07003096}
3097
Ville Syrjälä75147472014-11-24 18:28:11 +02003098static void intel_update_primary_planes(struct drm_device *dev)
3099{
Ville Syrjälä75147472014-11-24 18:28:11 +02003100 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003101
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003102 for_each_crtc(dev, crtc) {
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003103 struct intel_plane *plane = to_intel_plane(crtc->primary);
3104 struct intel_plane_state *plane_state;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003105
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003106 drm_modeset_lock_crtc(crtc, &plane->base);
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003107 plane_state = to_intel_plane_state(plane->base.state);
3108
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003109 if (plane_state->visible)
3110 plane->update_plane(&plane->base,
3111 to_intel_crtc_state(crtc->state),
3112 plane_state);
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003113
3114 drm_modeset_unlock_crtc(crtc);
Ville Syrjälä96a02912013-02-18 19:08:49 +02003115 }
3116}
3117
Chris Wilsonc0336662016-05-06 15:40:21 +01003118void intel_prepare_reset(struct drm_i915_private *dev_priv)
Ville Syrjälä75147472014-11-24 18:28:11 +02003119{
3120 /* no reset support for gen2 */
Chris Wilsonc0336662016-05-06 15:40:21 +01003121 if (IS_GEN2(dev_priv))
Ville Syrjälä75147472014-11-24 18:28:11 +02003122 return;
3123
3124 /* reset doesn't touch the display */
Chris Wilsonc0336662016-05-06 15:40:21 +01003125 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
Ville Syrjälä75147472014-11-24 18:28:11 +02003126 return;
3127
Chris Wilsonc0336662016-05-06 15:40:21 +01003128 drm_modeset_lock_all(dev_priv->dev);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003129 /*
3130 * Disabling the crtcs gracefully seems nicer. Also the
3131 * g33 docs say we should at least disable all the planes.
3132 */
Chris Wilsonc0336662016-05-06 15:40:21 +01003133 intel_display_suspend(dev_priv->dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003134}
3135
Chris Wilsonc0336662016-05-06 15:40:21 +01003136void intel_finish_reset(struct drm_i915_private *dev_priv)
Ville Syrjälä75147472014-11-24 18:28:11 +02003137{
Ville Syrjälä75147472014-11-24 18:28:11 +02003138 /* no reset support for gen2 */
Chris Wilsonc0336662016-05-06 15:40:21 +01003139 if (IS_GEN2(dev_priv))
Ville Syrjälä75147472014-11-24 18:28:11 +02003140 return;
3141
3142 /* reset doesn't touch the display */
Chris Wilsonc0336662016-05-06 15:40:21 +01003143 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv)) {
Ville Syrjälä75147472014-11-24 18:28:11 +02003144 /*
3145 * Flips in the rings have been nuked by the reset,
3146 * so update the base address of all primary
3147 * planes to the the last fb to make sure we're
3148 * showing the correct fb after a reset.
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003149 *
3150 * FIXME: Atomic will make this obsolete since we won't schedule
3151 * CS-based flips (which might get lost in gpu resets) any more.
Ville Syrjälä75147472014-11-24 18:28:11 +02003152 */
Chris Wilsonc0336662016-05-06 15:40:21 +01003153 intel_update_primary_planes(dev_priv->dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003154 return;
3155 }
3156
3157 /*
3158 * The display has been reset as well,
3159 * so need a full re-initialization.
3160 */
3161 intel_runtime_pm_disable_interrupts(dev_priv);
3162 intel_runtime_pm_enable_interrupts(dev_priv);
3163
Chris Wilsonc0336662016-05-06 15:40:21 +01003164 intel_modeset_init_hw(dev_priv->dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003165
3166 spin_lock_irq(&dev_priv->irq_lock);
3167 if (dev_priv->display.hpd_irq_setup)
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01003168 dev_priv->display.hpd_irq_setup(dev_priv);
Ville Syrjälä75147472014-11-24 18:28:11 +02003169 spin_unlock_irq(&dev_priv->irq_lock);
3170
Chris Wilsonc0336662016-05-06 15:40:21 +01003171 intel_display_resume(dev_priv->dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003172
3173 intel_hpd_init(dev_priv);
3174
Chris Wilsonc0336662016-05-06 15:40:21 +01003175 drm_modeset_unlock_all(dev_priv->dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003176}
3177
Chris Wilson7d5e3792014-03-04 13:15:08 +00003178static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3179{
Maarten Lankhorst68858432016-05-17 15:07:52 +02003180 return !list_empty_careful(&to_intel_crtc(crtc)->flip_work);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003181}
3182
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003183static void intel_update_pipe_config(struct intel_crtc *crtc,
3184 struct intel_crtc_state *old_crtc_state)
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003185{
3186 struct drm_device *dev = crtc->base.dev;
3187 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003188 struct intel_crtc_state *pipe_config =
3189 to_intel_crtc_state(crtc->base.state);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003190
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003191 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
3192 crtc->base.mode = crtc->base.state->mode;
3193
3194 DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
3195 old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
3196 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003197
3198 /*
3199 * Update pipe size and adjust fitter if needed: the reason for this is
3200 * that in compute_mode_changes we check the native mode (not the pfit
3201 * mode) to see if we can flip rather than do a full mode set. In the
3202 * fastboot case, we'll flip, but if we don't update the pipesrc and
3203 * pfit state, we'll end up with a big fb scanned out into the wrong
3204 * sized surface.
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003205 */
3206
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003207 I915_WRITE(PIPESRC(crtc->pipe),
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003208 ((pipe_config->pipe_src_w - 1) << 16) |
3209 (pipe_config->pipe_src_h - 1));
3210
3211 /* on skylake this is done by detaching scalers */
3212 if (INTEL_INFO(dev)->gen >= 9) {
3213 skl_detach_scalers(crtc);
3214
3215 if (pipe_config->pch_pfit.enabled)
3216 skylake_pfit_enable(crtc);
3217 } else if (HAS_PCH_SPLIT(dev)) {
3218 if (pipe_config->pch_pfit.enabled)
3219 ironlake_pfit_enable(crtc);
3220 else if (old_crtc_state->pch_pfit.enabled)
3221 ironlake_pfit_disable(crtc, true);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003222 }
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003223}
3224
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003225static void intel_fdi_normal_train(struct drm_crtc *crtc)
3226{
3227 struct drm_device *dev = crtc->dev;
3228 struct drm_i915_private *dev_priv = dev->dev_private;
3229 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3230 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003231 i915_reg_t reg;
3232 u32 temp;
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003233
3234 /* enable normal train */
3235 reg = FDI_TX_CTL(pipe);
3236 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07003237 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003238 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3239 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003240 } else {
3241 temp &= ~FDI_LINK_TRAIN_NONE;
3242 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003243 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003244 I915_WRITE(reg, temp);
3245
3246 reg = FDI_RX_CTL(pipe);
3247 temp = I915_READ(reg);
3248 if (HAS_PCH_CPT(dev)) {
3249 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3250 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3251 } else {
3252 temp &= ~FDI_LINK_TRAIN_NONE;
3253 temp |= FDI_LINK_TRAIN_NONE;
3254 }
3255 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3256
3257 /* wait one idle pattern time */
3258 POSTING_READ(reg);
3259 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003260
3261 /* IVB wants error correction enabled */
3262 if (IS_IVYBRIDGE(dev))
3263 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3264 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003265}
3266
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003267/* The FDI link training functions for ILK/Ibexpeak. */
3268static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3269{
3270 struct drm_device *dev = crtc->dev;
3271 struct drm_i915_private *dev_priv = dev->dev_private;
3272 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3273 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003274 i915_reg_t reg;
3275 u32 temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003276
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003277 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003278 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003279
Adam Jacksone1a44742010-06-25 15:32:14 -04003280 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3281 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003282 reg = FDI_RX_IMR(pipe);
3283 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003284 temp &= ~FDI_RX_SYMBOL_LOCK;
3285 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003286 I915_WRITE(reg, temp);
3287 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003288 udelay(150);
3289
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003290 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003291 reg = FDI_TX_CTL(pipe);
3292 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003293 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003294 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003295 temp &= ~FDI_LINK_TRAIN_NONE;
3296 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003297 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003298
Chris Wilson5eddb702010-09-11 13:48:45 +01003299 reg = FDI_RX_CTL(pipe);
3300 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003301 temp &= ~FDI_LINK_TRAIN_NONE;
3302 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003303 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3304
3305 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003306 udelay(150);
3307
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003308 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003309 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3310 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3311 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003312
Chris Wilson5eddb702010-09-11 13:48:45 +01003313 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003314 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003315 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003316 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3317
3318 if ((temp & FDI_RX_BIT_LOCK)) {
3319 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003320 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003321 break;
3322 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003323 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003324 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003325 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003326
3327 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003328 reg = FDI_TX_CTL(pipe);
3329 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003330 temp &= ~FDI_LINK_TRAIN_NONE;
3331 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003332 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003333
Chris Wilson5eddb702010-09-11 13:48:45 +01003334 reg = FDI_RX_CTL(pipe);
3335 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003336 temp &= ~FDI_LINK_TRAIN_NONE;
3337 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003338 I915_WRITE(reg, temp);
3339
3340 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003341 udelay(150);
3342
Chris Wilson5eddb702010-09-11 13:48:45 +01003343 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003344 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003345 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003346 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3347
3348 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003349 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003350 DRM_DEBUG_KMS("FDI train 2 done.\n");
3351 break;
3352 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003353 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003354 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003355 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003356
3357 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003358
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003359}
3360
Akshay Joshi0206e352011-08-16 15:34:10 -04003361static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003362 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3363 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3364 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3365 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3366};
3367
3368/* The FDI link training functions for SNB/Cougarpoint. */
3369static void gen6_fdi_link_train(struct drm_crtc *crtc)
3370{
3371 struct drm_device *dev = crtc->dev;
3372 struct drm_i915_private *dev_priv = dev->dev_private;
3373 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3374 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003375 i915_reg_t reg;
3376 u32 temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003377
Adam Jacksone1a44742010-06-25 15:32:14 -04003378 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3379 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003380 reg = FDI_RX_IMR(pipe);
3381 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003382 temp &= ~FDI_RX_SYMBOL_LOCK;
3383 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003384 I915_WRITE(reg, temp);
3385
3386 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003387 udelay(150);
3388
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003389 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003390 reg = FDI_TX_CTL(pipe);
3391 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003392 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003393 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003394 temp &= ~FDI_LINK_TRAIN_NONE;
3395 temp |= FDI_LINK_TRAIN_PATTERN_1;
3396 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3397 /* SNB-B */
3398 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003399 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003400
Daniel Vetterd74cf322012-10-26 10:58:13 +02003401 I915_WRITE(FDI_RX_MISC(pipe),
3402 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3403
Chris Wilson5eddb702010-09-11 13:48:45 +01003404 reg = FDI_RX_CTL(pipe);
3405 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003406 if (HAS_PCH_CPT(dev)) {
3407 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3408 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3409 } else {
3410 temp &= ~FDI_LINK_TRAIN_NONE;
3411 temp |= FDI_LINK_TRAIN_PATTERN_1;
3412 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003413 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3414
3415 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003416 udelay(150);
3417
Akshay Joshi0206e352011-08-16 15:34:10 -04003418 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003419 reg = FDI_TX_CTL(pipe);
3420 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003421 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3422 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003423 I915_WRITE(reg, temp);
3424
3425 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003426 udelay(500);
3427
Sean Paulfa37d392012-03-02 12:53:39 -05003428 for (retry = 0; retry < 5; retry++) {
3429 reg = FDI_RX_IIR(pipe);
3430 temp = I915_READ(reg);
3431 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3432 if (temp & FDI_RX_BIT_LOCK) {
3433 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3434 DRM_DEBUG_KMS("FDI train 1 done.\n");
3435 break;
3436 }
3437 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003438 }
Sean Paulfa37d392012-03-02 12:53:39 -05003439 if (retry < 5)
3440 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003441 }
3442 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003443 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003444
3445 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003446 reg = FDI_TX_CTL(pipe);
3447 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003448 temp &= ~FDI_LINK_TRAIN_NONE;
3449 temp |= FDI_LINK_TRAIN_PATTERN_2;
3450 if (IS_GEN6(dev)) {
3451 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3452 /* SNB-B */
3453 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3454 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003455 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003456
Chris Wilson5eddb702010-09-11 13:48:45 +01003457 reg = FDI_RX_CTL(pipe);
3458 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003459 if (HAS_PCH_CPT(dev)) {
3460 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3461 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3462 } else {
3463 temp &= ~FDI_LINK_TRAIN_NONE;
3464 temp |= FDI_LINK_TRAIN_PATTERN_2;
3465 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003466 I915_WRITE(reg, temp);
3467
3468 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003469 udelay(150);
3470
Akshay Joshi0206e352011-08-16 15:34:10 -04003471 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003472 reg = FDI_TX_CTL(pipe);
3473 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003474 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3475 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003476 I915_WRITE(reg, temp);
3477
3478 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003479 udelay(500);
3480
Sean Paulfa37d392012-03-02 12:53:39 -05003481 for (retry = 0; retry < 5; retry++) {
3482 reg = FDI_RX_IIR(pipe);
3483 temp = I915_READ(reg);
3484 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3485 if (temp & FDI_RX_SYMBOL_LOCK) {
3486 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3487 DRM_DEBUG_KMS("FDI train 2 done.\n");
3488 break;
3489 }
3490 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003491 }
Sean Paulfa37d392012-03-02 12:53:39 -05003492 if (retry < 5)
3493 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003494 }
3495 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003496 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003497
3498 DRM_DEBUG_KMS("FDI train done.\n");
3499}
3500
Jesse Barnes357555c2011-04-28 15:09:55 -07003501/* Manual link training for Ivy Bridge A0 parts */
3502static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3503{
3504 struct drm_device *dev = crtc->dev;
3505 struct drm_i915_private *dev_priv = dev->dev_private;
3506 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3507 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003508 i915_reg_t reg;
3509 u32 temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003510
3511 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3512 for train result */
3513 reg = FDI_RX_IMR(pipe);
3514 temp = I915_READ(reg);
3515 temp &= ~FDI_RX_SYMBOL_LOCK;
3516 temp &= ~FDI_RX_BIT_LOCK;
3517 I915_WRITE(reg, temp);
3518
3519 POSTING_READ(reg);
3520 udelay(150);
3521
Daniel Vetter01a415f2012-10-27 15:58:40 +02003522 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3523 I915_READ(FDI_RX_IIR(pipe)));
3524
Jesse Barnes139ccd32013-08-19 11:04:55 -07003525 /* Try each vswing and preemphasis setting twice before moving on */
3526 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3527 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003528 reg = FDI_TX_CTL(pipe);
3529 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003530 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3531 temp &= ~FDI_TX_ENABLE;
3532 I915_WRITE(reg, temp);
3533
3534 reg = FDI_RX_CTL(pipe);
3535 temp = I915_READ(reg);
3536 temp &= ~FDI_LINK_TRAIN_AUTO;
3537 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3538 temp &= ~FDI_RX_ENABLE;
3539 I915_WRITE(reg, temp);
3540
3541 /* enable CPU FDI TX and PCH FDI RX */
3542 reg = FDI_TX_CTL(pipe);
3543 temp = I915_READ(reg);
3544 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003545 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003546 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003547 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003548 temp |= snb_b_fdi_train_param[j/2];
3549 temp |= FDI_COMPOSITE_SYNC;
3550 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3551
3552 I915_WRITE(FDI_RX_MISC(pipe),
3553 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3554
3555 reg = FDI_RX_CTL(pipe);
3556 temp = I915_READ(reg);
3557 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3558 temp |= FDI_COMPOSITE_SYNC;
3559 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3560
3561 POSTING_READ(reg);
3562 udelay(1); /* should be 0.5us */
3563
3564 for (i = 0; i < 4; i++) {
3565 reg = FDI_RX_IIR(pipe);
3566 temp = I915_READ(reg);
3567 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3568
3569 if (temp & FDI_RX_BIT_LOCK ||
3570 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3571 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3572 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3573 i);
3574 break;
3575 }
3576 udelay(1); /* should be 0.5us */
3577 }
3578 if (i == 4) {
3579 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3580 continue;
3581 }
3582
3583 /* Train 2 */
3584 reg = FDI_TX_CTL(pipe);
3585 temp = I915_READ(reg);
3586 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3587 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3588 I915_WRITE(reg, temp);
3589
3590 reg = FDI_RX_CTL(pipe);
3591 temp = I915_READ(reg);
3592 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3593 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003594 I915_WRITE(reg, temp);
3595
3596 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003597 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003598
Jesse Barnes139ccd32013-08-19 11:04:55 -07003599 for (i = 0; i < 4; i++) {
3600 reg = FDI_RX_IIR(pipe);
3601 temp = I915_READ(reg);
3602 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003603
Jesse Barnes139ccd32013-08-19 11:04:55 -07003604 if (temp & FDI_RX_SYMBOL_LOCK ||
3605 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3606 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3607 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3608 i);
3609 goto train_done;
3610 }
3611 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003612 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003613 if (i == 4)
3614 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003615 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003616
Jesse Barnes139ccd32013-08-19 11:04:55 -07003617train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003618 DRM_DEBUG_KMS("FDI train done.\n");
3619}
3620
Daniel Vetter88cefb62012-08-12 19:27:14 +02003621static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003622{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003623 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003624 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003625 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003626 i915_reg_t reg;
3627 u32 temp;
Jesse Barnesc64e3112010-09-10 11:27:03 -07003628
Jesse Barnes0e23b992010-09-10 11:10:00 -07003629 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003630 reg = FDI_RX_CTL(pipe);
3631 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003632 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003633 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003634 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003635 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3636
3637 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003638 udelay(200);
3639
3640 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003641 temp = I915_READ(reg);
3642 I915_WRITE(reg, temp | FDI_PCDCLK);
3643
3644 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003645 udelay(200);
3646
Paulo Zanoni20749732012-11-23 15:30:38 -02003647 /* Enable CPU FDI TX PLL, always on for Ironlake */
3648 reg = FDI_TX_CTL(pipe);
3649 temp = I915_READ(reg);
3650 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3651 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003652
Paulo Zanoni20749732012-11-23 15:30:38 -02003653 POSTING_READ(reg);
3654 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003655 }
3656}
3657
Daniel Vetter88cefb62012-08-12 19:27:14 +02003658static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3659{
3660 struct drm_device *dev = intel_crtc->base.dev;
3661 struct drm_i915_private *dev_priv = dev->dev_private;
3662 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003663 i915_reg_t reg;
3664 u32 temp;
Daniel Vetter88cefb62012-08-12 19:27:14 +02003665
3666 /* Switch from PCDclk to Rawclk */
3667 reg = FDI_RX_CTL(pipe);
3668 temp = I915_READ(reg);
3669 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3670
3671 /* Disable CPU FDI TX PLL */
3672 reg = FDI_TX_CTL(pipe);
3673 temp = I915_READ(reg);
3674 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3675
3676 POSTING_READ(reg);
3677 udelay(100);
3678
3679 reg = FDI_RX_CTL(pipe);
3680 temp = I915_READ(reg);
3681 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3682
3683 /* Wait for the clocks to turn off. */
3684 POSTING_READ(reg);
3685 udelay(100);
3686}
3687
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003688static void ironlake_fdi_disable(struct drm_crtc *crtc)
3689{
3690 struct drm_device *dev = crtc->dev;
3691 struct drm_i915_private *dev_priv = dev->dev_private;
3692 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3693 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003694 i915_reg_t reg;
3695 u32 temp;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003696
3697 /* disable CPU FDI tx and PCH FDI rx */
3698 reg = FDI_TX_CTL(pipe);
3699 temp = I915_READ(reg);
3700 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3701 POSTING_READ(reg);
3702
3703 reg = FDI_RX_CTL(pipe);
3704 temp = I915_READ(reg);
3705 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003706 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003707 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3708
3709 POSTING_READ(reg);
3710 udelay(100);
3711
3712 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003713 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003714 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003715
3716 /* still set train pattern 1 */
3717 reg = FDI_TX_CTL(pipe);
3718 temp = I915_READ(reg);
3719 temp &= ~FDI_LINK_TRAIN_NONE;
3720 temp |= FDI_LINK_TRAIN_PATTERN_1;
3721 I915_WRITE(reg, temp);
3722
3723 reg = FDI_RX_CTL(pipe);
3724 temp = I915_READ(reg);
3725 if (HAS_PCH_CPT(dev)) {
3726 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3727 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3728 } else {
3729 temp &= ~FDI_LINK_TRAIN_NONE;
3730 temp |= FDI_LINK_TRAIN_PATTERN_1;
3731 }
3732 /* BPC in FDI rx is consistent with that in PIPECONF */
3733 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003734 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003735 I915_WRITE(reg, temp);
3736
3737 POSTING_READ(reg);
3738 udelay(100);
3739}
3740
Chris Wilson5dce5b932014-01-20 10:17:36 +00003741bool intel_has_pending_fb_unpin(struct drm_device *dev)
3742{
3743 struct intel_crtc *crtc;
3744
3745 /* Note that we don't need to be called with mode_config.lock here
3746 * as our list of CRTC objects is static for the lifetime of the
3747 * device and so cannot disappear as we iterate. Similarly, we can
3748 * happily treat the predicates as racy, atomic checks as userspace
3749 * cannot claim and pin a new fb without at least acquring the
3750 * struct_mutex and so serialising with us.
3751 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003752 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003753 if (atomic_read(&crtc->unpin_work_count) == 0)
3754 continue;
3755
Maarten Lankhorst68858432016-05-17 15:07:52 +02003756 if (!list_empty_careful(&crtc->flip_work))
Chris Wilson5dce5b932014-01-20 10:17:36 +00003757 intel_wait_for_vblank(dev, crtc->pipe);
3758
3759 return true;
3760 }
3761
3762 return false;
3763}
3764
Maarten Lankhorst68858432016-05-17 15:07:52 +02003765static void page_flip_completed(struct intel_crtc *intel_crtc, struct intel_flip_work *work)
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003766{
3767 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
Maarten Lankhorst143f73b32016-05-17 15:07:54 +02003768 struct drm_plane_state *new_plane_state;
3769 struct drm_plane *primary = intel_crtc->base.primary;
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003770
3771 if (work->event)
Gustavo Padovan560ce1d2016-04-14 10:48:15 -07003772 drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003773
3774 drm_crtc_vblank_put(&intel_crtc->base);
3775
Maarten Lankhorst143f73b32016-05-17 15:07:54 +02003776 new_plane_state = &work->old_plane_state[0]->base;
3777 if (work->num_planes >= 1 &&
3778 new_plane_state->plane == primary &&
3779 new_plane_state->fb)
3780 trace_i915_flip_complete(intel_crtc->plane,
3781 intel_fb_obj(new_plane_state->fb));
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003782
Maarten Lankhorst143f73b32016-05-17 15:07:54 +02003783 if (work->can_async_unpin) {
3784 list_del_init(&work->head);
3785 wake_up_all(&dev_priv->pending_flip_queue);
3786 }
3787
3788 queue_work(dev_priv->wq, &work->unpin_work);
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003789}
3790
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003791static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003792{
Chris Wilson0f911282012-04-17 10:05:38 +01003793 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003794 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003795 long ret;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003796
Daniel Vetter2c10d572012-12-20 21:24:07 +01003797 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003798
3799 ret = wait_event_interruptible_timeout(
3800 dev_priv->pending_flip_queue,
3801 !intel_crtc_has_pending_flip(crtc),
3802 60*HZ);
3803
3804 if (ret < 0)
3805 return ret;
3806
Maarten Lankhorst8dd634d2016-05-17 15:07:55 +02003807 WARN(ret == 0, "Stuck page flip\n");
Chris Wilson5bb61642012-09-27 21:25:58 +01003808
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003809 return 0;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003810}
3811
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003812static void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
3813{
3814 u32 temp;
3815
3816 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3817
3818 mutex_lock(&dev_priv->sb_lock);
3819
3820 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3821 temp |= SBI_SSCCTL_DISABLE;
3822 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
3823
3824 mutex_unlock(&dev_priv->sb_lock);
3825}
3826
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003827/* Program iCLKIP clock to the desired frequency */
3828static void lpt_program_iclkip(struct drm_crtc *crtc)
3829{
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003830 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003831 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003832 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3833 u32 temp;
3834
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003835 lpt_disable_iclkip(dev_priv);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003836
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003837 /* The iCLK virtual clock root frequency is in MHz,
3838 * but the adjusted_mode->crtc_clock in in KHz. To get the
3839 * divisors, it is necessary to divide one by another, so we
3840 * convert the virtual clock precision to KHz here for higher
3841 * precision.
3842 */
3843 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003844 u32 iclk_virtual_root_freq = 172800 * 1000;
3845 u32 iclk_pi_range = 64;
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003846 u32 desired_divisor;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003847
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003848 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
3849 clock << auxdiv);
3850 divsel = (desired_divisor / iclk_pi_range) - 2;
3851 phaseinc = desired_divisor % iclk_pi_range;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003852
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003853 /*
3854 * Near 20MHz is a corner case which is
3855 * out of range for the 7-bit divisor
3856 */
3857 if (divsel <= 0x7f)
3858 break;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003859 }
3860
3861 /* This should not happen with any sane values */
3862 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3863 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3864 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3865 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3866
3867 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 +03003868 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003869 auxdiv,
3870 divsel,
3871 phasedir,
3872 phaseinc);
3873
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003874 mutex_lock(&dev_priv->sb_lock);
3875
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003876 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003877 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003878 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3879 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3880 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3881 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3882 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3883 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003884 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003885
3886 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003887 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003888 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3889 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003890 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003891
3892 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003893 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003894 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003895 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003896
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003897 mutex_unlock(&dev_priv->sb_lock);
3898
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003899 /* Wait for initialization time */
3900 udelay(24);
3901
3902 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
3903}
3904
Ville Syrjälä8802e5b2016-02-17 21:41:12 +02003905int lpt_get_iclkip(struct drm_i915_private *dev_priv)
3906{
3907 u32 divsel, phaseinc, auxdiv;
3908 u32 iclk_virtual_root_freq = 172800 * 1000;
3909 u32 iclk_pi_range = 64;
3910 u32 desired_divisor;
3911 u32 temp;
3912
3913 if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
3914 return 0;
3915
3916 mutex_lock(&dev_priv->sb_lock);
3917
3918 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3919 if (temp & SBI_SSCCTL_DISABLE) {
3920 mutex_unlock(&dev_priv->sb_lock);
3921 return 0;
3922 }
3923
3924 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
3925 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
3926 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
3927 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
3928 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
3929
3930 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
3931 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
3932 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
3933
3934 mutex_unlock(&dev_priv->sb_lock);
3935
3936 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
3937
3938 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
3939 desired_divisor << auxdiv);
3940}
3941
Daniel Vetter275f01b22013-05-03 11:49:47 +02003942static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3943 enum pipe pch_transcoder)
3944{
3945 struct drm_device *dev = crtc->base.dev;
3946 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003947 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02003948
3949 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3950 I915_READ(HTOTAL(cpu_transcoder)));
3951 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
3952 I915_READ(HBLANK(cpu_transcoder)));
3953 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
3954 I915_READ(HSYNC(cpu_transcoder)));
3955
3956 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
3957 I915_READ(VTOTAL(cpu_transcoder)));
3958 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
3959 I915_READ(VBLANK(cpu_transcoder)));
3960 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
3961 I915_READ(VSYNC(cpu_transcoder)));
3962 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
3963 I915_READ(VSYNCSHIFT(cpu_transcoder)));
3964}
3965
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02003966static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003967{
3968 struct drm_i915_private *dev_priv = dev->dev_private;
3969 uint32_t temp;
3970
3971 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02003972 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003973 return;
3974
3975 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
3976 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
3977
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02003978 temp &= ~FDI_BC_BIFURCATION_SELECT;
3979 if (enable)
3980 temp |= FDI_BC_BIFURCATION_SELECT;
3981
3982 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003983 I915_WRITE(SOUTH_CHICKEN1, temp);
3984 POSTING_READ(SOUTH_CHICKEN1);
3985}
3986
3987static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
3988{
3989 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003990
3991 switch (intel_crtc->pipe) {
3992 case PIPE_A:
3993 break;
3994 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003995 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02003996 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003997 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02003998 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003999
4000 break;
4001 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004002 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004003
4004 break;
4005 default:
4006 BUG();
4007 }
4008}
4009
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004010/* Return which DP Port should be selected for Transcoder DP control */
4011static enum port
4012intel_trans_dp_port_sel(struct drm_crtc *crtc)
4013{
4014 struct drm_device *dev = crtc->dev;
4015 struct intel_encoder *encoder;
4016
4017 for_each_encoder_on_crtc(dev, crtc, encoder) {
4018 if (encoder->type == INTEL_OUTPUT_DISPLAYPORT ||
4019 encoder->type == INTEL_OUTPUT_EDP)
4020 return enc_to_dig_port(&encoder->base)->port;
4021 }
4022
4023 return -1;
4024}
4025
Jesse Barnesf67a5592011-01-05 10:31:48 -08004026/*
4027 * Enable PCH resources required for PCH ports:
4028 * - PCH PLLs
4029 * - FDI training & RX/TX
4030 * - update transcoder timings
4031 * - DP transcoding bits
4032 * - transcoder
4033 */
4034static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08004035{
4036 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004037 struct drm_i915_private *dev_priv = dev->dev_private;
4038 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4039 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004040 u32 temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004041
Daniel Vetterab9412b2013-05-03 11:49:46 +02004042 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004043
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004044 if (IS_IVYBRIDGE(dev))
4045 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4046
Daniel Vettercd986ab2012-10-26 10:58:12 +02004047 /* Write the TU size bits before fdi link training, so that error
4048 * detection works. */
4049 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4050 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4051
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004052 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07004053 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004054
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004055 /* We need to program the right clock selection before writing the pixel
4056 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004057 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004058 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004059
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004060 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004061 temp |= TRANS_DPLL_ENABLE(pipe);
4062 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02004063 if (intel_crtc->config->shared_dpll ==
4064 intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004065 temp |= sel;
4066 else
4067 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004068 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004069 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004070
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004071 /* XXX: pch pll's can be enabled any time before we enable the PCH
4072 * transcoder, and we actually should do this to not upset any PCH
4073 * transcoder that already use the clock when we share it.
4074 *
4075 * Note that enable_shared_dpll tries to do the right thing, but
4076 * get_shared_dpll unconditionally resets the pll - we need that to have
4077 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02004078 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004079
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004080 /* set transcoder timing, panel must allow it */
4081 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02004082 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004083
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004084 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004085
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004086 /* For PCH DP, enable TRANS_DP_CTL */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004087 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004088 const struct drm_display_mode *adjusted_mode =
4089 &intel_crtc->config->base.adjusted_mode;
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004090 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004091 i915_reg_t reg = TRANS_DP_CTL(pipe);
Chris Wilson5eddb702010-09-11 13:48:45 +01004092 temp = I915_READ(reg);
4093 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004094 TRANS_DP_SYNC_MASK |
4095 TRANS_DP_BPC_MASK);
Ville Syrjäläe3ef4472015-05-05 17:17:31 +03004096 temp |= TRANS_DP_OUTPUT_ENABLE;
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004097 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004098
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004099 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004100 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004101 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004102 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004103
4104 switch (intel_trans_dp_port_sel(crtc)) {
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004105 case PORT_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004106 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004107 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004108 case PORT_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004109 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004110 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004111 case PORT_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004112 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004113 break;
4114 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004115 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004116 }
4117
Chris Wilson5eddb702010-09-11 13:48:45 +01004118 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004119 }
4120
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004121 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004122}
4123
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004124static void lpt_pch_enable(struct drm_crtc *crtc)
4125{
4126 struct drm_device *dev = crtc->dev;
4127 struct drm_i915_private *dev_priv = dev->dev_private;
4128 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004129 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004130
Daniel Vetterab9412b2013-05-03 11:49:46 +02004131 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004132
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004133 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004134
Paulo Zanoni0540e482012-10-31 18:12:40 -02004135 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02004136 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004137
Paulo Zanoni937bb612012-10-31 18:12:47 -02004138 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004139}
4140
Daniel Vettera1520312013-05-03 11:49:50 +02004141static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004142{
4143 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004144 i915_reg_t dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004145 u32 temp;
4146
4147 temp = I915_READ(dslreg);
4148 udelay(500);
4149 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004150 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004151 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004152 }
4153}
4154
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004155static int
4156skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4157 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4158 int src_w, int src_h, int dst_w, int dst_h)
Chandra Kondurua1b22782015-04-07 15:28:45 -07004159{
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004160 struct intel_crtc_scaler_state *scaler_state =
4161 &crtc_state->scaler_state;
4162 struct intel_crtc *intel_crtc =
4163 to_intel_crtc(crtc_state->base.crtc);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004164 int need_scaling;
Chandra Konduru6156a452015-04-27 13:48:39 -07004165
4166 need_scaling = intel_rotation_90_or_270(rotation) ?
4167 (src_h != dst_w || src_w != dst_h):
4168 (src_w != dst_w || src_h != dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004169
4170 /*
4171 * if plane is being disabled or scaler is no more required or force detach
4172 * - free scaler binded to this plane/crtc
4173 * - in order to do this, update crtc->scaler_usage
4174 *
4175 * Here scaler state in crtc_state is set free so that
4176 * scaler can be assigned to other user. Actual register
4177 * update to free the scaler is done in plane/panel-fit programming.
4178 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4179 */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004180 if (force_detach || !need_scaling) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004181 if (*scaler_id >= 0) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004182 scaler_state->scaler_users &= ~(1 << scaler_user);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004183 scaler_state->scalers[*scaler_id].in_use = 0;
4184
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004185 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4186 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4187 intel_crtc->pipe, scaler_user, *scaler_id,
Chandra Kondurua1b22782015-04-07 15:28:45 -07004188 scaler_state->scaler_users);
4189 *scaler_id = -1;
4190 }
4191 return 0;
4192 }
4193
4194 /* range checks */
4195 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4196 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4197
4198 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4199 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004200 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
Chandra Kondurua1b22782015-04-07 15:28:45 -07004201 "size is out of scaler range\n",
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004202 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004203 return -EINVAL;
4204 }
4205
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004206 /* mark this plane as a scaler user in crtc_state */
4207 scaler_state->scaler_users |= (1 << scaler_user);
4208 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4209 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4210 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4211 scaler_state->scaler_users);
4212
4213 return 0;
4214}
4215
4216/**
4217 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4218 *
4219 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004220 *
4221 * Return
4222 * 0 - scaler_usage updated successfully
4223 * error - requested scaling cannot be supported or other error condition
4224 */
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004225int skl_update_scaler_crtc(struct intel_crtc_state *state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004226{
4227 struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03004228 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004229
4230 DRM_DEBUG_KMS("Updating scaler for [CRTC:%i] scaler_user index %u.%u\n",
4231 intel_crtc->base.base.id, intel_crtc->pipe, SKL_CRTC_INDEX);
4232
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004233 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
Ville Syrjäläfa5a7972015-10-15 17:01:58 +03004234 &state->scaler_state.scaler_id, BIT(DRM_ROTATE_0),
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004235 state->pipe_src_w, state->pipe_src_h,
Ville Syrjäläaad941d2015-09-25 16:38:56 +03004236 adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004237}
4238
4239/**
4240 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4241 *
4242 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004243 * @plane_state: atomic plane state to update
4244 *
4245 * Return
4246 * 0 - scaler_usage updated successfully
4247 * error - requested scaling cannot be supported or other error condition
4248 */
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004249static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4250 struct intel_plane_state *plane_state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004251{
4252
4253 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004254 struct intel_plane *intel_plane =
4255 to_intel_plane(plane_state->base.plane);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004256 struct drm_framebuffer *fb = plane_state->base.fb;
4257 int ret;
4258
4259 bool force_detach = !fb || !plane_state->visible;
4260
4261 DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
4262 intel_plane->base.base.id, intel_crtc->pipe,
4263 drm_plane_index(&intel_plane->base));
4264
4265 ret = skl_update_scaler(crtc_state, force_detach,
4266 drm_plane_index(&intel_plane->base),
4267 &plane_state->scaler_id,
4268 plane_state->base.rotation,
4269 drm_rect_width(&plane_state->src) >> 16,
4270 drm_rect_height(&plane_state->src) >> 16,
4271 drm_rect_width(&plane_state->dst),
4272 drm_rect_height(&plane_state->dst));
4273
4274 if (ret || plane_state->scaler_id < 0)
4275 return ret;
4276
Chandra Kondurua1b22782015-04-07 15:28:45 -07004277 /* check colorkey */
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004278 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004279 DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004280 intel_plane->base.base.id);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004281 return -EINVAL;
4282 }
4283
4284 /* Check src format */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004285 switch (fb->pixel_format) {
4286 case DRM_FORMAT_RGB565:
4287 case DRM_FORMAT_XBGR8888:
4288 case DRM_FORMAT_XRGB8888:
4289 case DRM_FORMAT_ABGR8888:
4290 case DRM_FORMAT_ARGB8888:
4291 case DRM_FORMAT_XRGB2101010:
4292 case DRM_FORMAT_XBGR2101010:
4293 case DRM_FORMAT_YUYV:
4294 case DRM_FORMAT_YVYU:
4295 case DRM_FORMAT_UYVY:
4296 case DRM_FORMAT_VYUY:
4297 break;
4298 default:
4299 DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
4300 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4301 return -EINVAL;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004302 }
4303
Chandra Kondurua1b22782015-04-07 15:28:45 -07004304 return 0;
4305}
4306
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004307static void skylake_scaler_disable(struct intel_crtc *crtc)
4308{
4309 int i;
4310
4311 for (i = 0; i < crtc->num_scalers; i++)
4312 skl_detach_scaler(crtc, i);
4313}
4314
4315static void skylake_pfit_enable(struct intel_crtc *crtc)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004316{
4317 struct drm_device *dev = crtc->base.dev;
4318 struct drm_i915_private *dev_priv = dev->dev_private;
4319 int pipe = crtc->pipe;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004320 struct intel_crtc_scaler_state *scaler_state =
4321 &crtc->config->scaler_state;
4322
4323 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4324
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004325 if (crtc->config->pch_pfit.enabled) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004326 int id;
4327
4328 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4329 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4330 return;
4331 }
4332
4333 id = scaler_state->scaler_id;
4334 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4335 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4336 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4337 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4338
4339 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004340 }
4341}
4342
Jesse Barnesb074cec2013-04-25 12:55:02 -07004343static void ironlake_pfit_enable(struct intel_crtc *crtc)
4344{
4345 struct drm_device *dev = crtc->base.dev;
4346 struct drm_i915_private *dev_priv = dev->dev_private;
4347 int pipe = crtc->pipe;
4348
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004349 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004350 /* Force use of hard-coded filter coefficients
4351 * as some pre-programmed values are broken,
4352 * e.g. x201.
4353 */
4354 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4355 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4356 PF_PIPE_SEL_IVB(pipe));
4357 else
4358 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004359 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4360 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004361 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004362}
4363
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004364void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004365{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004366 struct drm_device *dev = crtc->base.dev;
4367 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid77e4532013-09-24 13:52:55 -03004368
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004369 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004370 return;
4371
Maarten Lankhorst307e4492016-03-23 14:33:28 +01004372 /*
4373 * We can only enable IPS after we enable a plane and wait for a vblank
4374 * This function is called from post_plane_update, which is run after
4375 * a vblank wait.
4376 */
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004377
Paulo Zanonid77e4532013-09-24 13:52:55 -03004378 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004379 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004380 mutex_lock(&dev_priv->rps.hw_lock);
4381 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4382 mutex_unlock(&dev_priv->rps.hw_lock);
4383 /* Quoting Art Runyan: "its not safe to expect any particular
4384 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004385 * mailbox." Moreover, the mailbox may return a bogus state,
4386 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004387 */
4388 } else {
4389 I915_WRITE(IPS_CTL, IPS_ENABLE);
4390 /* The bit only becomes 1 in the next vblank, so this wait here
4391 * is essentially intel_wait_for_vblank. If we don't have this
4392 * and don't wait for vblanks until the end of crtc_enable, then
4393 * the HW state readout code will complain that the expected
4394 * IPS_CTL value is not the one we read. */
4395 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4396 DRM_ERROR("Timed out waiting for IPS enable\n");
4397 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004398}
4399
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004400void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004401{
4402 struct drm_device *dev = crtc->base.dev;
4403 struct drm_i915_private *dev_priv = dev->dev_private;
4404
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004405 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004406 return;
4407
4408 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004409 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004410 mutex_lock(&dev_priv->rps.hw_lock);
4411 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4412 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004413 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4414 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4415 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004416 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004417 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004418 POSTING_READ(IPS_CTL);
4419 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004420
4421 /* We need to wait for a vblank before we can disable the plane. */
4422 intel_wait_for_vblank(dev, crtc->pipe);
4423}
4424
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004425static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004426{
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004427 if (intel_crtc->overlay) {
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004428 struct drm_device *dev = intel_crtc->base.dev;
4429 struct drm_i915_private *dev_priv = dev->dev_private;
4430
4431 mutex_lock(&dev->struct_mutex);
4432 dev_priv->mm.interruptible = false;
4433 (void) intel_overlay_switch_off(intel_crtc->overlay);
4434 dev_priv->mm.interruptible = true;
4435 mutex_unlock(&dev->struct_mutex);
4436 }
4437
4438 /* Let userspace switch the overlay on again. In most cases userspace
4439 * has to recompute where to put it anyway.
4440 */
4441}
4442
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004443/**
4444 * intel_post_enable_primary - Perform operations after enabling primary plane
4445 * @crtc: the CRTC whose primary plane was just enabled
4446 *
4447 * Performs potentially sleeping operations that must be done after the primary
4448 * plane is enabled, such as updating FBC and IPS. Note that this may be
4449 * called due to an explicit primary plane update, or due to an implicit
4450 * re-enable that is caused when a sprite plane is updated to no longer
4451 * completely hide the primary plane.
4452 */
4453static void
4454intel_post_enable_primary(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004455{
4456 struct drm_device *dev = crtc->dev;
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004457 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004458 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4459 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004460
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004461 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004462 * FIXME IPS should be fine as long as one plane is
4463 * enabled, but in practice it seems to have problems
4464 * when going from primary only to sprite only and vice
4465 * versa.
4466 */
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004467 hsw_enable_ips(intel_crtc);
4468
Daniel Vetterf99d7062014-06-19 16:01:59 +02004469 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004470 * Gen2 reports pipe underruns whenever all planes are disabled.
4471 * So don't enable underrun reporting before at least some planes
4472 * are enabled.
4473 * FIXME: Need to fix the logic to work when we turn off all planes
4474 * but leave the pipe running.
Daniel Vetterf99d7062014-06-19 16:01:59 +02004475 */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004476 if (IS_GEN2(dev))
4477 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4478
Ville Syrjäläaca7b682015-10-30 19:22:21 +02004479 /* Underruns don't always raise interrupts, so check manually. */
4480 intel_check_cpu_fifo_underruns(dev_priv);
4481 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004482}
4483
Ville Syrjälä2622a082016-03-09 19:07:26 +02004484/* FIXME move all this to pre_plane_update() with proper state tracking */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004485static void
4486intel_pre_disable_primary(struct drm_crtc *crtc)
4487{
4488 struct drm_device *dev = crtc->dev;
4489 struct drm_i915_private *dev_priv = dev->dev_private;
4490 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4491 int pipe = intel_crtc->pipe;
4492
4493 /*
4494 * Gen2 reports pipe underruns whenever all planes are disabled.
4495 * So diasble underrun reporting before all the planes get disabled.
4496 * FIXME: Need to fix the logic to work when we turn off all planes
4497 * but leave the pipe running.
4498 */
4499 if (IS_GEN2(dev))
4500 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4501
4502 /*
Ville Syrjälä2622a082016-03-09 19:07:26 +02004503 * FIXME IPS should be fine as long as one plane is
4504 * enabled, but in practice it seems to have problems
4505 * when going from primary only to sprite only and vice
4506 * versa.
4507 */
4508 hsw_disable_ips(intel_crtc);
4509}
4510
4511/* FIXME get rid of this and use pre_plane_update */
4512static void
4513intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
4514{
4515 struct drm_device *dev = crtc->dev;
4516 struct drm_i915_private *dev_priv = dev->dev_private;
4517 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4518 int pipe = intel_crtc->pipe;
4519
4520 intel_pre_disable_primary(crtc);
4521
4522 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004523 * Vblank time updates from the shadow to live plane control register
4524 * are blocked if the memory self-refresh mode is active at that
4525 * moment. So to make sure the plane gets truly disabled, disable
4526 * first the self-refresh mode. The self-refresh enable bit in turn
4527 * will be checked/applied by the HW only at the next frame start
4528 * event which is after the vblank start event, so we need to have a
4529 * wait-for-vblank between disabling the plane and the pipe.
4530 */
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004531 if (HAS_GMCH_DISPLAY(dev)) {
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004532 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004533 dev_priv->wm.vlv.cxsr = false;
4534 intel_wait_for_vblank(dev, pipe);
4535 }
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004536}
4537
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004538static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004539{
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004540 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004541 struct drm_device *dev = crtc->base.dev;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +02004542 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01004543 struct intel_crtc_state *pipe_config =
4544 to_intel_crtc_state(crtc->base.state);
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004545 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4546 struct drm_plane *primary = crtc->base.primary;
4547 struct drm_plane_state *old_pri_state =
4548 drm_atomic_get_existing_plane_state(old_state, primary);
4549 bool modeset = needs_modeset(&pipe_config->base);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004550
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004551 if (old_pri_state) {
4552 struct intel_plane_state *primary_state =
4553 to_intel_plane_state(primary->state);
4554 struct intel_plane_state *old_primary_state =
4555 to_intel_plane_state(old_pri_state);
4556
Maarten Lankhorst2099def2016-05-17 15:07:59 +02004557 intel_fbc_pre_update(crtc, pipe_config, primary_state);
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +01004558
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004559 if (old_primary_state->visible &&
4560 (modeset || !primary_state->visible))
4561 intel_pre_disable_primary(&crtc->base);
4562 }
Ville Syrjälä852eb002015-06-24 22:00:07 +03004563
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01004564 if (pipe_config->disable_cxsr) {
Ville Syrjälä852eb002015-06-24 22:00:07 +03004565 crtc->wm.cxsr_allowed = false;
Maarten Lankhorst2dfd1782016-02-03 16:53:25 +01004566
Ville Syrjälä2622a082016-03-09 19:07:26 +02004567 /*
4568 * Vblank time updates from the shadow to live plane control register
4569 * are blocked if the memory self-refresh mode is active at that
4570 * moment. So to make sure the plane gets truly disabled, disable
4571 * first the self-refresh mode. The self-refresh enable bit in turn
4572 * will be checked/applied by the HW only at the next frame start
4573 * event which is after the vblank start event, so we need to have a
4574 * wait-for-vblank between disabling the plane and the pipe.
4575 */
4576 if (old_crtc_state->base.active) {
Maarten Lankhorst2dfd1782016-02-03 16:53:25 +01004577 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä2622a082016-03-09 19:07:26 +02004578 dev_priv->wm.vlv.cxsr = false;
4579 intel_wait_for_vblank(dev, crtc->pipe);
4580 }
Ville Syrjälä852eb002015-06-24 22:00:07 +03004581 }
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004582
Matt Ropered4a6a72016-02-23 17:20:13 -08004583 /*
4584 * IVB workaround: must disable low power watermarks for at least
4585 * one frame before enabling scaling. LP watermarks can be re-enabled
4586 * when scaling is disabled.
4587 *
4588 * WaCxSRDisabledForSpriteScaling:ivb
4589 */
4590 if (pipe_config->disable_lp_wm) {
4591 ilk_disable_lp_wm(dev);
4592 intel_wait_for_vblank(dev, crtc->pipe);
4593 }
4594
4595 /*
4596 * If we're doing a modeset, we're done. No need to do any pre-vblank
4597 * watermark programming here.
4598 */
4599 if (needs_modeset(&pipe_config->base))
4600 return;
4601
4602 /*
4603 * For platforms that support atomic watermarks, program the
4604 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
4605 * will be the intermediate values that are safe for both pre- and
4606 * post- vblank; when vblank happens, the 'active' values will be set
4607 * to the final 'target' values and we'll do this again to get the
4608 * optimal watermarks. For gen9+ platforms, the values we program here
4609 * will be the final target values which will get automatically latched
4610 * at vblank time; no further programming will be necessary.
4611 *
4612 * If a platform hasn't been transitioned to atomic watermarks yet,
4613 * we'll continue to update watermarks the old way, if flags tell
4614 * us to.
4615 */
4616 if (dev_priv->display.initial_watermarks != NULL)
4617 dev_priv->display.initial_watermarks(pipe_config);
Ville Syrjäläcaed3612016-03-09 19:07:25 +02004618 else if (pipe_config->update_wm_pre)
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004619 intel_update_watermarks(&crtc->base);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004620}
4621
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004622static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004623{
4624 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004625 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004626 struct drm_plane *p;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004627 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004628
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004629 intel_crtc_dpms_overlay_disable(intel_crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004630
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004631 drm_for_each_plane_mask(p, dev, plane_mask)
4632 to_intel_plane(p)->disable_plane(p, crtc);
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004633
Daniel Vetterf99d7062014-06-19 16:01:59 +02004634 /*
4635 * FIXME: Once we grow proper nuclear flip support out of this we need
4636 * to compute the mask of flip planes precisely. For the time being
4637 * consider this a flip to a NULL plane.
4638 */
4639 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004640}
4641
Jesse Barnesf67a5592011-01-05 10:31:48 -08004642static void ironlake_crtc_enable(struct drm_crtc *crtc)
4643{
4644 struct drm_device *dev = crtc->dev;
4645 struct drm_i915_private *dev_priv = dev->dev_private;
4646 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004647 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004648 int pipe = intel_crtc->pipe;
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004649 struct intel_crtc_state *pipe_config =
4650 to_intel_crtc_state(crtc->state);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004651
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004652 if (WARN_ON(intel_crtc->active))
Jesse Barnesf67a5592011-01-05 10:31:48 -08004653 return;
4654
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004655 /*
4656 * Sometimes spurious CPU pipe underruns happen during FDI
4657 * training, at least with VGA+HDMI cloning. Suppress them.
4658 *
4659 * On ILK we get an occasional spurious CPU pipe underruns
4660 * between eDP port A enable and vdd enable. Also PCH port
4661 * enable seems to result in the occasional CPU pipe underrun.
4662 *
4663 * Spurious PCH underruns also occur during PCH enabling.
4664 */
4665 if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
4666 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004667 if (intel_crtc->config->has_pch_encoder)
Ville Syrjälä81b088c2015-10-30 19:21:31 +02004668 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
4669
4670 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02004671 intel_prepare_shared_dpll(intel_crtc);
4672
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004673 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05304674 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004675
4676 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02004677 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004678
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004679 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02004680 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004681 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004682 }
4683
4684 ironlake_set_pipeconf(crtc);
4685
Jesse Barnesf67a5592011-01-05 10:31:48 -08004686 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004687
Daniel Vetterf6736a12013-06-05 13:34:30 +02004688 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004689 if (encoder->pre_enable)
4690 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004691
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004692 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004693 /* Note: FDI PLL enabling _must_ be done before we enable the
4694 * cpu pipes, hence this is separate from all the other fdi/pch
4695 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004696 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004697 } else {
4698 assert_fdi_tx_disabled(dev_priv, pipe);
4699 assert_fdi_rx_disabled(dev_priv, pipe);
4700 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004701
Jesse Barnesb074cec2013-04-25 12:55:02 -07004702 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004703
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004704 /*
4705 * On ILK+ LUT must be loaded before the pipe is running but with
4706 * clocks enabled
4707 */
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004708 intel_color_load_luts(&pipe_config->base);
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004709
Imre Deak1d5bf5d2016-02-29 22:10:33 +02004710 if (dev_priv->display.initial_watermarks != NULL)
4711 dev_priv->display.initial_watermarks(intel_crtc->config);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004712 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004713
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004714 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004715 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004716
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004717 assert_vblank_disabled(crtc);
4718 drm_crtc_vblank_on(crtc);
4719
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004720 for_each_encoder_on_crtc(dev, crtc, encoder)
4721 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004722
4723 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004724 cpt_verify_modeset(dev, intel_crtc->pipe);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004725
4726 /* Must wait for vblank to avoid spurious PCH FIFO underruns */
4727 if (intel_crtc->config->has_pch_encoder)
4728 intel_wait_for_vblank(dev, pipe);
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004729 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004730 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004731}
4732
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004733/* IPS only exists on ULT machines and is tied to pipe A. */
4734static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4735{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004736 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004737}
4738
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004739static void haswell_crtc_enable(struct drm_crtc *crtc)
4740{
4741 struct drm_device *dev = crtc->dev;
4742 struct drm_i915_private *dev_priv = dev->dev_private;
4743 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4744 struct intel_encoder *encoder;
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004745 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
Jani Nikula4d1de972016-03-18 17:05:42 +02004746 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004747 struct intel_crtc_state *pipe_config =
4748 to_intel_crtc_state(crtc->state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004749
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004750 if (WARN_ON(intel_crtc->active))
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004751 return;
4752
Ville Syrjälä81b088c2015-10-30 19:21:31 +02004753 if (intel_crtc->config->has_pch_encoder)
4754 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4755 false);
4756
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02004757 if (intel_crtc->config->shared_dpll)
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004758 intel_enable_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 Vetter229fca92014-04-24 23:55:09 +02004762
Jani Nikula4d1de972016-03-18 17:05:42 +02004763 if (!intel_crtc->config->has_dsi_encoder)
4764 intel_set_pipe_timings(intel_crtc);
4765
Jani Nikulabc58be62016-03-18 17:05:39 +02004766 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02004767
Jani Nikula4d1de972016-03-18 17:05:42 +02004768 if (cpu_transcoder != TRANSCODER_EDP &&
4769 !transcoder_is_dsi(cpu_transcoder)) {
4770 I915_WRITE(PIPE_MULT(cpu_transcoder),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004771 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07004772 }
4773
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004774 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02004775 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004776 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02004777 }
4778
Jani Nikula4d1de972016-03-18 17:05:42 +02004779 if (!intel_crtc->config->has_dsi_encoder)
4780 haswell_set_pipeconf(crtc);
4781
Jani Nikula391bf042016-03-18 17:05:40 +02004782 haswell_set_pipemisc(crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02004783
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004784 intel_color_set_csc(&pipe_config->base);
Daniel Vetter229fca92014-04-24 23:55:09 +02004785
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004786 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004787
Daniel Vetter6b698512015-11-28 11:05:39 +01004788 if (intel_crtc->config->has_pch_encoder)
4789 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4790 else
4791 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4792
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304793 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004794 if (encoder->pre_enable)
4795 encoder->pre_enable(encoder);
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304796 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004797
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004798 if (intel_crtc->config->has_pch_encoder)
Imre Deak4fe94672014-06-25 22:01:49 +03004799 dev_priv->display.fdi_link_train(crtc);
Imre Deak4fe94672014-06-25 22:01:49 +03004800
Jani Nikulaa65347b2015-11-27 12:21:46 +02004801 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304802 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004803
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07004804 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004805 skylake_pfit_enable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08004806 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07004807 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004808
4809 /*
4810 * On ILK+ LUT must be loaded before the pipe is running but with
4811 * clocks enabled
4812 */
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004813 intel_color_load_luts(&pipe_config->base);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004814
Paulo Zanoni1f544382012-10-24 11:32:00 -02004815 intel_ddi_set_pipe_settings(crtc);
Jani Nikulaa65347b2015-11-27 12:21:46 +02004816 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304817 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004818
Imre Deak1d5bf5d2016-02-29 22:10:33 +02004819 if (dev_priv->display.initial_watermarks != NULL)
4820 dev_priv->display.initial_watermarks(pipe_config);
4821 else
4822 intel_update_watermarks(crtc);
Jani Nikula4d1de972016-03-18 17:05:42 +02004823
4824 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
4825 if (!intel_crtc->config->has_dsi_encoder)
4826 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004827
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004828 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004829 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004830
Jani Nikulaa65347b2015-11-27 12:21:46 +02004831 if (intel_crtc->config->dp_encoder_is_mst)
Dave Airlie0e32b392014-05-02 14:02:48 +10004832 intel_ddi_set_vc_payload_alloc(crtc, true);
4833
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004834 assert_vblank_disabled(crtc);
4835 drm_crtc_vblank_on(crtc);
4836
Jani Nikula8807e552013-08-30 19:40:32 +03004837 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004838 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004839 intel_opregion_notify_encoder(encoder, true);
4840 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004841
Daniel Vetter6b698512015-11-28 11:05:39 +01004842 if (intel_crtc->config->has_pch_encoder) {
4843 intel_wait_for_vblank(dev, pipe);
4844 intel_wait_for_vblank(dev, pipe);
4845 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004846 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4847 true);
Daniel Vetter6b698512015-11-28 11:05:39 +01004848 }
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004849
Paulo Zanonie4916942013-09-20 16:21:19 -03004850 /* If we change the relative order between pipe/planes enabling, we need
4851 * to change the workaround. */
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004852 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
4853 if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
4854 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4855 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4856 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004857}
4858
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02004859static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004860{
4861 struct drm_device *dev = crtc->base.dev;
4862 struct drm_i915_private *dev_priv = dev->dev_private;
4863 int pipe = crtc->pipe;
4864
4865 /* To avoid upsetting the power well on haswell only disable the pfit if
4866 * it's in use. The hw state code will make sure we get this right. */
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02004867 if (force || crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004868 I915_WRITE(PF_CTL(pipe), 0);
4869 I915_WRITE(PF_WIN_POS(pipe), 0);
4870 I915_WRITE(PF_WIN_SZ(pipe), 0);
4871 }
4872}
4873
Jesse Barnes6be4a602010-09-10 10:26:01 -07004874static void ironlake_crtc_disable(struct drm_crtc *crtc)
4875{
4876 struct drm_device *dev = crtc->dev;
4877 struct drm_i915_private *dev_priv = dev->dev_private;
4878 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004879 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004880 int pipe = intel_crtc->pipe;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004881
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004882 /*
4883 * Sometimes spurious CPU pipe underruns happen when the
4884 * pipe is already disabled, but FDI RX/TX is still enabled.
4885 * Happens at least with VGA+HDMI cloning. Suppress them.
4886 */
4887 if (intel_crtc->config->has_pch_encoder) {
4888 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004889 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004890 }
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004891
Daniel Vetterea9d7582012-07-10 10:42:52 +02004892 for_each_encoder_on_crtc(dev, crtc, encoder)
4893 encoder->disable(encoder);
4894
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004895 drm_crtc_vblank_off(crtc);
4896 assert_vblank_disabled(crtc);
4897
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03004898 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004899
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02004900 ironlake_pfit_disable(intel_crtc, false);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004901
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004902 if (intel_crtc->config->has_pch_encoder)
Ville Syrjälä5a74f702015-05-05 17:17:38 +03004903 ironlake_fdi_disable(crtc);
4904
Daniel Vetterbf49ec82012-09-06 22:15:40 +02004905 for_each_encoder_on_crtc(dev, crtc, encoder)
4906 if (encoder->post_disable)
4907 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004908
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004909 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02004910 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004911
Daniel Vetterd925c592013-06-05 13:34:04 +02004912 if (HAS_PCH_CPT(dev)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004913 i915_reg_t reg;
4914 u32 temp;
4915
Daniel Vetterd925c592013-06-05 13:34:04 +02004916 /* disable TRANS_DP_CTL */
4917 reg = TRANS_DP_CTL(pipe);
4918 temp = I915_READ(reg);
4919 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
4920 TRANS_DP_PORT_SEL_MASK);
4921 temp |= TRANS_DP_PORT_SEL_NONE;
4922 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004923
Daniel Vetterd925c592013-06-05 13:34:04 +02004924 /* disable DPLL_SEL */
4925 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004926 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02004927 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08004928 }
Daniel Vetterd925c592013-06-05 13:34:04 +02004929
Daniel Vetterd925c592013-06-05 13:34:04 +02004930 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004931 }
Ville Syrjälä81b088c2015-10-30 19:21:31 +02004932
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004933 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä81b088c2015-10-30 19:21:31 +02004934 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004935}
4936
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004937static void haswell_crtc_disable(struct drm_crtc *crtc)
4938{
4939 struct drm_device *dev = crtc->dev;
4940 struct drm_i915_private *dev_priv = dev->dev_private;
4941 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4942 struct intel_encoder *encoder;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004943 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004944
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004945 if (intel_crtc->config->has_pch_encoder)
4946 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4947 false);
4948
Jani Nikula8807e552013-08-30 19:40:32 +03004949 for_each_encoder_on_crtc(dev, crtc, encoder) {
4950 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004951 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004952 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004953
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004954 drm_crtc_vblank_off(crtc);
4955 assert_vblank_disabled(crtc);
4956
Jani Nikula4d1de972016-03-18 17:05:42 +02004957 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
4958 if (!intel_crtc->config->has_dsi_encoder)
4959 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004960
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004961 if (intel_crtc->config->dp_encoder_is_mst)
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03004962 intel_ddi_set_vc_payload_alloc(crtc, false);
4963
Jani Nikulaa65347b2015-11-27 12:21:46 +02004964 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304965 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004966
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07004967 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004968 skylake_scaler_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08004969 else
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02004970 ironlake_pfit_disable(intel_crtc, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004971
Jani Nikulaa65347b2015-11-27 12:21:46 +02004972 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304973 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004974
Imre Deak97b040a2014-06-25 22:01:50 +03004975 for_each_encoder_on_crtc(dev, crtc, encoder)
4976 if (encoder->post_disable)
4977 encoder->post_disable(encoder);
Ville Syrjälä81b088c2015-10-30 19:21:31 +02004978
Ville Syrjälä92966a32015-12-08 16:05:48 +02004979 if (intel_crtc->config->has_pch_encoder) {
4980 lpt_disable_pch_transcoder(dev_priv);
Ville Syrjälä503a74e2015-12-04 22:22:14 +02004981 lpt_disable_iclkip(dev_priv);
Ville Syrjälä92966a32015-12-08 16:05:48 +02004982 intel_ddi_fdi_disable(crtc);
4983
Ville Syrjälä81b088c2015-10-30 19:21:31 +02004984 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4985 true);
Ville Syrjälä92966a32015-12-08 16:05:48 +02004986 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004987}
4988
Jesse Barnes2dd24552013-04-25 12:55:01 -07004989static void i9xx_pfit_enable(struct intel_crtc *crtc)
4990{
4991 struct drm_device *dev = crtc->base.dev;
4992 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004993 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07004994
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02004995 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07004996 return;
4997
Daniel Vetterc0b03412013-05-28 12:05:54 +02004998 /*
4999 * The panel fitter should only be adjusted whilst the pipe is disabled,
5000 * according to register description and PRM.
5001 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005002 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5003 assert_pipe_disabled(dev_priv, crtc->pipe);
5004
Jesse Barnesb074cec2013-04-25 12:55:02 -07005005 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5006 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005007
5008 /* Border color in case we don't scale up to the full screen. Black by
5009 * default, change to something else for debugging. */
5010 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005011}
5012
Dave Airlied05410f2014-06-05 13:22:59 +10005013static enum intel_display_power_domain port_to_power_domain(enum port port)
5014{
5015 switch (port) {
5016 case PORT_A:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005017 return POWER_DOMAIN_PORT_DDI_A_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005018 case PORT_B:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005019 return POWER_DOMAIN_PORT_DDI_B_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005020 case PORT_C:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005021 return POWER_DOMAIN_PORT_DDI_C_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005022 case PORT_D:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005023 return POWER_DOMAIN_PORT_DDI_D_LANES;
Xiong Zhangd8e19f92015-08-13 18:00:12 +08005024 case PORT_E:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005025 return POWER_DOMAIN_PORT_DDI_E_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005026 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005027 MISSING_CASE(port);
Dave Airlied05410f2014-06-05 13:22:59 +10005028 return POWER_DOMAIN_PORT_OTHER;
5029 }
5030}
5031
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005032static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
5033{
5034 switch (port) {
5035 case PORT_A:
5036 return POWER_DOMAIN_AUX_A;
5037 case PORT_B:
5038 return POWER_DOMAIN_AUX_B;
5039 case PORT_C:
5040 return POWER_DOMAIN_AUX_C;
5041 case PORT_D:
5042 return POWER_DOMAIN_AUX_D;
5043 case PORT_E:
5044 /* FIXME: Check VBT for actual wiring of PORT E */
5045 return POWER_DOMAIN_AUX_D;
5046 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005047 MISSING_CASE(port);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005048 return POWER_DOMAIN_AUX_A;
5049 }
5050}
5051
Imre Deak319be8a2014-03-04 19:22:57 +02005052enum intel_display_power_domain
5053intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02005054{
Imre Deak319be8a2014-03-04 19:22:57 +02005055 struct drm_device *dev = intel_encoder->base.dev;
5056 struct intel_digital_port *intel_dig_port;
5057
5058 switch (intel_encoder->type) {
5059 case INTEL_OUTPUT_UNKNOWN:
5060 /* Only DDI platforms should ever use this output type */
5061 WARN_ON_ONCE(!HAS_DDI(dev));
5062 case INTEL_OUTPUT_DISPLAYPORT:
5063 case INTEL_OUTPUT_HDMI:
5064 case INTEL_OUTPUT_EDP:
5065 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10005066 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10005067 case INTEL_OUTPUT_DP_MST:
5068 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5069 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02005070 case INTEL_OUTPUT_ANALOG:
5071 return POWER_DOMAIN_PORT_CRT;
5072 case INTEL_OUTPUT_DSI:
5073 return POWER_DOMAIN_PORT_DSI;
5074 default:
5075 return POWER_DOMAIN_PORT_OTHER;
5076 }
5077}
5078
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005079enum intel_display_power_domain
5080intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
5081{
5082 struct drm_device *dev = intel_encoder->base.dev;
5083 struct intel_digital_port *intel_dig_port;
5084
5085 switch (intel_encoder->type) {
5086 case INTEL_OUTPUT_UNKNOWN:
Imre Deak651174a2015-11-18 15:57:24 +02005087 case INTEL_OUTPUT_HDMI:
5088 /*
5089 * Only DDI platforms should ever use these output types.
5090 * We can get here after the HDMI detect code has already set
5091 * the type of the shared encoder. Since we can't be sure
5092 * what's the status of the given connectors, play safe and
5093 * run the DP detection too.
5094 */
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005095 WARN_ON_ONCE(!HAS_DDI(dev));
5096 case INTEL_OUTPUT_DISPLAYPORT:
5097 case INTEL_OUTPUT_EDP:
5098 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
5099 return port_to_aux_power_domain(intel_dig_port->port);
5100 case INTEL_OUTPUT_DP_MST:
5101 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5102 return port_to_aux_power_domain(intel_dig_port->port);
5103 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005104 MISSING_CASE(intel_encoder->type);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005105 return POWER_DOMAIN_AUX_A;
5106 }
5107}
5108
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005109static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
5110 struct intel_crtc_state *crtc_state)
Imre Deak319be8a2014-03-04 19:22:57 +02005111{
5112 struct drm_device *dev = crtc->dev;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005113 struct drm_encoder *encoder;
Imre Deak319be8a2014-03-04 19:22:57 +02005114 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5115 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02005116 unsigned long mask;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005117 enum transcoder transcoder = crtc_state->cpu_transcoder;
Imre Deak77d22dc2014-03-05 16:20:52 +02005118
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005119 if (!crtc_state->base.active)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005120 return 0;
5121
Imre Deak77d22dc2014-03-05 16:20:52 +02005122 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5123 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005124 if (crtc_state->pch_pfit.enabled ||
5125 crtc_state->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02005126 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5127
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005128 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
5129 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5130
Imre Deak319be8a2014-03-04 19:22:57 +02005131 mask |= BIT(intel_display_port_power_domain(intel_encoder));
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005132 }
Imre Deak319be8a2014-03-04 19:22:57 +02005133
Maarten Lankhorst15e7ec22016-03-14 09:27:54 +01005134 if (crtc_state->shared_dpll)
5135 mask |= BIT(POWER_DOMAIN_PLLS);
5136
Imre Deak77d22dc2014-03-05 16:20:52 +02005137 return mask;
5138}
5139
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005140static unsigned long
5141modeset_get_crtc_power_domains(struct drm_crtc *crtc,
5142 struct intel_crtc_state *crtc_state)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005143{
5144 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5145 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5146 enum intel_display_power_domain domain;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +02005147 unsigned long domains, new_domains, old_domains, ms_domain = 0;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005148
5149 old_domains = intel_crtc->enabled_power_domains;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005150 intel_crtc->enabled_power_domains = new_domains =
5151 get_crtc_power_domains(crtc, crtc_state);
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005152
Maarten Lankhorsta6747b72016-05-17 15:08:01 +02005153 if (needs_modeset(&crtc_state->base))
5154 ms_domain = BIT(POWER_DOMAIN_MODESET);
5155
5156 domains = (new_domains & ~old_domains) | ms_domain;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005157
5158 for_each_power_domain(domain, domains)
5159 intel_display_power_get(dev_priv, domain);
5160
Maarten Lankhorsta6747b72016-05-17 15:08:01 +02005161 return (old_domains & ~new_domains) | ms_domain;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005162}
5163
5164static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
5165 unsigned long domains)
5166{
5167 enum intel_display_power_domain domain;
5168
5169 for_each_power_domain(domain, domains)
5170 intel_display_power_put(dev_priv, domain);
5171}
5172
Mika Kaholaadafdc62015-08-18 14:36:59 +03005173static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
5174{
5175 int max_cdclk_freq = dev_priv->max_cdclk_freq;
5176
5177 if (INTEL_INFO(dev_priv)->gen >= 9 ||
5178 IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
5179 return max_cdclk_freq;
5180 else if (IS_CHERRYVIEW(dev_priv))
5181 return max_cdclk_freq*95/100;
5182 else if (INTEL_INFO(dev_priv)->gen < 4)
5183 return 2*max_cdclk_freq*90/100;
5184 else
5185 return max_cdclk_freq*90/100;
5186}
5187
Ville Syrjäläb2045352016-05-13 23:41:27 +03005188static int skl_calc_cdclk(int max_pixclk, int vco);
5189
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005190static void intel_update_max_cdclk(struct drm_device *dev)
5191{
5192 struct drm_i915_private *dev_priv = dev->dev_private;
5193
Rodrigo Vivief11bdb2015-10-28 04:16:45 -07005194 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005195 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
Ville Syrjäläb2045352016-05-13 23:41:27 +03005196 int max_cdclk, vco;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005197
Ville Syrjäläb2045352016-05-13 23:41:27 +03005198 vco = dev_priv->skl_preferred_vco_freq;
Ville Syrjälä63911d72016-05-13 23:41:32 +03005199 WARN_ON(vco != 8100000 && vco != 8640000);
Ville Syrjäläb2045352016-05-13 23:41:27 +03005200
5201 /*
5202 * Use the lower (vco 8640) cdclk values as a
5203 * first guess. skl_calc_cdclk() will correct it
5204 * if the preferred vco is 8100 instead.
5205 */
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005206 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005207 max_cdclk = 617143;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005208 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
Ville Syrjäläb2045352016-05-13 23:41:27 +03005209 max_cdclk = 540000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005210 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
Ville Syrjäläb2045352016-05-13 23:41:27 +03005211 max_cdclk = 432000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005212 else
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005213 max_cdclk = 308571;
Ville Syrjäläb2045352016-05-13 23:41:27 +03005214
5215 dev_priv->max_cdclk_freq = skl_calc_cdclk(max_cdclk, vco);
Matt Roper281c1142016-04-05 14:37:19 -07005216 } else if (IS_BROXTON(dev)) {
5217 dev_priv->max_cdclk_freq = 624000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005218 } else if (IS_BROADWELL(dev)) {
5219 /*
5220 * FIXME with extra cooling we can allow
5221 * 540 MHz for ULX and 675 Mhz for ULT.
5222 * How can we know if extra cooling is
5223 * available? PCI ID, VTB, something else?
5224 */
5225 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5226 dev_priv->max_cdclk_freq = 450000;
5227 else if (IS_BDW_ULX(dev))
5228 dev_priv->max_cdclk_freq = 450000;
5229 else if (IS_BDW_ULT(dev))
5230 dev_priv->max_cdclk_freq = 540000;
5231 else
5232 dev_priv->max_cdclk_freq = 675000;
Mika Kahola0904dea2015-06-12 10:11:32 +03005233 } else if (IS_CHERRYVIEW(dev)) {
5234 dev_priv->max_cdclk_freq = 320000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005235 } else if (IS_VALLEYVIEW(dev)) {
5236 dev_priv->max_cdclk_freq = 400000;
5237 } else {
5238 /* otherwise assume cdclk is fixed */
5239 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5240 }
5241
Mika Kaholaadafdc62015-08-18 14:36:59 +03005242 dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
5243
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005244 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5245 dev_priv->max_cdclk_freq);
Mika Kaholaadafdc62015-08-18 14:36:59 +03005246
5247 DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
5248 dev_priv->max_dotclk_freq);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005249}
5250
5251static void intel_update_cdclk(struct drm_device *dev)
5252{
5253 struct drm_i915_private *dev_priv = dev->dev_private;
5254
5255 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
Ville Syrjälä2f2a1212016-05-13 23:41:25 +03005256
Ville Syrjälä83d7c812016-05-13 23:41:35 +03005257 if (INTEL_GEN(dev_priv) >= 9)
Ville Syrjälä709e05c2016-05-13 23:41:33 +03005258 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz, VCO: %d kHz, ref: %d kHz\n",
5259 dev_priv->cdclk_freq, dev_priv->cdclk_pll.vco,
5260 dev_priv->cdclk_pll.ref);
Ville Syrjälä2f2a1212016-05-13 23:41:25 +03005261 else
5262 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5263 dev_priv->cdclk_freq);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005264
5265 /*
Ville Syrjäläb5d99ff2016-04-26 19:46:34 +03005266 * 9:0 CMBUS [sic] CDCLK frequency (cdfreq):
5267 * Programmng [sic] note: bit[9:2] should be programmed to the number
5268 * of cdclk that generates 4MHz reference clock freq which is used to
5269 * generate GMBus clock. This will vary with the cdclk freq.
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005270 */
Ville Syrjäläb5d99ff2016-04-26 19:46:34 +03005271 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005272 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005273}
5274
Ville Syrjälä92891e42016-05-11 22:44:45 +03005275/* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5276static int skl_cdclk_decimal(int cdclk)
5277{
5278 return DIV_ROUND_CLOSEST(cdclk - 1000, 500);
5279}
5280
Ville Syrjälä2b730012016-05-13 23:41:34 +03005281static void bxt_de_pll_disable(struct drm_i915_private *dev_priv)
5282{
5283 I915_WRITE(BXT_DE_PLL_ENABLE, 0);
5284
5285 /* Timeout 200us */
5286 if (wait_for((I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK) == 0, 1))
5287 DRM_ERROR("timeout waiting for DE PLL unlock\n");
Ville Syrjälä83d7c812016-05-13 23:41:35 +03005288
5289 dev_priv->cdclk_pll.vco = 0;
Ville Syrjälä2b730012016-05-13 23:41:34 +03005290}
5291
5292static void bxt_de_pll_enable(struct drm_i915_private *dev_priv, u32 ratio)
5293{
5294 u32 val;
5295
5296 val = I915_READ(BXT_DE_PLL_CTL);
5297 val &= ~BXT_DE_PLL_RATIO_MASK;
5298 val |= ratio;
5299 I915_WRITE(BXT_DE_PLL_CTL, val);
5300
5301 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5302
5303 /* Timeout 200us */
5304 if (wait_for((I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK) != 0, 1))
5305 DRM_ERROR("timeout waiting for DE PLL lock\n");
Ville Syrjälä83d7c812016-05-13 23:41:35 +03005306
5307 dev_priv->cdclk_pll.vco = ratio * dev_priv->cdclk_pll.ref;
Ville Syrjälä2b730012016-05-13 23:41:34 +03005308}
5309
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005310static void broxton_set_cdclk(struct drm_i915_private *dev_priv, int cdclk)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305311{
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305312 uint32_t divider;
5313 uint32_t ratio;
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005314 uint32_t current_cdclk;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305315 int ret;
5316
5317 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005318 switch (cdclk) {
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305319 case 144000:
5320 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5321 ratio = BXT_DE_PLL_RATIO(60);
5322 break;
5323 case 288000:
5324 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5325 ratio = BXT_DE_PLL_RATIO(60);
5326 break;
5327 case 384000:
5328 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5329 ratio = BXT_DE_PLL_RATIO(60);
5330 break;
5331 case 576000:
5332 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5333 ratio = BXT_DE_PLL_RATIO(60);
5334 break;
5335 case 624000:
5336 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5337 ratio = BXT_DE_PLL_RATIO(65);
5338 break;
5339 case 19200:
5340 /*
5341 * Bypass frequency with DE PLL disabled. Init ratio, divider
5342 * to suppress GCC warning.
5343 */
5344 ratio = 0;
5345 divider = 0;
5346 break;
5347 default:
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005348 DRM_ERROR("unsupported CDCLK freq %d", cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305349
5350 return;
5351 }
5352
5353 mutex_lock(&dev_priv->rps.hw_lock);
5354 /* Inform power controller of upcoming frequency change */
5355 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5356 0x80000000);
5357 mutex_unlock(&dev_priv->rps.hw_lock);
5358
5359 if (ret) {
5360 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005361 ret, cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305362 return;
5363 }
5364
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005365 current_cdclk = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305366 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005367 current_cdclk = current_cdclk * 500 + 1000;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305368
5369 /*
5370 * DE PLL has to be disabled when
5371 * - setting to 19.2MHz (bypass, PLL isn't used)
5372 * - before setting to 624MHz (PLL needs toggling)
5373 * - before setting to any frequency from 624MHz (PLL needs toggling)
5374 */
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005375 if (cdclk == 19200 || cdclk == 624000 ||
5376 current_cdclk == 624000) {
Ville Syrjälä2b730012016-05-13 23:41:34 +03005377 bxt_de_pll_disable(dev_priv);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305378 }
5379
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005380 if (cdclk != 19200) {
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305381 uint32_t val;
5382
Ville Syrjälä2b730012016-05-13 23:41:34 +03005383 bxt_de_pll_enable(dev_priv, ratio);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305384
Ville Syrjäläb8e75702016-05-11 22:44:52 +03005385 val = divider | skl_cdclk_decimal(cdclk);
Ville Syrjälä7fe62752016-05-11 22:44:51 +03005386 /*
5387 * FIXME if only the cd2x divider needs changing, it could be done
5388 * without shutting off the pipe (if only one pipe is active).
5389 */
5390 val |= BXT_CDCLK_CD2X_PIPE_NONE;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305391 /*
5392 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5393 * enable otherwise.
5394 */
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005395 if (cdclk >= 500000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305396 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305397 I915_WRITE(CDCLK_CTL, val);
5398 }
5399
5400 mutex_lock(&dev_priv->rps.hw_lock);
5401 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005402 DIV_ROUND_UP(cdclk, 25000));
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305403 mutex_unlock(&dev_priv->rps.hw_lock);
5404
5405 if (ret) {
5406 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005407 ret, cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305408 return;
5409 }
5410
Imre Deakc6c46962016-04-01 16:02:40 +03005411 intel_update_cdclk(dev_priv->dev);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305412}
5413
Imre Deakc2e001e2016-04-01 16:02:43 +03005414static bool broxton_cdclk_is_enabled(struct drm_i915_private *dev_priv)
5415{
5416 if (!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE))
5417 return false;
5418
5419 /* TODO: Check for a valid CDCLK rate */
5420
Imre Deakc2e001e2016-04-01 16:02:43 +03005421 return true;
5422}
5423
Imre Deakadc7f042016-04-04 17:27:10 +03005424bool broxton_cdclk_verify_state(struct drm_i915_private *dev_priv)
5425{
5426 return broxton_cdclk_is_enabled(dev_priv);
5427}
5428
Imre Deakc6c46962016-04-01 16:02:40 +03005429void broxton_init_cdclk(struct drm_i915_private *dev_priv)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305430{
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305431 /* check if cd clock is enabled */
Imre Deakc2e001e2016-04-01 16:02:43 +03005432 if (broxton_cdclk_is_enabled(dev_priv)) {
5433 DRM_DEBUG_KMS("CDCLK already enabled, won't reprogram it\n");
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305434 return;
5435 }
5436
Imre Deakc2e001e2016-04-01 16:02:43 +03005437 DRM_DEBUG_KMS("CDCLK not enabled, enabling it\n");
5438
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305439 /*
5440 * FIXME:
5441 * - The initial CDCLK needs to be read from VBT.
5442 * Need to make this change after VBT has changes for BXT.
5443 * - check if setting the max (or any) cdclk freq is really necessary
5444 * here, it belongs to modeset time
5445 */
Imre Deakc6c46962016-04-01 16:02:40 +03005446 broxton_set_cdclk(dev_priv, 624000);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305447}
5448
Imre Deakc6c46962016-04-01 16:02:40 +03005449void broxton_uninit_cdclk(struct drm_i915_private *dev_priv)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305450{
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305451 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
Imre Deakc6c46962016-04-01 16:02:40 +03005452 broxton_set_cdclk(dev_priv, 19200);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305453}
5454
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005455static int skl_calc_cdclk(int max_pixclk, int vco)
5456{
Ville Syrjälä63911d72016-05-13 23:41:32 +03005457 if (vco == 8640000) {
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005458 if (max_pixclk > 540000)
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005459 return 617143;
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005460 else if (max_pixclk > 432000)
5461 return 540000;
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005462 else if (max_pixclk > 308571)
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005463 return 432000;
5464 else
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005465 return 308571;
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005466 } else {
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005467 if (max_pixclk > 540000)
5468 return 675000;
5469 else if (max_pixclk > 450000)
5470 return 540000;
5471 else if (max_pixclk > 337500)
5472 return 450000;
5473 else
5474 return 337500;
5475 }
5476}
5477
Ville Syrjäläea617912016-05-13 23:41:24 +03005478static void
5479skl_dpll0_update(struct drm_i915_private *dev_priv)
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005480{
Ville Syrjäläea617912016-05-13 23:41:24 +03005481 u32 val;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005482
Ville Syrjälä709e05c2016-05-13 23:41:33 +03005483 dev_priv->cdclk_pll.ref = 24000;
5484
Ville Syrjäläea617912016-05-13 23:41:24 +03005485 val = I915_READ(LCPLL1_CTL);
5486 if ((val & LCPLL_PLL_ENABLE) == 0) {
Ville Syrjälä63911d72016-05-13 23:41:32 +03005487 dev_priv->cdclk_pll.vco = 0;
Ville Syrjäläea617912016-05-13 23:41:24 +03005488 return;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005489 }
5490
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005491 WARN_ON((val & LCPLL_PLL_LOCK) == 0);
5492
Ville Syrjäläea617912016-05-13 23:41:24 +03005493 val = I915_READ(DPLL_CTRL1);
5494
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005495 WARN_ON((val & (DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) |
5496 DPLL_CTRL1_SSC(SKL_DPLL0) |
5497 DPLL_CTRL1_OVERRIDE(SKL_DPLL0))) !=
5498 DPLL_CTRL1_OVERRIDE(SKL_DPLL0));
5499
Ville Syrjäläea617912016-05-13 23:41:24 +03005500 switch (val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) {
5501 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810, SKL_DPLL0):
5502 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1350, SKL_DPLL0):
5503 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1620, SKL_DPLL0):
5504 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2700, SKL_DPLL0):
Ville Syrjälä63911d72016-05-13 23:41:32 +03005505 dev_priv->cdclk_pll.vco = 8100000;
Ville Syrjäläea617912016-05-13 23:41:24 +03005506 break;
5507 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080, SKL_DPLL0):
5508 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2160, SKL_DPLL0):
Ville Syrjälä63911d72016-05-13 23:41:32 +03005509 dev_priv->cdclk_pll.vco = 8640000;
Ville Syrjäläea617912016-05-13 23:41:24 +03005510 break;
5511 default:
5512 MISSING_CASE(val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
Ville Syrjälä63911d72016-05-13 23:41:32 +03005513 dev_priv->cdclk_pll.vco = 0;
Ville Syrjäläea617912016-05-13 23:41:24 +03005514 break;
5515 }
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005516}
5517
Ville Syrjäläb2045352016-05-13 23:41:27 +03005518void skl_set_preferred_cdclk_vco(struct drm_i915_private *dev_priv, int vco)
5519{
5520 bool changed = dev_priv->skl_preferred_vco_freq != vco;
5521
5522 dev_priv->skl_preferred_vco_freq = vco;
5523
5524 if (changed)
5525 intel_update_max_cdclk(dev_priv->dev);
5526}
5527
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005528static void
Ville Syrjälä3861fc62016-05-11 22:44:50 +03005529skl_dpll0_enable(struct drm_i915_private *dev_priv, int vco)
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005530{
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005531 int min_cdclk = skl_calc_cdclk(0, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005532 u32 val;
5533
Ville Syrjälä63911d72016-05-13 23:41:32 +03005534 WARN_ON(vco != 8100000 && vco != 8640000);
Ville Syrjäläb2045352016-05-13 23:41:27 +03005535
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005536 /* select the minimum CDCLK before enabling DPLL 0 */
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005537 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_cdclk);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005538 I915_WRITE(CDCLK_CTL, val);
5539 POSTING_READ(CDCLK_CTL);
5540
5541 /*
5542 * We always enable DPLL0 with the lowest link rate possible, but still
5543 * taking into account the VCO required to operate the eDP panel at the
5544 * desired frequency. The usual DP link rates operate with a VCO of
5545 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5546 * The modeset code is responsible for the selection of the exact link
5547 * rate later on, with the constraint of choosing a frequency that
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005548 * works with vco.
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005549 */
5550 val = I915_READ(DPLL_CTRL1);
5551
5552 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5553 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5554 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
Ville Syrjälä63911d72016-05-13 23:41:32 +03005555 if (vco == 8640000)
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005556 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5557 SKL_DPLL0);
5558 else
5559 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5560 SKL_DPLL0);
5561
5562 I915_WRITE(DPLL_CTRL1, val);
5563 POSTING_READ(DPLL_CTRL1);
5564
5565 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5566
5567 if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
5568 DRM_ERROR("DPLL0 not locked\n");
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005569
Ville Syrjälä63911d72016-05-13 23:41:32 +03005570 dev_priv->cdclk_pll.vco = vco;
Ville Syrjäläb2045352016-05-13 23:41:27 +03005571
5572 /* We'll want to keep using the current vco from now on. */
5573 skl_set_preferred_cdclk_vco(dev_priv, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005574}
5575
Ville Syrjälä430e05d2016-05-11 22:44:47 +03005576static void
5577skl_dpll0_disable(struct drm_i915_private *dev_priv)
5578{
5579 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
5580 if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
5581 DRM_ERROR("Couldn't disable DPLL0\n");
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005582
Ville Syrjälä63911d72016-05-13 23:41:32 +03005583 dev_priv->cdclk_pll.vco = 0;
Ville Syrjälä430e05d2016-05-11 22:44:47 +03005584}
5585
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005586static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5587{
5588 int ret;
5589 u32 val;
5590
5591 /* inform PCU we want to change CDCLK */
5592 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5593 mutex_lock(&dev_priv->rps.hw_lock);
5594 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5595 mutex_unlock(&dev_priv->rps.hw_lock);
5596
5597 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5598}
5599
5600static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5601{
5602 unsigned int i;
5603
5604 for (i = 0; i < 15; i++) {
5605 if (skl_cdclk_pcu_ready(dev_priv))
5606 return true;
5607 udelay(10);
5608 }
5609
5610 return false;
5611}
5612
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005613static void skl_set_cdclk(struct drm_i915_private *dev_priv, int cdclk, int vco)
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005614{
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005615 struct drm_device *dev = dev_priv->dev;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005616 u32 freq_select, pcu_ack;
5617
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005618 WARN_ON((cdclk == 24000) != (vco == 0));
5619
Ville Syrjälä63911d72016-05-13 23:41:32 +03005620 DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005621
5622 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5623 DRM_ERROR("failed to inform PCU about cdclk change\n");
5624 return;
5625 }
5626
5627 /* set CDCLK_CTL */
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005628 switch (cdclk) {
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005629 case 450000:
5630 case 432000:
5631 freq_select = CDCLK_FREQ_450_432;
5632 pcu_ack = 1;
5633 break;
5634 case 540000:
5635 freq_select = CDCLK_FREQ_540;
5636 pcu_ack = 2;
5637 break;
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005638 case 308571:
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005639 case 337500:
5640 default:
5641 freq_select = CDCLK_FREQ_337_308;
5642 pcu_ack = 0;
5643 break;
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005644 case 617143:
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005645 case 675000:
5646 freq_select = CDCLK_FREQ_675_617;
5647 pcu_ack = 3;
5648 break;
5649 }
5650
Ville Syrjälä63911d72016-05-13 23:41:32 +03005651 if (dev_priv->cdclk_pll.vco != 0 &&
5652 dev_priv->cdclk_pll.vco != vco)
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005653 skl_dpll0_disable(dev_priv);
5654
Ville Syrjälä63911d72016-05-13 23:41:32 +03005655 if (dev_priv->cdclk_pll.vco != vco)
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005656 skl_dpll0_enable(dev_priv, vco);
5657
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005658 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(cdclk));
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005659 POSTING_READ(CDCLK_CTL);
5660
5661 /* inform PCU of the change */
5662 mutex_lock(&dev_priv->rps.hw_lock);
5663 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5664 mutex_unlock(&dev_priv->rps.hw_lock);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005665
5666 intel_update_cdclk(dev);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005667}
5668
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005669static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv);
5670
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005671void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5672{
Ville Syrjälä709e05c2016-05-13 23:41:33 +03005673 skl_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref, 0);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005674}
5675
5676void skl_init_cdclk(struct drm_i915_private *dev_priv)
5677{
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005678 int cdclk, vco;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005679
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005680 skl_sanitize_cdclk(dev_priv);
5681
Ville Syrjälä63911d72016-05-13 23:41:32 +03005682 if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0) {
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005683 /*
5684 * Use the current vco as our initial
5685 * guess as to what the preferred vco is.
5686 */
5687 if (dev_priv->skl_preferred_vco_freq == 0)
5688 skl_set_preferred_cdclk_vco(dev_priv,
Ville Syrjälä63911d72016-05-13 23:41:32 +03005689 dev_priv->cdclk_pll.vco);
Ville Syrjälä70c2c182016-05-13 23:41:30 +03005690 return;
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005691 }
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005692
Ville Syrjälä70c2c182016-05-13 23:41:30 +03005693 vco = dev_priv->skl_preferred_vco_freq;
5694 if (vco == 0)
Ville Syrjälä63911d72016-05-13 23:41:32 +03005695 vco = 8100000;
Ville Syrjälä70c2c182016-05-13 23:41:30 +03005696 cdclk = skl_calc_cdclk(0, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005697
Ville Syrjälä70c2c182016-05-13 23:41:30 +03005698 skl_set_cdclk(dev_priv, cdclk, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005699}
5700
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005701static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305702{
Ville Syrjälä09492492016-05-13 23:41:28 +03005703 uint32_t cdctl, expected;
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305704
Shobhit Kumarf1b391a2015-11-05 18:05:32 +05305705 /*
5706 * check if the pre-os intialized the display
5707 * There is SWF18 scratchpad register defined which is set by the
5708 * pre-os which can be used by the OS drivers to check the status
5709 */
5710 if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
5711 goto sanitize;
5712
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305713 /* Is PLL enabled and locked ? */
Ville Syrjälä09492492016-05-13 23:41:28 +03005714 if ((I915_READ(LCPLL1_CTL) & (LCPLL_PLL_ENABLE | LCPLL_PLL_LOCK)) !=
5715 (LCPLL_PLL_ENABLE | LCPLL_PLL_LOCK))
5716 goto sanitize;
5717
5718 if ((I915_READ(DPLL_CTRL1) & (DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) |
5719 DPLL_CTRL1_SSC(SKL_DPLL0) |
5720 DPLL_CTRL1_OVERRIDE(SKL_DPLL0))) !=
5721 DPLL_CTRL1_OVERRIDE(SKL_DPLL0))
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305722 goto sanitize;
5723
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005724 intel_update_cdclk(dev_priv->dev);
5725
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305726 /* DPLL okay; verify the cdclock
5727 *
5728 * Noticed in some instances that the freq selection is correct but
5729 * decimal part is programmed wrong from BIOS where pre-os does not
5730 * enable display. Verify the same as well.
5731 */
Ville Syrjälä09492492016-05-13 23:41:28 +03005732 cdctl = I915_READ(CDCLK_CTL);
5733 expected = (cdctl & CDCLK_FREQ_SEL_MASK) |
5734 skl_cdclk_decimal(dev_priv->cdclk_freq);
5735 if (cdctl == expected)
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305736 /* All well; nothing to sanitize */
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005737 return;
5738
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305739sanitize:
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005740 DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
Clint Taylorc89e39f2016-05-13 23:41:21 +03005741
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005742 /* force cdclk programming */
5743 dev_priv->cdclk_freq = 0;
5744 /* force full PLL disable + enable */
Ville Syrjälä63911d72016-05-13 23:41:32 +03005745 dev_priv->cdclk_pll.vco = -1;
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305746}
5747
Jesse Barnes30a970c2013-11-04 13:48:12 -08005748/* Adjust CDclk dividers to allow high res or save power if possible */
5749static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5750{
5751 struct drm_i915_private *dev_priv = dev->dev_private;
5752 u32 val, cmd;
5753
Vandana Kannan164dfd22014-11-24 13:37:41 +05305754 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5755 != dev_priv->cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02005756
Ville Syrjälädfcab172014-06-13 13:37:47 +03005757 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08005758 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03005759 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005760 cmd = 1;
5761 else
5762 cmd = 0;
5763
5764 mutex_lock(&dev_priv->rps.hw_lock);
5765 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5766 val &= ~DSPFREQGUAR_MASK;
5767 val |= (cmd << DSPFREQGUAR_SHIFT);
5768 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5769 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5770 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5771 50)) {
5772 DRM_ERROR("timed out waiting for CDclk change\n");
5773 }
5774 mutex_unlock(&dev_priv->rps.hw_lock);
5775
Ville Syrjälä54433e92015-05-26 20:42:31 +03005776 mutex_lock(&dev_priv->sb_lock);
5777
Ville Syrjälädfcab172014-06-13 13:37:47 +03005778 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005779 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005780
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005781 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005782
Jesse Barnes30a970c2013-11-04 13:48:12 -08005783 /* adjust cdclk divider */
5784 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Vandana Kannan87d5d252015-09-24 23:29:17 +03005785 val &= ~CCK_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005786 val |= divider;
5787 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03005788
5789 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
Vandana Kannan87d5d252015-09-24 23:29:17 +03005790 CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
Ville Syrjäläa877e802014-06-13 13:37:52 +03005791 50))
5792 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08005793 }
5794
Jesse Barnes30a970c2013-11-04 13:48:12 -08005795 /* adjust self-refresh exit latency value */
5796 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5797 val &= ~0x7f;
5798
5799 /*
5800 * For high bandwidth configs, we set a higher latency in the bunit
5801 * so that the core display fetch happens in time to avoid underruns.
5802 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03005803 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005804 val |= 4500 / 250; /* 4.5 usec */
5805 else
5806 val |= 3000 / 250; /* 3.0 usec */
5807 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
Ville Syrjälä54433e92015-05-26 20:42:31 +03005808
Ville Syrjäläa5805162015-05-26 20:42:30 +03005809 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005810
Ville Syrjäläb6283052015-06-03 15:45:07 +03005811 intel_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005812}
5813
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005814static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5815{
5816 struct drm_i915_private *dev_priv = dev->dev_private;
5817 u32 val, cmd;
5818
Vandana Kannan164dfd22014-11-24 13:37:41 +05305819 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5820 != dev_priv->cdclk_freq);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005821
5822 switch (cdclk) {
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005823 case 333333:
5824 case 320000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005825 case 266667:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005826 case 200000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005827 break;
5828 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01005829 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005830 return;
5831 }
5832
Ville Syrjälä9d0d3fd2015-03-02 20:07:17 +02005833 /*
5834 * Specs are full of misinformation, but testing on actual
5835 * hardware has shown that we just need to write the desired
5836 * CCK divider into the Punit register.
5837 */
5838 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5839
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005840 mutex_lock(&dev_priv->rps.hw_lock);
5841 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5842 val &= ~DSPFREQGUAR_MASK_CHV;
5843 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5844 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5845 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5846 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5847 50)) {
5848 DRM_ERROR("timed out waiting for CDclk change\n");
5849 }
5850 mutex_unlock(&dev_priv->rps.hw_lock);
5851
Ville Syrjäläb6283052015-06-03 15:45:07 +03005852 intel_update_cdclk(dev);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005853}
5854
Jesse Barnes30a970c2013-11-04 13:48:12 -08005855static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5856 int max_pixclk)
5857{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005858 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005859 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005860
Jesse Barnes30a970c2013-11-04 13:48:12 -08005861 /*
5862 * Really only a few cases to deal with, as only 4 CDclks are supported:
5863 * 200MHz
5864 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005865 * 320/333MHz (depends on HPLL freq)
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005866 * 400MHz (VLV only)
5867 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5868 * of the lower bin and adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005869 *
5870 * We seem to get an unstable or solid color picture at 200MHz.
5871 * Not sure what's wrong. For now use 200MHz only when all pipes
5872 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08005873 */
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005874 if (!IS_CHERRYVIEW(dev_priv) &&
5875 max_pixclk > freq_320*limit/100)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005876 return 400000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005877 else if (max_pixclk > 266667*limit/100)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005878 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005879 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005880 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005881 else
5882 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005883}
5884
Ville Syrjäläc44deb62016-05-11 22:44:43 +03005885static int broxton_calc_cdclk(int max_pixclk)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005886{
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305887 /*
5888 * FIXME:
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305889 * - set 19.2MHz bypass frequency if there are no active pipes
5890 */
Ville Syrjälä760e1472016-05-11 22:44:46 +03005891 if (max_pixclk > 576000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305892 return 624000;
Ville Syrjälä760e1472016-05-11 22:44:46 +03005893 else if (max_pixclk > 384000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305894 return 576000;
Ville Syrjälä760e1472016-05-11 22:44:46 +03005895 else if (max_pixclk > 288000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305896 return 384000;
Ville Syrjälä760e1472016-05-11 22:44:46 +03005897 else if (max_pixclk > 144000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305898 return 288000;
5899 else
5900 return 144000;
5901}
5902
Maarten Lankhorste8788cb2016-02-16 10:25:11 +01005903/* Compute the max pixel clock for new configuration. */
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005904static int intel_mode_max_pixclk(struct drm_device *dev,
5905 struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005906{
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005907 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
5908 struct drm_i915_private *dev_priv = dev->dev_private;
5909 struct drm_crtc *crtc;
5910 struct drm_crtc_state *crtc_state;
5911 unsigned max_pixclk = 0, i;
5912 enum pipe pipe;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005913
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005914 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
5915 sizeof(intel_state->min_pixclk));
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005916
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005917 for_each_crtc_in_state(state, crtc, crtc_state, i) {
5918 int pixclk = 0;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005919
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005920 if (crtc_state->enable)
5921 pixclk = crtc_state->adjusted_mode.crtc_clock;
5922
5923 intel_state->min_pixclk[i] = pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005924 }
5925
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005926 for_each_pipe(dev_priv, pipe)
5927 max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
5928
Jesse Barnes30a970c2013-11-04 13:48:12 -08005929 return max_pixclk;
5930}
5931
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005932static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005933{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005934 struct drm_device *dev = state->dev;
5935 struct drm_i915_private *dev_priv = dev->dev_private;
5936 int max_pixclk = intel_mode_max_pixclk(dev, state);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005937 struct intel_atomic_state *intel_state =
5938 to_intel_atomic_state(state);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005939
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005940 intel_state->cdclk = intel_state->dev_cdclk =
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005941 valleyview_calc_cdclk(dev_priv, max_pixclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305942
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005943 if (!intel_state->active_crtcs)
5944 intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
5945
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005946 return 0;
5947}
Jesse Barnes30a970c2013-11-04 13:48:12 -08005948
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005949static int broxton_modeset_calc_cdclk(struct drm_atomic_state *state)
5950{
Ville Syrjälä4e5ca602016-05-11 22:44:44 +03005951 int max_pixclk = ilk_max_pixel_rate(state);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005952 struct intel_atomic_state *intel_state =
5953 to_intel_atomic_state(state);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005954
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005955 intel_state->cdclk = intel_state->dev_cdclk =
Ville Syrjäläc44deb62016-05-11 22:44:43 +03005956 broxton_calc_cdclk(max_pixclk);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005957
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005958 if (!intel_state->active_crtcs)
Ville Syrjäläc44deb62016-05-11 22:44:43 +03005959 intel_state->dev_cdclk = broxton_calc_cdclk(0);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005960
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005961 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005962}
5963
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005964static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
5965{
5966 unsigned int credits, default_credits;
5967
5968 if (IS_CHERRYVIEW(dev_priv))
5969 default_credits = PFI_CREDIT(12);
5970 else
5971 default_credits = PFI_CREDIT(8);
5972
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03005973 if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005974 /* CHV suggested value is 31 or 63 */
5975 if (IS_CHERRYVIEW(dev_priv))
Ville Syrjäläfcc00082015-05-26 20:22:40 +03005976 credits = PFI_CREDIT_63;
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005977 else
5978 credits = PFI_CREDIT(15);
5979 } else {
5980 credits = default_credits;
5981 }
5982
5983 /*
5984 * WA - write default credits before re-programming
5985 * FIXME: should we also set the resend bit here?
5986 */
5987 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5988 default_credits);
5989
5990 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5991 credits | PFI_CREDIT_RESEND);
5992
5993 /*
5994 * FIXME is this guaranteed to clear
5995 * immediately or should we poll for it?
5996 */
5997 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
5998}
5999
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006000static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006001{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006002 struct drm_device *dev = old_state->dev;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006003 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006004 struct intel_atomic_state *old_intel_state =
6005 to_intel_atomic_state(old_state);
6006 unsigned req_cdclk = old_intel_state->dev_cdclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006007
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006008 /*
6009 * FIXME: We can end up here with all power domains off, yet
6010 * with a CDCLK frequency other than the minimum. To account
6011 * for this take the PIPE-A power domain, which covers the HW
6012 * blocks needed for the following programming. This can be
6013 * removed once it's guaranteed that we get here either with
6014 * the minimum CDCLK set, or the required power domains
6015 * enabled.
6016 */
6017 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006018
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006019 if (IS_CHERRYVIEW(dev))
6020 cherryview_set_cdclk(dev, req_cdclk);
6021 else
6022 valleyview_set_cdclk(dev, req_cdclk);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006023
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006024 vlv_program_pfi_credits(dev_priv);
Imre Deak738c05c2014-11-19 16:25:37 +02006025
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006026 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006027}
6028
Jesse Barnes89b667f2013-04-18 14:51:36 -07006029static void valleyview_crtc_enable(struct drm_crtc *crtc)
6030{
6031 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006032 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006033 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6034 struct intel_encoder *encoder;
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006035 struct intel_crtc_state *pipe_config =
6036 to_intel_crtc_state(crtc->state);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006037 int pipe = intel_crtc->pipe;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006038
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006039 if (WARN_ON(intel_crtc->active))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006040 return;
6041
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006042 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306043 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006044
6045 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02006046 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006047
Ville Syrjäläc14b0482014-10-16 20:52:34 +03006048 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
6049 struct drm_i915_private *dev_priv = dev->dev_private;
6050
6051 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6052 I915_WRITE(CHV_CANVAS(pipe), 0);
6053 }
6054
Daniel Vetter5b18e572014-04-24 23:55:06 +02006055 i9xx_set_pipeconf(intel_crtc);
6056
Jesse Barnes89b667f2013-04-18 14:51:36 -07006057 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006058
Daniel Vettera72e4c92014-09-30 10:56:47 +02006059 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006060
Jesse Barnes89b667f2013-04-18 14:51:36 -07006061 for_each_encoder_on_crtc(dev, crtc, encoder)
6062 if (encoder->pre_pll_enable)
6063 encoder->pre_pll_enable(encoder);
6064
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006065 if (IS_CHERRYVIEW(dev)) {
6066 chv_prepare_pll(intel_crtc, intel_crtc->config);
6067 chv_enable_pll(intel_crtc, intel_crtc->config);
6068 } else {
6069 vlv_prepare_pll(intel_crtc, intel_crtc->config);
6070 vlv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006071 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07006072
6073 for_each_encoder_on_crtc(dev, crtc, encoder)
6074 if (encoder->pre_enable)
6075 encoder->pre_enable(encoder);
6076
Jesse Barnes2dd24552013-04-25 12:55:01 -07006077 i9xx_pfit_enable(intel_crtc);
6078
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006079 intel_color_load_luts(&pipe_config->base);
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006080
Ville Syrjäläcaed3612016-03-09 19:07:25 +02006081 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006082 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006083
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006084 assert_vblank_disabled(crtc);
6085 drm_crtc_vblank_on(crtc);
6086
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006087 for_each_encoder_on_crtc(dev, crtc, encoder)
6088 encoder->enable(encoder);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006089}
6090
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006091static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6092{
6093 struct drm_device *dev = crtc->base.dev;
6094 struct drm_i915_private *dev_priv = dev->dev_private;
6095
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006096 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6097 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006098}
6099
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006100static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006101{
6102 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006103 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006104 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006105 struct intel_encoder *encoder;
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006106 struct intel_crtc_state *pipe_config =
6107 to_intel_crtc_state(crtc->state);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006108 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08006109
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006110 if (WARN_ON(intel_crtc->active))
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006111 return;
6112
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006113 i9xx_set_pll_dividers(intel_crtc);
6114
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006115 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306116 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006117
6118 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02006119 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006120
Daniel Vetter5b18e572014-04-24 23:55:06 +02006121 i9xx_set_pipeconf(intel_crtc);
6122
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006123 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01006124
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006125 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006126 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006127
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02006128 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02006129 if (encoder->pre_enable)
6130 encoder->pre_enable(encoder);
6131
Daniel Vetterf6736a12013-06-05 13:34:30 +02006132 i9xx_enable_pll(intel_crtc);
6133
Jesse Barnes2dd24552013-04-25 12:55:01 -07006134 i9xx_pfit_enable(intel_crtc);
6135
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006136 intel_color_load_luts(&pipe_config->base);
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006137
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006138 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006139 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006140
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006141 assert_vblank_disabled(crtc);
6142 drm_crtc_vblank_on(crtc);
6143
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006144 for_each_encoder_on_crtc(dev, crtc, encoder)
6145 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006146}
6147
Daniel Vetter87476d62013-04-11 16:29:06 +02006148static void i9xx_pfit_disable(struct intel_crtc *crtc)
6149{
6150 struct drm_device *dev = crtc->base.dev;
6151 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02006152
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006153 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02006154 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02006155
6156 assert_pipe_disabled(dev_priv, crtc->pipe);
6157
Daniel Vetter328d8e82013-05-08 10:36:31 +02006158 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6159 I915_READ(PFIT_CONTROL));
6160 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02006161}
6162
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006163static void i9xx_crtc_disable(struct drm_crtc *crtc)
6164{
6165 struct drm_device *dev = crtc->dev;
6166 struct drm_i915_private *dev_priv = dev->dev_private;
6167 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006168 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006169 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006170
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006171 /*
6172 * On gen2 planes are double buffered but the pipe isn't, so we must
6173 * wait for planes to fully turn off before disabling the pipe.
6174 */
Ander Conselvan de Oliveira90e83e52016-03-22 10:11:24 +02006175 if (IS_GEN2(dev))
6176 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006177
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006178 for_each_encoder_on_crtc(dev, crtc, encoder)
6179 encoder->disable(encoder);
6180
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006181 drm_crtc_vblank_off(crtc);
6182 assert_vblank_disabled(crtc);
6183
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03006184 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006185
Daniel Vetter87476d62013-04-11 16:29:06 +02006186 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006187
Jesse Barnes89b667f2013-04-18 14:51:36 -07006188 for_each_encoder_on_crtc(dev, crtc, encoder)
6189 if (encoder->post_disable)
6190 encoder->post_disable(encoder);
6191
Jani Nikulaa65347b2015-11-27 12:21:46 +02006192 if (!intel_crtc->config->has_dsi_encoder) {
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006193 if (IS_CHERRYVIEW(dev))
6194 chv_disable_pll(dev_priv, pipe);
6195 else if (IS_VALLEYVIEW(dev))
6196 vlv_disable_pll(dev_priv, pipe);
6197 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03006198 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006199 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006200
Ville Syrjäläd6db9952015-07-08 23:45:49 +03006201 for_each_encoder_on_crtc(dev, crtc, encoder)
6202 if (encoder->post_pll_disable)
6203 encoder->post_pll_disable(encoder);
6204
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006205 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006206 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006207}
6208
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006209static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006210{
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006211 struct intel_encoder *encoder;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006212 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006213 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006214 enum intel_display_power_domain domain;
6215 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006216
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006217 if (!intel_crtc->active)
6218 return;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006219
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006220 if (to_intel_plane_state(crtc->primary->state)->visible) {
Maarten Lankhorst68858432016-05-17 15:07:52 +02006221 WARN_ON(list_empty(&intel_crtc->flip_work));
Maarten Lankhorstfc32b1f2015-10-19 17:09:23 +02006222
Ville Syrjälä2622a082016-03-09 19:07:26 +02006223 intel_pre_disable_primary_noatomic(crtc);
Maarten Lankhorst54a419612015-11-23 10:25:28 +01006224
6225 intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
6226 to_intel_plane_state(crtc->primary->state)->visible = false;
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006227 }
6228
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006229 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006230
6231 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was enabled, now disabled\n",
6232 crtc->base.id);
6233
6234 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
6235 crtc->state->active = false;
Matt Roper37d90782015-09-24 15:53:06 -07006236 intel_crtc->active = false;
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006237 crtc->enabled = false;
6238 crtc->state->connector_mask = 0;
6239 crtc->state->encoder_mask = 0;
6240
6241 for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
6242 encoder->base.crtc = NULL;
6243
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -02006244 intel_fbc_disable(intel_crtc);
Matt Roper37d90782015-09-24 15:53:06 -07006245 intel_update_watermarks(crtc);
Maarten Lankhorst1f7457b2015-07-13 11:55:05 +02006246 intel_disable_shared_dpll(intel_crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006247
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006248 domains = intel_crtc->enabled_power_domains;
6249 for_each_power_domain(domain, domains)
6250 intel_display_power_put(dev_priv, domain);
6251 intel_crtc->enabled_power_domains = 0;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006252
6253 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
6254 dev_priv->min_pixclk[intel_crtc->pipe] = 0;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006255}
6256
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006257/*
6258 * turn all crtc's off, but do not adjust state
6259 * This has to be paired with a call to intel_modeset_setup_hw_state.
6260 */
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006261int intel_display_suspend(struct drm_device *dev)
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006262{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006263 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006264 struct drm_atomic_state *state;
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006265 int ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006266
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006267 state = drm_atomic_helper_suspend(dev);
6268 ret = PTR_ERR_OR_ZERO(state);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006269 if (ret)
6270 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006271 else
6272 dev_priv->modeset_restore_state = state;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +02006273
6274 /*
6275 * Make sure all unpin_work completes before returning.
6276 */
6277 flush_workqueue(dev_priv->wq);
6278
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006279 return ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006280}
6281
Chris Wilsonea5b2132010-08-04 13:50:23 +01006282void intel_encoder_destroy(struct drm_encoder *encoder)
6283{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006284 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01006285
Chris Wilsonea5b2132010-08-04 13:50:23 +01006286 drm_encoder_cleanup(encoder);
6287 kfree(intel_encoder);
6288}
6289
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006290/* Cross check the actual hw state with our own modeset state tracking (and it's
6291 * internal consistency). */
Maarten Lankhorst03f476e2016-05-17 15:08:00 +02006292static void intel_connector_verify_state(struct intel_connector *connector,
6293 struct drm_connector_state *conn_state)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006294{
Maarten Lankhorst03f476e2016-05-17 15:08:00 +02006295 struct drm_crtc *crtc = conn_state->crtc;
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006296
6297 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6298 connector->base.base.id,
6299 connector->base.name);
6300
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006301 if (connector->get_hw_state(connector)) {
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006302 struct intel_encoder *encoder = connector->encoder;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006303
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006304 I915_STATE_WARN(!crtc,
6305 "connector enabled without attached crtc\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006306
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006307 if (!crtc)
6308 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006309
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006310 I915_STATE_WARN(!crtc->state->active,
6311 "connector is active, but attached crtc isn't\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006312
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006313 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006314 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006315
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006316 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006317 "atomic encoder doesn't match attached encoder\n");
Dave Airlie36cd7442014-05-02 13:44:18 +10006318
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006319 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006320 "attached encoder crtc differs from connector crtc\n");
6321 } else {
Maarten Lankhorst4d688a22015-08-05 12:37:06 +02006322 I915_STATE_WARN(crtc && crtc->state->active,
6323 "attached crtc is active, but connector isn't\n");
Maarten Lankhorst03f476e2016-05-17 15:08:00 +02006324 I915_STATE_WARN(!crtc && conn_state->best_encoder,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006325 "best encoder set without crtc!\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006326 }
6327}
6328
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006329int intel_connector_init(struct intel_connector *connector)
6330{
Maarten Lankhorst5350a032016-01-04 12:53:15 +01006331 drm_atomic_helper_connector_reset(&connector->base);
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006332
Maarten Lankhorst5350a032016-01-04 12:53:15 +01006333 if (!connector->base.state)
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006334 return -ENOMEM;
6335
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006336 return 0;
6337}
6338
6339struct intel_connector *intel_connector_alloc(void)
6340{
6341 struct intel_connector *connector;
6342
6343 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6344 if (!connector)
6345 return NULL;
6346
6347 if (intel_connector_init(connector) < 0) {
6348 kfree(connector);
6349 return NULL;
6350 }
6351
6352 return connector;
6353}
6354
Daniel Vetterf0947c32012-07-02 13:10:34 +02006355/* Simple connector->get_hw_state implementation for encoders that support only
6356 * one connector and no cloning and hence the encoder state determines the state
6357 * of the connector. */
6358bool intel_connector_get_hw_state(struct intel_connector *connector)
6359{
Daniel Vetter24929352012-07-02 20:28:59 +02006360 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02006361 struct intel_encoder *encoder = connector->encoder;
6362
6363 return encoder->get_hw_state(encoder, &pipe);
6364}
6365
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006366static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006367{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006368 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6369 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006370
6371 return 0;
6372}
6373
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006374static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006375 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006376{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006377 struct drm_atomic_state *state = pipe_config->base.state;
6378 struct intel_crtc *other_crtc;
6379 struct intel_crtc_state *other_crtc_state;
6380
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006381 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6382 pipe_name(pipe), pipe_config->fdi_lanes);
6383 if (pipe_config->fdi_lanes > 4) {
6384 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6385 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006386 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006387 }
6388
Paulo Zanonibafb6552013-11-02 21:07:44 -07006389 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006390 if (pipe_config->fdi_lanes > 2) {
6391 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6392 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006393 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006394 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006395 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006396 }
6397 }
6398
6399 if (INTEL_INFO(dev)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006400 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006401
6402 /* Ivybridge 3 pipe is really complicated */
6403 switch (pipe) {
6404 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006405 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006406 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006407 if (pipe_config->fdi_lanes <= 2)
6408 return 0;
6409
6410 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6411 other_crtc_state =
6412 intel_atomic_get_crtc_state(state, other_crtc);
6413 if (IS_ERR(other_crtc_state))
6414 return PTR_ERR(other_crtc_state);
6415
6416 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006417 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6418 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006419 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006420 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006421 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006422 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02006423 if (pipe_config->fdi_lanes > 2) {
6424 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6425 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006426 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02006427 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006428
6429 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6430 other_crtc_state =
6431 intel_atomic_get_crtc_state(state, other_crtc);
6432 if (IS_ERR(other_crtc_state))
6433 return PTR_ERR(other_crtc_state);
6434
6435 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006436 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006437 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006438 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006439 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006440 default:
6441 BUG();
6442 }
6443}
6444
Daniel Vettere29c22c2013-02-21 00:00:16 +01006445#define RETRY 1
6446static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006447 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02006448{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006449 struct drm_device *dev = intel_crtc->base.dev;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006450 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006451 int lane, link_bw, fdi_dotclock, ret;
6452 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006453
Daniel Vettere29c22c2013-02-21 00:00:16 +01006454retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02006455 /* FDI is a binary signal running at ~2.7GHz, encoding
6456 * each output octet as 10 bits. The actual frequency
6457 * is stored as a divider into a 100MHz clock, and the
6458 * mode pixel clock is stored in units of 1KHz.
6459 * Hence the bw of each lane in terms of the mode signal
6460 * is:
6461 */
Ville Syrjälä21a727b2016-02-17 21:41:10 +02006462 link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006463
Damien Lespiau241bfc32013-09-25 16:45:37 +01006464 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006465
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006466 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006467 pipe_config->pipe_bpp);
6468
6469 pipe_config->fdi_lanes = lane;
6470
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006471 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006472 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006473
Ville Syrjäläe3b247d2016-02-17 21:41:09 +02006474 ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006475 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01006476 pipe_config->pipe_bpp -= 2*3;
6477 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6478 pipe_config->pipe_bpp);
6479 needs_recompute = true;
6480 pipe_config->bw_constrained = true;
6481
6482 goto retry;
6483 }
6484
6485 if (needs_recompute)
6486 return RETRY;
6487
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006488 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006489}
6490
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006491static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6492 struct intel_crtc_state *pipe_config)
6493{
6494 if (pipe_config->pipe_bpp > 24)
6495 return false;
6496
6497 /* HSW can handle pixel rate up to cdclk? */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03006498 if (IS_HASWELL(dev_priv))
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006499 return true;
6500
6501 /*
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03006502 * We compare against max which means we must take
6503 * the increased cdclk requirement into account when
6504 * calculating the new cdclk.
6505 *
6506 * Should measure whether using a lower cdclk w/o IPS
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006507 */
6508 return ilk_pipe_pixel_rate(pipe_config) <=
6509 dev_priv->max_cdclk_freq * 95 / 100;
6510}
6511
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006512static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006513 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006514{
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006515 struct drm_device *dev = crtc->base.dev;
6516 struct drm_i915_private *dev_priv = dev->dev_private;
6517
Jani Nikulad330a952014-01-21 11:24:25 +02006518 pipe_config->ips_enabled = i915.enable_ips &&
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006519 hsw_crtc_supports_ips(crtc) &&
6520 pipe_config_supports_ips(dev_priv, pipe_config);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006521}
6522
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006523static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
6524{
6525 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6526
6527 /* GDG double wide on either pipe, otherwise pipe A only */
6528 return INTEL_INFO(dev_priv)->gen < 4 &&
6529 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
6530}
6531
Daniel Vettera43f6e02013-06-07 23:10:32 +02006532static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006533 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006534{
Daniel Vettera43f6e02013-06-07 23:10:32 +02006535 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02006536 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006537 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Chris Wilson89749352010-09-12 18:25:19 +01006538
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006539 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006540 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006541 int clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006542
6543 /*
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006544 * Enable double wide mode when the dot clock
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006545 * is > 90% of the (display) core speed.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006546 */
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006547 if (intel_crtc_supports_double_wide(crtc) &&
6548 adjusted_mode->crtc_clock > clock_limit) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006549 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006550 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006551 }
6552
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006553 if (adjusted_mode->crtc_clock > clock_limit) {
6554 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
6555 adjusted_mode->crtc_clock, clock_limit,
6556 yesno(pipe_config->double_wide));
Daniel Vettere29c22c2013-02-21 00:00:16 +01006557 return -EINVAL;
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006558 }
Zhenyu Wang2c072452009-06-05 15:38:42 +08006559 }
Chris Wilson89749352010-09-12 18:25:19 +01006560
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006561 /*
6562 * Pipe horizontal size must be even in:
6563 * - DVO ganged mode
6564 * - LVDS dual channel mode
6565 * - Double wide pipe
6566 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006567 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006568 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6569 pipe_config->pipe_src_w &= ~1;
6570
Damien Lespiau8693a822013-05-03 18:48:11 +01006571 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6572 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03006573 */
6574 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
Ville Syrjäläaad941d2015-09-25 16:38:56 +03006575 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006576 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03006577
Damien Lespiauf5adf942013-06-24 18:29:34 +01006578 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02006579 hsw_compute_ips_config(crtc, pipe_config);
6580
Daniel Vetter877d48d2013-04-19 11:24:43 +02006581 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02006582 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006583
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +02006584 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006585}
6586
Ville Syrjälä1652d192015-03-31 14:12:01 +03006587static int skylake_get_display_clock_speed(struct drm_device *dev)
6588{
6589 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläea617912016-05-13 23:41:24 +03006590 uint32_t cdctl;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006591
Ville Syrjäläea617912016-05-13 23:41:24 +03006592 skl_dpll0_update(dev_priv);
6593
Ville Syrjälä63911d72016-05-13 23:41:32 +03006594 if (dev_priv->cdclk_pll.vco == 0)
Ville Syrjälä709e05c2016-05-13 23:41:33 +03006595 return dev_priv->cdclk_pll.ref;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006596
Ville Syrjäläea617912016-05-13 23:41:24 +03006597 cdctl = I915_READ(CDCLK_CTL);
Ville Syrjälä1652d192015-03-31 14:12:01 +03006598
Ville Syrjälä63911d72016-05-13 23:41:32 +03006599 if (dev_priv->cdclk_pll.vco == 8640000) {
Ville Syrjälä1652d192015-03-31 14:12:01 +03006600 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6601 case CDCLK_FREQ_450_432:
6602 return 432000;
6603 case CDCLK_FREQ_337_308:
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03006604 return 308571;
Ville Syrjäläea617912016-05-13 23:41:24 +03006605 case CDCLK_FREQ_540:
6606 return 540000;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006607 case CDCLK_FREQ_675_617:
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03006608 return 617143;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006609 default:
Ville Syrjäläea617912016-05-13 23:41:24 +03006610 MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
Ville Syrjälä1652d192015-03-31 14:12:01 +03006611 }
6612 } else {
Ville Syrjälä1652d192015-03-31 14:12:01 +03006613 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6614 case CDCLK_FREQ_450_432:
6615 return 450000;
6616 case CDCLK_FREQ_337_308:
6617 return 337500;
Ville Syrjäläea617912016-05-13 23:41:24 +03006618 case CDCLK_FREQ_540:
6619 return 540000;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006620 case CDCLK_FREQ_675_617:
6621 return 675000;
6622 default:
Ville Syrjäläea617912016-05-13 23:41:24 +03006623 MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
Ville Syrjälä1652d192015-03-31 14:12:01 +03006624 }
6625 }
6626
Ville Syrjälä709e05c2016-05-13 23:41:33 +03006627 return dev_priv->cdclk_pll.ref;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006628}
6629
Ville Syrjälä83d7c812016-05-13 23:41:35 +03006630static void bxt_de_pll_update(struct drm_i915_private *dev_priv)
6631{
6632 u32 val;
6633
6634 dev_priv->cdclk_pll.ref = 19200;
6635
6636 val = I915_READ(BXT_DE_PLL_ENABLE);
6637 if ((val & BXT_DE_PLL_PLL_ENABLE) == 0) {
6638 dev_priv->cdclk_pll.vco = 0;
6639 return;
6640 }
6641
6642 WARN_ON((val & BXT_DE_PLL_LOCK) == 0);
6643
6644 val = I915_READ(BXT_DE_PLL_CTL);
6645 dev_priv->cdclk_pll.vco = (val & BXT_DE_PLL_RATIO_MASK) *
6646 dev_priv->cdclk_pll.ref;
6647}
6648
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006649static int broxton_get_display_clock_speed(struct drm_device *dev)
6650{
6651 struct drm_i915_private *dev_priv = to_i915(dev);
6652 uint32_t cdctl = I915_READ(CDCLK_CTL);
6653 uint32_t pll_ratio = I915_READ(BXT_DE_PLL_CTL) & BXT_DE_PLL_RATIO_MASK;
6654 uint32_t pll_enab = I915_READ(BXT_DE_PLL_ENABLE);
6655 int cdclk;
6656
Ville Syrjälä83d7c812016-05-13 23:41:35 +03006657 bxt_de_pll_update(dev_priv);
6658
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006659 if (!(pll_enab & BXT_DE_PLL_PLL_ENABLE))
6660 return 19200;
6661
6662 cdclk = 19200 * pll_ratio / 2;
6663
6664 switch (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) {
6665 case BXT_CDCLK_CD2X_DIV_SEL_1:
6666 return cdclk; /* 576MHz or 624MHz */
6667 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
6668 return cdclk * 2 / 3; /* 384MHz */
6669 case BXT_CDCLK_CD2X_DIV_SEL_2:
6670 return cdclk / 2; /* 288MHz */
6671 case BXT_CDCLK_CD2X_DIV_SEL_4:
6672 return cdclk / 4; /* 144MHz */
6673 }
6674
6675 /* error case, do as if DE PLL isn't enabled */
6676 return 19200;
6677}
6678
Ville Syrjälä1652d192015-03-31 14:12:01 +03006679static int broadwell_get_display_clock_speed(struct drm_device *dev)
6680{
6681 struct drm_i915_private *dev_priv = dev->dev_private;
6682 uint32_t lcpll = I915_READ(LCPLL_CTL);
6683 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6684
6685 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6686 return 800000;
6687 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6688 return 450000;
6689 else if (freq == LCPLL_CLK_FREQ_450)
6690 return 450000;
6691 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6692 return 540000;
6693 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6694 return 337500;
6695 else
6696 return 675000;
6697}
6698
6699static int haswell_get_display_clock_speed(struct drm_device *dev)
6700{
6701 struct drm_i915_private *dev_priv = dev->dev_private;
6702 uint32_t lcpll = I915_READ(LCPLL_CTL);
6703 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6704
6705 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6706 return 800000;
6707 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6708 return 450000;
6709 else if (freq == LCPLL_CLK_FREQ_450)
6710 return 450000;
6711 else if (IS_HSW_ULT(dev))
6712 return 337500;
6713 else
6714 return 540000;
Jesse Barnes79e53942008-11-07 14:24:08 -08006715}
6716
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006717static int valleyview_get_display_clock_speed(struct drm_device *dev)
6718{
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03006719 return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
6720 CCK_DISPLAY_CLOCK_CONTROL);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006721}
6722
Ville Syrjäläb37a6432015-03-31 14:11:54 +03006723static int ilk_get_display_clock_speed(struct drm_device *dev)
6724{
6725 return 450000;
6726}
6727
Jesse Barnese70236a2009-09-21 10:42:27 -07006728static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006729{
Jesse Barnese70236a2009-09-21 10:42:27 -07006730 return 400000;
6731}
Jesse Barnes79e53942008-11-07 14:24:08 -08006732
Jesse Barnese70236a2009-09-21 10:42:27 -07006733static int i915_get_display_clock_speed(struct drm_device *dev)
6734{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006735 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006736}
Jesse Barnes79e53942008-11-07 14:24:08 -08006737
Jesse Barnese70236a2009-09-21 10:42:27 -07006738static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6739{
6740 return 200000;
6741}
Jesse Barnes79e53942008-11-07 14:24:08 -08006742
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006743static int pnv_get_display_clock_speed(struct drm_device *dev)
6744{
6745 u16 gcfgc = 0;
6746
6747 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6748
6749 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6750 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006751 return 266667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006752 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006753 return 333333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006754 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006755 return 444444;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006756 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6757 return 200000;
6758 default:
6759 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6760 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006761 return 133333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006762 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006763 return 166667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006764 }
6765}
6766
Jesse Barnese70236a2009-09-21 10:42:27 -07006767static int i915gm_get_display_clock_speed(struct drm_device *dev)
6768{
6769 u16 gcfgc = 0;
6770
6771 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6772
6773 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Ville Syrjäläe907f172015-03-31 14:09:47 +03006774 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006775 else {
6776 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6777 case GC_DISPLAY_CLOCK_333_MHZ:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006778 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006779 default:
6780 case GC_DISPLAY_CLOCK_190_200_MHZ:
6781 return 190000;
6782 }
6783 }
6784}
Jesse Barnes79e53942008-11-07 14:24:08 -08006785
Jesse Barnese70236a2009-09-21 10:42:27 -07006786static int i865_get_display_clock_speed(struct drm_device *dev)
6787{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006788 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006789}
6790
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006791static int i85x_get_display_clock_speed(struct drm_device *dev)
Jesse Barnese70236a2009-09-21 10:42:27 -07006792{
6793 u16 hpllcc = 0;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006794
Ville Syrjälä65cd2b32015-05-22 11:22:32 +03006795 /*
6796 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6797 * encoding is different :(
6798 * FIXME is this the right way to detect 852GM/852GMV?
6799 */
6800 if (dev->pdev->revision == 0x1)
6801 return 133333;
6802
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006803 pci_bus_read_config_word(dev->pdev->bus,
6804 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6805
Jesse Barnese70236a2009-09-21 10:42:27 -07006806 /* Assume that the hardware is in the high speed state. This
6807 * should be the default.
6808 */
6809 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6810 case GC_CLOCK_133_200:
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006811 case GC_CLOCK_133_200_2:
Jesse Barnese70236a2009-09-21 10:42:27 -07006812 case GC_CLOCK_100_200:
6813 return 200000;
6814 case GC_CLOCK_166_250:
6815 return 250000;
6816 case GC_CLOCK_100_133:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006817 return 133333;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006818 case GC_CLOCK_133_266:
6819 case GC_CLOCK_133_266_2:
6820 case GC_CLOCK_166_266:
6821 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006822 }
6823
6824 /* Shouldn't happen */
6825 return 0;
6826}
6827
6828static int i830_get_display_clock_speed(struct drm_device *dev)
6829{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006830 return 133333;
Jesse Barnes79e53942008-11-07 14:24:08 -08006831}
6832
Ville Syrjälä34edce22015-05-22 11:22:33 +03006833static unsigned int intel_hpll_vco(struct drm_device *dev)
6834{
6835 struct drm_i915_private *dev_priv = dev->dev_private;
6836 static const unsigned int blb_vco[8] = {
6837 [0] = 3200000,
6838 [1] = 4000000,
6839 [2] = 5333333,
6840 [3] = 4800000,
6841 [4] = 6400000,
6842 };
6843 static const unsigned int pnv_vco[8] = {
6844 [0] = 3200000,
6845 [1] = 4000000,
6846 [2] = 5333333,
6847 [3] = 4800000,
6848 [4] = 2666667,
6849 };
6850 static const unsigned int cl_vco[8] = {
6851 [0] = 3200000,
6852 [1] = 4000000,
6853 [2] = 5333333,
6854 [3] = 6400000,
6855 [4] = 3333333,
6856 [5] = 3566667,
6857 [6] = 4266667,
6858 };
6859 static const unsigned int elk_vco[8] = {
6860 [0] = 3200000,
6861 [1] = 4000000,
6862 [2] = 5333333,
6863 [3] = 4800000,
6864 };
6865 static const unsigned int ctg_vco[8] = {
6866 [0] = 3200000,
6867 [1] = 4000000,
6868 [2] = 5333333,
6869 [3] = 6400000,
6870 [4] = 2666667,
6871 [5] = 4266667,
6872 };
6873 const unsigned int *vco_table;
6874 unsigned int vco;
6875 uint8_t tmp = 0;
6876
6877 /* FIXME other chipsets? */
6878 if (IS_GM45(dev))
6879 vco_table = ctg_vco;
6880 else if (IS_G4X(dev))
6881 vco_table = elk_vco;
6882 else if (IS_CRESTLINE(dev))
6883 vco_table = cl_vco;
6884 else if (IS_PINEVIEW(dev))
6885 vco_table = pnv_vco;
6886 else if (IS_G33(dev))
6887 vco_table = blb_vco;
6888 else
6889 return 0;
6890
6891 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
6892
6893 vco = vco_table[tmp & 0x7];
6894 if (vco == 0)
6895 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
6896 else
6897 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
6898
6899 return vco;
6900}
6901
6902static int gm45_get_display_clock_speed(struct drm_device *dev)
6903{
6904 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6905 uint16_t tmp = 0;
6906
6907 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6908
6909 cdclk_sel = (tmp >> 12) & 0x1;
6910
6911 switch (vco) {
6912 case 2666667:
6913 case 4000000:
6914 case 5333333:
6915 return cdclk_sel ? 333333 : 222222;
6916 case 3200000:
6917 return cdclk_sel ? 320000 : 228571;
6918 default:
6919 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
6920 return 222222;
6921 }
6922}
6923
6924static int i965gm_get_display_clock_speed(struct drm_device *dev)
6925{
6926 static const uint8_t div_3200[] = { 16, 10, 8 };
6927 static const uint8_t div_4000[] = { 20, 12, 10 };
6928 static const uint8_t div_5333[] = { 24, 16, 14 };
6929 const uint8_t *div_table;
6930 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6931 uint16_t tmp = 0;
6932
6933 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6934
6935 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
6936
6937 if (cdclk_sel >= ARRAY_SIZE(div_3200))
6938 goto fail;
6939
6940 switch (vco) {
6941 case 3200000:
6942 div_table = div_3200;
6943 break;
6944 case 4000000:
6945 div_table = div_4000;
6946 break;
6947 case 5333333:
6948 div_table = div_5333;
6949 break;
6950 default:
6951 goto fail;
6952 }
6953
6954 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
6955
Damien Lespiaucaf4e252015-06-04 16:56:18 +01006956fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03006957 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
6958 return 200000;
6959}
6960
6961static int g33_get_display_clock_speed(struct drm_device *dev)
6962{
6963 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
6964 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
6965 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
6966 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
6967 const uint8_t *div_table;
6968 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6969 uint16_t tmp = 0;
6970
6971 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6972
6973 cdclk_sel = (tmp >> 4) & 0x7;
6974
6975 if (cdclk_sel >= ARRAY_SIZE(div_3200))
6976 goto fail;
6977
6978 switch (vco) {
6979 case 3200000:
6980 div_table = div_3200;
6981 break;
6982 case 4000000:
6983 div_table = div_4000;
6984 break;
6985 case 4800000:
6986 div_table = div_4800;
6987 break;
6988 case 5333333:
6989 div_table = div_5333;
6990 break;
6991 default:
6992 goto fail;
6993 }
6994
6995 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
6996
Damien Lespiaucaf4e252015-06-04 16:56:18 +01006997fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03006998 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
6999 return 190476;
7000}
7001
Zhenyu Wang2c072452009-06-05 15:38:42 +08007002static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007003intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007004{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007005 while (*num > DATA_LINK_M_N_MASK ||
7006 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08007007 *num >>= 1;
7008 *den >>= 1;
7009 }
7010}
7011
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007012static void compute_m_n(unsigned int m, unsigned int n,
7013 uint32_t *ret_m, uint32_t *ret_n)
7014{
7015 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7016 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7017 intel_reduce_m_n_ratio(ret_m, ret_n);
7018}
7019
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007020void
7021intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7022 int pixel_clock, int link_clock,
7023 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007024{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007025 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007026
7027 compute_m_n(bits_per_pixel * pixel_clock,
7028 link_clock * nlanes * 8,
7029 &m_n->gmch_m, &m_n->gmch_n);
7030
7031 compute_m_n(pixel_clock, link_clock,
7032 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08007033}
7034
Chris Wilsona7615032011-01-12 17:04:08 +00007035static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7036{
Jani Nikulad330a952014-01-21 11:24:25 +02007037 if (i915.panel_use_ssc >= 0)
7038 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007039 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07007040 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00007041}
7042
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007043static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007044{
Daniel Vetter7df00d72013-05-21 21:54:55 +02007045 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007046}
Daniel Vetterf47709a2013-03-28 10:42:02 +01007047
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007048static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7049{
7050 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007051}
7052
Daniel Vetterf47709a2013-03-28 10:42:02 +01007053static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007054 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03007055 struct dpll *reduced_clock)
Jesse Barnesa7516a02011-12-15 12:30:37 -08007056{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007057 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007058 u32 fp, fp2 = 0;
7059
7060 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007061 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007062 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007063 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007064 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007065 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007066 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007067 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007068 }
7069
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007070 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007071
Daniel Vetterf47709a2013-03-28 10:42:02 +01007072 crtc->lowfreq_avail = false;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007073 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07007074 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007075 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007076 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007077 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007078 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007079 }
7080}
7081
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007082static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7083 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07007084{
7085 u32 reg_val;
7086
7087 /*
7088 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7089 * and set it to a reasonable value instead.
7090 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007091 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007092 reg_val &= 0xffffff00;
7093 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007094 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007095
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007096 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007097 reg_val &= 0x8cffffff;
7098 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007099 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007100
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007101 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007102 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007103 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007104
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007105 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007106 reg_val &= 0x00ffffff;
7107 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007108 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007109}
7110
Daniel Vetterb5518422013-05-03 11:49:48 +02007111static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7112 struct intel_link_m_n *m_n)
7113{
7114 struct drm_device *dev = crtc->base.dev;
7115 struct drm_i915_private *dev_priv = dev->dev_private;
7116 int pipe = crtc->pipe;
7117
Daniel Vettere3b95f12013-05-03 11:49:49 +02007118 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7119 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7120 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7121 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007122}
7123
7124static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07007125 struct intel_link_m_n *m_n,
7126 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02007127{
7128 struct drm_device *dev = crtc->base.dev;
7129 struct drm_i915_private *dev_priv = dev->dev_private;
7130 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007131 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02007132
7133 if (INTEL_INFO(dev)->gen >= 5) {
7134 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7135 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7136 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7137 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07007138 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7139 * for gen < 8) and if DRRS is supported (to make sure the
7140 * registers are not unnecessarily accessed).
7141 */
Durgadoss R44395bf2015-02-13 15:33:02 +05307142 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007143 crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07007144 I915_WRITE(PIPE_DATA_M2(transcoder),
7145 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7146 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7147 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7148 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7149 }
Daniel Vetterb5518422013-05-03 11:49:48 +02007150 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02007151 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7152 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7153 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7154 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007155 }
7156}
7157
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307158void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007159{
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307160 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7161
7162 if (m_n == M1_N1) {
7163 dp_m_n = &crtc->config->dp_m_n;
7164 dp_m2_n2 = &crtc->config->dp_m2_n2;
7165 } else if (m_n == M2_N2) {
7166
7167 /*
7168 * M2_N2 registers are not supported. Hence m2_n2 divider value
7169 * needs to be programmed into M1_N1.
7170 */
7171 dp_m_n = &crtc->config->dp_m2_n2;
7172 } else {
7173 DRM_ERROR("Unsupported divider value\n");
7174 return;
7175 }
7176
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007177 if (crtc->config->has_pch_encoder)
7178 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007179 else
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307180 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007181}
7182
Daniel Vetter251ac862015-06-18 10:30:24 +02007183static void vlv_compute_dpll(struct intel_crtc *crtc,
7184 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007185{
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007186 pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007187 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007188 if (crtc->pipe != PIPE_A)
7189 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007190
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007191 /* DPLL not used with DSI, but still need the rest set up */
Ville Syrjälä187a1c02016-04-18 20:34:04 +03007192 if (!pipe_config->has_dsi_encoder)
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007193 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
7194 DPLL_EXT_BUFFER_ENABLE_VLV;
7195
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007196 pipe_config->dpll_hw_state.dpll_md =
7197 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
7198}
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007199
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007200static void chv_compute_dpll(struct intel_crtc *crtc,
7201 struct intel_crtc_state *pipe_config)
7202{
7203 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007204 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007205 if (crtc->pipe != PIPE_A)
7206 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7207
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007208 /* DPLL not used with DSI, but still need the rest set up */
Ville Syrjälä187a1c02016-04-18 20:34:04 +03007209 if (!pipe_config->has_dsi_encoder)
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007210 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
7211
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007212 pipe_config->dpll_hw_state.dpll_md =
7213 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007214}
7215
Ville Syrjäläd288f652014-10-28 13:20:22 +02007216static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007217 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007218{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007219 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007220 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007221 enum pipe pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007222 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007223 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007224 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007225
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007226 /* Enable Refclk */
7227 I915_WRITE(DPLL(pipe),
7228 pipe_config->dpll_hw_state.dpll &
7229 ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
7230
7231 /* No need to actually set up the DPLL with DSI */
7232 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7233 return;
7234
Ville Syrjäläa5805162015-05-26 20:42:30 +03007235 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01007236
Ville Syrjäläd288f652014-10-28 13:20:22 +02007237 bestn = pipe_config->dpll.n;
7238 bestm1 = pipe_config->dpll.m1;
7239 bestm2 = pipe_config->dpll.m2;
7240 bestp1 = pipe_config->dpll.p1;
7241 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007242
Jesse Barnes89b667f2013-04-18 14:51:36 -07007243 /* See eDP HDMI DPIO driver vbios notes doc */
7244
7245 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007246 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007247 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007248
7249 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007250 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007251
7252 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007253 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007254 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007255 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007256
7257 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007258 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007259
7260 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007261 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7262 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7263 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007264 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07007265
7266 /*
7267 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7268 * but we don't support that).
7269 * Note: don't use the DAC post divider as it seems unstable.
7270 */
7271 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007272 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007273
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007274 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007275 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007276
Jesse Barnes89b667f2013-04-18 14:51:36 -07007277 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02007278 if (pipe_config->port_clock == 162000 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007279 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
7280 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007281 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b01202013-07-05 19:21:38 +03007282 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007283 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007284 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007285 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007286
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02007287 if (pipe_config->has_dp_encoder) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07007288 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007289 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007290 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007291 0x0df40000);
7292 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007293 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007294 0x0df70000);
7295 } else { /* HDMI or VGA */
7296 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007297 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007298 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007299 0x0df70000);
7300 else
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 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007304
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007305 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007306 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007307 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
7308 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
Jesse Barnes89b667f2013-04-18 14:51:36 -07007309 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007310 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007311
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007312 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Ville Syrjäläa5805162015-05-26 20:42:30 +03007313 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007314}
7315
Ville Syrjäläd288f652014-10-28 13:20:22 +02007316static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007317 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007318{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007319 struct drm_device *dev = crtc->base.dev;
7320 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007321 enum pipe pipe = crtc->pipe;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007322 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307323 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007324 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307325 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307326 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007327
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007328 /* Enable Refclk and SSC */
7329 I915_WRITE(DPLL(pipe),
7330 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
7331
7332 /* No need to actually set up the DPLL with DSI */
7333 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7334 return;
7335
Ville Syrjäläd288f652014-10-28 13:20:22 +02007336 bestn = pipe_config->dpll.n;
7337 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7338 bestm1 = pipe_config->dpll.m1;
7339 bestm2 = pipe_config->dpll.m2 >> 22;
7340 bestp1 = pipe_config->dpll.p1;
7341 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307342 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307343 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307344 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007345
Ville Syrjäläa5805162015-05-26 20:42:30 +03007346 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007347
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007348 /* p1 and p2 divider */
7349 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7350 5 << DPIO_CHV_S1_DIV_SHIFT |
7351 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7352 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7353 1 << DPIO_CHV_K_DIV_SHIFT);
7354
7355 /* Feedback post-divider - m2 */
7356 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7357
7358 /* Feedback refclk divider - n and m1 */
7359 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7360 DPIO_CHV_M1_DIV_BY_2 |
7361 1 << DPIO_CHV_N_DIV_SHIFT);
7362
7363 /* M2 fraction division */
Ville Syrjälä25a25df2015-07-08 23:45:47 +03007364 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007365
7366 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307367 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7368 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7369 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7370 if (bestm2_frac)
7371 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7372 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007373
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05307374 /* Program digital lock detect threshold */
7375 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7376 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7377 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7378 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7379 if (!bestm2_frac)
7380 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7381 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7382
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007383 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307384 if (vco == 5400000) {
7385 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7386 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7387 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7388 tribuf_calcntr = 0x9;
7389 } else if (vco <= 6200000) {
7390 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7391 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7392 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7393 tribuf_calcntr = 0x9;
7394 } else if (vco <= 6480000) {
7395 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7396 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7397 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7398 tribuf_calcntr = 0x8;
7399 } else {
7400 /* Not supported. Apply the same limits as in the max case */
7401 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7402 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7403 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7404 tribuf_calcntr = 0;
7405 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007406 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7407
Ville Syrjälä968040b2015-03-11 22:52:08 +02007408 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307409 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7410 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7411 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7412
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007413 /* AFC Recal */
7414 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7415 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7416 DPIO_AFC_RECAL);
7417
Ville Syrjäläa5805162015-05-26 20:42:30 +03007418 mutex_unlock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007419}
7420
Ville Syrjäläd288f652014-10-28 13:20:22 +02007421/**
7422 * vlv_force_pll_on - forcibly enable just the PLL
7423 * @dev_priv: i915 private structure
7424 * @pipe: pipe PLL to enable
7425 * @dpll: PLL configuration
7426 *
7427 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7428 * in cases where we need the PLL enabled even when @pipe is not going to
7429 * be enabled.
7430 */
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007431int vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7432 const struct dpll *dpll)
Ville Syrjäläd288f652014-10-28 13:20:22 +02007433{
7434 struct intel_crtc *crtc =
7435 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007436 struct intel_crtc_state *pipe_config;
7437
7438 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
7439 if (!pipe_config)
7440 return -ENOMEM;
7441
7442 pipe_config->base.crtc = &crtc->base;
7443 pipe_config->pixel_multiplier = 1;
7444 pipe_config->dpll = *dpll;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007445
7446 if (IS_CHERRYVIEW(dev)) {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007447 chv_compute_dpll(crtc, pipe_config);
7448 chv_prepare_pll(crtc, pipe_config);
7449 chv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007450 } else {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007451 vlv_compute_dpll(crtc, pipe_config);
7452 vlv_prepare_pll(crtc, pipe_config);
7453 vlv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007454 }
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007455
7456 kfree(pipe_config);
7457
7458 return 0;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007459}
7460
7461/**
7462 * vlv_force_pll_off - forcibly disable just the PLL
7463 * @dev_priv: i915 private structure
7464 * @pipe: pipe PLL to disable
7465 *
7466 * Disable the PLL for @pipe. To be used in cases where we need
7467 * the PLL enabled even when @pipe is not going to be enabled.
7468 */
7469void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7470{
7471 if (IS_CHERRYVIEW(dev))
7472 chv_disable_pll(to_i915(dev), pipe);
7473 else
7474 vlv_disable_pll(to_i915(dev), pipe);
7475}
7476
Daniel Vetter251ac862015-06-18 10:30:24 +02007477static void i9xx_compute_dpll(struct intel_crtc *crtc,
7478 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03007479 struct dpll *reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007480{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007481 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007482 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007483 u32 dpll;
7484 bool is_sdvo;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007485 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007486
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007487 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307488
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007489 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7490 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007491
7492 dpll = DPLL_VGA_MODE_DIS;
7493
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007494 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007495 dpll |= DPLLB_MODE_LVDS;
7496 else
7497 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007498
Daniel Vetteref1b4602013-06-01 17:17:04 +02007499 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007500 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02007501 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007502 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02007503
7504 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007505 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007506
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007507 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007508 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007509
7510 /* compute bitmask from p1 value */
7511 if (IS_PINEVIEW(dev))
7512 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7513 else {
7514 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7515 if (IS_G4X(dev) && reduced_clock)
7516 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7517 }
7518 switch (clock->p2) {
7519 case 5:
7520 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7521 break;
7522 case 7:
7523 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7524 break;
7525 case 10:
7526 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7527 break;
7528 case 14:
7529 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7530 break;
7531 }
7532 if (INTEL_INFO(dev)->gen >= 4)
7533 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7534
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007535 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007536 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007537 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007538 intel_panel_use_ssc(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007539 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7540 else
7541 dpll |= PLL_REF_INPUT_DREFCLK;
7542
7543 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007544 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007545
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007546 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007547 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007548 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007549 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007550 }
7551}
7552
Daniel Vetter251ac862015-06-18 10:30:24 +02007553static void i8xx_compute_dpll(struct intel_crtc *crtc,
7554 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03007555 struct dpll *reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007556{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007557 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007558 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007559 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007560 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007561
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007562 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307563
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007564 dpll = DPLL_VGA_MODE_DIS;
7565
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007566 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007567 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7568 } else {
7569 if (clock->p1 == 2)
7570 dpll |= PLL_P1_DIVIDE_BY_TWO;
7571 else
7572 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7573 if (clock->p2 == 4)
7574 dpll |= PLL_P2_DIVIDE_BY_4;
7575 }
7576
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007577 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007578 dpll |= DPLL_DVO_2X_MODE;
7579
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007580 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007581 intel_panel_use_ssc(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007582 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7583 else
7584 dpll |= PLL_REF_INPUT_DREFCLK;
7585
7586 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007587 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007588}
7589
Daniel Vetter8a654f32013-06-01 17:16:22 +02007590static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007591{
7592 struct drm_device *dev = intel_crtc->base.dev;
7593 struct drm_i915_private *dev_priv = dev->dev_private;
7594 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007595 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03007596 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007597 uint32_t crtc_vtotal, crtc_vblank_end;
7598 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007599
7600 /* We need to be careful not to changed the adjusted mode, for otherwise
7601 * the hw state checker will get angry at the mismatch. */
7602 crtc_vtotal = adjusted_mode->crtc_vtotal;
7603 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007604
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007605 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007606 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007607 crtc_vtotal -= 1;
7608 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007609
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007610 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007611 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7612 else
7613 vsyncshift = adjusted_mode->crtc_hsync_start -
7614 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007615 if (vsyncshift < 0)
7616 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007617 }
7618
7619 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007620 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007621
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007622 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007623 (adjusted_mode->crtc_hdisplay - 1) |
7624 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007625 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007626 (adjusted_mode->crtc_hblank_start - 1) |
7627 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007628 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007629 (adjusted_mode->crtc_hsync_start - 1) |
7630 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7631
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007632 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007633 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007634 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007635 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007636 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007637 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007638 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007639 (adjusted_mode->crtc_vsync_start - 1) |
7640 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7641
Paulo Zanonib5e508d2012-10-24 11:34:43 -02007642 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7643 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7644 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7645 * bits. */
7646 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7647 (pipe == PIPE_B || pipe == PIPE_C))
7648 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7649
Jani Nikulabc58be62016-03-18 17:05:39 +02007650}
7651
7652static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
7653{
7654 struct drm_device *dev = intel_crtc->base.dev;
7655 struct drm_i915_private *dev_priv = dev->dev_private;
7656 enum pipe pipe = intel_crtc->pipe;
7657
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007658 /* pipesrc controls the size that is scaled from, which should
7659 * always be the user's requested size.
7660 */
7661 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007662 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7663 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007664}
7665
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007666static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007667 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007668{
7669 struct drm_device *dev = crtc->base.dev;
7670 struct drm_i915_private *dev_priv = dev->dev_private;
7671 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7672 uint32_t tmp;
7673
7674 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007675 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7676 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007677 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007678 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7679 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007680 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007681 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7682 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007683
7684 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007685 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7686 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007687 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007688 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7689 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007690 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007691 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7692 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007693
7694 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007695 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7696 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7697 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007698 }
Jani Nikulabc58be62016-03-18 17:05:39 +02007699}
7700
7701static void intel_get_pipe_src_size(struct intel_crtc *crtc,
7702 struct intel_crtc_state *pipe_config)
7703{
7704 struct drm_device *dev = crtc->base.dev;
7705 struct drm_i915_private *dev_priv = dev->dev_private;
7706 u32 tmp;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007707
7708 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03007709 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7710 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7711
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007712 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7713 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007714}
7715
Daniel Vetterf6a83282014-02-11 15:28:57 -08007716void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007717 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03007718{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007719 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7720 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7721 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7722 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007723
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007724 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7725 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7726 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7727 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007728
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007729 mode->flags = pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007730 mode->type = DRM_MODE_TYPE_DRIVER;
Jesse Barnesbabea612013-06-26 18:57:38 +03007731
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007732 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7733 mode->flags |= pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007734
7735 mode->hsync = drm_mode_hsync(mode);
7736 mode->vrefresh = drm_mode_vrefresh(mode);
7737 drm_mode_set_name(mode);
Jesse Barnesbabea612013-06-26 18:57:38 +03007738}
7739
Daniel Vetter84b046f2013-02-19 18:48:54 +01007740static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7741{
7742 struct drm_device *dev = intel_crtc->base.dev;
7743 struct drm_i915_private *dev_priv = dev->dev_private;
7744 uint32_t pipeconf;
7745
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007746 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007747
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03007748 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7749 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7750 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02007751
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007752 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007753 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007754
Daniel Vetterff9ce462013-04-24 14:57:17 +02007755 /* only g4x and later have fancy bpc/dither controls */
Wayne Boyer666a4532015-12-09 12:29:35 -08007756 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007757 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007758 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02007759 pipeconf |= PIPECONF_DITHER_EN |
7760 PIPECONF_DITHER_TYPE_SP;
7761
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007762 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007763 case 18:
7764 pipeconf |= PIPECONF_6BPC;
7765 break;
7766 case 24:
7767 pipeconf |= PIPECONF_8BPC;
7768 break;
7769 case 30:
7770 pipeconf |= PIPECONF_10BPC;
7771 break;
7772 default:
7773 /* Case prevented by intel_choose_pipe_bpp_dither. */
7774 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01007775 }
7776 }
7777
7778 if (HAS_PIPE_CXSR(dev)) {
7779 if (intel_crtc->lowfreq_avail) {
7780 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7781 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7782 } else {
7783 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01007784 }
7785 }
7786
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007787 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007788 if (INTEL_INFO(dev)->gen < 4 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007789 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007790 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7791 else
7792 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7793 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01007794 pipeconf |= PIPECONF_PROGRESSIVE;
7795
Wayne Boyer666a4532015-12-09 12:29:35 -08007796 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
7797 intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007798 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03007799
Daniel Vetter84b046f2013-02-19 18:48:54 +01007800 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7801 POSTING_READ(PIPECONF(intel_crtc->pipe));
7802}
7803
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007804static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
7805 struct intel_crtc_state *crtc_state)
7806{
7807 struct drm_device *dev = crtc->base.dev;
7808 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007809 const struct intel_limit *limit;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007810 int refclk = 48000;
7811
7812 memset(&crtc_state->dpll_hw_state, 0,
7813 sizeof(crtc_state->dpll_hw_state));
7814
7815 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7816 if (intel_panel_use_ssc(dev_priv)) {
7817 refclk = dev_priv->vbt.lvds_ssc_freq;
7818 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7819 }
7820
7821 limit = &intel_limits_i8xx_lvds;
7822 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO)) {
7823 limit = &intel_limits_i8xx_dvo;
7824 } else {
7825 limit = &intel_limits_i8xx_dac;
7826 }
7827
7828 if (!crtc_state->clock_set &&
7829 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7830 refclk, NULL, &crtc_state->dpll)) {
7831 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7832 return -EINVAL;
7833 }
7834
7835 i8xx_compute_dpll(crtc, crtc_state, NULL);
7836
7837 return 0;
7838}
7839
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007840static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
7841 struct intel_crtc_state *crtc_state)
7842{
7843 struct drm_device *dev = crtc->base.dev;
7844 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007845 const struct intel_limit *limit;
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007846 int refclk = 96000;
7847
7848 memset(&crtc_state->dpll_hw_state, 0,
7849 sizeof(crtc_state->dpll_hw_state));
7850
7851 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7852 if (intel_panel_use_ssc(dev_priv)) {
7853 refclk = dev_priv->vbt.lvds_ssc_freq;
7854 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7855 }
7856
7857 if (intel_is_dual_link_lvds(dev))
7858 limit = &intel_limits_g4x_dual_channel_lvds;
7859 else
7860 limit = &intel_limits_g4x_single_channel_lvds;
7861 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
7862 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
7863 limit = &intel_limits_g4x_hdmi;
7864 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
7865 limit = &intel_limits_g4x_sdvo;
7866 } else {
7867 /* The option is for other outputs */
7868 limit = &intel_limits_i9xx_sdvo;
7869 }
7870
7871 if (!crtc_state->clock_set &&
7872 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7873 refclk, NULL, &crtc_state->dpll)) {
7874 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7875 return -EINVAL;
7876 }
7877
7878 i9xx_compute_dpll(crtc, crtc_state, NULL);
7879
7880 return 0;
7881}
7882
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007883static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
7884 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08007885{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007886 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007887 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007888 const struct intel_limit *limit;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007889 int refclk = 96000;
Jesse Barnes79e53942008-11-07 14:24:08 -08007890
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03007891 memset(&crtc_state->dpll_hw_state, 0,
7892 sizeof(crtc_state->dpll_hw_state));
7893
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007894 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7895 if (intel_panel_use_ssc(dev_priv)) {
7896 refclk = dev_priv->vbt.lvds_ssc_freq;
7897 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7898 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007899
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007900 limit = &intel_limits_pineview_lvds;
7901 } else {
7902 limit = &intel_limits_pineview_sdvo;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007903 }
Jani Nikulaf2335332013-09-13 11:03:09 +03007904
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007905 if (!crtc_state->clock_set &&
7906 !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7907 refclk, NULL, &crtc_state->dpll)) {
7908 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7909 return -EINVAL;
7910 }
7911
7912 i9xx_compute_dpll(crtc, crtc_state, NULL);
7913
7914 return 0;
7915}
7916
7917static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7918 struct intel_crtc_state *crtc_state)
7919{
7920 struct drm_device *dev = crtc->base.dev;
7921 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007922 const struct intel_limit *limit;
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007923 int refclk = 96000;
7924
7925 memset(&crtc_state->dpll_hw_state, 0,
7926 sizeof(crtc_state->dpll_hw_state));
7927
7928 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7929 if (intel_panel_use_ssc(dev_priv)) {
7930 refclk = dev_priv->vbt.lvds_ssc_freq;
7931 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007932 }
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007933
7934 limit = &intel_limits_i9xx_lvds;
7935 } else {
7936 limit = &intel_limits_i9xx_sdvo;
7937 }
7938
7939 if (!crtc_state->clock_set &&
7940 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7941 refclk, NULL, &crtc_state->dpll)) {
7942 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7943 return -EINVAL;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007944 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007945
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007946 i9xx_compute_dpll(crtc, crtc_state, NULL);
Eric Anholtf564048e2011-03-30 13:01:02 -07007947
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007948 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07007949}
7950
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02007951static int chv_crtc_compute_clock(struct intel_crtc *crtc,
7952 struct intel_crtc_state *crtc_state)
7953{
7954 int refclk = 100000;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007955 const struct intel_limit *limit = &intel_limits_chv;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02007956
7957 memset(&crtc_state->dpll_hw_state, 0,
7958 sizeof(crtc_state->dpll_hw_state));
7959
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02007960 if (!crtc_state->clock_set &&
7961 !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7962 refclk, NULL, &crtc_state->dpll)) {
7963 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7964 return -EINVAL;
7965 }
7966
7967 chv_compute_dpll(crtc, crtc_state);
7968
7969 return 0;
7970}
7971
7972static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
7973 struct intel_crtc_state *crtc_state)
7974{
7975 int refclk = 100000;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007976 const struct intel_limit *limit = &intel_limits_vlv;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02007977
7978 memset(&crtc_state->dpll_hw_state, 0,
7979 sizeof(crtc_state->dpll_hw_state));
7980
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02007981 if (!crtc_state->clock_set &&
7982 !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7983 refclk, NULL, &crtc_state->dpll)) {
7984 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7985 return -EINVAL;
7986 }
7987
7988 vlv_compute_dpll(crtc, crtc_state);
7989
7990 return 0;
7991}
7992
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007993static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007994 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007995{
7996 struct drm_device *dev = crtc->base.dev;
7997 struct drm_i915_private *dev_priv = dev->dev_private;
7998 uint32_t tmp;
7999
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02008000 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
8001 return;
8002
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008003 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02008004 if (!(tmp & PFIT_ENABLE))
8005 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008006
Daniel Vetter06922822013-07-11 13:35:40 +02008007 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008008 if (INTEL_INFO(dev)->gen < 4) {
8009 if (crtc->pipe != PIPE_B)
8010 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008011 } else {
8012 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
8013 return;
8014 }
8015
Daniel Vetter06922822013-07-11 13:35:40 +02008016 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008017 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008018}
8019
Jesse Barnesacbec812013-09-20 11:29:32 -07008020static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008021 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07008022{
8023 struct drm_device *dev = crtc->base.dev;
8024 struct drm_i915_private *dev_priv = dev->dev_private;
8025 int pipe = pipe_config->cpu_transcoder;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03008026 struct dpll clock;
Jesse Barnesacbec812013-09-20 11:29:32 -07008027 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07008028 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07008029
Ville Syrjäläb5219732016-03-15 16:40:01 +02008030 /* In case of DSI, DPLL will not be used */
8031 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
Shobhit Kumarf573de52014-07-30 20:32:37 +05308032 return;
8033
Ville Syrjäläa5805162015-05-26 20:42:30 +03008034 mutex_lock(&dev_priv->sb_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08008035 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008036 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesacbec812013-09-20 11:29:32 -07008037
8038 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
8039 clock.m2 = mdiv & DPIO_M2DIV_MASK;
8040 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
8041 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
8042 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
8043
Imre Deakdccbea32015-06-22 23:35:51 +03008044 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07008045}
8046
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008047static void
8048i9xx_get_initial_plane_config(struct intel_crtc *crtc,
8049 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008050{
8051 struct drm_device *dev = crtc->base.dev;
8052 struct drm_i915_private *dev_priv = dev->dev_private;
8053 u32 val, base, offset;
8054 int pipe = crtc->pipe, plane = crtc->plane;
8055 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008056 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008057 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008058 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008059
Damien Lespiau42a7b082015-02-05 19:35:13 +00008060 val = I915_READ(DSPCNTR(plane));
8061 if (!(val & DISPLAY_PLANE_ENABLE))
8062 return;
8063
Damien Lespiaud9806c92015-01-21 14:07:19 +00008064 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008065 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008066 DRM_DEBUG_KMS("failed to alloc fb\n");
8067 return;
8068 }
8069
Damien Lespiau1b842c82015-01-21 13:50:54 +00008070 fb = &intel_fb->base;
8071
Daniel Vetter18c52472015-02-10 17:16:09 +00008072 if (INTEL_INFO(dev)->gen >= 4) {
8073 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008074 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00008075 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8076 }
8077 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008078
8079 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00008080 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008081 fb->pixel_format = fourcc;
8082 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008083
8084 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008085 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008086 offset = I915_READ(DSPTILEOFF(plane));
8087 else
8088 offset = I915_READ(DSPLINOFF(plane));
8089 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8090 } else {
8091 base = I915_READ(DSPADDR(plane));
8092 }
8093 plane_config->base = base;
8094
8095 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008096 fb->width = ((val >> 16) & 0xfff) + 1;
8097 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008098
8099 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008100 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008101
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008102 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008103 fb->pixel_format,
8104 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008105
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008106 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008107
Damien Lespiau2844a922015-01-20 12:51:48 +00008108 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8109 pipe_name(pipe), plane, fb->width, fb->height,
8110 fb->bits_per_pixel, base, fb->pitches[0],
8111 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008112
Damien Lespiau2d140302015-02-05 17:22:18 +00008113 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008114}
8115
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008116static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008117 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008118{
8119 struct drm_device *dev = crtc->base.dev;
8120 struct drm_i915_private *dev_priv = dev->dev_private;
8121 int pipe = pipe_config->cpu_transcoder;
8122 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03008123 struct dpll clock;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008124 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008125 int refclk = 100000;
8126
Ville Syrjäläb5219732016-03-15 16:40:01 +02008127 /* In case of DSI, DPLL will not be used */
8128 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
8129 return;
8130
Ville Syrjäläa5805162015-05-26 20:42:30 +03008131 mutex_lock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008132 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8133 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8134 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8135 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
Imre Deak0d7b6b12015-07-02 14:29:58 +03008136 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008137 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008138
8139 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008140 clock.m2 = (pll_dw0 & 0xff) << 22;
8141 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
8142 clock.m2 |= pll_dw2 & 0x3fffff;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008143 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8144 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8145 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8146
Imre Deakdccbea32015-06-22 23:35:51 +03008147 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008148}
8149
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008150static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008151 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008152{
8153 struct drm_device *dev = crtc->base.dev;
8154 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak17290502016-02-12 18:55:11 +02008155 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008156 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02008157 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008158
Imre Deak17290502016-02-12 18:55:11 +02008159 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8160 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +02008161 return false;
8162
Daniel Vettere143a212013-07-04 12:01:15 +02008163 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008164 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02008165
Imre Deak17290502016-02-12 18:55:11 +02008166 ret = false;
8167
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008168 tmp = I915_READ(PIPECONF(crtc->pipe));
8169 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02008170 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008171
Wayne Boyer666a4532015-12-09 12:29:35 -08008172 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008173 switch (tmp & PIPECONF_BPC_MASK) {
8174 case PIPECONF_6BPC:
8175 pipe_config->pipe_bpp = 18;
8176 break;
8177 case PIPECONF_8BPC:
8178 pipe_config->pipe_bpp = 24;
8179 break;
8180 case PIPECONF_10BPC:
8181 pipe_config->pipe_bpp = 30;
8182 break;
8183 default:
8184 break;
8185 }
8186 }
8187
Wayne Boyer666a4532015-12-09 12:29:35 -08008188 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
8189 (tmp & PIPECONF_COLOR_RANGE_SELECT))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008190 pipe_config->limited_color_range = true;
8191
Ville Syrjälä282740f2013-09-04 18:30:03 +03008192 if (INTEL_INFO(dev)->gen < 4)
8193 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8194
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008195 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +02008196 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008197
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008198 i9xx_get_pfit_config(crtc, pipe_config);
8199
Daniel Vetter6c49f242013-06-06 12:45:25 +02008200 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjäläc2317752016-03-15 16:39:56 +02008201 /* No way to read it out on pipes B and C */
8202 if (IS_CHERRYVIEW(dev) && crtc->pipe != PIPE_A)
8203 tmp = dev_priv->chv_dpll_md[crtc->pipe];
8204 else
8205 tmp = I915_READ(DPLL_MD(crtc->pipe));
Daniel Vetter6c49f242013-06-06 12:45:25 +02008206 pipe_config->pixel_multiplier =
8207 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8208 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008209 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02008210 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8211 tmp = I915_READ(DPLL(crtc->pipe));
8212 pipe_config->pixel_multiplier =
8213 ((tmp & SDVO_MULTIPLIER_MASK)
8214 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8215 } else {
8216 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8217 * port and will be fixed up in the encoder->get_config
8218 * function. */
8219 pipe_config->pixel_multiplier = 1;
8220 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008221 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
Wayne Boyer666a4532015-12-09 12:29:35 -08008222 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03008223 /*
8224 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8225 * on 830. Filter it out here so that we don't
8226 * report errors due to that.
8227 */
8228 if (IS_I830(dev))
8229 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8230
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008231 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8232 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03008233 } else {
8234 /* Mask out read-only status bits. */
8235 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8236 DPLL_PORTC_READY_MASK |
8237 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008238 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02008239
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008240 if (IS_CHERRYVIEW(dev))
8241 chv_crtc_clock_get(crtc, pipe_config);
8242 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07008243 vlv_crtc_clock_get(crtc, pipe_config);
8244 else
8245 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03008246
Ville Syrjälä0f646142015-08-26 19:39:18 +03008247 /*
8248 * Normally the dotclock is filled in by the encoder .get_config()
8249 * but in case the pipe is enabled w/o any ports we need a sane
8250 * default.
8251 */
8252 pipe_config->base.adjusted_mode.crtc_clock =
8253 pipe_config->port_clock / pipe_config->pixel_multiplier;
8254
Imre Deak17290502016-02-12 18:55:11 +02008255 ret = true;
8256
8257out:
8258 intel_display_power_put(dev_priv, power_domain);
8259
8260 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008261}
8262
Paulo Zanonidde86e22012-12-01 12:04:25 -02008263static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07008264{
8265 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008266 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008267 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008268 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008269 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008270 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07008271 bool has_ck505 = false;
8272 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008273
8274 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01008275 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07008276 switch (encoder->type) {
8277 case INTEL_OUTPUT_LVDS:
8278 has_panel = true;
8279 has_lvds = true;
8280 break;
8281 case INTEL_OUTPUT_EDP:
8282 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03008283 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07008284 has_cpu_edp = true;
8285 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008286 default:
8287 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008288 }
8289 }
8290
Keith Packard99eb6a02011-09-26 14:29:12 -07008291 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008292 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07008293 can_ssc = has_ck505;
8294 } else {
8295 has_ck505 = false;
8296 can_ssc = true;
8297 }
8298
Imre Deak2de69052013-05-08 13:14:04 +03008299 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
8300 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008301
8302 /* Ironlake: try to setup display ref clock before DPLL
8303 * enabling. This is only under driver's control after
8304 * PCH B stepping, previous chipset stepping should be
8305 * ignoring this setting.
8306 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008307 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008308
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008309 /* As we must carefully and slowly disable/enable each source in turn,
8310 * compute the final state we want first and check if we need to
8311 * make any changes at all.
8312 */
8313 final = val;
8314 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07008315 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008316 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07008317 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008318 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8319
8320 final &= ~DREF_SSC_SOURCE_MASK;
8321 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8322 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008323
Keith Packard199e5d72011-09-22 12:01:57 -07008324 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008325 final |= DREF_SSC_SOURCE_ENABLE;
8326
8327 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8328 final |= DREF_SSC1_ENABLE;
8329
8330 if (has_cpu_edp) {
8331 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8332 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8333 else
8334 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8335 } else
8336 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8337 } else {
8338 final |= DREF_SSC_SOURCE_DISABLE;
8339 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8340 }
8341
8342 if (final == val)
8343 return;
8344
8345 /* Always enable nonspread source */
8346 val &= ~DREF_NONSPREAD_SOURCE_MASK;
8347
8348 if (has_ck505)
8349 val |= DREF_NONSPREAD_CK505_ENABLE;
8350 else
8351 val |= DREF_NONSPREAD_SOURCE_ENABLE;
8352
8353 if (has_panel) {
8354 val &= ~DREF_SSC_SOURCE_MASK;
8355 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008356
Keith Packard199e5d72011-09-22 12:01:57 -07008357 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07008358 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008359 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008360 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02008361 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008362 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008363
8364 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008365 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008366 POSTING_READ(PCH_DREF_CONTROL);
8367 udelay(200);
8368
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008369 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008370
8371 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07008372 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07008373 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008374 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008375 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02008376 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008377 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07008378 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008379 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008380
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008381 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008382 POSTING_READ(PCH_DREF_CONTROL);
8383 udelay(200);
8384 } else {
8385 DRM_DEBUG_KMS("Disabling SSC entirely\n");
8386
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008387 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07008388
8389 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008390 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008391
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008392 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008393 POSTING_READ(PCH_DREF_CONTROL);
8394 udelay(200);
8395
8396 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008397 val &= ~DREF_SSC_SOURCE_MASK;
8398 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008399
8400 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008401 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008402
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008403 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008404 POSTING_READ(PCH_DREF_CONTROL);
8405 udelay(200);
8406 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008407
8408 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008409}
8410
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008411static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02008412{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008413 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008414
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008415 tmp = I915_READ(SOUTH_CHICKEN2);
8416 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8417 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008418
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008419 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
8420 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
8421 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02008422
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008423 tmp = I915_READ(SOUTH_CHICKEN2);
8424 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8425 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008426
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008427 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
8428 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
8429 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008430}
8431
8432/* WaMPhyProgramming:hsw */
8433static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8434{
8435 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008436
8437 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8438 tmp &= ~(0xFF << 24);
8439 tmp |= (0x12 << 24);
8440 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8441
Paulo Zanonidde86e22012-12-01 12:04:25 -02008442 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8443 tmp |= (1 << 11);
8444 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8445
8446 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8447 tmp |= (1 << 11);
8448 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8449
Paulo Zanonidde86e22012-12-01 12:04:25 -02008450 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8451 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8452 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8453
8454 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8455 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8456 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8457
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008458 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8459 tmp &= ~(7 << 13);
8460 tmp |= (5 << 13);
8461 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008462
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008463 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8464 tmp &= ~(7 << 13);
8465 tmp |= (5 << 13);
8466 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008467
8468 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8469 tmp &= ~0xFF;
8470 tmp |= 0x1C;
8471 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8472
8473 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8474 tmp &= ~0xFF;
8475 tmp |= 0x1C;
8476 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8477
8478 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8479 tmp &= ~(0xFF << 16);
8480 tmp |= (0x1C << 16);
8481 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8482
8483 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8484 tmp &= ~(0xFF << 16);
8485 tmp |= (0x1C << 16);
8486 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8487
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008488 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8489 tmp |= (1 << 27);
8490 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008491
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008492 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8493 tmp |= (1 << 27);
8494 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008495
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008496 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8497 tmp &= ~(0xF << 28);
8498 tmp |= (4 << 28);
8499 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008500
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008501 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8502 tmp &= ~(0xF << 28);
8503 tmp |= (4 << 28);
8504 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008505}
8506
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008507/* Implements 3 different sequences from BSpec chapter "Display iCLK
8508 * Programming" based on the parameters passed:
8509 * - Sequence to enable CLKOUT_DP
8510 * - Sequence to enable CLKOUT_DP without spread
8511 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8512 */
8513static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8514 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008515{
8516 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008517 uint32_t reg, tmp;
8518
8519 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8520 with_spread = true;
Ville Syrjäläc2699522015-08-27 23:55:59 +03008521 if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008522 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008523
Ville Syrjäläa5805162015-05-26 20:42:30 +03008524 mutex_lock(&dev_priv->sb_lock);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008525
8526 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8527 tmp &= ~SBI_SSCCTL_DISABLE;
8528 tmp |= SBI_SSCCTL_PATHALT;
8529 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8530
8531 udelay(24);
8532
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008533 if (with_spread) {
8534 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8535 tmp &= ~SBI_SSCCTL_PATHALT;
8536 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008537
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008538 if (with_fdi) {
8539 lpt_reset_fdi_mphy(dev_priv);
8540 lpt_program_fdi_mphy(dev_priv);
8541 }
8542 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02008543
Ville Syrjäläc2699522015-08-27 23:55:59 +03008544 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008545 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8546 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8547 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01008548
Ville Syrjäläa5805162015-05-26 20:42:30 +03008549 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008550}
8551
Paulo Zanoni47701c32013-07-23 11:19:25 -03008552/* Sequence to disable CLKOUT_DP */
8553static void lpt_disable_clkout_dp(struct drm_device *dev)
8554{
8555 struct drm_i915_private *dev_priv = dev->dev_private;
8556 uint32_t reg, tmp;
8557
Ville Syrjäläa5805162015-05-26 20:42:30 +03008558 mutex_lock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008559
Ville Syrjäläc2699522015-08-27 23:55:59 +03008560 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni47701c32013-07-23 11:19:25 -03008561 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8562 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8563 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8564
8565 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8566 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8567 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8568 tmp |= SBI_SSCCTL_PATHALT;
8569 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8570 udelay(32);
8571 }
8572 tmp |= SBI_SSCCTL_DISABLE;
8573 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8574 }
8575
Ville Syrjäläa5805162015-05-26 20:42:30 +03008576 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008577}
8578
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008579#define BEND_IDX(steps) ((50 + (steps)) / 5)
8580
8581static const uint16_t sscdivintphase[] = {
8582 [BEND_IDX( 50)] = 0x3B23,
8583 [BEND_IDX( 45)] = 0x3B23,
8584 [BEND_IDX( 40)] = 0x3C23,
8585 [BEND_IDX( 35)] = 0x3C23,
8586 [BEND_IDX( 30)] = 0x3D23,
8587 [BEND_IDX( 25)] = 0x3D23,
8588 [BEND_IDX( 20)] = 0x3E23,
8589 [BEND_IDX( 15)] = 0x3E23,
8590 [BEND_IDX( 10)] = 0x3F23,
8591 [BEND_IDX( 5)] = 0x3F23,
8592 [BEND_IDX( 0)] = 0x0025,
8593 [BEND_IDX( -5)] = 0x0025,
8594 [BEND_IDX(-10)] = 0x0125,
8595 [BEND_IDX(-15)] = 0x0125,
8596 [BEND_IDX(-20)] = 0x0225,
8597 [BEND_IDX(-25)] = 0x0225,
8598 [BEND_IDX(-30)] = 0x0325,
8599 [BEND_IDX(-35)] = 0x0325,
8600 [BEND_IDX(-40)] = 0x0425,
8601 [BEND_IDX(-45)] = 0x0425,
8602 [BEND_IDX(-50)] = 0x0525,
8603};
8604
8605/*
8606 * Bend CLKOUT_DP
8607 * steps -50 to 50 inclusive, in steps of 5
8608 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
8609 * change in clock period = -(steps / 10) * 5.787 ps
8610 */
8611static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
8612{
8613 uint32_t tmp;
8614 int idx = BEND_IDX(steps);
8615
8616 if (WARN_ON(steps % 5 != 0))
8617 return;
8618
8619 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
8620 return;
8621
8622 mutex_lock(&dev_priv->sb_lock);
8623
8624 if (steps % 10 != 0)
8625 tmp = 0xAAAAAAAB;
8626 else
8627 tmp = 0x00000000;
8628 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
8629
8630 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
8631 tmp &= 0xffff0000;
8632 tmp |= sscdivintphase[idx];
8633 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
8634
8635 mutex_unlock(&dev_priv->sb_lock);
8636}
8637
8638#undef BEND_IDX
8639
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008640static void lpt_init_pch_refclk(struct drm_device *dev)
8641{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008642 struct intel_encoder *encoder;
8643 bool has_vga = false;
8644
Damien Lespiaub2784e12014-08-05 11:29:37 +01008645 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008646 switch (encoder->type) {
8647 case INTEL_OUTPUT_ANALOG:
8648 has_vga = true;
8649 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008650 default:
8651 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008652 }
8653 }
8654
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008655 if (has_vga) {
8656 lpt_bend_clkout_dp(to_i915(dev), 0);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008657 lpt_enable_clkout_dp(dev, true, true);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008658 } else {
Paulo Zanoni47701c32013-07-23 11:19:25 -03008659 lpt_disable_clkout_dp(dev);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008660 }
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008661}
8662
Paulo Zanonidde86e22012-12-01 12:04:25 -02008663/*
8664 * Initialize reference clocks when the driver loads
8665 */
8666void intel_init_pch_refclk(struct drm_device *dev)
8667{
8668 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8669 ironlake_init_pch_refclk(dev);
8670 else if (HAS_PCH_LPT(dev))
8671 lpt_init_pch_refclk(dev);
8672}
8673
Daniel Vetter6ff93602013-04-19 11:24:36 +02008674static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03008675{
8676 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8677 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8678 int pipe = intel_crtc->pipe;
8679 uint32_t val;
8680
Daniel Vetter78114072013-06-13 00:54:57 +02008681 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03008682
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008683 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03008684 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008685 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008686 break;
8687 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008688 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008689 break;
8690 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008691 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008692 break;
8693 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008694 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008695 break;
8696 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03008697 /* Case prevented by intel_choose_pipe_bpp_dither. */
8698 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03008699 }
8700
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008701 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03008702 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8703
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008704 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03008705 val |= PIPECONF_INTERLACED_ILK;
8706 else
8707 val |= PIPECONF_PROGRESSIVE;
8708
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008709 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008710 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008711
Paulo Zanonic8203562012-09-12 10:06:29 -03008712 I915_WRITE(PIPECONF(pipe), val);
8713 POSTING_READ(PIPECONF(pipe));
8714}
8715
Daniel Vetter6ff93602013-04-19 11:24:36 +02008716static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008717{
Jani Nikula391bf042016-03-18 17:05:40 +02008718 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008719 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008720 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jani Nikula391bf042016-03-18 17:05:40 +02008721 u32 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008722
Jani Nikula391bf042016-03-18 17:05:40 +02008723 if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008724 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8725
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008726 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008727 val |= PIPECONF_INTERLACED_ILK;
8728 else
8729 val |= PIPECONF_PROGRESSIVE;
8730
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008731 I915_WRITE(PIPECONF(cpu_transcoder), val);
8732 POSTING_READ(PIPECONF(cpu_transcoder));
Jani Nikula391bf042016-03-18 17:05:40 +02008733}
8734
Jani Nikula391bf042016-03-18 17:05:40 +02008735static void haswell_set_pipemisc(struct drm_crtc *crtc)
8736{
8737 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8738 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8739
8740 if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
8741 u32 val = 0;
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008742
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008743 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008744 case 18:
8745 val |= PIPEMISC_DITHER_6_BPC;
8746 break;
8747 case 24:
8748 val |= PIPEMISC_DITHER_8_BPC;
8749 break;
8750 case 30:
8751 val |= PIPEMISC_DITHER_10_BPC;
8752 break;
8753 case 36:
8754 val |= PIPEMISC_DITHER_12_BPC;
8755 break;
8756 default:
8757 /* Case prevented by pipe_config_set_bpp. */
8758 BUG();
8759 }
8760
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008761 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008762 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8763
Jani Nikula391bf042016-03-18 17:05:40 +02008764 I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008765 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008766}
8767
Paulo Zanonid4b19312012-11-29 11:29:32 -02008768int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8769{
8770 /*
8771 * Account for spread spectrum to avoid
8772 * oversubscribing the link. Max center spread
8773 * is 2.5%; use 5% for safety's sake.
8774 */
8775 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02008776 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02008777}
8778
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008779static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02008780{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008781 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03008782}
8783
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008784static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
8785 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03008786 struct dpll *reduced_clock)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008787{
8788 struct drm_crtc *crtc = &intel_crtc->base;
8789 struct drm_device *dev = crtc->dev;
8790 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008791 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008792 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008793 struct drm_connector_state *connector_state;
8794 struct intel_encoder *encoder;
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008795 u32 dpll, fp, fp2;
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02008796 int factor, i;
Daniel Vetter09ede542013-04-30 14:01:45 +02008797 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008798
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008799 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008800 if (connector_state->crtc != crtc_state->base.crtc)
8801 continue;
8802
8803 encoder = to_intel_encoder(connector_state->best_encoder);
8804
8805 switch (encoder->type) {
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008806 case INTEL_OUTPUT_LVDS:
8807 is_lvds = true;
8808 break;
8809 case INTEL_OUTPUT_SDVO:
8810 case INTEL_OUTPUT_HDMI:
8811 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008812 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008813 default:
8814 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008815 }
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008816 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008817
Chris Wilsonc1858122010-12-03 21:35:48 +00008818 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07008819 factor = 21;
8820 if (is_lvds) {
8821 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008822 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02008823 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07008824 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008825 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07008826 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00008827
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008828 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Chris Wilsonc1858122010-12-03 21:35:48 +00008829
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008830 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
8831 fp |= FP_CB_TUNE;
8832
8833 if (reduced_clock) {
8834 fp2 = i9xx_dpll_compute_fp(reduced_clock);
8835
8836 if (reduced_clock->m < factor * reduced_clock->n)
8837 fp2 |= FP_CB_TUNE;
8838 } else {
8839 fp2 = fp;
8840 }
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008841
Chris Wilson5eddb702010-09-11 13:48:45 +01008842 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08008843
Eric Anholta07d6782011-03-30 13:01:08 -07008844 if (is_lvds)
8845 dpll |= DPLLB_MODE_LVDS;
8846 else
8847 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008848
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008849 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02008850 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008851
8852 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008853 dpll |= DPLL_SDVO_HIGH_SPEED;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008854 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008855 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08008856
Eric Anholta07d6782011-03-30 13:01:08 -07008857 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008858 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008859 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008860 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008861
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008862 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07008863 case 5:
8864 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8865 break;
8866 case 7:
8867 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8868 break;
8869 case 10:
8870 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8871 break;
8872 case 14:
8873 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8874 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08008875 }
8876
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02008877 if (is_lvds && intel_panel_use_ssc(dev_priv))
Kristian Høgsberg43565a02009-02-13 20:56:52 -05008878 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08008879 else
8880 dpll |= PLL_REF_INPUT_DREFCLK;
8881
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008882 dpll |= DPLL_VCO_ENABLE;
8883
8884 crtc_state->dpll_hw_state.dpll = dpll;
8885 crtc_state->dpll_hw_state.fp0 = fp;
8886 crtc_state->dpll_hw_state.fp1 = fp2;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008887}
8888
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008889static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8890 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008891{
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008892 struct drm_device *dev = crtc->base.dev;
8893 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03008894 struct dpll reduced_clock;
Ander Conselvan de Oliveira7ed9f892016-03-21 18:00:07 +02008895 bool has_reduced_clock = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008896 struct intel_shared_dpll *pll;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008897 const struct intel_limit *limit;
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008898 int refclk = 120000;
Jesse Barnes79e53942008-11-07 14:24:08 -08008899
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03008900 memset(&crtc_state->dpll_hw_state, 0,
8901 sizeof(crtc_state->dpll_hw_state));
8902
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02008903 crtc->lowfreq_avail = false;
8904
8905 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
8906 if (!crtc_state->has_pch_encoder)
8907 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008908
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008909 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
8910 if (intel_panel_use_ssc(dev_priv)) {
8911 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
8912 dev_priv->vbt.lvds_ssc_freq);
8913 refclk = dev_priv->vbt.lvds_ssc_freq;
8914 }
8915
8916 if (intel_is_dual_link_lvds(dev)) {
8917 if (refclk == 100000)
8918 limit = &intel_limits_ironlake_dual_lvds_100m;
8919 else
8920 limit = &intel_limits_ironlake_dual_lvds;
8921 } else {
8922 if (refclk == 100000)
8923 limit = &intel_limits_ironlake_single_lvds_100m;
8924 else
8925 limit = &intel_limits_ironlake_single_lvds;
8926 }
8927 } else {
8928 limit = &intel_limits_ironlake_dac;
8929 }
8930
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02008931 if (!crtc_state->clock_set &&
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008932 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8933 refclk, NULL, &crtc_state->dpll)) {
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02008934 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8935 return -EINVAL;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008936 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008937
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008938 ironlake_compute_dpll(crtc, crtc_state,
8939 has_reduced_clock ? &reduced_clock : NULL);
Daniel Vetter66e985c2013-06-05 13:34:20 +02008940
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02008941 pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
8942 if (pll == NULL) {
8943 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
8944 pipe_name(crtc->pipe));
8945 return -EINVAL;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02008946 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008947
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02008948 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
8949 has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008950 crtc->lowfreq_avail = true;
Daniel Vettere2b78262013-06-07 23:10:03 +02008951
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008952 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008953}
8954
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008955static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8956 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02008957{
8958 struct drm_device *dev = crtc->base.dev;
8959 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008960 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02008961
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008962 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8963 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8964 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8965 & ~TU_SIZE_MASK;
8966 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8967 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8968 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8969}
8970
8971static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8972 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008973 struct intel_link_m_n *m_n,
8974 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008975{
8976 struct drm_device *dev = crtc->base.dev;
8977 struct drm_i915_private *dev_priv = dev->dev_private;
8978 enum pipe pipe = crtc->pipe;
8979
8980 if (INTEL_INFO(dev)->gen >= 5) {
8981 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8982 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8983 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8984 & ~TU_SIZE_MASK;
8985 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8986 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8987 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008988 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8989 * gen < 8) and if DRRS is supported (to make sure the
8990 * registers are not unnecessarily read).
8991 */
8992 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008993 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008994 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8995 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8996 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8997 & ~TU_SIZE_MASK;
8998 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8999 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
9000 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9001 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009002 } else {
9003 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
9004 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
9005 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
9006 & ~TU_SIZE_MASK;
9007 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
9008 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
9009 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9010 }
9011}
9012
9013void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009014 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009015{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02009016 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009017 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
9018 else
9019 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009020 &pipe_config->dp_m_n,
9021 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009022}
9023
Daniel Vetter72419202013-04-04 13:28:53 +02009024static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009025 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02009026{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009027 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009028 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02009029}
9030
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009031static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009032 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009033{
9034 struct drm_device *dev = crtc->base.dev;
9035 struct drm_i915_private *dev_priv = dev->dev_private;
Chandra Kondurua1b22782015-04-07 15:28:45 -07009036 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
9037 uint32_t ps_ctrl = 0;
9038 int id = -1;
9039 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009040
Chandra Kondurua1b22782015-04-07 15:28:45 -07009041 /* find scaler attached to this pipe */
9042 for (i = 0; i < crtc->num_scalers; i++) {
9043 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
9044 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
9045 id = i;
9046 pipe_config->pch_pfit.enabled = true;
9047 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9048 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9049 break;
9050 }
9051 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009052
Chandra Kondurua1b22782015-04-07 15:28:45 -07009053 scaler_state->scaler_id = id;
9054 if (id >= 0) {
9055 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9056 } else {
9057 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009058 }
9059}
9060
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009061static void
9062skylake_get_initial_plane_config(struct intel_crtc *crtc,
9063 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009064{
9065 struct drm_device *dev = crtc->base.dev;
9066 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau40f46282015-02-27 11:15:21 +00009067 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009068 int pipe = crtc->pipe;
9069 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009070 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009071 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009072 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009073
Damien Lespiaud9806c92015-01-21 14:07:19 +00009074 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009075 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009076 DRM_DEBUG_KMS("failed to alloc fb\n");
9077 return;
9078 }
9079
Damien Lespiau1b842c82015-01-21 13:50:54 +00009080 fb = &intel_fb->base;
9081
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009082 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00009083 if (!(val & PLANE_CTL_ENABLE))
9084 goto error;
9085
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009086 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9087 fourcc = skl_format_to_fourcc(pixel_format,
9088 val & PLANE_CTL_ORDER_RGBX,
9089 val & PLANE_CTL_ALPHA_MASK);
9090 fb->pixel_format = fourcc;
9091 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9092
Damien Lespiau40f46282015-02-27 11:15:21 +00009093 tiling = val & PLANE_CTL_TILED_MASK;
9094 switch (tiling) {
9095 case PLANE_CTL_TILED_LINEAR:
9096 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9097 break;
9098 case PLANE_CTL_TILED_X:
9099 plane_config->tiling = I915_TILING_X;
9100 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9101 break;
9102 case PLANE_CTL_TILED_Y:
9103 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9104 break;
9105 case PLANE_CTL_TILED_YF:
9106 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9107 break;
9108 default:
9109 MISSING_CASE(tiling);
9110 goto error;
9111 }
9112
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009113 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9114 plane_config->base = base;
9115
9116 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9117
9118 val = I915_READ(PLANE_SIZE(pipe, 0));
9119 fb->height = ((val >> 16) & 0xfff) + 1;
9120 fb->width = ((val >> 0) & 0x1fff) + 1;
9121
9122 val = I915_READ(PLANE_STRIDE(pipe, 0));
Ville Syrjälä7b49f942016-01-12 21:08:32 +02009123 stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
Damien Lespiau40f46282015-02-27 11:15:21 +00009124 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009125 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9126
9127 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009128 fb->pixel_format,
9129 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009130
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009131 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009132
9133 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9134 pipe_name(pipe), fb->width, fb->height,
9135 fb->bits_per_pixel, base, fb->pitches[0],
9136 plane_config->size);
9137
Damien Lespiau2d140302015-02-05 17:22:18 +00009138 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009139 return;
9140
9141error:
9142 kfree(fb);
9143}
9144
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009145static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009146 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009147{
9148 struct drm_device *dev = crtc->base.dev;
9149 struct drm_i915_private *dev_priv = dev->dev_private;
9150 uint32_t tmp;
9151
9152 tmp = I915_READ(PF_CTL(crtc->pipe));
9153
9154 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009155 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009156 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9157 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02009158
9159 /* We currently do not free assignements of panel fitters on
9160 * ivb/hsw (since we don't use the higher upscaling modes which
9161 * differentiates them) so just WARN about this case for now. */
9162 if (IS_GEN7(dev)) {
9163 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9164 PF_PIPE_SEL_IVB(crtc->pipe));
9165 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009166 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009167}
9168
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009169static void
9170ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9171 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009172{
9173 struct drm_device *dev = crtc->base.dev;
9174 struct drm_i915_private *dev_priv = dev->dev_private;
9175 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009176 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009177 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009178 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009179 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009180 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009181
Damien Lespiau42a7b082015-02-05 19:35:13 +00009182 val = I915_READ(DSPCNTR(pipe));
9183 if (!(val & DISPLAY_PLANE_ENABLE))
9184 return;
9185
Damien Lespiaud9806c92015-01-21 14:07:19 +00009186 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009187 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009188 DRM_DEBUG_KMS("failed to alloc fb\n");
9189 return;
9190 }
9191
Damien Lespiau1b842c82015-01-21 13:50:54 +00009192 fb = &intel_fb->base;
9193
Daniel Vetter18c52472015-02-10 17:16:09 +00009194 if (INTEL_INFO(dev)->gen >= 4) {
9195 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00009196 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00009197 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9198 }
9199 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009200
9201 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00009202 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009203 fb->pixel_format = fourcc;
9204 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009205
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009206 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009207 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009208 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009209 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00009210 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009211 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009212 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009213 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009214 }
9215 plane_config->base = base;
9216
9217 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009218 fb->width = ((val >> 16) & 0xfff) + 1;
9219 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009220
9221 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009222 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009223
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009224 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009225 fb->pixel_format,
9226 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009227
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009228 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009229
Damien Lespiau2844a922015-01-20 12:51:48 +00009230 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9231 pipe_name(pipe), fb->width, fb->height,
9232 fb->bits_per_pixel, base, fb->pitches[0],
9233 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009234
Damien Lespiau2d140302015-02-05 17:22:18 +00009235 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009236}
9237
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009238static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009239 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009240{
9241 struct drm_device *dev = crtc->base.dev;
9242 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak17290502016-02-12 18:55:11 +02009243 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009244 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02009245 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009246
Imre Deak17290502016-02-12 18:55:11 +02009247 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9248 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03009249 return false;
9250
Daniel Vettere143a212013-07-04 12:01:15 +02009251 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009252 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02009253
Imre Deak17290502016-02-12 18:55:11 +02009254 ret = false;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009255 tmp = I915_READ(PIPECONF(crtc->pipe));
9256 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02009257 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009258
Ville Syrjälä42571ae2013-09-06 23:29:00 +03009259 switch (tmp & PIPECONF_BPC_MASK) {
9260 case PIPECONF_6BPC:
9261 pipe_config->pipe_bpp = 18;
9262 break;
9263 case PIPECONF_8BPC:
9264 pipe_config->pipe_bpp = 24;
9265 break;
9266 case PIPECONF_10BPC:
9267 pipe_config->pipe_bpp = 30;
9268 break;
9269 case PIPECONF_12BPC:
9270 pipe_config->pipe_bpp = 36;
9271 break;
9272 default:
9273 break;
9274 }
9275
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02009276 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9277 pipe_config->limited_color_range = true;
9278
Daniel Vetterab9412b2013-05-03 11:49:46 +02009279 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02009280 struct intel_shared_dpll *pll;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009281 enum intel_dpll_id pll_id;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009282
Daniel Vetter88adfff2013-03-28 10:42:01 +01009283 pipe_config->has_pch_encoder = true;
9284
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009285 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9286 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9287 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02009288
9289 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009290
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009291 if (HAS_PCH_IBX(dev_priv)) {
Imre Deakd9a7bc62016-05-12 16:18:50 +03009292 /*
9293 * The pipe->pch transcoder and pch transcoder->pll
9294 * mapping is fixed.
9295 */
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009296 pll_id = (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009297 } else {
9298 tmp = I915_READ(PCH_DPLL_SEL);
9299 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009300 pll_id = DPLL_ID_PCH_PLL_B;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009301 else
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009302 pll_id= DPLL_ID_PCH_PLL_A;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009303 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02009304
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009305 pipe_config->shared_dpll =
9306 intel_get_shared_dpll_by_id(dev_priv, pll_id);
9307 pll = pipe_config->shared_dpll;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009308
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +02009309 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9310 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02009311
9312 tmp = pipe_config->dpll_hw_state.dpll;
9313 pipe_config->pixel_multiplier =
9314 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9315 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03009316
9317 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009318 } else {
9319 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009320 }
9321
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009322 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +02009323 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009324
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009325 ironlake_get_pfit_config(crtc, pipe_config);
9326
Imre Deak17290502016-02-12 18:55:11 +02009327 ret = true;
9328
9329out:
9330 intel_display_power_put(dev_priv, power_domain);
9331
9332 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009333}
9334
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009335static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9336{
9337 struct drm_device *dev = dev_priv->dev;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009338 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009339
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009340 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05009341 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009342 pipe_name(crtc->pipe));
9343
Rob Clarke2c719b2014-12-15 13:56:32 -05009344 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9345 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
Ville Syrjälä01403de2015-09-18 20:03:33 +03009346 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9347 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009348 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9349 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009350 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03009351 if (IS_HASWELL(dev))
Rob Clarke2c719b2014-12-15 13:56:32 -05009352 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03009353 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009354 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009355 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009356 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009357 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009358 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009359
Paulo Zanoni9926ada2014-04-01 19:39:47 -03009360 /*
9361 * In theory we can still leave IRQs enabled, as long as only the HPD
9362 * interrupts remain enabled. We used to check for that, but since it's
9363 * gen-specific and since we only disable LCPLL after we fully disable
9364 * the interrupts, the check below should be enough.
9365 */
Rob Clarke2c719b2014-12-15 13:56:32 -05009366 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009367}
9368
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009369static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9370{
9371 struct drm_device *dev = dev_priv->dev;
9372
9373 if (IS_HASWELL(dev))
9374 return I915_READ(D_COMP_HSW);
9375 else
9376 return I915_READ(D_COMP_BDW);
9377}
9378
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009379static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9380{
9381 struct drm_device *dev = dev_priv->dev;
9382
9383 if (IS_HASWELL(dev)) {
9384 mutex_lock(&dev_priv->rps.hw_lock);
9385 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9386 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03009387 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009388 mutex_unlock(&dev_priv->rps.hw_lock);
9389 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009390 I915_WRITE(D_COMP_BDW, val);
9391 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009392 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009393}
9394
9395/*
9396 * This function implements pieces of two sequences from BSpec:
9397 * - Sequence for display software to disable LCPLL
9398 * - Sequence for display software to allow package C8+
9399 * The steps implemented here are just the steps that actually touch the LCPLL
9400 * register. Callers should take care of disabling all the display engine
9401 * functions, doing the mode unset, fixing interrupts, etc.
9402 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009403static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9404 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009405{
9406 uint32_t val;
9407
9408 assert_can_disable_lcpll(dev_priv);
9409
9410 val = I915_READ(LCPLL_CTL);
9411
9412 if (switch_to_fclk) {
9413 val |= LCPLL_CD_SOURCE_FCLK;
9414 I915_WRITE(LCPLL_CTL, val);
9415
9416 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9417 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9418 DRM_ERROR("Switching to FCLK failed\n");
9419
9420 val = I915_READ(LCPLL_CTL);
9421 }
9422
9423 val |= LCPLL_PLL_DISABLE;
9424 I915_WRITE(LCPLL_CTL, val);
9425 POSTING_READ(LCPLL_CTL);
9426
9427 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9428 DRM_ERROR("LCPLL still locked\n");
9429
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009430 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009431 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009432 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009433 ndelay(100);
9434
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009435 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9436 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009437 DRM_ERROR("D_COMP RCOMP still in progress\n");
9438
9439 if (allow_power_down) {
9440 val = I915_READ(LCPLL_CTL);
9441 val |= LCPLL_POWER_DOWN_ALLOW;
9442 I915_WRITE(LCPLL_CTL, val);
9443 POSTING_READ(LCPLL_CTL);
9444 }
9445}
9446
9447/*
9448 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9449 * source.
9450 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009451static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009452{
9453 uint32_t val;
9454
9455 val = I915_READ(LCPLL_CTL);
9456
9457 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9458 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9459 return;
9460
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009461 /*
9462 * Make sure we're not on PC8 state before disabling PC8, otherwise
9463 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009464 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02009465 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03009466
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009467 if (val & LCPLL_POWER_DOWN_ALLOW) {
9468 val &= ~LCPLL_POWER_DOWN_ALLOW;
9469 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02009470 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009471 }
9472
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009473 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009474 val |= D_COMP_COMP_FORCE;
9475 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009476 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009477
9478 val = I915_READ(LCPLL_CTL);
9479 val &= ~LCPLL_PLL_DISABLE;
9480 I915_WRITE(LCPLL_CTL, val);
9481
9482 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9483 DRM_ERROR("LCPLL not locked yet\n");
9484
9485 if (val & LCPLL_CD_SOURCE_FCLK) {
9486 val = I915_READ(LCPLL_CTL);
9487 val &= ~LCPLL_CD_SOURCE_FCLK;
9488 I915_WRITE(LCPLL_CTL, val);
9489
9490 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9491 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9492 DRM_ERROR("Switching back to LCPLL failed\n");
9493 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03009494
Mika Kuoppala59bad942015-01-16 11:34:40 +02009495 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ville Syrjäläb6283052015-06-03 15:45:07 +03009496 intel_update_cdclk(dev_priv->dev);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009497}
9498
Paulo Zanoni765dab672014-03-07 20:08:18 -03009499/*
9500 * Package states C8 and deeper are really deep PC states that can only be
9501 * reached when all the devices on the system allow it, so even if the graphics
9502 * device allows PC8+, it doesn't mean the system will actually get to these
9503 * states. Our driver only allows PC8+ when going into runtime PM.
9504 *
9505 * The requirements for PC8+ are that all the outputs are disabled, the power
9506 * well is disabled and most interrupts are disabled, and these are also
9507 * requirements for runtime PM. When these conditions are met, we manually do
9508 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9509 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9510 * hang the machine.
9511 *
9512 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9513 * the state of some registers, so when we come back from PC8+ we need to
9514 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9515 * need to take care of the registers kept by RC6. Notice that this happens even
9516 * if we don't put the device in PCI D3 state (which is what currently happens
9517 * because of the runtime PM support).
9518 *
9519 * For more, read "Display Sequences for Package C8" on the hardware
9520 * documentation.
9521 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009522void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009523{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009524 struct drm_device *dev = dev_priv->dev;
9525 uint32_t val;
9526
Paulo Zanonic67a4702013-08-19 13:18:09 -03009527 DRM_DEBUG_KMS("Enabling package C8+\n");
9528
Ville Syrjäläc2699522015-08-27 23:55:59 +03009529 if (HAS_PCH_LPT_LP(dev)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009530 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9531 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9532 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9533 }
9534
9535 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009536 hsw_disable_lcpll(dev_priv, true, true);
9537}
9538
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009539void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009540{
9541 struct drm_device *dev = dev_priv->dev;
9542 uint32_t val;
9543
Paulo Zanonic67a4702013-08-19 13:18:09 -03009544 DRM_DEBUG_KMS("Disabling package C8+\n");
9545
9546 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009547 lpt_init_pch_refclk(dev);
9548
Ville Syrjäläc2699522015-08-27 23:55:59 +03009549 if (HAS_PCH_LPT_LP(dev)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009550 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9551 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9552 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9553 }
Paulo Zanonic67a4702013-08-19 13:18:09 -03009554}
9555
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009556static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309557{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009558 struct drm_device *dev = old_state->dev;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009559 struct intel_atomic_state *old_intel_state =
9560 to_intel_atomic_state(old_state);
9561 unsigned int req_cdclk = old_intel_state->dev_cdclk;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309562
Imre Deakc6c46962016-04-01 16:02:40 +03009563 broxton_set_cdclk(to_i915(dev), req_cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309564}
9565
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009566/* compute the max rate for new configuration */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009567static int ilk_max_pixel_rate(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009568{
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009569 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
9570 struct drm_i915_private *dev_priv = state->dev->dev_private;
9571 struct drm_crtc *crtc;
9572 struct drm_crtc_state *cstate;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009573 struct intel_crtc_state *crtc_state;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009574 unsigned max_pixel_rate = 0, i;
9575 enum pipe pipe;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009576
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009577 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
9578 sizeof(intel_state->min_pixclk));
9579
9580 for_each_crtc_in_state(state, crtc, cstate, i) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009581 int pixel_rate;
9582
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009583 crtc_state = to_intel_crtc_state(cstate);
9584 if (!crtc_state->base.enable) {
9585 intel_state->min_pixclk[i] = 0;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009586 continue;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009587 }
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009588
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009589 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009590
9591 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009592 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009593 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9594
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009595 intel_state->min_pixclk[i] = pixel_rate;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009596 }
9597
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009598 for_each_pipe(dev_priv, pipe)
9599 max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
9600
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009601 return max_pixel_rate;
9602}
9603
9604static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9605{
9606 struct drm_i915_private *dev_priv = dev->dev_private;
9607 uint32_t val, data;
9608 int ret;
9609
9610 if (WARN((I915_READ(LCPLL_CTL) &
9611 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9612 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9613 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9614 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9615 "trying to change cdclk frequency with cdclk not enabled\n"))
9616 return;
9617
9618 mutex_lock(&dev_priv->rps.hw_lock);
9619 ret = sandybridge_pcode_write(dev_priv,
9620 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9621 mutex_unlock(&dev_priv->rps.hw_lock);
9622 if (ret) {
9623 DRM_ERROR("failed to inform pcode about cdclk change\n");
9624 return;
9625 }
9626
9627 val = I915_READ(LCPLL_CTL);
9628 val |= LCPLL_CD_SOURCE_FCLK;
9629 I915_WRITE(LCPLL_CTL, val);
9630
Tvrtko Ursulin5ba00172016-03-03 14:36:45 +00009631 if (wait_for_us(I915_READ(LCPLL_CTL) &
9632 LCPLL_CD_SOURCE_FCLK_DONE, 1))
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009633 DRM_ERROR("Switching to FCLK failed\n");
9634
9635 val = I915_READ(LCPLL_CTL);
9636 val &= ~LCPLL_CLK_FREQ_MASK;
9637
9638 switch (cdclk) {
9639 case 450000:
9640 val |= LCPLL_CLK_FREQ_450;
9641 data = 0;
9642 break;
9643 case 540000:
9644 val |= LCPLL_CLK_FREQ_54O_BDW;
9645 data = 1;
9646 break;
9647 case 337500:
9648 val |= LCPLL_CLK_FREQ_337_5_BDW;
9649 data = 2;
9650 break;
9651 case 675000:
9652 val |= LCPLL_CLK_FREQ_675_BDW;
9653 data = 3;
9654 break;
9655 default:
9656 WARN(1, "invalid cdclk frequency\n");
9657 return;
9658 }
9659
9660 I915_WRITE(LCPLL_CTL, val);
9661
9662 val = I915_READ(LCPLL_CTL);
9663 val &= ~LCPLL_CD_SOURCE_FCLK;
9664 I915_WRITE(LCPLL_CTL, val);
9665
Tvrtko Ursulin5ba00172016-03-03 14:36:45 +00009666 if (wait_for_us((I915_READ(LCPLL_CTL) &
9667 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009668 DRM_ERROR("Switching back to LCPLL failed\n");
9669
9670 mutex_lock(&dev_priv->rps.hw_lock);
9671 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9672 mutex_unlock(&dev_priv->rps.hw_lock);
9673
Ville Syrjälä7f1052a2016-04-26 19:46:32 +03009674 I915_WRITE(CDCLK_FREQ, DIV_ROUND_CLOSEST(cdclk, 1000) - 1);
9675
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009676 intel_update_cdclk(dev);
9677
9678 WARN(cdclk != dev_priv->cdclk_freq,
9679 "cdclk requested %d kHz but got %d kHz\n",
9680 cdclk, dev_priv->cdclk_freq);
9681}
9682
Ville Syrjälä587c7912016-05-11 22:44:41 +03009683static int broadwell_calc_cdclk(int max_pixclk)
9684{
9685 if (max_pixclk > 540000)
9686 return 675000;
9687 else if (max_pixclk > 450000)
9688 return 540000;
9689 else if (max_pixclk > 337500)
9690 return 450000;
9691 else
9692 return 337500;
9693}
9694
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009695static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009696{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009697 struct drm_i915_private *dev_priv = to_i915(state->dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009698 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009699 int max_pixclk = ilk_max_pixel_rate(state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009700 int cdclk;
9701
9702 /*
9703 * FIXME should also account for plane ratio
9704 * once 64bpp pixel formats are supported.
9705 */
Ville Syrjälä587c7912016-05-11 22:44:41 +03009706 cdclk = broadwell_calc_cdclk(max_pixclk);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009707
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009708 if (cdclk > dev_priv->max_cdclk_freq) {
Maarten Lankhorst63ba5342015-11-24 11:29:03 +01009709 DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9710 cdclk, dev_priv->max_cdclk_freq);
9711 return -EINVAL;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009712 }
9713
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009714 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
9715 if (!intel_state->active_crtcs)
Ville Syrjälä587c7912016-05-11 22:44:41 +03009716 intel_state->dev_cdclk = broadwell_calc_cdclk(0);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009717
9718 return 0;
9719}
9720
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009721static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009722{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009723 struct drm_device *dev = old_state->dev;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009724 struct intel_atomic_state *old_intel_state =
9725 to_intel_atomic_state(old_state);
9726 unsigned req_cdclk = old_intel_state->dev_cdclk;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009727
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009728 broadwell_set_cdclk(dev, req_cdclk);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009729}
9730
Clint Taylorc89e39f2016-05-13 23:41:21 +03009731static int skl_modeset_calc_cdclk(struct drm_atomic_state *state)
9732{
9733 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
9734 struct drm_i915_private *dev_priv = to_i915(state->dev);
9735 const int max_pixclk = ilk_max_pixel_rate(state);
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03009736 int vco = intel_state->cdclk_pll_vco;
Clint Taylorc89e39f2016-05-13 23:41:21 +03009737 int cdclk;
9738
9739 /*
9740 * FIXME should also account for plane ratio
9741 * once 64bpp pixel formats are supported.
9742 */
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03009743 cdclk = skl_calc_cdclk(max_pixclk, vco);
Clint Taylorc89e39f2016-05-13 23:41:21 +03009744
9745 /*
9746 * FIXME move the cdclk caclulation to
9747 * compute_config() so we can fail gracegully.
9748 */
9749 if (cdclk > dev_priv->max_cdclk_freq) {
9750 DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9751 cdclk, dev_priv->max_cdclk_freq);
9752 cdclk = dev_priv->max_cdclk_freq;
9753 }
9754
9755 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
9756 if (!intel_state->active_crtcs)
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03009757 intel_state->dev_cdclk = skl_calc_cdclk(0, vco);
Clint Taylorc89e39f2016-05-13 23:41:21 +03009758
9759 return 0;
9760}
9761
9762static void skl_modeset_commit_cdclk(struct drm_atomic_state *old_state)
9763{
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03009764 struct drm_i915_private *dev_priv = to_i915(old_state->dev);
9765 struct intel_atomic_state *intel_state = to_intel_atomic_state(old_state);
9766 unsigned int req_cdclk = intel_state->dev_cdclk;
9767 unsigned int req_vco = intel_state->cdclk_pll_vco;
Clint Taylorc89e39f2016-05-13 23:41:21 +03009768
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03009769 skl_set_cdclk(dev_priv, req_cdclk, req_vco);
Clint Taylorc89e39f2016-05-13 23:41:21 +03009770}
9771
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009772static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9773 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009774{
Mika Kaholaaf3997b2016-02-05 13:29:28 +02009775 struct intel_encoder *intel_encoder =
9776 intel_ddi_get_crtc_new_encoder(crtc_state);
9777
9778 if (intel_encoder->type != INTEL_OUTPUT_DSI) {
9779 if (!intel_ddi_pll_select(crtc, crtc_state))
9780 return -EINVAL;
9781 }
Daniel Vetter716c2e52014-06-25 22:02:02 +03009782
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009783 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02009784
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009785 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009786}
9787
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309788static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9789 enum port port,
9790 struct intel_crtc_state *pipe_config)
9791{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009792 enum intel_dpll_id id;
9793
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309794 switch (port) {
9795 case PORT_A:
9796 pipe_config->ddi_pll_sel = SKL_DPLL0;
Imre Deak08250c42016-03-14 19:55:34 +02009797 id = DPLL_ID_SKL_DPLL0;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309798 break;
9799 case PORT_B:
9800 pipe_config->ddi_pll_sel = SKL_DPLL1;
Imre Deak08250c42016-03-14 19:55:34 +02009801 id = DPLL_ID_SKL_DPLL1;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309802 break;
9803 case PORT_C:
9804 pipe_config->ddi_pll_sel = SKL_DPLL2;
Imre Deak08250c42016-03-14 19:55:34 +02009805 id = DPLL_ID_SKL_DPLL2;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309806 break;
9807 default:
9808 DRM_ERROR("Incorrect port type\n");
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009809 return;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309810 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009811
9812 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309813}
9814
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009815static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9816 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009817 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009818{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009819 enum intel_dpll_id id;
Ander Conselvan de Oliveiraa3c988e2016-03-08 17:46:27 +02009820 u32 temp;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009821
9822 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9823 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9824
9825 switch (pipe_config->ddi_pll_sel) {
Damien Lespiau3148ade2014-11-21 16:14:56 +00009826 case SKL_DPLL0:
Ander Conselvan de Oliveiraa3c988e2016-03-08 17:46:27 +02009827 id = DPLL_ID_SKL_DPLL0;
9828 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009829 case SKL_DPLL1:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009830 id = DPLL_ID_SKL_DPLL1;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009831 break;
9832 case SKL_DPLL2:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009833 id = DPLL_ID_SKL_DPLL2;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009834 break;
9835 case SKL_DPLL3:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009836 id = DPLL_ID_SKL_DPLL3;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009837 break;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009838 default:
9839 MISSING_CASE(pipe_config->ddi_pll_sel);
9840 return;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009841 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009842
9843 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009844}
9845
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009846static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9847 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009848 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009849{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009850 enum intel_dpll_id id;
9851
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009852 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9853
9854 switch (pipe_config->ddi_pll_sel) {
9855 case PORT_CLK_SEL_WRPLL1:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009856 id = DPLL_ID_WRPLL1;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009857 break;
9858 case PORT_CLK_SEL_WRPLL2:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009859 id = DPLL_ID_WRPLL2;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009860 break;
Maarten Lankhorst00490c22015-11-16 14:42:12 +01009861 case PORT_CLK_SEL_SPLL:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009862 id = DPLL_ID_SPLL;
Ville Syrjälä79bd23d2015-12-01 23:32:07 +02009863 break;
Ander Conselvan de Oliveira9d16da62016-03-08 17:46:26 +02009864 case PORT_CLK_SEL_LCPLL_810:
9865 id = DPLL_ID_LCPLL_810;
9866 break;
9867 case PORT_CLK_SEL_LCPLL_1350:
9868 id = DPLL_ID_LCPLL_1350;
9869 break;
9870 case PORT_CLK_SEL_LCPLL_2700:
9871 id = DPLL_ID_LCPLL_2700;
9872 break;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009873 default:
9874 MISSING_CASE(pipe_config->ddi_pll_sel);
9875 /* fall through */
9876 case PORT_CLK_SEL_NONE:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009877 return;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009878 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009879
9880 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009881}
9882
Jani Nikulacf304292016-03-18 17:05:41 +02009883static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
9884 struct intel_crtc_state *pipe_config,
9885 unsigned long *power_domain_mask)
9886{
9887 struct drm_device *dev = crtc->base.dev;
9888 struct drm_i915_private *dev_priv = dev->dev_private;
9889 enum intel_display_power_domain power_domain;
9890 u32 tmp;
9891
Imre Deakd9a7bc62016-05-12 16:18:50 +03009892 /*
9893 * The pipe->transcoder mapping is fixed with the exception of the eDP
9894 * transcoder handled below.
9895 */
Jani Nikulacf304292016-03-18 17:05:41 +02009896 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
9897
9898 /*
9899 * XXX: Do intel_display_power_get_if_enabled before reading this (for
9900 * consistency and less surprising code; it's in always on power).
9901 */
9902 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9903 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9904 enum pipe trans_edp_pipe;
9905 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9906 default:
9907 WARN(1, "unknown pipe linked to edp transcoder\n");
9908 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9909 case TRANS_DDI_EDP_INPUT_A_ON:
9910 trans_edp_pipe = PIPE_A;
9911 break;
9912 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9913 trans_edp_pipe = PIPE_B;
9914 break;
9915 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9916 trans_edp_pipe = PIPE_C;
9917 break;
9918 }
9919
9920 if (trans_edp_pipe == crtc->pipe)
9921 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9922 }
9923
9924 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
9925 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9926 return false;
9927 *power_domain_mask |= BIT(power_domain);
9928
9929 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
9930
9931 return tmp & PIPECONF_ENABLE;
9932}
9933
Jani Nikula4d1de972016-03-18 17:05:42 +02009934static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
9935 struct intel_crtc_state *pipe_config,
9936 unsigned long *power_domain_mask)
9937{
9938 struct drm_device *dev = crtc->base.dev;
9939 struct drm_i915_private *dev_priv = dev->dev_private;
9940 enum intel_display_power_domain power_domain;
9941 enum port port;
9942 enum transcoder cpu_transcoder;
9943 u32 tmp;
9944
9945 pipe_config->has_dsi_encoder = false;
9946
9947 for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
9948 if (port == PORT_A)
9949 cpu_transcoder = TRANSCODER_DSI_A;
9950 else
9951 cpu_transcoder = TRANSCODER_DSI_C;
9952
9953 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
9954 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9955 continue;
9956 *power_domain_mask |= BIT(power_domain);
9957
Imre Deakdb18b6a2016-03-24 12:41:40 +02009958 /*
9959 * The PLL needs to be enabled with a valid divider
9960 * configuration, otherwise accessing DSI registers will hang
9961 * the machine. See BSpec North Display Engine
9962 * registers/MIPI[BXT]. We can break out here early, since we
9963 * need the same DSI PLL to be enabled for both DSI ports.
9964 */
9965 if (!intel_dsi_pll_is_enabled(dev_priv))
9966 break;
9967
Jani Nikula4d1de972016-03-18 17:05:42 +02009968 /* XXX: this works for video mode only */
9969 tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
9970 if (!(tmp & DPI_ENABLE))
9971 continue;
9972
9973 tmp = I915_READ(MIPI_CTRL(port));
9974 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
9975 continue;
9976
9977 pipe_config->cpu_transcoder = cpu_transcoder;
9978 pipe_config->has_dsi_encoder = true;
9979 break;
9980 }
9981
9982 return pipe_config->has_dsi_encoder;
9983}
9984
Daniel Vetter26804af2014-06-25 22:01:55 +03009985static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009986 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +03009987{
9988 struct drm_device *dev = crtc->base.dev;
9989 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009990 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +03009991 enum port port;
9992 uint32_t tmp;
9993
9994 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9995
9996 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9997
Rodrigo Vivief11bdb2015-10-28 04:16:45 -07009998 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009999 skylake_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M3760b592014-08-22 09:49:11 +053010000 else if (IS_BROXTON(dev))
10001 bxt_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +000010002 else
10003 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +030010004
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010005 pll = pipe_config->shared_dpll;
10006 if (pll) {
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020010007 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
10008 &pipe_config->dpll_hw_state));
Daniel Vetterd452c5b2014-07-04 11:27:39 -030010009 }
10010
Daniel Vetter26804af2014-06-25 22:01:55 +030010011 /*
10012 * Haswell has only FDI/PCH transcoder A. It is which is connected to
10013 * DDI E. So just check whether this pipe is wired to DDI E and whether
10014 * the PCH transcoder is on.
10015 */
Damien Lespiauca370452013-12-03 13:56:24 +000010016 if (INTEL_INFO(dev)->gen < 9 &&
10017 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +030010018 pipe_config->has_pch_encoder = true;
10019
10020 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
10021 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
10022 FDI_DP_PORT_WIDTH_SHIFT) + 1;
10023
10024 ironlake_get_fdi_m_n_config(crtc, pipe_config);
10025 }
10026}
10027
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010028static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010029 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010030{
10031 struct drm_device *dev = crtc->base.dev;
10032 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak17290502016-02-12 18:55:11 +020010033 enum intel_display_power_domain power_domain;
10034 unsigned long power_domain_mask;
Jani Nikulacf304292016-03-18 17:05:41 +020010035 bool active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010036
Imre Deak17290502016-02-12 18:55:11 +020010037 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
10038 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +020010039 return false;
Imre Deak17290502016-02-12 18:55:11 +020010040 power_domain_mask = BIT(power_domain);
10041
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010042 pipe_config->shared_dpll = NULL;
Daniel Vetterc0d43d62013-06-07 23:11:08 +020010043
Jani Nikulacf304292016-03-18 17:05:41 +020010044 active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
Daniel Vettereccb1402013-05-22 00:50:22 +020010045
Jani Nikula4d1de972016-03-18 17:05:42 +020010046 if (IS_BROXTON(dev_priv)) {
10047 bxt_get_dsi_transcoder_state(crtc, pipe_config,
10048 &power_domain_mask);
10049 WARN_ON(active && pipe_config->has_dsi_encoder);
10050 if (pipe_config->has_dsi_encoder)
10051 active = true;
10052 }
10053
Jani Nikulacf304292016-03-18 17:05:41 +020010054 if (!active)
Imre Deak17290502016-02-12 18:55:11 +020010055 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010056
Jani Nikula4d1de972016-03-18 17:05:42 +020010057 if (!pipe_config->has_dsi_encoder) {
10058 haswell_get_ddi_port_state(crtc, pipe_config);
10059 intel_get_pipe_timings(crtc, pipe_config);
10060 }
Daniel Vetter627eb5a2013-04-29 19:33:42 +020010061
Jani Nikulabc58be62016-03-18 17:05:39 +020010062 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010063
Lionel Landwerlin05dc6982016-03-16 10:57:15 +000010064 pipe_config->gamma_mode =
10065 I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
10066
Chandra Kondurua1b22782015-04-07 15:28:45 -070010067 if (INTEL_INFO(dev)->gen >= 9) {
10068 skl_init_scalers(dev, crtc, pipe_config);
10069 }
10070
Chandra Konduruaf99ced2015-05-11 14:35:47 -070010071 if (INTEL_INFO(dev)->gen >= 9) {
10072 pipe_config->scaler_state.scaler_id = -1;
10073 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
10074 }
10075
Imre Deak17290502016-02-12 18:55:11 +020010076 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
10077 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
10078 power_domain_mask |= BIT(power_domain);
Rodrigo Vivi1c132b42015-09-02 15:19:26 -070010079 if (INTEL_INFO(dev)->gen >= 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +000010080 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -080010081 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -070010082 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesbd2e2442014-11-13 17:51:47 +000010083 }
Daniel Vetter88adfff2013-03-28 10:42:01 +010010084
Jesse Barnese59150d2014-01-07 13:30:45 -080010085 if (IS_HASWELL(dev))
10086 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
10087 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030010088
Jani Nikula4d1de972016-03-18 17:05:42 +020010089 if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
10090 !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
Clint Taylorebb69c92014-09-30 10:30:22 -070010091 pipe_config->pixel_multiplier =
10092 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
10093 } else {
10094 pipe_config->pixel_multiplier = 1;
10095 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020010096
Imre Deak17290502016-02-12 18:55:11 +020010097out:
10098 for_each_power_domain(power_domain, power_domain_mask)
10099 intel_display_power_put(dev_priv, power_domain);
10100
Jani Nikulacf304292016-03-18 17:05:41 +020010101 return active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010102}
10103
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010104static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
10105 const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +010010106{
10107 struct drm_device *dev = crtc->dev;
10108 struct drm_i915_private *dev_priv = dev->dev_private;
10109 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +030010110 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +010010111
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010112 if (plane_state && plane_state->visible) {
10113 unsigned int width = plane_state->base.crtc_w;
10114 unsigned int height = plane_state->base.crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +030010115 unsigned int stride = roundup_pow_of_two(width) * 4;
10116
10117 switch (stride) {
10118 default:
10119 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
10120 width, stride);
10121 stride = 256;
10122 /* fallthrough */
10123 case 256:
10124 case 512:
10125 case 1024:
10126 case 2048:
10127 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010128 }
10129
Ville Syrjälädc41c152014-08-13 11:57:05 +030010130 cntl |= CURSOR_ENABLE |
10131 CURSOR_GAMMA_ENABLE |
10132 CURSOR_FORMAT_ARGB |
10133 CURSOR_STRIDE(stride);
10134
10135 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010136 }
Chris Wilson560b85b2010-08-07 11:01:38 +010010137
Ville Syrjälädc41c152014-08-13 11:57:05 +030010138 if (intel_crtc->cursor_cntl != 0 &&
10139 (intel_crtc->cursor_base != base ||
10140 intel_crtc->cursor_size != size ||
10141 intel_crtc->cursor_cntl != cntl)) {
10142 /* On these chipsets we can only modify the base/size/stride
10143 * whilst the cursor is disabled.
10144 */
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010145 I915_WRITE(CURCNTR(PIPE_A), 0);
10146 POSTING_READ(CURCNTR(PIPE_A));
Ville Syrjälädc41c152014-08-13 11:57:05 +030010147 intel_crtc->cursor_cntl = 0;
10148 }
10149
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010150 if (intel_crtc->cursor_base != base) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010151 I915_WRITE(CURBASE(PIPE_A), base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010152 intel_crtc->cursor_base = base;
10153 }
Ville Syrjälädc41c152014-08-13 11:57:05 +030010154
10155 if (intel_crtc->cursor_size != size) {
10156 I915_WRITE(CURSIZE, size);
10157 intel_crtc->cursor_size = size;
10158 }
10159
Chris Wilson4b0e3332014-05-30 16:35:26 +030010160 if (intel_crtc->cursor_cntl != cntl) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010161 I915_WRITE(CURCNTR(PIPE_A), cntl);
10162 POSTING_READ(CURCNTR(PIPE_A));
Chris Wilson4b0e3332014-05-30 16:35:26 +030010163 intel_crtc->cursor_cntl = cntl;
10164 }
Chris Wilson560b85b2010-08-07 11:01:38 +010010165}
10166
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010167static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
10168 const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +010010169{
10170 struct drm_device *dev = crtc->dev;
10171 struct drm_i915_private *dev_priv = dev->dev_private;
10172 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10173 int pipe = intel_crtc->pipe;
Ville Syrjälä663f3122015-12-14 13:16:48 +020010174 uint32_t cntl = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +010010175
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010176 if (plane_state && plane_state->visible) {
Chris Wilson4b0e3332014-05-30 16:35:26 +030010177 cntl = MCURSOR_GAMMA_ENABLE;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010178 switch (plane_state->base.crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010179 case 64:
10180 cntl |= CURSOR_MODE_64_ARGB_AX;
10181 break;
10182 case 128:
10183 cntl |= CURSOR_MODE_128_ARGB_AX;
10184 break;
10185 case 256:
10186 cntl |= CURSOR_MODE_256_ARGB_AX;
10187 break;
10188 default:
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010189 MISSING_CASE(plane_state->base.crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010190 return;
Chris Wilson560b85b2010-08-07 11:01:38 +010010191 }
Chris Wilson4b0e3332014-05-30 16:35:26 +030010192 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010193
Bob Paauwefc6f93b2015-08-31 14:03:30 -070010194 if (HAS_DDI(dev))
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010195 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010196
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010197 if (plane_state->base.rotation == BIT(DRM_ROTATE_180))
10198 cntl |= CURSOR_ROTATE_180;
10199 }
Ville Syrjälä4398ad42014-10-23 07:41:34 -070010200
Chris Wilson4b0e3332014-05-30 16:35:26 +030010201 if (intel_crtc->cursor_cntl != cntl) {
10202 I915_WRITE(CURCNTR(pipe), cntl);
10203 POSTING_READ(CURCNTR(pipe));
10204 intel_crtc->cursor_cntl = cntl;
10205 }
10206
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010207 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010208 I915_WRITE(CURBASE(pipe), base);
10209 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010210
10211 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010212}
10213
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010214/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +010010215static void intel_crtc_update_cursor(struct drm_crtc *crtc,
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010216 const struct intel_plane_state *plane_state)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010217{
10218 struct drm_device *dev = crtc->dev;
10219 struct drm_i915_private *dev_priv = dev->dev_private;
10220 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10221 int pipe = intel_crtc->pipe;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010222 u32 base = intel_crtc->cursor_addr;
10223 u32 pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010224
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010225 if (plane_state) {
10226 int x = plane_state->base.crtc_x;
10227 int y = plane_state->base.crtc_y;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010228
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010229 if (x < 0) {
10230 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10231 x = -x;
10232 }
10233 pos |= x << CURSOR_X_SHIFT;
10234
10235 if (y < 0) {
10236 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10237 y = -y;
10238 }
10239 pos |= y << CURSOR_Y_SHIFT;
10240
10241 /* ILK+ do this automagically */
10242 if (HAS_GMCH_DISPLAY(dev) &&
10243 plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
10244 base += (plane_state->base.crtc_h *
10245 plane_state->base.crtc_w - 1) * 4;
10246 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010247 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010248
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010249 I915_WRITE(CURPOS(pipe), pos);
10250
Ville Syrjälä8ac54662014-08-12 19:39:54 +030010251 if (IS_845G(dev) || IS_I865G(dev))
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010252 i845_update_cursor(crtc, base, plane_state);
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010253 else
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010254 i9xx_update_cursor(crtc, base, plane_state);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010255}
10256
Ville Syrjälädc41c152014-08-13 11:57:05 +030010257static bool cursor_size_ok(struct drm_device *dev,
10258 uint32_t width, uint32_t height)
10259{
10260 if (width == 0 || height == 0)
10261 return false;
10262
10263 /*
10264 * 845g/865g are special in that they are only limited by
10265 * the width of their cursors, the height is arbitrary up to
10266 * the precision of the register. Everything else requires
10267 * square cursors, limited to a few power-of-two sizes.
10268 */
10269 if (IS_845G(dev) || IS_I865G(dev)) {
10270 if ((width & 63) != 0)
10271 return false;
10272
10273 if (width > (IS_845G(dev) ? 64 : 512))
10274 return false;
10275
10276 if (height > 1023)
10277 return false;
10278 } else {
10279 switch (width | height) {
10280 case 256:
10281 case 128:
10282 if (IS_GEN2(dev))
10283 return false;
10284 case 64:
10285 break;
10286 default:
10287 return false;
10288 }
10289 }
10290
10291 return true;
10292}
10293
Jesse Barnes79e53942008-11-07 14:24:08 -080010294/* VESA 640x480x72Hz mode to set on the pipe */
10295static struct drm_display_mode load_detect_mode = {
10296 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10297 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10298};
10299
Daniel Vettera8bb6812014-02-10 18:00:39 +010010300struct drm_framebuffer *
10301__intel_framebuffer_create(struct drm_device *dev,
10302 struct drm_mode_fb_cmd2 *mode_cmd,
10303 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +010010304{
10305 struct intel_framebuffer *intel_fb;
10306 int ret;
10307
10308 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010309 if (!intel_fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010310 return ERR_PTR(-ENOMEM);
Chris Wilsond2dff872011-04-19 08:36:26 +010010311
10312 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010313 if (ret)
10314 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +010010315
10316 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010317
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010318err:
10319 kfree(intel_fb);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010320 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +010010321}
10322
Daniel Vetterb5ea6422014-03-02 21:18:00 +010010323static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +010010324intel_framebuffer_create(struct drm_device *dev,
10325 struct drm_mode_fb_cmd2 *mode_cmd,
10326 struct drm_i915_gem_object *obj)
10327{
10328 struct drm_framebuffer *fb;
10329 int ret;
10330
10331 ret = i915_mutex_lock_interruptible(dev);
10332 if (ret)
10333 return ERR_PTR(ret);
10334 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10335 mutex_unlock(&dev->struct_mutex);
10336
10337 return fb;
10338}
10339
Chris Wilsond2dff872011-04-19 08:36:26 +010010340static u32
10341intel_framebuffer_pitch_for_width(int width, int bpp)
10342{
10343 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10344 return ALIGN(pitch, 64);
10345}
10346
10347static u32
10348intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10349{
10350 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +020010351 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +010010352}
10353
10354static struct drm_framebuffer *
10355intel_framebuffer_create_for_mode(struct drm_device *dev,
10356 struct drm_display_mode *mode,
10357 int depth, int bpp)
10358{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010359 struct drm_framebuffer *fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010360 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +000010361 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +010010362
Dave Gordond37cd8a2016-04-22 19:14:32 +010010363 obj = i915_gem_object_create(dev,
Chris Wilsond2dff872011-04-19 08:36:26 +010010364 intel_framebuffer_size_for_mode(mode, bpp));
Chris Wilsonfe3db792016-04-25 13:32:13 +010010365 if (IS_ERR(obj))
10366 return ERR_CAST(obj);
Chris Wilsond2dff872011-04-19 08:36:26 +010010367
10368 mode_cmd.width = mode->hdisplay;
10369 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010370 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10371 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +000010372 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +010010373
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010374 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
10375 if (IS_ERR(fb))
10376 drm_gem_object_unreference_unlocked(&obj->base);
10377
10378 return fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010379}
10380
10381static struct drm_framebuffer *
10382mode_fits_in_fbdev(struct drm_device *dev,
10383 struct drm_display_mode *mode)
10384{
Daniel Vetter06957262015-08-10 13:34:08 +020010385#ifdef CONFIG_DRM_FBDEV_EMULATION
Chris Wilsond2dff872011-04-19 08:36:26 +010010386 struct drm_i915_private *dev_priv = dev->dev_private;
10387 struct drm_i915_gem_object *obj;
10388 struct drm_framebuffer *fb;
10389
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010390 if (!dev_priv->fbdev)
10391 return NULL;
10392
10393 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010394 return NULL;
10395
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010396 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010397 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +010010398
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010399 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010400 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10401 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +010010402 return NULL;
10403
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010404 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +010010405 return NULL;
10406
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010407 drm_framebuffer_reference(fb);
Chris Wilsond2dff872011-04-19 08:36:26 +010010408 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +020010409#else
10410 return NULL;
10411#endif
Chris Wilsond2dff872011-04-19 08:36:26 +010010412}
10413
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010414static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10415 struct drm_crtc *crtc,
10416 struct drm_display_mode *mode,
10417 struct drm_framebuffer *fb,
10418 int x, int y)
10419{
10420 struct drm_plane_state *plane_state;
10421 int hdisplay, vdisplay;
10422 int ret;
10423
10424 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10425 if (IS_ERR(plane_state))
10426 return PTR_ERR(plane_state);
10427
10428 if (mode)
10429 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10430 else
10431 hdisplay = vdisplay = 0;
10432
10433 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10434 if (ret)
10435 return ret;
10436 drm_atomic_set_fb_for_plane(plane_state, fb);
10437 plane_state->crtc_x = 0;
10438 plane_state->crtc_y = 0;
10439 plane_state->crtc_w = hdisplay;
10440 plane_state->crtc_h = vdisplay;
10441 plane_state->src_x = x << 16;
10442 plane_state->src_y = y << 16;
10443 plane_state->src_w = hdisplay << 16;
10444 plane_state->src_h = vdisplay << 16;
10445
10446 return 0;
10447}
10448
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010449bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +010010450 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -050010451 struct intel_load_detect_pipe *old,
10452 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010453{
10454 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010455 struct intel_encoder *intel_encoder =
10456 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -080010457 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +010010458 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -080010459 struct drm_crtc *crtc = NULL;
10460 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +020010461 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -050010462 struct drm_mode_config *config = &dev->mode_config;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010463 struct drm_atomic_state *state = NULL, *restore_state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010464 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010465 struct intel_crtc_state *crtc_state;
Rob Clark51fd3712013-11-19 12:10:12 -050010466 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010467
Chris Wilsond2dff872011-04-19 08:36:26 +010010468 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010469 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010470 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010471
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010472 old->restore_state = NULL;
10473
Rob Clark51fd3712013-11-19 12:10:12 -050010474retry:
10475 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10476 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010477 goto fail;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020010478
Jesse Barnes79e53942008-11-07 14:24:08 -080010479 /*
10480 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +010010481 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010482 * - if the connector already has an assigned crtc, use it (but make
10483 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +010010484 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010485 * - try to find the first unused crtc that can drive this connector,
10486 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -080010487 */
10488
10489 /* See if we already have a CRTC for this connector */
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010490 if (connector->state->crtc) {
10491 crtc = connector->state->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +010010492
Rob Clark51fd3712013-11-19 12:10:12 -050010493 ret = drm_modeset_lock(&crtc->mutex, ctx);
10494 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010495 goto fail;
Chris Wilson8261b192011-04-19 23:18:09 +010010496
10497 /* Make sure the crtc and connector are running */
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010498 goto found;
Jesse Barnes79e53942008-11-07 14:24:08 -080010499 }
10500
10501 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010502 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010503 i++;
10504 if (!(encoder->possible_crtcs & (1 << i)))
10505 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010506
10507 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
10508 if (ret)
10509 goto fail;
10510
10511 if (possible_crtc->state->enable) {
10512 drm_modeset_unlock(&possible_crtc->mutex);
Ville Syrjäläa4592492014-08-11 13:15:36 +030010513 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010514 }
Ville Syrjäläa4592492014-08-11 13:15:36 +030010515
10516 crtc = possible_crtc;
10517 break;
Jesse Barnes79e53942008-11-07 14:24:08 -080010518 }
10519
10520 /*
10521 * If we didn't find an unused CRTC, don't use any.
10522 */
10523 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +010010524 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010525 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010526 }
10527
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010528found:
10529 intel_crtc = to_intel_crtc(crtc);
10530
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010531 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10532 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010533 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010534
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010535 state = drm_atomic_state_alloc(dev);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010536 restore_state = drm_atomic_state_alloc(dev);
10537 if (!state || !restore_state) {
10538 ret = -ENOMEM;
10539 goto fail;
10540 }
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010541
10542 state->acquire_ctx = ctx;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010543 restore_state->acquire_ctx = ctx;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010544
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010545 connector_state = drm_atomic_get_connector_state(state, connector);
10546 if (IS_ERR(connector_state)) {
10547 ret = PTR_ERR(connector_state);
10548 goto fail;
10549 }
10550
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010551 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
10552 if (ret)
10553 goto fail;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010554
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010555 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10556 if (IS_ERR(crtc_state)) {
10557 ret = PTR_ERR(crtc_state);
10558 goto fail;
10559 }
10560
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010561 crtc_state->base.active = crtc_state->base.enable = true;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010562
Chris Wilson64927112011-04-20 07:25:26 +010010563 if (!mode)
10564 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -080010565
Chris Wilsond2dff872011-04-19 08:36:26 +010010566 /* We need a framebuffer large enough to accommodate all accesses
10567 * that the plane may generate whilst we perform load detection.
10568 * We can not rely on the fbcon either being present (we get called
10569 * during its initialisation to detect all boot displays, or it may
10570 * not even exist) or that it is large enough to satisfy the
10571 * requested mode.
10572 */
Daniel Vetter94352cf2012-07-05 22:51:56 +020010573 fb = mode_fits_in_fbdev(dev, mode);
10574 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010575 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010576 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
Chris Wilsond2dff872011-04-19 08:36:26 +010010577 } else
10578 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010579 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010580 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010581 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010582 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010583
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010584 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10585 if (ret)
10586 goto fail;
10587
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010588 drm_framebuffer_unreference(fb);
10589
10590 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
10591 if (ret)
10592 goto fail;
10593
10594 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
10595 if (!ret)
10596 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
10597 if (!ret)
10598 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
10599 if (ret) {
10600 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
10601 goto fail;
10602 }
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030010603
Maarten Lankhorst3ba86072016-02-29 09:18:57 +010010604 ret = drm_atomic_commit(state);
10605 if (ret) {
Chris Wilson64927112011-04-20 07:25:26 +010010606 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010607 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010608 }
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010609
10610 old->restore_state = restore_state;
Chris Wilson71731882011-04-19 23:10:58 +010010611
Jesse Barnes79e53942008-11-07 14:24:08 -080010612 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010613 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +010010614 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010615
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010616fail:
Ander Conselvan de Oliveirae5d958e2015-04-21 17:12:57 +030010617 drm_atomic_state_free(state);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010618 drm_atomic_state_free(restore_state);
10619 restore_state = state = NULL;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010620
Rob Clark51fd3712013-11-19 12:10:12 -050010621 if (ret == -EDEADLK) {
10622 drm_modeset_backoff(ctx);
10623 goto retry;
10624 }
10625
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010626 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -080010627}
10628
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010629void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020010630 struct intel_load_detect_pipe *old,
10631 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010632{
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010633 struct intel_encoder *intel_encoder =
10634 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +010010635 struct drm_encoder *encoder = &intel_encoder->base;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010636 struct drm_atomic_state *state = old->restore_state;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010637 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080010638
Chris Wilsond2dff872011-04-19 08:36:26 +010010639 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010640 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010641 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010642
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010643 if (!state)
Chris Wilson0622a532011-04-21 09:32:11 +010010644 return;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010645
10646 ret = drm_atomic_commit(state);
10647 if (ret) {
10648 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
10649 drm_atomic_state_free(state);
Jesse Barnes79e53942008-11-07 14:24:08 -080010650 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010651}
10652
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010653static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010654 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010655{
10656 struct drm_i915_private *dev_priv = dev->dev_private;
10657 u32 dpll = pipe_config->dpll_hw_state.dpll;
10658
10659 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +020010660 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010661 else if (HAS_PCH_SPLIT(dev))
10662 return 120000;
10663 else if (!IS_GEN2(dev))
10664 return 96000;
10665 else
10666 return 48000;
10667}
10668
Jesse Barnes79e53942008-11-07 14:24:08 -080010669/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010670static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010671 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -080010672{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010673 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080010674 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010675 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010676 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -080010677 u32 fp;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +030010678 struct dpll clock;
Imre Deakdccbea32015-06-22 23:35:51 +030010679 int port_clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010680 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -080010681
10682 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +030010683 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010684 else
Ville Syrjälä293623f2013-09-13 16:18:46 +030010685 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010686
10687 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010688 if (IS_PINEVIEW(dev)) {
10689 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10690 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +080010691 } else {
10692 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10693 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10694 }
10695
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010696 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010697 if (IS_PINEVIEW(dev))
10698 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10699 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +080010700 else
10701 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -080010702 DPLL_FPA01_P1_POST_DIV_SHIFT);
10703
10704 switch (dpll & DPLL_MODE_MASK) {
10705 case DPLLB_MODE_DAC_SERIAL:
10706 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10707 5 : 10;
10708 break;
10709 case DPLLB_MODE_LVDS:
10710 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10711 7 : 14;
10712 break;
10713 default:
Zhao Yakui28c97732009-10-09 11:39:41 +080010714 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -080010715 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010716 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010717 }
10718
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010719 if (IS_PINEVIEW(dev))
Imre Deakdccbea32015-06-22 23:35:51 +030010720 port_clock = pnv_calc_dpll_params(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010721 else
Imre Deakdccbea32015-06-22 23:35:51 +030010722 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010723 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +020010724 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010725 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -080010726
10727 if (is_lvds) {
10728 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10729 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010730
10731 if (lvds & LVDS_CLKB_POWER_UP)
10732 clock.p2 = 7;
10733 else
10734 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -080010735 } else {
10736 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10737 clock.p1 = 2;
10738 else {
10739 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10740 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10741 }
10742 if (dpll & PLL_P2_DIVIDE_BY_4)
10743 clock.p2 = 4;
10744 else
10745 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -080010746 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010747
Imre Deakdccbea32015-06-22 23:35:51 +030010748 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010749 }
10750
Ville Syrjälä18442d02013-09-13 16:00:08 +030010751 /*
10752 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +010010753 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +030010754 * encoder's get_config() function.
10755 */
Imre Deakdccbea32015-06-22 23:35:51 +030010756 pipe_config->port_clock = port_clock;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010757}
10758
Ville Syrjälä6878da02013-09-13 15:59:11 +030010759int intel_dotclock_calculate(int link_freq,
10760 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010761{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010762 /*
10763 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010764 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010765 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010766 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010767 *
10768 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010769 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -080010770 */
10771
Ville Syrjälä6878da02013-09-13 15:59:11 +030010772 if (!m_n->link_n)
10773 return 0;
10774
10775 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10776}
10777
Ville Syrjälä18442d02013-09-13 16:00:08 +030010778static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010779 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +030010780{
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020010781 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä18442d02013-09-13 16:00:08 +030010782
10783 /* read out port_clock from the DPLL */
10784 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +030010785
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010786 /*
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020010787 * In case there is an active pipe without active ports,
10788 * we may need some idea for the dotclock anyway.
10789 * Calculate one based on the FDI configuration.
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010790 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010791 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä21a727b2016-02-17 21:41:10 +020010792 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjälä18442d02013-09-13 16:00:08 +030010793 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -080010794}
10795
10796/** Returns the currently programmed mode of the given pipe. */
10797struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10798 struct drm_crtc *crtc)
10799{
Jesse Barnes548f2452011-02-17 10:40:53 -080010800 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080010801 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010802 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010803 struct drm_display_mode *mode;
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010804 struct intel_crtc_state *pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -020010805 int htot = I915_READ(HTOTAL(cpu_transcoder));
10806 int hsync = I915_READ(HSYNC(cpu_transcoder));
10807 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10808 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +030010809 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -080010810
10811 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10812 if (!mode)
10813 return NULL;
10814
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010815 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
10816 if (!pipe_config) {
10817 kfree(mode);
10818 return NULL;
10819 }
10820
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010821 /*
10822 * Construct a pipe_config sufficient for getting the clock info
10823 * back out of crtc_clock_get.
10824 *
10825 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10826 * to use a real value here instead.
10827 */
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010828 pipe_config->cpu_transcoder = (enum transcoder) pipe;
10829 pipe_config->pixel_multiplier = 1;
10830 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10831 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10832 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
10833 i9xx_crtc_clock_get(intel_crtc, pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010834
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010835 mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -080010836 mode->hdisplay = (htot & 0xffff) + 1;
10837 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10838 mode->hsync_start = (hsync & 0xffff) + 1;
10839 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10840 mode->vdisplay = (vtot & 0xffff) + 1;
10841 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10842 mode->vsync_start = (vsync & 0xffff) + 1;
10843 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10844
10845 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -080010846
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010847 kfree(pipe_config);
10848
Jesse Barnes79e53942008-11-07 14:24:08 -080010849 return mode;
10850}
10851
Tvrtko Ursulin7d993732016-04-28 12:57:00 +010010852void intel_mark_busy(struct drm_i915_private *dev_priv)
Jesse Barnes652c3932009-08-17 13:31:43 -070010853{
Chris Wilsonf62a0072014-02-21 17:55:39 +000010854 if (dev_priv->mm.busy)
10855 return;
10856
Paulo Zanoni43694d62014-03-07 20:08:08 -030010857 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -030010858 i915_update_gfx_val(dev_priv);
Tvrtko Ursulin7d993732016-04-28 12:57:00 +010010859 if (INTEL_GEN(dev_priv) >= 6)
Chris Wilson43cf3bf2015-03-18 09:48:22 +000010860 gen6_rps_busy(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +000010861 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +010010862}
10863
Tvrtko Ursulin7d993732016-04-28 12:57:00 +010010864void intel_mark_idle(struct drm_i915_private *dev_priv)
Chris Wilsonf047e392012-07-21 12:31:41 +010010865{
Chris Wilsonf62a0072014-02-21 17:55:39 +000010866 if (!dev_priv->mm.busy)
10867 return;
10868
10869 dev_priv->mm.busy = false;
10870
Tvrtko Ursulin7d993732016-04-28 12:57:00 +010010871 if (INTEL_GEN(dev_priv) >= 6)
10872 gen6_rps_idle(dev_priv);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -030010873
Paulo Zanoni43694d62014-03-07 20:08:08 -030010874 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +010010875}
10876
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020010877void intel_free_flip_work(struct intel_flip_work *work)
Maarten Lankhorst03f476e2016-05-17 15:08:00 +020010878{
10879 kfree(work->old_connector_state);
10880 kfree(work->new_connector_state);
10881 kfree(work);
10882}
10883
Jesse Barnes79e53942008-11-07 14:24:08 -080010884static void intel_crtc_destroy(struct drm_crtc *crtc)
10885{
10886 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010887 struct drm_device *dev = crtc->dev;
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020010888 struct intel_flip_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +020010889
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010890 spin_lock_irq(&dev->event_lock);
Maarten Lankhorst68858432016-05-17 15:07:52 +020010891 while (!list_empty(&intel_crtc->flip_work)) {
10892 work = list_first_entry(&intel_crtc->flip_work,
10893 struct intel_flip_work, head);
10894 list_del_init(&work->head);
10895 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010896
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020010897 cancel_work_sync(&work->mmio_work);
10898 cancel_work_sync(&work->unpin_work);
Maarten Lankhorst03f476e2016-05-17 15:08:00 +020010899 intel_free_flip_work(work);
Maarten Lankhorst68858432016-05-17 15:07:52 +020010900
10901 spin_lock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010902 }
Maarten Lankhorst68858432016-05-17 15:07:52 +020010903 spin_unlock_irq(&dev->event_lock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010904
10905 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010906
Jesse Barnes79e53942008-11-07 14:24:08 -080010907 kfree(intel_crtc);
10908}
10909
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020010910static void intel_crtc_post_flip_update(struct intel_flip_work *work,
10911 struct drm_crtc *crtc)
10912{
10913 struct intel_crtc_state *crtc_state = work->new_crtc_state;
10914 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10915
10916 if (crtc_state->disable_cxsr)
10917 intel_crtc->wm.cxsr_allowed = true;
10918
10919 if (crtc_state->update_wm_post && crtc_state->base.active)
10920 intel_update_watermarks(crtc);
10921
10922 if (work->num_planes > 0 &&
10923 work->old_plane_state[0]->base.plane == crtc->primary) {
10924 struct intel_plane_state *plane_state =
10925 work->new_plane_state[0];
10926
10927 if (plane_state->visible &&
10928 (needs_modeset(&crtc_state->base) ||
10929 !work->old_plane_state[0]->visible))
10930 intel_post_enable_primary(crtc);
10931 }
10932}
10933
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010934static void intel_unpin_work_fn(struct work_struct *__work)
10935{
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020010936 struct intel_flip_work *work =
10937 container_of(__work, struct intel_flip_work, unpin_work);
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020010938 struct drm_crtc *crtc = work->old_crtc_state->base.crtc;
10939 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10940 struct drm_device *dev = crtc->dev;
10941 struct drm_i915_private *dev_priv = dev->dev_private;
10942 int i;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010943
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020010944 if (work->fb_bits)
10945 intel_frontbuffer_flip_complete(dev, work->fb_bits);
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020010946
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020010947 /*
10948 * Unless work->can_async_unpin is false, there's no way to ensure
10949 * that work->new_crtc_state contains valid memory during unpin
10950 * because intel_atomic_commit may free it before this runs.
10951 */
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020010952 if (!work->can_async_unpin) {
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020010953 intel_crtc_post_flip_update(work, crtc);
10954
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020010955 if (dev_priv->display.optimize_watermarks)
10956 dev_priv->display.optimize_watermarks(work->new_crtc_state);
10957 }
10958
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020010959 if (work->fb_bits & to_intel_plane(crtc->primary)->frontbuffer_bit)
10960 intel_fbc_post_update(intel_crtc);
10961
10962 if (work->put_power_domains)
10963 modeset_put_power_domains(dev_priv, work->put_power_domains);
10964
10965 /* Make sure mmio work is completely finished before freeing all state here. */
10966 flush_work(&work->mmio_work);
10967
Maarten Lankhorst03f476e2016-05-17 15:08:00 +020010968 if (!work->can_async_unpin &&
10969 (work->new_crtc_state->update_pipe ||
10970 needs_modeset(&work->new_crtc_state->base))) {
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020010971 /* This must be called before work is unpinned for serialization. */
10972 intel_modeset_verify_crtc(crtc, &work->old_crtc_state->base,
10973 &work->new_crtc_state->base);
10974
Maarten Lankhorst03f476e2016-05-17 15:08:00 +020010975 for (i = 0; i < work->num_new_connectors; i++) {
10976 struct drm_connector_state *conn_state =
10977 work->new_connector_state[i];
10978 struct drm_connector *con = conn_state->connector;
10979
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020010980 WARN_ON(!con);
10981
Maarten Lankhorst03f476e2016-05-17 15:08:00 +020010982 intel_connector_verify_state(to_intel_connector(con),
10983 conn_state);
10984 }
10985 }
10986
10987 for (i = 0; i < work->num_old_connectors; i++) {
10988 struct drm_connector_state *old_con_state =
10989 work->old_connector_state[i];
10990 struct drm_connector *con =
10991 old_con_state->connector;
10992
10993 con->funcs->atomic_destroy_state(con, old_con_state);
10994 }
10995
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020010996 if (!work->can_async_unpin || !list_empty(&work->head)) {
10997 spin_lock_irq(&dev->event_lock);
10998 WARN(list_empty(&work->head) != work->can_async_unpin,
10999 "[CRTC:%i] Pin work %p async %i with %i planes, active %i -> %i ms %i\n",
11000 crtc->base.id, work, work->can_async_unpin, work->num_planes,
11001 work->old_crtc_state->base.active, work->new_crtc_state->base.active,
11002 needs_modeset(&work->new_crtc_state->base));
11003
11004 if (!list_empty(&work->head))
11005 list_del(&work->head);
11006
11007 wake_up_all(&dev_priv->pending_flip_queue);
11008 spin_unlock_irq(&dev->event_lock);
11009 }
11010
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020011011 /* New crtc_state freed? */
11012 if (work->free_new_crtc_state)
11013 intel_crtc_destroy_state(crtc, &work->new_crtc_state->base);
11014
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011015 intel_crtc_destroy_state(crtc, &work->old_crtc_state->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +000011016
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011017 for (i = 0; i < work->num_planes; i++) {
11018 struct intel_plane_state *old_plane_state =
11019 work->old_plane_state[i];
11020 struct drm_framebuffer *old_fb = old_plane_state->base.fb;
11021 struct drm_plane *plane = old_plane_state->base.plane;
11022 struct drm_i915_gem_request *req;
Daniel Vetterf99d7062014-06-19 16:01:59 +020011023
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011024 req = old_plane_state->wait_req;
11025 old_plane_state->wait_req = NULL;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020011026 if (req)
11027 i915_gem_request_unreference(req);
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011028
11029 fence_put(old_plane_state->base.fence);
11030 old_plane_state->base.fence = NULL;
11031
11032 if (old_fb &&
11033 (plane->type != DRM_PLANE_TYPE_CURSOR ||
11034 !INTEL_INFO(dev_priv)->cursor_needs_physical)) {
11035 mutex_lock(&dev->struct_mutex);
11036 intel_unpin_fb_obj(old_fb, old_plane_state->base.rotation);
11037 mutex_unlock(&dev->struct_mutex);
11038 }
11039
11040 intel_plane_destroy_state(plane, &old_plane_state->base);
11041 }
11042
11043 if (!WARN_ON(atomic_read(&intel_crtc->unpin_work_count) == 0))
11044 atomic_dec(&intel_crtc->unpin_work_count);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011045
Maarten Lankhorst03f476e2016-05-17 15:08:00 +020011046 intel_free_flip_work(work);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011047}
11048
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011049
11050static bool pageflip_finished(struct intel_crtc *crtc,
11051 struct intel_flip_work *work)
11052{
11053 if (!atomic_read(&work->pending))
11054 return false;
11055
11056 smp_rmb();
11057
Daniel Vetterf3260382014-09-15 14:55:23 +020011058 /*
Maarten Lankhorst8dd634d2016-05-17 15:07:55 +020011059 * MMIO work completes when vblank is different from
11060 * flip_queued_vblank.
Chris Wilsone7d841c2012-12-03 11:36:30 +000011061 */
Maarten Lankhorst8dd634d2016-05-17 15:07:55 +020011062 return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011063}
11064
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011065void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
Chris Wilsone7d841c2012-12-03 11:36:30 +000011066{
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011067 struct drm_device *dev = dev_priv->dev;
11068 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11069 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11070 struct intel_flip_work *work;
11071 unsigned long flags;
11072
11073 /* Ignore early vblank irqs */
11074 if (!crtc)
11075 return;
11076
11077 /*
11078 * This is called both by irq handlers and the reset code (to complete
11079 * lost pageflips) so needs the full irqsave spinlocks.
11080 */
11081 spin_lock_irqsave(&dev->event_lock, flags);
Maarten Lankhorst68858432016-05-17 15:07:52 +020011082 while (!list_empty(&intel_crtc->flip_work)) {
11083 work = list_first_entry(&intel_crtc->flip_work,
11084 struct intel_flip_work,
11085 head);
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011086
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011087 if (!pageflip_finished(intel_crtc, work) ||
11088 work_busy(&work->unpin_work))
Maarten Lankhorst68858432016-05-17 15:07:52 +020011089 break;
11090
11091 page_flip_completed(intel_crtc, work);
11092 }
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011093 spin_unlock_irqrestore(&dev->event_lock, flags);
11094}
11095
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011096static void intel_mmio_flip_work_func(struct work_struct *w)
Damien Lespiauff944562014-11-20 14:58:16 +000011097{
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011098 struct intel_flip_work *work =
11099 container_of(w, struct intel_flip_work, mmio_work);
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011100 struct drm_crtc *crtc = work->old_crtc_state->base.crtc;
11101 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11102 struct intel_crtc_state *crtc_state = work->new_crtc_state;
11103 struct drm_device *dev = crtc->dev;
Maarten Lankhorstaa420dd2016-05-17 15:07:51 +020011104 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011105 struct drm_i915_gem_request *req;
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020011106 int i, ret;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011107
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020011108 if (!needs_modeset(&crtc_state->base) && crtc_state->update_pipe) {
11109 work->put_power_domains =
11110 modeset_get_crtc_power_domains(crtc, crtc_state);
11111 }
11112
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011113 for (i = 0; i < work->num_planes; i++) {
11114 struct intel_plane_state *old_plane_state = work->old_plane_state[i];
11115
11116 /* For framebuffer backed by dmabuf, wait for fence */
11117 if (old_plane_state->base.fence)
11118 WARN_ON(fence_wait(old_plane_state->base.fence, false) < 0);
11119
11120 req = old_plane_state->wait_req;
11121 if (!req)
11122 continue;
11123
11124 WARN_ON(__i915_wait_request(req, false, NULL,
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011125 &dev_priv->rps.mmioflips));
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011126 }
Sourab Gupta84c33a62014-06-02 16:47:17 +053011127
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020011128 ret = drm_crtc_vblank_get(crtc);
11129 I915_STATE_WARN(ret < 0, "enabling vblank failed with %i\n", ret);
11130
11131 if (work->num_planes &&
11132 work->old_plane_state[0]->base.plane == crtc->primary)
11133 intel_fbc_enable(intel_crtc, work->new_crtc_state, work->new_plane_state[0]);
11134
11135 intel_frontbuffer_flip_prepare(dev, work->fb_bits);
Alex Goinsfd8e0582015-11-25 18:43:38 -080011136
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011137 intel_pipe_update_start(intel_crtc);
11138 if (!needs_modeset(&crtc_state->base)) {
11139 if (crtc_state->base.color_mgmt_changed || crtc_state->update_pipe) {
11140 intel_color_set_csc(&crtc_state->base);
11141 intel_color_load_luts(&crtc_state->base);
11142 }
Sourab Gupta84c33a62014-06-02 16:47:17 +053011143
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011144 if (crtc_state->update_pipe)
11145 intel_update_pipe_config(intel_crtc, work->old_crtc_state);
11146 else if (INTEL_INFO(dev)->gen >= 9)
11147 skl_detach_scalers(intel_crtc);
11148 }
11149
11150 for (i = 0; i < work->num_planes; i++) {
11151 struct intel_plane_state *new_plane_state = work->new_plane_state[i];
11152 struct intel_plane *plane = to_intel_plane(new_plane_state->base.plane);
11153
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020011154 if (new_plane_state->visible)
11155 plane->update_plane(&plane->base, crtc_state, new_plane_state);
11156 else
11157 plane->disable_plane(&plane->base, crtc);
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011158 }
11159
11160 intel_pipe_update_end(intel_crtc, work);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011161}
11162
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011163/**
11164 * intel_wm_need_update - Check whether watermarks need updating
11165 * @plane: drm plane
11166 * @state: new plane state
11167 *
11168 * Check current plane state versus the new one to determine whether
11169 * watermarks need to be recalculated.
11170 *
11171 * Returns true or false.
11172 */
11173static bool intel_wm_need_update(struct drm_plane *plane,
11174 struct drm_plane_state *state)
11175{
Matt Roperd21fbe82015-09-24 15:53:12 -070011176 struct intel_plane_state *new = to_intel_plane_state(state);
11177 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
11178
11179 /* Update watermarks on tiling or size changes. */
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010011180 if (new->visible != cur->visible)
11181 return true;
11182
11183 if (!cur->base.fb || !new->base.fb)
11184 return false;
11185
11186 if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
11187 cur->base.rotation != new->base.rotation ||
Matt Roperd21fbe82015-09-24 15:53:12 -070011188 drm_rect_width(&new->src) != drm_rect_width(&cur->src) ||
11189 drm_rect_height(&new->src) != drm_rect_height(&cur->src) ||
11190 drm_rect_width(&new->dst) != drm_rect_width(&cur->dst) ||
11191 drm_rect_height(&new->dst) != drm_rect_height(&cur->dst))
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011192 return true;
11193
11194 return false;
11195}
11196
Matt Roperd21fbe82015-09-24 15:53:12 -070011197static bool needs_scaling(struct intel_plane_state *state)
11198{
11199 int src_w = drm_rect_width(&state->src) >> 16;
11200 int src_h = drm_rect_height(&state->src) >> 16;
11201 int dst_w = drm_rect_width(&state->dst);
11202 int dst_h = drm_rect_height(&state->dst);
11203
11204 return (src_w != dst_w || src_h != dst_h);
11205}
11206
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011207int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
11208 struct drm_plane_state *plane_state)
11209{
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010011210 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011211 struct drm_crtc *crtc = crtc_state->crtc;
11212 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11213 struct drm_plane *plane = plane_state->plane;
11214 struct drm_device *dev = crtc->dev;
Matt Ropered4a6a72016-02-23 17:20:13 -080011215 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011216 struct intel_plane_state *old_plane_state =
11217 to_intel_plane_state(plane->state);
11218 int idx = intel_crtc->base.base.id, ret;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011219 bool mode_changed = needs_modeset(crtc_state);
11220 bool was_crtc_enabled = crtc->state->active;
11221 bool is_crtc_enabled = crtc_state->active;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011222 bool turn_off, turn_on, visible, was_visible;
11223 struct drm_framebuffer *fb = plane_state->fb;
11224
11225 if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
11226 plane->type != DRM_PLANE_TYPE_CURSOR) {
11227 ret = skl_update_scaler_plane(
11228 to_intel_crtc_state(crtc_state),
11229 to_intel_plane_state(plane_state));
11230 if (ret)
11231 return ret;
11232 }
11233
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011234 was_visible = old_plane_state->visible;
11235 visible = to_intel_plane_state(plane_state)->visible;
11236
11237 if (!was_crtc_enabled && WARN_ON(was_visible))
11238 was_visible = false;
11239
Maarten Lankhorst35c08f42015-12-03 14:31:07 +010011240 /*
11241 * Visibility is calculated as if the crtc was on, but
11242 * after scaler setup everything depends on it being off
11243 * when the crtc isn't active.
Ville Syrjäläf818ffe2016-04-29 17:31:18 +030011244 *
11245 * FIXME this is wrong for watermarks. Watermarks should also
11246 * be computed as if the pipe would be active. Perhaps move
11247 * per-plane wm computation to the .check_plane() hook, and
11248 * only combine the results from all planes in the current place?
Maarten Lankhorst35c08f42015-12-03 14:31:07 +010011249 */
11250 if (!is_crtc_enabled)
11251 to_intel_plane_state(plane_state)->visible = visible = false;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011252
11253 if (!was_visible && !visible)
11254 return 0;
11255
Maarten Lankhorste8861672016-02-24 11:24:26 +010011256 if (fb != old_plane_state->base.fb)
11257 pipe_config->fb_changed = true;
11258
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011259 turn_off = was_visible && (!visible || mode_changed);
11260 turn_on = visible && (!was_visible || mode_changed);
11261
11262 DRM_DEBUG_ATOMIC("[CRTC:%i] has [PLANE:%i] with fb %i\n", idx,
11263 plane->base.id, fb ? fb->base.id : -1);
11264
11265 DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
11266 plane->base.id, was_visible, visible,
11267 turn_off, turn_on, mode_changed);
11268
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011269 if (turn_on) {
11270 pipe_config->update_wm_pre = true;
11271
11272 /* must disable cxsr around plane enable/disable */
11273 if (plane->type != DRM_PLANE_TYPE_CURSOR)
11274 pipe_config->disable_cxsr = true;
11275 } else if (turn_off) {
11276 pipe_config->update_wm_post = true;
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010011277
Ville Syrjälä852eb002015-06-24 22:00:07 +030011278 /* must disable cxsr around plane enable/disable */
Maarten Lankhorste8861672016-02-24 11:24:26 +010011279 if (plane->type != DRM_PLANE_TYPE_CURSOR)
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010011280 pipe_config->disable_cxsr = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011281 } else if (intel_wm_need_update(plane, plane_state)) {
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011282 /* FIXME bollocks */
11283 pipe_config->update_wm_pre = true;
11284 pipe_config->update_wm_post = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011285 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011286
Matt Ropered4a6a72016-02-23 17:20:13 -080011287 /* Pre-gen9 platforms need two-step watermark updates */
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011288 if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
11289 INTEL_INFO(dev)->gen < 9 && dev_priv->display.optimize_watermarks)
Matt Ropered4a6a72016-02-23 17:20:13 -080011290 to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
11291
Rodrigo Vivi8be6ca82015-08-24 16:38:23 -070011292 if (visible || was_visible)
Maarten Lankhorstcd202f62016-03-09 10:35:44 +010011293 pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011294
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +010011295 /*
11296 * WaCxSRDisabledForSpriteScaling:ivb
11297 *
11298 * cstate->update_wm was already set above, so this flag will
11299 * take effect when we commit and program watermarks.
11300 */
11301 if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev) &&
11302 needs_scaling(to_intel_plane_state(plane_state)) &&
11303 !needs_scaling(old_plane_state))
11304 pipe_config->disable_lp_wm = true;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011305
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011306 return 0;
11307}
11308
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011309static bool encoders_cloneable(const struct intel_encoder *a,
11310 const struct intel_encoder *b)
11311{
11312 /* masks could be asymmetric, so check both ways */
11313 return a == b || (a->cloneable & (1 << b->type) &&
11314 b->cloneable & (1 << a->type));
11315}
11316
11317static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11318 struct intel_crtc *crtc,
11319 struct intel_encoder *encoder)
11320{
11321 struct intel_encoder *source_encoder;
11322 struct drm_connector *connector;
11323 struct drm_connector_state *connector_state;
11324 int i;
11325
11326 for_each_connector_in_state(state, connector, connector_state, i) {
11327 if (connector_state->crtc != &crtc->base)
11328 continue;
11329
11330 source_encoder =
11331 to_intel_encoder(connector_state->best_encoder);
11332 if (!encoders_cloneable(encoder, source_encoder))
11333 return false;
11334 }
11335
11336 return true;
11337}
11338
11339static bool check_encoder_cloning(struct drm_atomic_state *state,
11340 struct intel_crtc *crtc)
11341{
11342 struct intel_encoder *encoder;
11343 struct drm_connector *connector;
11344 struct drm_connector_state *connector_state;
11345 int i;
11346
11347 for_each_connector_in_state(state, connector, connector_state, i) {
11348 if (connector_state->crtc != &crtc->base)
11349 continue;
11350
11351 encoder = to_intel_encoder(connector_state->best_encoder);
11352 if (!check_single_encoder_cloning(state, crtc, encoder))
11353 return false;
11354 }
11355
11356 return true;
11357}
11358
11359static int intel_crtc_atomic_check(struct drm_crtc *crtc,
11360 struct drm_crtc_state *crtc_state)
11361{
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020011362 struct drm_device *dev = crtc->dev;
Maarten Lankhorstad421372015-06-15 12:33:42 +020011363 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011364 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020011365 struct intel_crtc_state *pipe_config =
11366 to_intel_crtc_state(crtc_state);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011367 struct drm_atomic_state *state = crtc_state->state;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020011368 int ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011369 bool mode_changed = needs_modeset(crtc_state);
11370
11371 if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
11372 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
11373 return -EINVAL;
11374 }
11375
Ville Syrjälä852eb002015-06-24 22:00:07 +030011376 if (mode_changed && !crtc_state->active)
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011377 pipe_config->update_wm_post = true;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020011378
Maarten Lankhorstad421372015-06-15 12:33:42 +020011379 if (mode_changed && crtc_state->enable &&
11380 dev_priv->display.crtc_compute_clock &&
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020011381 !WARN_ON(pipe_config->shared_dpll)) {
Maarten Lankhorstad421372015-06-15 12:33:42 +020011382 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
11383 pipe_config);
11384 if (ret)
11385 return ret;
11386 }
11387
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000011388 if (crtc_state->color_mgmt_changed) {
11389 ret = intel_color_check(crtc, crtc_state);
11390 if (ret)
11391 return ret;
11392 }
11393
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020011394 ret = 0;
Matt Roper86c8bbb2015-09-24 15:53:16 -070011395 if (dev_priv->display.compute_pipe_wm) {
Maarten Lankhorste3bddde2016-03-01 11:07:22 +010011396 ret = dev_priv->display.compute_pipe_wm(pipe_config);
Matt Ropered4a6a72016-02-23 17:20:13 -080011397 if (ret) {
11398 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
Matt Roper86c8bbb2015-09-24 15:53:16 -070011399 return ret;
Matt Ropered4a6a72016-02-23 17:20:13 -080011400 }
11401 }
11402
11403 if (dev_priv->display.compute_intermediate_wm &&
11404 !to_intel_atomic_state(state)->skip_intermediate_wm) {
11405 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
11406 return 0;
11407
11408 /*
11409 * Calculate 'intermediate' watermarks that satisfy both the
11410 * old state and the new state. We can program these
11411 * immediately.
11412 */
11413 ret = dev_priv->display.compute_intermediate_wm(crtc->dev,
11414 intel_crtc,
11415 pipe_config);
11416 if (ret) {
11417 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
11418 return ret;
11419 }
Ville Syrjäläe3d54572016-05-13 10:10:42 -070011420 } else if (dev_priv->display.compute_intermediate_wm) {
11421 if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
11422 pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
Matt Roper86c8bbb2015-09-24 15:53:16 -070011423 }
11424
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020011425 if (INTEL_INFO(dev)->gen >= 9) {
11426 if (mode_changed)
11427 ret = skl_update_scaler_crtc(pipe_config);
11428
11429 if (!ret)
11430 ret = intel_atomic_setup_scalers(dev, intel_crtc,
11431 pipe_config);
11432 }
11433
11434 return ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011435}
11436
Jani Nikula65b38e02015-04-13 11:26:56 +030011437static const struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011438 .mode_set_base_atomic = intel_pipe_set_base_atomic,
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011439 .atomic_check = intel_crtc_atomic_check,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011440};
11441
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020011442static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
11443{
11444 struct intel_connector *connector;
11445
11446 for_each_intel_connector(dev, connector) {
Daniel Vetter8863dc72016-05-06 15:39:03 +020011447 if (connector->base.state->crtc)
11448 drm_connector_unreference(&connector->base);
11449
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020011450 if (connector->base.encoder) {
11451 connector->base.state->best_encoder =
11452 connector->base.encoder;
11453 connector->base.state->crtc =
11454 connector->base.encoder->crtc;
Daniel Vetter8863dc72016-05-06 15:39:03 +020011455
11456 drm_connector_reference(&connector->base);
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020011457 } else {
11458 connector->base.state->best_encoder = NULL;
11459 connector->base.state->crtc = NULL;
11460 }
11461 }
11462}
11463
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011464static void
Robin Schroereba905b2014-05-18 02:24:50 +020011465connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011466 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011467{
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011468 int bpp = pipe_config->pipe_bpp;
11469
11470 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
11471 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030011472 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011473
11474 /* Don't use an invalid EDID bpc value */
11475 if (connector->base.display_info.bpc &&
11476 connector->base.display_info.bpc * 3 < bpp) {
11477 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
11478 bpp, connector->base.display_info.bpc*3);
11479 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
11480 }
11481
Jani Nikula013dd9e2016-01-13 16:35:20 +020011482 /* Clamp bpp to default limit on screens without EDID 1.4 */
11483 if (connector->base.display_info.bpc == 0) {
11484 int type = connector->base.connector_type;
11485 int clamp_bpp = 24;
11486
11487 /* Fall back to 18 bpp when DP sink capability is unknown. */
11488 if (type == DRM_MODE_CONNECTOR_DisplayPort ||
11489 type == DRM_MODE_CONNECTOR_eDP)
11490 clamp_bpp = 18;
11491
11492 if (bpp > clamp_bpp) {
11493 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of %d\n",
11494 bpp, clamp_bpp);
11495 pipe_config->pipe_bpp = clamp_bpp;
11496 }
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011497 }
11498}
11499
11500static int
11501compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011502 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011503{
11504 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011505 struct drm_atomic_state *state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011506 struct drm_connector *connector;
11507 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011508 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011509
Wayne Boyer666a4532015-12-09 12:29:35 -080011510 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011511 bpp = 10*3;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011512 else if (INTEL_INFO(dev)->gen >= 5)
11513 bpp = 12*3;
11514 else
11515 bpp = 8*3;
11516
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011517
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011518 pipe_config->pipe_bpp = bpp;
11519
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011520 state = pipe_config->base.state;
11521
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011522 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011523 for_each_connector_in_state(state, connector, connector_state, i) {
11524 if (connector_state->crtc != &crtc->base)
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011525 continue;
11526
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011527 connected_sink_compute_bpp(to_intel_connector(connector),
11528 pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011529 }
11530
11531 return bpp;
11532}
11533
Daniel Vetter644db712013-09-19 14:53:58 +020011534static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
11535{
11536 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
11537 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010011538 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020011539 mode->crtc_hdisplay, mode->crtc_hsync_start,
11540 mode->crtc_hsync_end, mode->crtc_htotal,
11541 mode->crtc_vdisplay, mode->crtc_vsync_start,
11542 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
11543}
11544
Daniel Vetterc0b03412013-05-28 12:05:54 +020011545static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011546 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020011547 const char *context)
11548{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011549 struct drm_device *dev = crtc->base.dev;
11550 struct drm_plane *plane;
11551 struct intel_plane *intel_plane;
11552 struct intel_plane_state *state;
11553 struct drm_framebuffer *fb;
11554
11555 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
11556 context, pipe_config, pipe_name(crtc->pipe));
Daniel Vetterc0b03412013-05-28 12:05:54 +020011557
Jani Nikulada205632016-03-15 21:51:10 +020011558 DRM_DEBUG_KMS("cpu_transcoder: %s\n", transcoder_name(pipe_config->cpu_transcoder));
Daniel Vetterc0b03412013-05-28 12:05:54 +020011559 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
11560 pipe_config->pipe_bpp, pipe_config->dither);
11561 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11562 pipe_config->has_pch_encoder,
11563 pipe_config->fdi_lanes,
11564 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
11565 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
11566 pipe_config->fdi_m_n.tu);
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030011567 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 +030011568 pipe_config->has_dp_encoder,
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030011569 pipe_config->lane_count,
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030011570 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
11571 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
11572 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011573
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030011574 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 -070011575 pipe_config->has_dp_encoder,
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030011576 pipe_config->lane_count,
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011577 pipe_config->dp_m2_n2.gmch_m,
11578 pipe_config->dp_m2_n2.gmch_n,
11579 pipe_config->dp_m2_n2.link_m,
11580 pipe_config->dp_m2_n2.link_n,
11581 pipe_config->dp_m2_n2.tu);
11582
Daniel Vetter55072d12014-11-20 16:10:28 +010011583 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
11584 pipe_config->has_audio,
11585 pipe_config->has_infoframe);
11586
Daniel Vetterc0b03412013-05-28 12:05:54 +020011587 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011588 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020011589 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011590 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
11591 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030011592 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030011593 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
11594 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Tvrtko Ursulin0ec463d2015-05-13 16:51:08 +010011595 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
11596 crtc->num_scalers,
11597 pipe_config->scaler_state.scaler_users,
11598 pipe_config->scaler_state.scaler_id);
Daniel Vetterc0b03412013-05-28 12:05:54 +020011599 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
11600 pipe_config->gmch_pfit.control,
11601 pipe_config->gmch_pfit.pgm_ratios,
11602 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010011603 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020011604 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010011605 pipe_config->pch_pfit.size,
11606 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030011607 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030011608 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011609
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010011610 if (IS_BROXTON(dev)) {
Imre Deak05712c12015-06-18 17:25:54 +030011611 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010011612 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
Imre Deakc8453332015-06-18 17:25:55 +030011613 "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010011614 pipe_config->ddi_pll_sel,
11615 pipe_config->dpll_hw_state.ebb0,
Imre Deak05712c12015-06-18 17:25:54 +030011616 pipe_config->dpll_hw_state.ebb4,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010011617 pipe_config->dpll_hw_state.pll0,
11618 pipe_config->dpll_hw_state.pll1,
11619 pipe_config->dpll_hw_state.pll2,
11620 pipe_config->dpll_hw_state.pll3,
11621 pipe_config->dpll_hw_state.pll6,
11622 pipe_config->dpll_hw_state.pll8,
Imre Deak05712c12015-06-18 17:25:54 +030011623 pipe_config->dpll_hw_state.pll9,
Imre Deakc8453332015-06-18 17:25:55 +030011624 pipe_config->dpll_hw_state.pll10,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010011625 pipe_config->dpll_hw_state.pcsdw12);
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070011626 } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010011627 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
11628 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
11629 pipe_config->ddi_pll_sel,
11630 pipe_config->dpll_hw_state.ctrl1,
11631 pipe_config->dpll_hw_state.cfgcr1,
11632 pipe_config->dpll_hw_state.cfgcr2);
11633 } else if (HAS_DDI(dev)) {
Ville Syrjälä1260f072016-02-17 21:41:08 +020011634 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 +010011635 pipe_config->ddi_pll_sel,
Maarten Lankhorst00490c22015-11-16 14:42:12 +010011636 pipe_config->dpll_hw_state.wrpll,
11637 pipe_config->dpll_hw_state.spll);
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010011638 } else {
11639 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
11640 "fp0: 0x%x, fp1: 0x%x\n",
11641 pipe_config->dpll_hw_state.dpll,
11642 pipe_config->dpll_hw_state.dpll_md,
11643 pipe_config->dpll_hw_state.fp0,
11644 pipe_config->dpll_hw_state.fp1);
11645 }
11646
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011647 DRM_DEBUG_KMS("planes on this crtc\n");
11648 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
11649 intel_plane = to_intel_plane(plane);
11650 if (intel_plane->pipe != crtc->pipe)
11651 continue;
11652
11653 state = to_intel_plane_state(plane->state);
11654 fb = state->base.fb;
11655 if (!fb) {
11656 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
11657 "disabled, scaler_id = %d\n",
11658 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
11659 plane->base.id, intel_plane->pipe,
11660 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
11661 drm_plane_index(plane), state->scaler_id);
11662 continue;
11663 }
11664
11665 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
11666 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
11667 plane->base.id, intel_plane->pipe,
11668 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
11669 drm_plane_index(plane));
11670 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
11671 fb->base.id, fb->width, fb->height, fb->pixel_format);
11672 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
11673 state->scaler_id,
11674 state->src.x1 >> 16, state->src.y1 >> 16,
11675 drm_rect_width(&state->src) >> 16,
11676 drm_rect_height(&state->src) >> 16,
11677 state->dst.x1, state->dst.y1,
11678 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
11679 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020011680}
11681
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011682static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011683{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011684 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011685 struct drm_connector *connector;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011686 unsigned int used_ports = 0;
11687
11688 /*
11689 * Walk the connector list instead of the encoder
11690 * list to detect the problem on ddi platforms
11691 * where there's just one encoder per digital port.
11692 */
Ville Syrjälä0bff4852015-12-10 18:22:31 +020011693 drm_for_each_connector(connector, dev) {
11694 struct drm_connector_state *connector_state;
11695 struct intel_encoder *encoder;
11696
11697 connector_state = drm_atomic_get_existing_connector_state(state, connector);
11698 if (!connector_state)
11699 connector_state = connector->state;
11700
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011701 if (!connector_state->best_encoder)
11702 continue;
11703
11704 encoder = to_intel_encoder(connector_state->best_encoder);
11705
11706 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011707
11708 switch (encoder->type) {
11709 unsigned int port_mask;
11710 case INTEL_OUTPUT_UNKNOWN:
11711 if (WARN_ON(!HAS_DDI(dev)))
11712 break;
11713 case INTEL_OUTPUT_DISPLAYPORT:
11714 case INTEL_OUTPUT_HDMI:
11715 case INTEL_OUTPUT_EDP:
11716 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
11717
11718 /* the same port mustn't appear more than once */
11719 if (used_ports & port_mask)
11720 return false;
11721
11722 used_ports |= port_mask;
11723 default:
11724 break;
11725 }
11726 }
11727
11728 return true;
11729}
11730
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011731static void
11732clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
11733{
11734 struct drm_crtc_state tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070011735 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030011736 struct intel_dpll_hw_state dpll_hw_state;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020011737 struct intel_shared_dpll *shared_dpll;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030011738 uint32_t ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020011739 bool force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011740
Ander Conselvan de Oliveira7546a382015-05-20 09:03:27 +030011741 /* FIXME: before the switch to atomic started, a new pipe_config was
11742 * kzalloc'd. Code that depends on any field being zero should be
11743 * fixed, so that the crtc_state can be safely duplicated. For now,
11744 * only fields that are know to not cause problems are preserved. */
11745
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011746 tmp_state = crtc_state->base;
Chandra Konduru663a3642015-04-07 15:28:41 -070011747 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030011748 shared_dpll = crtc_state->shared_dpll;
11749 dpll_hw_state = crtc_state->dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030011750 ddi_pll_sel = crtc_state->ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020011751 force_thru = crtc_state->pch_pfit.force_thru;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030011752
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011753 memset(crtc_state, 0, sizeof *crtc_state);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030011754
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011755 crtc_state->base = tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070011756 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030011757 crtc_state->shared_dpll = shared_dpll;
11758 crtc_state->dpll_hw_state = dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030011759 crtc_state->ddi_pll_sel = ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020011760 crtc_state->pch_pfit.force_thru = force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011761}
11762
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030011763static int
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011764intel_modeset_pipe_config(struct drm_crtc *crtc,
Maarten Lankhorstb3592832015-06-15 12:33:38 +020011765 struct intel_crtc_state *pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +020011766{
Maarten Lankhorstb3592832015-06-15 12:33:38 +020011767 struct drm_atomic_state *state = pipe_config->base.state;
Daniel Vetter7758a112012-07-08 19:40:39 +020011768 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011769 struct drm_connector *connector;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011770 struct drm_connector_state *connector_state;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011771 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011772 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010011773 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020011774
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011775 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020011776
Daniel Vettere143a212013-07-04 12:01:15 +020011777 pipe_config->cpu_transcoder =
11778 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011779
Imre Deak2960bc92013-07-30 13:36:32 +030011780 /*
11781 * Sanitize sync polarity flags based on requested ones. If neither
11782 * positive or negative polarity is requested, treat this as meaning
11783 * negative polarity.
11784 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011785 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030011786 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011787 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030011788
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011789 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030011790 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011791 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030011792
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011793 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
11794 pipe_config);
11795 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011796 goto fail;
11797
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030011798 /*
11799 * Determine the real pipe dimensions. Note that stereo modes can
11800 * increase the actual pipe size due to the frame doubling and
11801 * insertion of additional space for blanks between the frame. This
11802 * is stored in the crtc timings. We use the requested mode to do this
11803 * computation to clearly distinguish it from the adjusted mode, which
11804 * can be changed by the connectors in the below retry loop.
11805 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011806 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080011807 &pipe_config->pipe_src_w,
11808 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030011809
Daniel Vettere29c22c2013-02-21 00:00:16 +010011810encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020011811 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020011812 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020011813 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020011814
Daniel Vetter135c81b2013-07-21 21:37:09 +020011815 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011816 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
11817 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020011818
Daniel Vetter7758a112012-07-08 19:40:39 +020011819 /* Pass our mode to the connectors and the CRTC to give them a chance to
11820 * adjust it according to limitations or connector properties, and also
11821 * a chance to reject the mode entirely.
11822 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011823 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011824 if (connector_state->crtc != crtc)
11825 continue;
11826
11827 encoder = to_intel_encoder(connector_state->best_encoder);
11828
Daniel Vetterefea6e82013-07-21 21:36:59 +020011829 if (!(encoder->compute_config(encoder, pipe_config))) {
11830 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020011831 goto fail;
11832 }
11833 }
11834
Daniel Vetterff9a6752013-06-01 17:16:21 +020011835 /* Set default port clock if not overwritten by the encoder. Needs to be
11836 * done afterwards in case the encoder adjusts the mode. */
11837 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011838 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010011839 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020011840
Daniel Vettera43f6e02013-06-07 23:10:32 +020011841 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010011842 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020011843 DRM_DEBUG_KMS("CRTC fixup failed\n");
11844 goto fail;
11845 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010011846
11847 if (ret == RETRY) {
11848 if (WARN(!retry, "loop in pipe configuration computation\n")) {
11849 ret = -EINVAL;
11850 goto fail;
11851 }
11852
11853 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
11854 retry = false;
11855 goto encoder_retry;
11856 }
11857
Daniel Vettere8fa4272015-08-12 11:43:34 +020011858 /* Dithering seems to not pass-through bits correctly when it should, so
11859 * only enable it on 6bpc panels. */
11860 pipe_config->dither = pipe_config->pipe_bpp == 6*3;
Daniel Vetter62f0ace2015-08-26 18:57:26 +020011861 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011862 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011863
Daniel Vetter7758a112012-07-08 19:40:39 +020011864fail:
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030011865 return ret;
Daniel Vetter7758a112012-07-08 19:40:39 +020011866}
11867
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011868static void
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020011869intel_modeset_update_crtc_state(struct drm_atomic_state *state)
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011870{
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011871 struct drm_crtc *crtc;
11872 struct drm_crtc_state *crtc_state;
Maarten Lankhorst8a75d152015-07-13 16:30:14 +020011873 int i;
Daniel Vetterea9d7582012-07-10 10:42:52 +020011874
Ville Syrjälä76688512014-01-10 11:28:06 +020011875 /* Double check state. */
Maarten Lankhorst8a75d152015-07-13 16:30:14 +020011876 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst3cb480b2015-06-01 12:49:49 +020011877 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
Maarten Lankhorstfc467a222015-06-01 12:50:07 +020011878
11879 /* Update hwmode for vblank functions */
11880 if (crtc->state->active)
11881 crtc->hwmode = crtc->state->adjusted_mode;
11882 else
11883 crtc->hwmode.crtc_clock = 0;
Maarten Lankhorst61067a52015-09-23 16:29:36 +020011884
11885 /*
11886 * Update legacy state to satisfy fbc code. This can
11887 * be removed when fbc uses the atomic state.
11888 */
11889 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
11890 struct drm_plane_state *plane_state = crtc->primary->state;
11891
11892 crtc->primary->fb = plane_state->fb;
11893 crtc->x = plane_state->src_x >> 16;
11894 crtc->y = plane_state->src_y >> 16;
11895 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020011896 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020011897}
11898
Ville Syrjälä3bd26262013-09-06 23:29:02 +030011899static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011900{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030011901 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011902
11903 if (clock1 == clock2)
11904 return true;
11905
11906 if (!clock1 || !clock2)
11907 return false;
11908
11909 diff = abs(clock1 - clock2);
11910
11911 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
11912 return true;
11913
11914 return false;
11915}
11916
Daniel Vetter25c5b262012-07-08 22:08:04 +020011917#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
11918 list_for_each_entry((intel_crtc), \
11919 &(dev)->mode_config.crtc_list, \
11920 base.head) \
Jani Nikula95150bd2015-11-24 21:21:56 +020011921 for_each_if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020011922
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011923static bool
11924intel_compare_m_n(unsigned int m, unsigned int n,
11925 unsigned int m2, unsigned int n2,
11926 bool exact)
11927{
11928 if (m == m2 && n == n2)
11929 return true;
11930
11931 if (exact || !m || !n || !m2 || !n2)
11932 return false;
11933
11934 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
11935
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010011936 if (n > n2) {
11937 while (n > n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011938 m2 <<= 1;
11939 n2 <<= 1;
11940 }
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010011941 } else if (n < n2) {
11942 while (n < n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011943 m <<= 1;
11944 n <<= 1;
11945 }
11946 }
11947
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010011948 if (n != n2)
11949 return false;
11950
11951 return intel_fuzzy_clock_check(m, m2);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011952}
11953
11954static bool
11955intel_compare_link_m_n(const struct intel_link_m_n *m_n,
11956 struct intel_link_m_n *m2_n2,
11957 bool adjust)
11958{
11959 if (m_n->tu == m2_n2->tu &&
11960 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
11961 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
11962 intel_compare_m_n(m_n->link_m, m_n->link_n,
11963 m2_n2->link_m, m2_n2->link_n, !adjust)) {
11964 if (adjust)
11965 *m2_n2 = *m_n;
11966
11967 return true;
11968 }
11969
11970 return false;
11971}
11972
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011973static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020011974intel_pipe_config_compare(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011975 struct intel_crtc_state *current_config,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011976 struct intel_crtc_state *pipe_config,
11977 bool adjust)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011978{
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011979 bool ret = true;
11980
11981#define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
11982 do { \
11983 if (!adjust) \
11984 DRM_ERROR(fmt, ##__VA_ARGS__); \
11985 else \
11986 DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
11987 } while (0)
11988
Daniel Vetter66e985c2013-06-05 13:34:20 +020011989#define PIPE_CONF_CHECK_X(name) \
11990 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011991 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter66e985c2013-06-05 13:34:20 +020011992 "(expected 0x%08x, found 0x%08x)\n", \
11993 current_config->name, \
11994 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011995 ret = false; \
Daniel Vetter66e985c2013-06-05 13:34:20 +020011996 }
11997
Daniel Vetter08a24032013-04-19 11:25:34 +020011998#define PIPE_CONF_CHECK_I(name) \
11999 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012000 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter08a24032013-04-19 11:25:34 +020012001 "(expected %i, found %i)\n", \
12002 current_config->name, \
12003 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012004 ret = false; \
12005 }
12006
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012007#define PIPE_CONF_CHECK_P(name) \
12008 if (current_config->name != pipe_config->name) { \
12009 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12010 "(expected %p, found %p)\n", \
12011 current_config->name, \
12012 pipe_config->name); \
12013 ret = false; \
12014 }
12015
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012016#define PIPE_CONF_CHECK_M_N(name) \
12017 if (!intel_compare_link_m_n(&current_config->name, \
12018 &pipe_config->name,\
12019 adjust)) { \
12020 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12021 "(expected tu %i gmch %i/%i link %i/%i, " \
12022 "found tu %i, gmch %i/%i link %i/%i)\n", \
12023 current_config->name.tu, \
12024 current_config->name.gmch_m, \
12025 current_config->name.gmch_n, \
12026 current_config->name.link_m, \
12027 current_config->name.link_n, \
12028 pipe_config->name.tu, \
12029 pipe_config->name.gmch_m, \
12030 pipe_config->name.gmch_n, \
12031 pipe_config->name.link_m, \
12032 pipe_config->name.link_n); \
12033 ret = false; \
12034 }
12035
Daniel Vetter55c561a2016-03-30 11:34:36 +020012036/* This is required for BDW+ where there is only one set of registers for
12037 * switching between high and low RR.
12038 * This macro can be used whenever a comparison has to be made between one
12039 * hw state and multiple sw state variables.
12040 */
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012041#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
12042 if (!intel_compare_link_m_n(&current_config->name, \
12043 &pipe_config->name, adjust) && \
12044 !intel_compare_link_m_n(&current_config->alt_name, \
12045 &pipe_config->name, adjust)) { \
12046 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12047 "(expected tu %i gmch %i/%i link %i/%i, " \
12048 "or tu %i gmch %i/%i link %i/%i, " \
12049 "found tu %i, gmch %i/%i link %i/%i)\n", \
12050 current_config->name.tu, \
12051 current_config->name.gmch_m, \
12052 current_config->name.gmch_n, \
12053 current_config->name.link_m, \
12054 current_config->name.link_n, \
12055 current_config->alt_name.tu, \
12056 current_config->alt_name.gmch_m, \
12057 current_config->alt_name.gmch_n, \
12058 current_config->alt_name.link_m, \
12059 current_config->alt_name.link_n, \
12060 pipe_config->name.tu, \
12061 pipe_config->name.gmch_m, \
12062 pipe_config->name.gmch_n, \
12063 pipe_config->name.link_m, \
12064 pipe_config->name.link_n); \
12065 ret = false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010012066 }
12067
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012068#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12069 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012070 INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012071 "(expected %i, found %i)\n", \
12072 current_config->name & (mask), \
12073 pipe_config->name & (mask)); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012074 ret = false; \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012075 }
12076
Ville Syrjälä5e550652013-09-06 23:29:07 +030012077#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12078 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012079 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012080 "(expected %i, found %i)\n", \
12081 current_config->name, \
12082 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012083 ret = false; \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012084 }
12085
Daniel Vetterbb760062013-06-06 14:55:52 +020012086#define PIPE_CONF_QUIRK(quirk) \
12087 ((current_config->quirks | pipe_config->quirks) & (quirk))
12088
Daniel Vettereccb1402013-05-22 00:50:22 +020012089 PIPE_CONF_CHECK_I(cpu_transcoder);
12090
Daniel Vetter08a24032013-04-19 11:25:34 +020012091 PIPE_CONF_CHECK_I(has_pch_encoder);
12092 PIPE_CONF_CHECK_I(fdi_lanes);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012093 PIPE_CONF_CHECK_M_N(fdi_m_n);
Daniel Vetter08a24032013-04-19 11:25:34 +020012094
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012095 PIPE_CONF_CHECK_I(has_dp_encoder);
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012096 PIPE_CONF_CHECK_I(lane_count);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012097
12098 if (INTEL_INFO(dev)->gen < 8) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012099 PIPE_CONF_CHECK_M_N(dp_m_n);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012100
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012101 if (current_config->has_drrs)
12102 PIPE_CONF_CHECK_M_N(dp_m2_n2);
12103 } else
12104 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012105
Jani Nikulaa65347b2015-11-27 12:21:46 +020012106 PIPE_CONF_CHECK_I(has_dsi_encoder);
12107
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012108 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12109 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12110 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12111 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12112 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12113 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012114
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012115 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12116 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12117 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12118 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12119 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12120 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012121
Daniel Vetterc93f54c2013-06-27 19:47:19 +020012122 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b52014-04-24 23:54:47 +020012123 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012124 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
Wayne Boyer666a4532015-12-09 12:29:35 -080012125 IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012126 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080012127 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020012128
Daniel Vetter9ed109a2014-04-24 23:54:52 +020012129 PIPE_CONF_CHECK_I(has_audio);
12130
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012131 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012132 DRM_MODE_FLAG_INTERLACE);
12133
Daniel Vetterbb760062013-06-06 14:55:52 +020012134 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012135 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012136 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012137 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012138 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012139 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012140 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012141 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012142 DRM_MODE_FLAG_NVSYNC);
12143 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012144
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030012145 PIPE_CONF_CHECK_X(gmch_pfit.control);
Daniel Vettere2ff2d42015-07-15 14:15:50 +020012146 /* pfit ratios are autocomputed by the hw on gen4+ */
12147 if (INTEL_INFO(dev)->gen < 4)
Ville Syrjälä7f7d8dd2016-03-15 16:40:07 +020012148 PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030012149 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
Daniel Vetter99535992014-04-13 12:00:33 +020012150
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020012151 if (!adjust) {
12152 PIPE_CONF_CHECK_I(pipe_src_w);
12153 PIPE_CONF_CHECK_I(pipe_src_h);
12154
12155 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12156 if (current_config->pch_pfit.enabled) {
12157 PIPE_CONF_CHECK_X(pch_pfit.pos);
12158 PIPE_CONF_CHECK_X(pch_pfit.size);
12159 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012160
Maarten Lankhorst7aefe2b2015-09-14 11:30:10 +020012161 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12162 }
Chandra Kondurua1b22782015-04-07 15:28:45 -070012163
Jesse Barnese59150d2014-01-07 13:30:45 -080012164 /* BDW+ don't expose a synchronous way to read the state */
12165 if (IS_HASWELL(dev))
12166 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012167
Ville Syrjälä282740f2013-09-04 18:30:03 +030012168 PIPE_CONF_CHECK_I(double_wide);
12169
Daniel Vetter26804af2014-06-25 22:01:55 +030012170 PIPE_CONF_CHECK_X(ddi_pll_sel);
12171
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012172 PIPE_CONF_CHECK_P(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012173 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020012174 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012175 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12176 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030012177 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Maarten Lankhorst00490c22015-11-16 14:42:12 +010012178 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000012179 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12180 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12181 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012182
Ville Syrjälä47eacba2016-04-12 22:14:35 +030012183 PIPE_CONF_CHECK_X(dsi_pll.ctrl);
12184 PIPE_CONF_CHECK_X(dsi_pll.div);
12185
Ville Syrjälä42571ae2013-09-06 23:29:00 +030012186 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12187 PIPE_CONF_CHECK_I(pipe_bpp);
12188
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012189 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080012190 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030012191
Daniel Vetter66e985c2013-06-05 13:34:20 +020012192#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020012193#undef PIPE_CONF_CHECK_I
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012194#undef PIPE_CONF_CHECK_P
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012195#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030012196#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020012197#undef PIPE_CONF_QUIRK
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012198#undef INTEL_ERR_OR_DBG_KMS
Daniel Vetter627eb5a2013-04-29 19:33:42 +020012199
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012200 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012201}
12202
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020012203static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
12204 const struct intel_crtc_state *pipe_config)
12205{
12206 if (pipe_config->has_pch_encoder) {
Ville Syrjälä21a727b2016-02-17 21:41:10 +020012207 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020012208 &pipe_config->fdi_m_n);
12209 int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
12210
12211 /*
12212 * FDI already provided one idea for the dotclock.
12213 * Yell if the encoder disagrees.
12214 */
12215 WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
12216 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
12217 fdi_dotclock, dotclock);
12218 }
12219}
12220
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012221static void verify_wm_state(struct drm_crtc *crtc,
12222 struct drm_crtc_state *new_state)
Damien Lespiau08db6652014-11-04 17:06:52 +000012223{
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012224 struct drm_device *dev = crtc->dev;
Damien Lespiau08db6652014-11-04 17:06:52 +000012225 struct drm_i915_private *dev_priv = dev->dev_private;
12226 struct skl_ddb_allocation hw_ddb, *sw_ddb;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012227 struct skl_ddb_entry *hw_entry, *sw_entry;
12228 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12229 const enum pipe pipe = intel_crtc->pipe;
Damien Lespiau08db6652014-11-04 17:06:52 +000012230 int plane;
12231
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012232 if (INTEL_INFO(dev)->gen < 9 || !new_state->active)
Damien Lespiau08db6652014-11-04 17:06:52 +000012233 return;
12234
12235 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12236 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12237
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012238 /* planes */
12239 for_each_plane(dev_priv, pipe, plane) {
12240 hw_entry = &hw_ddb.plane[pipe][plane];
12241 sw_entry = &sw_ddb->plane[pipe][plane];
Damien Lespiau08db6652014-11-04 17:06:52 +000012242
12243 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12244 continue;
12245
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012246 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12247 "(expected (%u,%u), found (%u,%u))\n",
12248 pipe_name(pipe), plane + 1,
12249 sw_entry->start, sw_entry->end,
12250 hw_entry->start, hw_entry->end);
12251 }
12252
12253 /* cursor */
12254 hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
12255 sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
12256
12257 if (!skl_ddb_entry_equal(hw_entry, sw_entry)) {
Damien Lespiau08db6652014-11-04 17:06:52 +000012258 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12259 "(expected (%u,%u), found (%u,%u))\n",
12260 pipe_name(pipe),
12261 sw_entry->start, sw_entry->end,
12262 hw_entry->start, hw_entry->end);
12263 }
12264}
12265
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012266static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012267verify_connector_state(struct drm_device *dev, struct drm_crtc *crtc)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012268{
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012269 struct drm_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012270
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012271 drm_for_each_connector(connector, dev) {
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012272 struct drm_encoder *encoder = connector->encoder;
12273 struct drm_connector_state *state = connector->state;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012274
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012275 if (state->crtc != crtc)
12276 continue;
12277
Maarten Lankhorst03f476e2016-05-17 15:08:00 +020012278 intel_connector_verify_state(to_intel_connector(connector),
12279 connector->state);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012280
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012281 I915_STATE_WARN(state->best_encoder != encoder,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012282 "connector's atomic encoder doesn't match legacy encoder\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012283 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012284}
12285
12286static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012287verify_encoder_state(struct drm_device *dev)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012288{
12289 struct intel_encoder *encoder;
12290 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012291
Damien Lespiaub2784e12014-08-05 11:29:37 +010012292 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012293 bool enabled = false;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012294 enum pipe pipe;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012295
12296 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
12297 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030012298 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012299
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012300 for_each_intel_connector(dev, connector) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012301 if (connector->base.state->best_encoder != &encoder->base)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012302 continue;
12303 enabled = true;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012304
12305 I915_STATE_WARN(connector->base.state->crtc !=
12306 encoder->base.crtc,
12307 "connector's crtc doesn't match encoder crtc\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012308 }
Dave Airlie0e32b392014-05-02 14:02:48 +100012309
Rob Clarke2c719b2014-12-15 13:56:32 -050012310 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012311 "encoder's enabled state mismatch "
12312 "(expected %i, found %i)\n",
12313 !!encoder->base.crtc, enabled);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012314
12315 if (!encoder->base.crtc) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012316 bool active;
12317
12318 active = encoder->get_hw_state(encoder, &pipe);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012319 I915_STATE_WARN(active,
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012320 "encoder detached but still enabled on pipe %c.\n",
12321 pipe_name(pipe));
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012322 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012323 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012324}
12325
12326static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012327verify_crtc_state(struct drm_crtc *crtc,
12328 struct drm_crtc_state *old_crtc_state,
12329 struct drm_crtc_state *new_crtc_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012330{
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012331 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030012332 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012333 struct intel_encoder *encoder;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012334 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12335 struct intel_crtc_state *pipe_config, *sw_config;
12336 struct drm_atomic_state *old_state;
12337 bool active;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012338
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012339 old_state = old_crtc_state->state;
12340 __drm_atomic_helper_crtc_destroy_state(crtc, old_crtc_state);
12341 pipe_config = to_intel_crtc_state(old_crtc_state);
12342 memset(pipe_config, 0, sizeof(*pipe_config));
12343 pipe_config->base.crtc = crtc;
12344 pipe_config->base.state = old_state;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012345
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012346 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012347
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012348 active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012349
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012350 /* hw state is inconsistent with the pipe quirk */
12351 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12352 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
12353 active = new_crtc_state->active;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012354
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012355 I915_STATE_WARN(new_crtc_state->active != active,
12356 "crtc active state doesn't match with hw state "
12357 "(expected %i, found %i)\n", new_crtc_state->active, active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012358
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012359 I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
12360 "transitional active state does not match atomic hw state "
12361 "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012362
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012363 for_each_encoder_on_crtc(dev, crtc, encoder) {
12364 enum pipe pipe;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012365
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012366 active = encoder->get_hw_state(encoder, &pipe);
12367 I915_STATE_WARN(active != new_crtc_state->active,
12368 "[ENCODER:%i] active %i with crtc active %i\n",
12369 encoder->base.base.id, active, new_crtc_state->active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012370
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012371 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
12372 "Encoder connected to wrong pipe %c\n",
12373 pipe_name(pipe));
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012374
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012375 if (active)
12376 encoder->get_config(encoder, pipe_config);
12377 }
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012378
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012379 if (!new_crtc_state->active)
12380 return;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012381
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012382 intel_pipe_config_sanity_check(dev_priv, pipe_config);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012383
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012384 sw_config = to_intel_crtc_state(crtc->state);
12385 if (!intel_pipe_config_compare(dev, sw_config,
12386 pipe_config, false)) {
12387 I915_STATE_WARN(1, "pipe state doesn't match!\n");
12388 intel_dump_pipe_config(intel_crtc, pipe_config,
12389 "[hw state]");
12390 intel_dump_pipe_config(intel_crtc, sw_config,
12391 "[sw state]");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012392 }
12393}
12394
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012395static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012396verify_single_dpll_state(struct drm_i915_private *dev_priv,
12397 struct intel_shared_dpll *pll,
12398 struct drm_crtc *crtc,
12399 struct drm_crtc_state *new_state)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012400{
12401 struct intel_dpll_hw_state dpll_hw_state;
12402 unsigned crtc_mask;
12403 bool active;
12404
12405 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
12406
12407 DRM_DEBUG_KMS("%s\n", pll->name);
12408
12409 active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
12410
12411 if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
12412 I915_STATE_WARN(!pll->on && pll->active_mask,
12413 "pll in active use but not on in sw tracking\n");
12414 I915_STATE_WARN(pll->on && !pll->active_mask,
12415 "pll is on but not used by any active crtc\n");
12416 I915_STATE_WARN(pll->on != active,
12417 "pll on state mismatch (expected %i, found %i)\n",
12418 pll->on, active);
12419 }
12420
12421 if (!crtc) {
12422 I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
12423 "more active pll users than references: %x vs %x\n",
12424 pll->active_mask, pll->config.crtc_mask);
12425
12426 return;
12427 }
12428
12429 crtc_mask = 1 << drm_crtc_index(crtc);
12430
12431 if (new_state->active)
12432 I915_STATE_WARN(!(pll->active_mask & crtc_mask),
12433 "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
12434 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
12435 else
12436 I915_STATE_WARN(pll->active_mask & crtc_mask,
12437 "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
12438 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
12439
12440 I915_STATE_WARN(!(pll->config.crtc_mask & crtc_mask),
12441 "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
12442 crtc_mask, pll->config.crtc_mask);
12443
12444 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state,
12445 &dpll_hw_state,
12446 sizeof(dpll_hw_state)),
12447 "pll hw state mismatch\n");
12448}
12449
12450static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012451verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
12452 struct drm_crtc_state *old_crtc_state,
12453 struct drm_crtc_state *new_crtc_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012454{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012455 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012456 struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
12457 struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
12458
12459 if (new_state->shared_dpll)
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012460 verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012461
12462 if (old_state->shared_dpll &&
12463 old_state->shared_dpll != new_state->shared_dpll) {
12464 unsigned crtc_mask = 1 << drm_crtc_index(crtc);
12465 struct intel_shared_dpll *pll = old_state->shared_dpll;
12466
12467 I915_STATE_WARN(pll->active_mask & crtc_mask,
12468 "pll active mismatch (didn't expect pipe %c in active mask)\n",
12469 pipe_name(drm_crtc_index(crtc)));
12470 I915_STATE_WARN(pll->config.crtc_mask & crtc_mask,
12471 "pll enabled crtcs mismatch (found %x in enabled mask)\n",
12472 pipe_name(drm_crtc_index(crtc)));
12473 }
12474}
12475
12476static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012477intel_modeset_verify_crtc(struct drm_crtc *crtc,
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012478 struct drm_crtc_state *old_state,
12479 struct drm_crtc_state *new_state)
12480{
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012481 verify_wm_state(crtc, new_state);
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012482 verify_crtc_state(crtc, old_state, new_state);
12483 verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012484}
12485
12486static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012487verify_disabled_dpll_state(struct drm_device *dev)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012488{
12489 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012490 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020012491
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012492 for (i = 0; i < dev_priv->num_shared_dpll; i++)
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012493 verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012494}
Daniel Vetter53589012013-06-05 13:34:16 +020012495
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012496static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012497intel_modeset_verify_disabled(struct drm_device *dev)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012498{
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012499 verify_encoder_state(dev);
12500 verify_connector_state(dev, NULL);
12501 verify_disabled_dpll_state(dev);
Daniel Vettere2e1ed42012-07-08 21:14:38 +020012502}
12503
Ville Syrjälä80715b22014-05-15 20:23:23 +030012504static void update_scanline_offset(struct intel_crtc *crtc)
12505{
12506 struct drm_device *dev = crtc->base.dev;
12507
12508 /*
12509 * The scanline counter increments at the leading edge of hsync.
12510 *
12511 * On most platforms it starts counting from vtotal-1 on the
12512 * first active line. That means the scanline counter value is
12513 * always one less than what we would expect. Ie. just after
12514 * start of vblank, which also occurs at start of hsync (on the
12515 * last active line), the scanline counter will read vblank_start-1.
12516 *
12517 * On gen2 the scanline counter starts counting from 1 instead
12518 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
12519 * to keep the value positive), instead of adding one.
12520 *
12521 * On HSW+ the behaviour of the scanline counter depends on the output
12522 * type. For DP ports it behaves like most other platforms, but on HDMI
12523 * there's an extra 1 line difference. So we need to add two instead of
12524 * one to the value.
12525 */
12526 if (IS_GEN2(dev)) {
Ville Syrjälä124abe02015-09-08 13:40:45 +030012527 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030012528 int vtotal;
12529
Ville Syrjälä124abe02015-09-08 13:40:45 +030012530 vtotal = adjusted_mode->crtc_vtotal;
12531 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
Ville Syrjälä80715b22014-05-15 20:23:23 +030012532 vtotal /= 2;
12533
12534 crtc->scanline_offset = vtotal - 1;
12535 } else if (HAS_DDI(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +030012536 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030012537 crtc->scanline_offset = 2;
12538 } else
12539 crtc->scanline_offset = 1;
12540}
12541
Maarten Lankhorstad421372015-06-15 12:33:42 +020012542static void intel_modeset_clear_plls(struct drm_atomic_state *state)
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012543{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012544 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012545 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstad421372015-06-15 12:33:42 +020012546 struct intel_shared_dpll_config *shared_dpll = NULL;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012547 struct drm_crtc *crtc;
12548 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012549 int i;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012550
12551 if (!dev_priv->display.crtc_compute_clock)
Maarten Lankhorstad421372015-06-15 12:33:42 +020012552 return;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012553
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012554 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010012555 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012556 struct intel_shared_dpll *old_dpll =
12557 to_intel_crtc_state(crtc->state)->shared_dpll;
Maarten Lankhorstad421372015-06-15 12:33:42 +020012558
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010012559 if (!needs_modeset(crtc_state))
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012560 continue;
12561
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012562 to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010012563
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012564 if (!old_dpll)
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010012565 continue;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012566
Maarten Lankhorstad421372015-06-15 12:33:42 +020012567 if (!shared_dpll)
12568 shared_dpll = intel_atomic_get_shared_dpll_state(state);
12569
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012570 intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012571 }
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012572}
12573
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020012574/*
12575 * This implements the workaround described in the "notes" section of the mode
12576 * set sequence documentation. When going from no pipes or single pipe to
12577 * multiple pipes, and planes are enabled after the pipe, we need to wait at
12578 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
12579 */
12580static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
12581{
12582 struct drm_crtc_state *crtc_state;
12583 struct intel_crtc *intel_crtc;
12584 struct drm_crtc *crtc;
12585 struct intel_crtc_state *first_crtc_state = NULL;
12586 struct intel_crtc_state *other_crtc_state = NULL;
12587 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
12588 int i;
12589
12590 /* look at all crtc's that are going to be enabled in during modeset */
12591 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12592 intel_crtc = to_intel_crtc(crtc);
12593
12594 if (!crtc_state->active || !needs_modeset(crtc_state))
12595 continue;
12596
12597 if (first_crtc_state) {
12598 other_crtc_state = to_intel_crtc_state(crtc_state);
12599 break;
12600 } else {
12601 first_crtc_state = to_intel_crtc_state(crtc_state);
12602 first_pipe = intel_crtc->pipe;
12603 }
12604 }
12605
12606 /* No workaround needed? */
12607 if (!first_crtc_state)
12608 return 0;
12609
12610 /* w/a possibly needed, check how many crtc's are already enabled. */
12611 for_each_intel_crtc(state->dev, intel_crtc) {
12612 struct intel_crtc_state *pipe_config;
12613
12614 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
12615 if (IS_ERR(pipe_config))
12616 return PTR_ERR(pipe_config);
12617
12618 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
12619
12620 if (!pipe_config->base.active ||
12621 needs_modeset(&pipe_config->base))
12622 continue;
12623
12624 /* 2 or more enabled crtcs means no need for w/a */
12625 if (enabled_pipe != INVALID_PIPE)
12626 return 0;
12627
12628 enabled_pipe = intel_crtc->pipe;
12629 }
12630
12631 if (enabled_pipe != INVALID_PIPE)
12632 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
12633 else if (other_crtc_state)
12634 other_crtc_state->hsw_workaround_pipe = first_pipe;
12635
12636 return 0;
12637}
12638
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012639static int intel_modeset_all_pipes(struct drm_atomic_state *state)
12640{
12641 struct drm_crtc *crtc;
12642 struct drm_crtc_state *crtc_state;
12643 int ret = 0;
12644
12645 /* add all active pipes to the state */
12646 for_each_crtc(state->dev, crtc) {
12647 crtc_state = drm_atomic_get_crtc_state(state, crtc);
12648 if (IS_ERR(crtc_state))
12649 return PTR_ERR(crtc_state);
12650
12651 if (!crtc_state->active || needs_modeset(crtc_state))
12652 continue;
12653
12654 crtc_state->mode_changed = true;
12655
12656 ret = drm_atomic_add_affected_connectors(state, crtc);
12657 if (ret)
12658 break;
12659
12660 ret = drm_atomic_add_affected_planes(state, crtc);
12661 if (ret)
12662 break;
12663 }
12664
12665 return ret;
12666}
12667
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012668static int intel_modeset_checks(struct drm_atomic_state *state)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012669{
Maarten Lankhorst565602d2015-12-10 12:33:57 +010012670 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
12671 struct drm_i915_private *dev_priv = state->dev->dev_private;
12672 struct drm_crtc *crtc;
12673 struct drm_crtc_state *crtc_state;
12674 int ret = 0, i;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012675
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012676 if (!check_digital_port_conflicts(state)) {
12677 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
12678 return -EINVAL;
12679 }
12680
Maarten Lankhorst565602d2015-12-10 12:33:57 +010012681 intel_state->modeset = true;
12682 intel_state->active_crtcs = dev_priv->active_crtcs;
12683
12684 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12685 if (crtc_state->active)
12686 intel_state->active_crtcs |= 1 << i;
12687 else
12688 intel_state->active_crtcs &= ~(1 << i);
Matt Roper8b4a7d02016-05-12 07:06:00 -070012689
12690 if (crtc_state->active != crtc->state->active)
12691 intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010012692 }
12693
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012694 /*
12695 * See if the config requires any additional preparation, e.g.
12696 * to adjust global state with pipes off. We need to do this
12697 * here so we can get the modeset_pipe updated config for the new
12698 * mode set on this crtc. For other crtcs we need to use the
12699 * adjusted_mode bits in the crtc directly.
12700 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012701 if (dev_priv->display.modeset_calc_cdclk) {
Clint Taylorc89e39f2016-05-13 23:41:21 +030012702 if (!intel_state->cdclk_pll_vco)
Ville Syrjälä63911d72016-05-13 23:41:32 +030012703 intel_state->cdclk_pll_vco = dev_priv->cdclk_pll.vco;
Ville Syrjäläb2045352016-05-13 23:41:27 +030012704 if (!intel_state->cdclk_pll_vco)
12705 intel_state->cdclk_pll_vco = dev_priv->skl_preferred_vco_freq;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012706
Clint Taylorc89e39f2016-05-13 23:41:21 +030012707 ret = dev_priv->display.modeset_calc_cdclk(state);
12708 if (ret < 0)
12709 return ret;
12710
12711 if (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
Ville Syrjälä63911d72016-05-13 23:41:32 +030012712 intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco)
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012713 ret = intel_modeset_all_pipes(state);
12714
12715 if (ret < 0)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012716 return ret;
Maarten Lankhorste8788cb2016-02-16 10:25:11 +010012717
12718 DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
12719 intel_state->cdclk, intel_state->dev_cdclk);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012720 } else
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010012721 to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012722
Maarten Lankhorstad421372015-06-15 12:33:42 +020012723 intel_modeset_clear_plls(state);
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012724
Maarten Lankhorst565602d2015-12-10 12:33:57 +010012725 if (IS_HASWELL(dev_priv))
Maarten Lankhorstad421372015-06-15 12:33:42 +020012726 return haswell_mode_set_planes_workaround(state);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020012727
Maarten Lankhorstad421372015-06-15 12:33:42 +020012728 return 0;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012729}
12730
Matt Roperaa363132015-09-24 15:53:18 -070012731/*
12732 * Handle calculation of various watermark data at the end of the atomic check
12733 * phase. The code here should be run after the per-crtc and per-plane 'check'
12734 * handlers to ensure that all derived state has been updated.
12735 */
Matt Roper55994c22016-05-12 07:06:08 -070012736static int calc_watermark_data(struct drm_atomic_state *state)
Matt Roperaa363132015-09-24 15:53:18 -070012737{
12738 struct drm_device *dev = state->dev;
Matt Roper98d39492016-05-12 07:06:03 -070012739 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roper98d39492016-05-12 07:06:03 -070012740
12741 /* Is there platform-specific watermark information to calculate? */
12742 if (dev_priv->display.compute_global_watermarks)
Matt Roper55994c22016-05-12 07:06:08 -070012743 return dev_priv->display.compute_global_watermarks(state);
12744
12745 return 0;
Matt Roperaa363132015-09-24 15:53:18 -070012746}
12747
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020012748/**
12749 * intel_atomic_check - validate state object
12750 * @dev: drm device
12751 * @state: state to validate
12752 */
12753static int intel_atomic_check(struct drm_device *dev,
12754 struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020012755{
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020012756 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roperaa363132015-09-24 15:53:18 -070012757 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012758 struct drm_crtc *crtc;
12759 struct drm_crtc_state *crtc_state;
12760 int ret, i;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020012761 bool any_ms = false;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012762
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020012763 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020012764 if (ret)
12765 return ret;
12766
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012767 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012768 struct intel_crtc_state *pipe_config =
12769 to_intel_crtc_state(crtc_state);
Daniel Vetter1ed51de2015-07-15 14:15:51 +020012770
12771 /* Catch I915_MODE_FLAG_INHERITED */
12772 if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
12773 crtc_state->mode_changed = true;
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012774
Daniel Vetter26495482015-07-15 14:15:52 +020012775 if (!needs_modeset(crtc_state))
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012776 continue;
12777
Daniel Vetteraf4a8792016-05-09 09:31:25 +020012778 if (!crtc_state->enable) {
12779 any_ms = true;
12780 continue;
12781 }
12782
Daniel Vetter26495482015-07-15 14:15:52 +020012783 /* FIXME: For only active_changed we shouldn't need to do any
12784 * state recomputation at all. */
12785
Daniel Vetter1ed51de2015-07-15 14:15:51 +020012786 ret = drm_atomic_add_affected_connectors(state, crtc);
12787 if (ret)
12788 return ret;
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012789
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012790 ret = intel_modeset_pipe_config(crtc, pipe_config);
Maarten Lankhorst25aa1c32016-05-03 10:30:38 +020012791 if (ret) {
12792 intel_dump_pipe_config(to_intel_crtc(crtc),
12793 pipe_config, "[failed]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012794 return ret;
Maarten Lankhorst25aa1c32016-05-03 10:30:38 +020012795 }
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012796
Jani Nikula73831232015-11-19 10:26:30 +020012797 if (i915.fastboot &&
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020012798 intel_pipe_config_compare(dev,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012799 to_intel_crtc_state(crtc->state),
Daniel Vetter1ed51de2015-07-15 14:15:51 +020012800 pipe_config, true)) {
Daniel Vetter26495482015-07-15 14:15:52 +020012801 crtc_state->mode_changed = false;
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020012802 to_intel_crtc_state(crtc_state)->update_pipe = true;
Daniel Vetter26495482015-07-15 14:15:52 +020012803 }
12804
Daniel Vetteraf4a8792016-05-09 09:31:25 +020012805 if (needs_modeset(crtc_state))
Daniel Vetter26495482015-07-15 14:15:52 +020012806 any_ms = true;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020012807
Daniel Vetteraf4a8792016-05-09 09:31:25 +020012808 ret = drm_atomic_add_affected_planes(state, crtc);
12809 if (ret)
12810 return ret;
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012811
Daniel Vetter26495482015-07-15 14:15:52 +020012812 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
12813 needs_modeset(crtc_state) ?
12814 "[modeset]" : "[fastset]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012815 }
12816
Maarten Lankhorst61333b62015-06-15 12:33:50 +020012817 if (any_ms) {
12818 ret = intel_modeset_checks(state);
12819
12820 if (ret)
12821 return ret;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012822 } else
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020012823 intel_state->cdclk = dev_priv->cdclk_freq;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012824
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020012825 ret = drm_atomic_helper_check_planes(dev, state);
Matt Roperaa363132015-09-24 15:53:18 -070012826 if (ret)
12827 return ret;
12828
Paulo Zanonif51be2e2016-01-19 11:35:50 -020012829 intel_fbc_choose_crtc(dev_priv, state);
Matt Roper55994c22016-05-12 07:06:08 -070012830 return calc_watermark_data(state);
Daniel Vettera6778b32012-07-02 09:56:42 +020012831}
12832
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020012833static bool needs_work(struct drm_crtc_state *crtc_state)
12834{
12835 /* hw state checker needs to run */
12836 if (needs_modeset(crtc_state))
12837 return true;
12838
12839 /* unpin old fb's, possibly vblank update */
12840 if (crtc_state->planes_changed)
12841 return true;
12842
12843 /* pipe parameters need to be updated, and hw state checker */
12844 if (to_intel_crtc_state(crtc_state)->update_pipe)
12845 return true;
12846
12847 /* vblank event requested? */
12848 if (crtc_state->event)
12849 return true;
12850
12851 return false;
12852}
12853
Maarten Lankhorst5008e872015-08-18 13:40:05 +020012854static int intel_atomic_prepare_commit(struct drm_device *dev,
12855 struct drm_atomic_state *state,
Maarten Lankhorst81072bf2016-04-26 16:11:45 +020012856 bool nonblock)
Maarten Lankhorst5008e872015-08-18 13:40:05 +020012857{
Maarten Lankhorst7580d772015-08-18 13:40:06 +020012858 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020012859 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020012860 struct drm_plane_state *plane_state;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020012861 struct drm_crtc_state *crtc_state;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020012862 struct drm_plane *plane;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020012863 struct drm_crtc *crtc;
12864 int i, ret;
12865
Maarten Lankhorst5008e872015-08-18 13:40:05 +020012866 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020012867 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12868 struct intel_flip_work *work;
12869
Maarten Lankhorst95c2ccdc2016-05-17 15:08:02 +020012870 if (!state->legacy_cursor_update) {
12871 ret = intel_crtc_wait_for_pending_flips(crtc);
12872 if (ret)
12873 return ret;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020012874
Maarten Lankhorst95c2ccdc2016-05-17 15:08:02 +020012875 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
12876 flush_workqueue(dev_priv->wq);
12877 }
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020012878
12879 /* test if we need to update something */
12880 if (!needs_work(crtc_state))
12881 continue;
12882
12883 intel_state->work[i] = work =
12884 kzalloc(sizeof(**intel_state->work), GFP_KERNEL);
12885
12886 if (!work)
12887 return -ENOMEM;
12888
12889 if (needs_modeset(crtc_state) ||
12890 to_intel_crtc_state(crtc_state)->update_pipe) {
12891 work->num_old_connectors = hweight32(crtc->state->connector_mask);
12892
12893 work->old_connector_state = kcalloc(work->num_old_connectors,
12894 sizeof(*work->old_connector_state),
12895 GFP_KERNEL);
12896
12897 work->num_new_connectors = hweight32(crtc_state->connector_mask);
12898 work->new_connector_state = kcalloc(work->num_new_connectors,
12899 sizeof(*work->new_connector_state),
12900 GFP_KERNEL);
12901
12902 if (!work->old_connector_state || !work->new_connector_state)
12903 return -ENOMEM;
12904 }
Maarten Lankhorst5008e872015-08-18 13:40:05 +020012905 }
12906
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020012907 if (intel_state->modeset && nonblock) {
12908 DRM_DEBUG_ATOMIC("Nonblock modesets are not yet supported!\n");
12909 return -EINVAL;
12910 }
12911
Maarten Lankhorstf9356752015-08-18 13:40:05 +020012912 ret = mutex_lock_interruptible(&dev->struct_mutex);
12913 if (ret)
12914 return ret;
12915
Maarten Lankhorst5008e872015-08-18 13:40:05 +020012916 ret = drm_atomic_helper_prepare_planes(dev, state);
Chris Wilsonf7e58382016-04-13 17:35:07 +010012917 mutex_unlock(&dev->struct_mutex);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020012918
Dave Airlie21daaee2016-05-05 09:56:30 +100012919 if (!ret && !nonblock) {
Maarten Lankhorst7580d772015-08-18 13:40:06 +020012920 for_each_plane_in_state(state, plane, plane_state, i) {
12921 struct intel_plane_state *intel_plane_state =
12922 to_intel_plane_state(plane_state);
12923
Maarten Lankhorst84fc4942016-05-17 15:07:53 +020012924 if (plane_state->fence) {
12925 long lret = fence_wait(plane_state->fence, true);
12926
12927 if (lret < 0) {
12928 ret = lret;
12929 break;
12930 }
12931 }
12932
Maarten Lankhorst7580d772015-08-18 13:40:06 +020012933 if (!intel_plane_state->wait_req)
12934 continue;
12935
12936 ret = __i915_wait_request(intel_plane_state->wait_req,
Chris Wilson299259a2016-04-13 17:35:06 +010012937 true, NULL, NULL);
Chris Wilsonf7e58382016-04-13 17:35:07 +010012938 if (ret) {
Chris Wilsonf4457ae2016-04-13 17:35:08 +010012939 /* Any hang should be swallowed by the wait */
12940 WARN_ON(ret == -EIO);
Chris Wilsonf7e58382016-04-13 17:35:07 +010012941 mutex_lock(&dev->struct_mutex);
12942 drm_atomic_helper_cleanup_planes(dev, state);
12943 mutex_unlock(&dev->struct_mutex);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020012944 break;
Chris Wilsonf7e58382016-04-13 17:35:07 +010012945 }
Maarten Lankhorst7580d772015-08-18 13:40:06 +020012946 }
Maarten Lankhorst7580d772015-08-18 13:40:06 +020012947 }
Maarten Lankhorst5008e872015-08-18 13:40:05 +020012948
12949 return ret;
12950}
12951
Maarten Lankhorsta2991412016-05-17 15:07:48 +020012952u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
12953{
12954 struct drm_device *dev = crtc->base.dev;
12955
12956 if (!dev->max_vblank_count)
12957 return drm_accurate_vblank_count(&crtc->base);
12958
12959 return dev->driver->get_vblank_counter(dev, crtc->pipe);
12960}
12961
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020012962static void intel_prepare_work(struct drm_crtc *crtc,
12963 struct intel_flip_work *work,
12964 struct drm_atomic_state *state,
12965 struct drm_crtc_state *old_crtc_state)
Maarten Lankhorste8861672016-02-24 11:24:26 +010012966{
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020012967 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12968 struct drm_plane_state *old_plane_state;
12969 struct drm_plane *plane;
12970 int i, j = 0;
Maarten Lankhorste8861672016-02-24 11:24:26 +010012971
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020012972 INIT_WORK(&work->unpin_work, intel_unpin_work_fn);
12973 INIT_WORK(&work->mmio_work, intel_mmio_flip_work_func);
12974 atomic_inc(&intel_crtc->unpin_work_count);
Maarten Lankhorste8861672016-02-24 11:24:26 +010012975
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020012976 for_each_plane_in_state(state, plane, old_plane_state, i) {
12977 struct intel_plane_state *old_state = to_intel_plane_state(old_plane_state);
12978 struct intel_plane_state *new_state = to_intel_plane_state(plane->state);
Maarten Lankhorste8861672016-02-24 11:24:26 +010012979
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020012980 if (old_state->base.crtc != crtc &&
12981 new_state->base.crtc != crtc)
Maarten Lankhorste8861672016-02-24 11:24:26 +010012982 continue;
12983
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020012984 if (plane->type == DRM_PLANE_TYPE_PRIMARY) {
12985 plane->fb = new_state->base.fb;
12986 crtc->x = new_state->base.src_x >> 16;
12987 crtc->y = new_state->base.src_y >> 16;
Maarten Lankhorste8861672016-02-24 11:24:26 +010012988 }
12989
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020012990 old_state->wait_req = new_state->wait_req;
12991 new_state->wait_req = NULL;
12992
12993 old_state->base.fence = new_state->base.fence;
12994 new_state->base.fence = NULL;
12995
12996 /* remove plane state from the atomic state and move it to work */
12997 old_plane_state->state = NULL;
12998 state->planes[i] = NULL;
12999 state->plane_states[i] = NULL;
13000
13001 work->old_plane_state[j] = old_state;
13002 work->new_plane_state[j++] = new_state;
Maarten Lankhorste8861672016-02-24 11:24:26 +010013003 }
13004
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013005 old_crtc_state->state = NULL;
13006 state->crtcs[drm_crtc_index(crtc)] = NULL;
13007 state->crtc_states[drm_crtc_index(crtc)] = NULL;
Maarten Lankhorste8861672016-02-24 11:24:26 +010013008
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013009 work->old_crtc_state = to_intel_crtc_state(old_crtc_state);
13010 work->new_crtc_state = to_intel_crtc_state(crtc->state);
13011 work->num_planes = j;
Maarten Lankhorste8861672016-02-24 11:24:26 +010013012
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013013 work->event = crtc->state->event;
13014 crtc->state->event = NULL;
Maarten Lankhorste8861672016-02-24 11:24:26 +010013015
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013016 if (needs_modeset(crtc->state) || work->new_crtc_state->update_pipe) {
13017 struct drm_connector *conn;
13018 struct drm_connector_state *old_conn_state;
13019 int k = 0;
Maarten Lankhorste8861672016-02-24 11:24:26 +010013020
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013021 j = 0;
13022
13023 /*
13024 * intel_unpin_work_fn cannot depend on the connector list
13025 * because it may be freed from underneath it, so add
13026 * them all to the work struct while we're holding locks.
13027 */
13028 for_each_connector_in_state(state, conn, old_conn_state, i) {
13029 if (old_conn_state->crtc == crtc) {
13030 work->old_connector_state[j++] = old_conn_state;
13031
13032 state->connectors[i] = NULL;
13033 state->connector_states[i] = NULL;
13034 }
13035 }
13036
13037 /* If another crtc has stolen the connector from state,
13038 * then for_each_connector_in_state is no longer reliable,
13039 * so use drm_for_each_connector here.
13040 */
13041 drm_for_each_connector(conn, state->dev)
13042 if (conn->state->crtc == crtc)
13043 work->new_connector_state[k++] = conn->state;
13044
13045 WARN(j != work->num_old_connectors, "j = %i, expected %i\n", j, work->num_old_connectors);
13046 WARN(k != work->num_new_connectors, "k = %i, expected %i\n", k, work->num_new_connectors);
13047 } else if (!work->new_crtc_state->update_wm_post)
13048 work->can_async_unpin = true;
13049
13050 work->fb_bits = work->new_crtc_state->fb_bits;
Maarten Lankhorste8861672016-02-24 11:24:26 +010013051}
13052
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013053static void intel_schedule_unpin(struct drm_crtc *crtc,
13054 struct intel_atomic_state *state,
13055 struct intel_flip_work *work)
Maarten Lankhorste8861672016-02-24 11:24:26 +010013056{
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013057 struct drm_device *dev = crtc->dev;
13058 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorste8861672016-02-24 11:24:26 +010013059
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013060 to_intel_crtc(crtc)->config = work->new_crtc_state;
Maarten Lankhorste8861672016-02-24 11:24:26 +010013061
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013062 queue_work(dev_priv->wq, &work->unpin_work);
13063}
Maarten Lankhorste8861672016-02-24 11:24:26 +010013064
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020013065static void intel_schedule_flip(struct drm_crtc *crtc,
13066 struct intel_atomic_state *state,
13067 struct intel_flip_work *work,
13068 bool nonblock)
13069{
13070 struct intel_crtc_state *crtc_state = work->new_crtc_state;
13071
13072 if (crtc_state->base.planes_changed ||
13073 needs_modeset(&crtc_state->base) ||
13074 crtc_state->update_pipe) {
13075 if (nonblock)
13076 schedule_work(&work->mmio_work);
13077 else
13078 intel_mmio_flip_work_func(&work->mmio_work);
13079 } else {
13080 int ret;
13081
13082 ret = drm_crtc_vblank_get(crtc);
13083 I915_STATE_WARN(ret < 0, "enabling vblank failed with %i\n", ret);
13084
13085 work->flip_queued_vblank = intel_crtc_get_vblank_counter(to_intel_crtc(crtc));
13086 smp_mb__before_atomic();
13087 atomic_set(&work->pending, 1);
13088 }
13089}
13090
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013091static void intel_schedule_update(struct drm_crtc *crtc,
13092 struct intel_atomic_state *state,
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020013093 struct intel_flip_work *work,
13094 bool nonblock)
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013095{
13096 struct drm_device *dev = crtc->dev;
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020013097 struct intel_crtc_state *pipe_config = work->new_crtc_state;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013098
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020013099 if (!pipe_config->base.active && work->can_async_unpin) {
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013100 INIT_LIST_HEAD(&work->head);
13101 intel_schedule_unpin(crtc, state, work);
13102 return;
13103 }
13104
13105 spin_lock_irq(&dev->event_lock);
13106 list_add_tail(&work->head, &to_intel_crtc(crtc)->flip_work);
13107 spin_unlock_irq(&dev->event_lock);
13108
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020013109 if (!pipe_config->base.active)
13110 intel_schedule_unpin(crtc, state, work);
13111 else
13112 intel_schedule_flip(crtc, state, work, nonblock);
Maarten Lankhorste8861672016-02-24 11:24:26 +010013113}
13114
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013115/**
13116 * intel_atomic_commit - commit validated state object
13117 * @dev: DRM device
13118 * @state: the top-level driver state object
Maarten Lankhorst81072bf2016-04-26 16:11:45 +020013119 * @nonblock: nonblocking commit
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013120 *
13121 * This function commits a top-level state object that has been validated
13122 * with drm_atomic_helper_check().
13123 *
13124 * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
13125 * we can only handle plane-related operations and do not yet support
Maarten Lankhorst81072bf2016-04-26 16:11:45 +020013126 * nonblocking commit.
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013127 *
13128 * RETURNS
13129 * Zero for success or -errno.
13130 */
13131static int intel_atomic_commit(struct drm_device *dev,
13132 struct drm_atomic_state *state,
Maarten Lankhorst81072bf2016-04-26 16:11:45 +020013133 bool nonblock)
Daniel Vettera6778b32012-07-02 09:56:42 +020013134{
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013135 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Jani Nikulafbee40d2014-03-31 14:27:18 +030013136 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013137 struct drm_crtc_state *old_crtc_state;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013138 struct drm_crtc *crtc;
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013139 int ret = 0, i;
Daniel Vettera6778b32012-07-02 09:56:42 +020013140
Maarten Lankhorst81072bf2016-04-26 16:11:45 +020013141 ret = intel_atomic_prepare_commit(dev, state, nonblock);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013142 if (ret) {
13143 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013144 return ret;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013145 }
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013146
Maarten Lankhorst1c5e19f2015-06-01 12:50:06 +020013147 drm_atomic_helper_swap_state(dev, state);
Matt Roper279e99d2016-05-12 07:06:02 -070013148 dev_priv->wm.distrust_bios_wm = false;
Matt Roper734fa012016-05-12 15:11:40 -070013149 dev_priv->wm.skl_results = intel_state->wm_results;
Maarten Lankhorsta1475e72016-03-14 09:27:53 +010013150 intel_shared_dpll_commit(state);
Maarten Lankhorst1c5e19f2015-06-01 12:50:06 +020013151
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013152 if (intel_state->modeset) {
13153 memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
13154 sizeof(intel_state->min_pixclk));
13155 dev_priv->active_crtcs = intel_state->active_crtcs;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013156 dev_priv->atomic_cdclk_freq = intel_state->cdclk;
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013157 }
13158
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013159 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013160 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13161
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013162 if (!needs_modeset(crtc->state))
13163 continue;
13164
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013165 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
Daniel Vetter460da9162013-03-27 00:44:51 +010013166
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013167 intel_state->work[i]->put_power_domains =
13168 modeset_get_crtc_power_domains(crtc,
13169 to_intel_crtc_state(crtc->state));
13170
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013171 if (old_crtc_state->active) {
13172 intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013173 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013174 intel_crtc->active = false;
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -020013175 intel_fbc_disable(intel_crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013176 intel_disable_shared_dpll(intel_crtc);
Ville Syrjälä9bbc8258a2015-11-20 22:09:20 +020013177
13178 /*
13179 * Underruns don't always raise
13180 * interrupts, so check manually.
13181 */
13182 intel_check_cpu_fifo_underruns(dev_priv);
13183 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorstb9001112015-11-19 16:07:16 +010013184
13185 if (!crtc->state->active)
13186 intel_update_watermarks(crtc);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013187 }
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010013188 }
Daniel Vetter7758a112012-07-08 19:40:39 +020013189
Daniel Vetterea9d7582012-07-10 10:42:52 +020013190 /* Only after disabling all output pipelines that will be changed can we
13191 * update the the output configuration. */
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013192 intel_modeset_update_crtc_state(state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020013193
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013194 if (intel_state->modeset) {
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013195 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013196
13197 if (dev_priv->display.modeset_commit_cdclk &&
Clint Taylorc89e39f2016-05-13 23:41:21 +030013198 (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
Ville Syrjälä63911d72016-05-13 23:41:32 +030013199 intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco))
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013200 dev_priv->display.modeset_commit_cdclk(state);
Maarten Lankhorstf6d19732016-03-23 14:58:07 +010013201
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013202 intel_modeset_verify_disabled(dev);
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013203 }
Daniel Vetter47fab732012-10-26 10:58:18 +020013204
Daniel Vettera6778b32012-07-02 09:56:42 +020013205 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013206 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020013207 struct intel_flip_work *work = intel_state->work[i];
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013208 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13209 bool modeset = needs_modeset(crtc->state);
Patrik Jakobsson9f836f92015-11-16 16:20:01 +010013210
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013211 if (modeset && crtc->state->active) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013212 update_scanline_offset(to_intel_crtc(crtc));
13213 dev_priv->display.crtc_enable(crtc);
13214 }
13215
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013216 if (!modeset)
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013217 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013218
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013219 if (!work) {
13220 if (!list_empty_careful(&intel_crtc->flip_work)) {
13221 spin_lock_irq(&dev->event_lock);
13222 if (!list_empty(&intel_crtc->flip_work))
13223 work = list_last_entry(&intel_crtc->flip_work,
13224 struct intel_flip_work, head);
13225
13226 if (work && work->new_crtc_state == to_intel_crtc_state(old_crtc_state)) {
13227 work->free_new_crtc_state = true;
13228 state->crtc_states[i] = NULL;
13229 state->crtcs[i] = NULL;
13230 }
13231 spin_unlock_irq(&dev->event_lock);
13232 }
13233 continue;
13234 }
13235
13236 intel_state->work[i] = NULL;
13237 intel_prepare_work(crtc, work, state, old_crtc_state);
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020013238 intel_schedule_update(crtc, intel_state, work, nonblock);
Matt Ropered4a6a72016-02-23 17:20:13 -080013239 }
13240
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020013241 /* FIXME: add subpixel order */
13242
Maarten Lankhorstee165b12015-08-05 12:37:00 +020013243 drm_atomic_state_free(state);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013244
Mika Kuoppala75714942015-12-16 09:26:48 +020013245 /* As one of the primary mmio accessors, KMS has a high likelihood
13246 * of triggering bugs in unclaimed access. After we finish
13247 * modesetting, see if an error has been flagged, and if so
13248 * enable debugging for the next modeset - and hope we catch
13249 * the culprit.
13250 *
13251 * XXX note that we assume display power is on at this point.
13252 * This might hold true now but we need to add pm helper to check
13253 * unclaimed only when the hardware is on, as atomic commits
13254 * can happen also when the device is completely off.
13255 */
13256 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
13257
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013258 return 0;
Daniel Vetterf30da182013-04-11 20:22:50 +020013259}
13260
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013261void intel_crtc_restore_mode(struct drm_crtc *crtc)
13262{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013263 struct drm_device *dev = crtc->dev;
13264 struct drm_atomic_state *state;
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013265 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013266 int ret;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013267
13268 state = drm_atomic_state_alloc(dev);
13269 if (!state) {
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013270 DRM_DEBUG_KMS("[CRTC:%d] crtc restore failed, out of memory",
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013271 crtc->base.id);
13272 return;
13273 }
13274
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013275 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013276
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013277retry:
13278 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13279 ret = PTR_ERR_OR_ZERO(crtc_state);
13280 if (!ret) {
13281 if (!crtc_state->active)
13282 goto out;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013283
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013284 crtc_state->mode_changed = true;
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013285 ret = drm_atomic_commit(state);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013286 }
13287
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013288 if (ret == -EDEADLK) {
13289 drm_atomic_state_clear(state);
13290 drm_modeset_backoff(state->acquire_ctx);
13291 goto retry;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013292 }
13293
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013294 if (ret)
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013295out:
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013296 drm_atomic_state_free(state);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013297}
13298
Daniel Vetter25c5b262012-07-08 22:08:04 +020013299#undef for_each_intel_crtc_masked
13300
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013301static const struct drm_crtc_funcs intel_crtc_funcs = {
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000013302 .gamma_set = drm_atomic_helper_legacy_gamma_set,
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013303 .set_config = drm_atomic_helper_set_config,
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000013304 .set_property = drm_atomic_helper_crtc_set_property,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013305 .destroy = intel_crtc_destroy,
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020013306 .page_flip = drm_atomic_helper_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080013307 .atomic_duplicate_state = intel_crtc_duplicate_state,
13308 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013309};
13310
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020013311static struct fence *intel_get_excl_fence(struct drm_i915_gem_object *obj)
13312{
13313 struct reservation_object *resv;
13314
13315
13316 if (!obj->base.dma_buf)
13317 return NULL;
13318
13319 resv = obj->base.dma_buf->resv;
13320
13321 /* For framebuffer backed by dmabuf, wait for fence */
13322 while (1) {
13323 struct fence *fence_excl, *ret = NULL;
13324
13325 rcu_read_lock();
13326
13327 fence_excl = rcu_dereference(resv->fence_excl);
13328 if (fence_excl)
13329 ret = fence_get_rcu(fence_excl);
13330
13331 rcu_read_unlock();
13332
13333 if (ret == fence_excl)
13334 return ret;
13335 }
13336}
13337
Matt Roper6beb8c232014-12-01 15:40:14 -080013338/**
13339 * intel_prepare_plane_fb - Prepare fb for usage on plane
13340 * @plane: drm plane to prepare for
13341 * @fb: framebuffer to prepare for presentation
13342 *
13343 * Prepares a framebuffer for usage on a display plane. Generally this
13344 * involves pinning the underlying object and updating the frontbuffer tracking
13345 * bits. Some older platforms need special physical address handling for
13346 * cursor planes.
13347 *
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013348 * Must be called with struct_mutex held.
13349 *
Matt Roper6beb8c232014-12-01 15:40:14 -080013350 * Returns 0 on success, negative error code on failure.
13351 */
13352int
13353intel_prepare_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013354 const struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070013355{
13356 struct drm_device *dev = plane->dev;
Maarten Lankhorst844f9112015-09-02 10:42:40 +020013357 struct drm_framebuffer *fb = new_state->fb;
Matt Roper6beb8c232014-12-01 15:40:14 -080013358 struct intel_plane *intel_plane = to_intel_plane(plane);
Matt Roper6beb8c232014-12-01 15:40:14 -080013359 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013360 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
Maarten Lankhorst15c86bd2016-05-17 15:08:03 +020013361 struct drm_crtc *crtc = new_state->crtc ?: plane->state->crtc;
Matt Roper6beb8c232014-12-01 15:40:14 -080013362 int ret = 0;
Matt Roper465c1202014-05-29 08:06:54 -070013363
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013364 if (!obj && !old_obj)
Matt Roper465c1202014-05-29 08:06:54 -070013365 return 0;
13366
Maarten Lankhorst15c86bd2016-05-17 15:08:03 +020013367 if (WARN_ON(!new_state->state) || WARN_ON(!crtc) ||
13368 WARN_ON(!to_intel_atomic_state(new_state->state)->work[to_intel_crtc(crtc)->pipe])) {
13369 if (WARN_ON(old_obj != obj))
13370 return -EINVAL;
13371
13372 return 0;
13373 }
13374
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013375 if (old_obj) {
13376 struct drm_crtc_state *crtc_state =
13377 drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
13378
13379 /* Big Hammer, we also need to ensure that any pending
13380 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
13381 * current scanout is retired before unpinning the old
13382 * framebuffer. Note that we rely on userspace rendering
13383 * into the buffer attached to the pipe they are waiting
13384 * on. If not, userspace generates a GPU hang with IPEHR
13385 * point to the MI_WAIT_FOR_EVENT.
13386 *
13387 * This should only fail upon a hung GPU, in which case we
13388 * can safely continue.
13389 */
13390 if (needs_modeset(crtc_state))
13391 ret = i915_gem_object_wait_rendering(old_obj, true);
Chris Wilsonf4457ae2016-04-13 17:35:08 +010013392 if (ret) {
13393 /* GPU hangs should have been swallowed by the wait */
13394 WARN_ON(ret == -EIO);
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013395 return ret;
Chris Wilsonf4457ae2016-04-13 17:35:08 +010013396 }
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013397 }
13398
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013399 if (!obj) {
13400 ret = 0;
13401 } else if (plane->type == DRM_PLANE_TYPE_CURSOR &&
Matt Roper6beb8c232014-12-01 15:40:14 -080013402 INTEL_INFO(dev)->cursor_needs_physical) {
13403 int align = IS_I830(dev) ? 16 * 1024 : 256;
13404 ret = i915_gem_object_attach_phys(obj, align);
13405 if (ret)
13406 DRM_DEBUG_KMS("failed to attach phys object\n");
13407 } else {
Ville Syrjälä3465c582016-02-15 22:54:43 +020013408 ret = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
Matt Roper6beb8c232014-12-01 15:40:14 -080013409 }
13410
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013411 if (ret == 0) {
13412 if (obj) {
13413 struct intel_plane_state *plane_state =
13414 to_intel_plane_state(new_state);
13415
13416 i915_gem_request_assign(&plane_state->wait_req,
13417 obj->last_write_req);
Maarten Lankhorst84fc4942016-05-17 15:07:53 +020013418
13419 plane_state->base.fence = intel_get_excl_fence(obj);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013420 }
13421
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030013422 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013423 }
Matt Roper6beb8c232014-12-01 15:40:14 -080013424
Matt Roper6beb8c232014-12-01 15:40:14 -080013425 return ret;
13426}
13427
Matt Roper38f3ce32014-12-02 07:45:25 -080013428/**
13429 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13430 * @plane: drm plane to clean up for
13431 * @fb: old framebuffer that was on plane
13432 *
13433 * Cleans up a framebuffer that has just been removed from a plane.
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013434 *
13435 * Must be called with struct_mutex held.
Matt Roper38f3ce32014-12-02 07:45:25 -080013436 */
13437void
13438intel_cleanup_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013439 const struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080013440{
13441 struct drm_device *dev = plane->dev;
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013442 struct intel_plane *intel_plane = to_intel_plane(plane);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013443 struct intel_plane_state *old_intel_state;
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013444 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
13445 struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
Matt Roper38f3ce32014-12-02 07:45:25 -080013446
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013447 old_intel_state = to_intel_plane_state(old_state);
13448
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013449 if (!obj && !old_obj)
Matt Roper38f3ce32014-12-02 07:45:25 -080013450 return;
13451
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013452 if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
13453 !INTEL_INFO(dev)->cursor_needs_physical))
Ville Syrjälä3465c582016-02-15 22:54:43 +020013454 intel_unpin_fb_obj(old_state->fb, old_state->rotation);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013455
13456 /* prepare_fb aborted? */
13457 if ((old_obj && (old_obj->frontbuffer_bits & intel_plane->frontbuffer_bit)) ||
13458 (obj && !(obj->frontbuffer_bits & intel_plane->frontbuffer_bit)))
13459 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013460
13461 i915_gem_request_assign(&old_intel_state->wait_req, NULL);
Maarten Lankhorst84fc4942016-05-17 15:07:53 +020013462
13463 fence_put(old_intel_state->base.fence);
13464 old_intel_state->base.fence = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070013465}
13466
Chandra Konduru6156a452015-04-27 13:48:39 -070013467int
13468skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13469{
13470 int max_scale;
13471 struct drm_device *dev;
13472 struct drm_i915_private *dev_priv;
13473 int crtc_clock, cdclk;
13474
Maarten Lankhorstbf8a0af2015-11-24 11:29:02 +010013475 if (!intel_crtc || !crtc_state->base.enable)
Chandra Konduru6156a452015-04-27 13:48:39 -070013476 return DRM_PLANE_HELPER_NO_SCALING;
13477
13478 dev = intel_crtc->base.dev;
13479 dev_priv = dev->dev_private;
13480 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013481 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
Chandra Konduru6156a452015-04-27 13:48:39 -070013482
Tvrtko Ursulin54bf1ce2015-10-20 17:17:07 +010013483 if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
Chandra Konduru6156a452015-04-27 13:48:39 -070013484 return DRM_PLANE_HELPER_NO_SCALING;
13485
13486 /*
13487 * skl max scale is lower of:
13488 * close to 3 but not 3, -1 is for that purpose
13489 * or
13490 * cdclk/crtc_clock
13491 */
13492 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
13493
13494 return max_scale;
13495}
13496
Matt Roper465c1202014-05-29 08:06:54 -070013497static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013498intel_check_primary_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013499 struct intel_crtc_state *crtc_state,
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013500 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070013501{
Matt Roper2b875c22014-12-01 15:40:13 -080013502 struct drm_crtc *crtc = state->base.crtc;
13503 struct drm_framebuffer *fb = state->base.fb;
Chandra Konduru6156a452015-04-27 13:48:39 -070013504 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013505 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13506 bool can_position = false;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013507
Ville Syrjälä693bdc22016-01-15 20:46:53 +020013508 if (INTEL_INFO(plane->dev)->gen >= 9) {
13509 /* use scaler when colorkey is not required */
13510 if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
13511 min_scale = 1;
13512 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
13513 }
Sonika Jindald8106362015-04-10 14:37:28 +053013514 can_position = true;
Chandra Konduru6156a452015-04-27 13:48:39 -070013515 }
Sonika Jindald8106362015-04-10 14:37:28 +053013516
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013517 return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
13518 &state->dst, &state->clip,
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020013519 min_scale, max_scale,
13520 can_position, true,
13521 &state->visible);
Matt Roper465c1202014-05-29 08:06:54 -070013522}
13523
Matt Ropercf4c7c12014-12-04 10:27:42 -080013524/**
Matt Roper4a3b8762014-12-23 10:41:51 -080013525 * intel_plane_destroy - destroy a plane
13526 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080013527 *
Matt Roper4a3b8762014-12-23 10:41:51 -080013528 * Common destruction function for all types of planes (primary, cursor,
13529 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080013530 */
Matt Roper4a3b8762014-12-23 10:41:51 -080013531void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070013532{
13533 struct intel_plane *intel_plane = to_intel_plane(plane);
13534 drm_plane_cleanup(plane);
13535 kfree(intel_plane);
13536}
13537
Matt Roper65a3fea2015-01-21 16:35:42 -080013538const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070013539 .update_plane = drm_atomic_helper_update_plane,
13540 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070013541 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080013542 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080013543 .atomic_get_property = intel_plane_atomic_get_property,
13544 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080013545 .atomic_duplicate_state = intel_plane_duplicate_state,
13546 .atomic_destroy_state = intel_plane_destroy_state,
13547
Matt Roper465c1202014-05-29 08:06:54 -070013548};
13549
13550static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
13551 int pipe)
13552{
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013553 struct intel_plane *primary = NULL;
13554 struct intel_plane_state *state = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070013555 const uint32_t *intel_primary_formats;
Thierry Reding45e37432015-08-12 16:54:28 +020013556 unsigned int num_formats;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013557 int ret;
Matt Roper465c1202014-05-29 08:06:54 -070013558
13559 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013560 if (!primary)
13561 goto fail;
Matt Roper465c1202014-05-29 08:06:54 -070013562
Matt Roper8e7d6882015-01-21 16:35:41 -080013563 state = intel_create_plane_state(&primary->base);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013564 if (!state)
13565 goto fail;
Matt Roper8e7d6882015-01-21 16:35:41 -080013566 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080013567
Matt Roper465c1202014-05-29 08:06:54 -070013568 primary->can_scale = false;
13569 primary->max_downscale = 1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013570 if (INTEL_INFO(dev)->gen >= 9) {
13571 primary->can_scale = true;
Chandra Konduruaf99ced2015-05-11 14:35:47 -070013572 state->scaler_id = -1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013573 }
Matt Roper465c1202014-05-29 08:06:54 -070013574 primary->pipe = pipe;
13575 primary->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030013576 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080013577 primary->check_plane = intel_check_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070013578 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
13579 primary->plane = !pipe;
13580
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013581 if (INTEL_INFO(dev)->gen >= 9) {
13582 intel_primary_formats = skl_primary_formats;
13583 num_formats = ARRAY_SIZE(skl_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010013584
13585 primary->update_plane = skylake_update_primary_plane;
13586 primary->disable_plane = skylake_disable_primary_plane;
13587 } else if (HAS_PCH_SPLIT(dev)) {
13588 intel_primary_formats = i965_primary_formats;
13589 num_formats = ARRAY_SIZE(i965_primary_formats);
13590
13591 primary->update_plane = ironlake_update_primary_plane;
13592 primary->disable_plane = i9xx_disable_primary_plane;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013593 } else if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau568db4f2015-05-12 16:13:18 +010013594 intel_primary_formats = i965_primary_formats;
13595 num_formats = ARRAY_SIZE(i965_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010013596
13597 primary->update_plane = i9xx_update_primary_plane;
13598 primary->disable_plane = i9xx_disable_primary_plane;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013599 } else {
13600 intel_primary_formats = i8xx_primary_formats;
13601 num_formats = ARRAY_SIZE(i8xx_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010013602
13603 primary->update_plane = i9xx_update_primary_plane;
13604 primary->disable_plane = i9xx_disable_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070013605 }
13606
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013607 ret = drm_universal_plane_init(dev, &primary->base, 0,
13608 &intel_plane_funcs,
13609 intel_primary_formats, num_formats,
13610 DRM_PLANE_TYPE_PRIMARY, NULL);
13611 if (ret)
13612 goto fail;
Sonika Jindal48404c12014-08-22 14:06:04 +053013613
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013614 if (INTEL_INFO(dev)->gen >= 4)
13615 intel_create_rotation_property(dev, primary);
Sonika Jindal48404c12014-08-22 14:06:04 +053013616
Matt Roperea2c67b2014-12-23 10:41:52 -080013617 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
13618
Matt Roper465c1202014-05-29 08:06:54 -070013619 return &primary->base;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013620
13621fail:
13622 kfree(state);
13623 kfree(primary);
13624
13625 return NULL;
Matt Roper465c1202014-05-29 08:06:54 -070013626}
13627
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013628void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
13629{
13630 if (!dev->mode_config.rotation_property) {
13631 unsigned long flags = BIT(DRM_ROTATE_0) |
13632 BIT(DRM_ROTATE_180);
13633
13634 if (INTEL_INFO(dev)->gen >= 9)
13635 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
13636
13637 dev->mode_config.rotation_property =
13638 drm_mode_create_rotation_property(dev, flags);
13639 }
13640 if (dev->mode_config.rotation_property)
13641 drm_object_attach_property(&plane->base.base,
13642 dev->mode_config.rotation_property,
13643 plane->base.state->rotation);
13644}
13645
Matt Roper3d7d6512014-06-10 08:28:13 -070013646static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030013647intel_check_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013648 struct intel_crtc_state *crtc_state,
Gustavo Padovan852e7872014-09-05 17:22:31 -030013649 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070013650{
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013651 struct drm_crtc *crtc = crtc_state->base.crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080013652 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013653 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Ville Syrjäläb29ec922015-12-18 19:24:39 +020013654 enum pipe pipe = to_intel_plane(plane)->pipe;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013655 unsigned stride;
13656 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013657
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013658 ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
13659 &state->dst, &state->clip,
Gustavo Padovan852e7872014-09-05 17:22:31 -030013660 DRM_PLANE_HELPER_NO_SCALING,
13661 DRM_PLANE_HELPER_NO_SCALING,
13662 true, true, &state->visible);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013663 if (ret)
13664 return ret;
13665
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013666 /* if we want to turn off the cursor ignore width and height */
13667 if (!obj)
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020013668 return 0;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013669
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013670 /* Check for which cursor types we support */
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013671 if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013672 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
13673 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013674 return -EINVAL;
13675 }
13676
Matt Roperea2c67b2014-12-23 10:41:52 -080013677 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
13678 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013679 DRM_DEBUG_KMS("buffer is too small\n");
13680 return -ENOMEM;
13681 }
13682
Ville Syrjälä3a656b52015-03-09 21:08:37 +020013683 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013684 DRM_DEBUG_KMS("cursor cannot be tiled\n");
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020013685 return -EINVAL;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013686 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013687
Ville Syrjäläb29ec922015-12-18 19:24:39 +020013688 /*
13689 * There's something wrong with the cursor on CHV pipe C.
13690 * If it straddles the left edge of the screen then
13691 * moving it away from the edge or disabling it often
13692 * results in a pipe underrun, and often that can lead to
13693 * dead pipe (constant underrun reported, and it scans
13694 * out just a solid color). To recover from that, the
13695 * display power well must be turned off and on again.
13696 * Refuse the put the cursor into that compromised position.
13697 */
13698 if (IS_CHERRYVIEW(plane->dev) && pipe == PIPE_C &&
13699 state->visible && state->base.crtc_x < 0) {
13700 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
13701 return -EINVAL;
13702 }
13703
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020013704 return 0;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013705}
13706
Matt Roperf4a2cf22014-12-01 15:40:12 -080013707static void
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013708intel_disable_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst7fabf5e2015-06-15 12:33:47 +020013709 struct drm_crtc *crtc)
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013710{
Maarten Lankhorstf2858022016-01-07 11:54:09 +010013711 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13712
13713 intel_crtc->cursor_addr = 0;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010013714 intel_crtc_update_cursor(crtc, NULL);
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013715}
13716
13717static void
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010013718intel_update_cursor_plane(struct drm_plane *plane,
13719 const struct intel_crtc_state *crtc_state,
13720 const struct intel_plane_state *state)
Gustavo Padovan852e7872014-09-05 17:22:31 -030013721{
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010013722 struct drm_crtc *crtc = crtc_state->base.crtc;
13723 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roperea2c67b2014-12-23 10:41:52 -080013724 struct drm_device *dev = plane->dev;
Matt Roper2b875c22014-12-01 15:40:13 -080013725 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080013726 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070013727
Matt Roperf4a2cf22014-12-01 15:40:12 -080013728 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080013729 addr = 0;
Matt Roperf4a2cf22014-12-01 15:40:12 -080013730 else if (!INTEL_INFO(dev)->cursor_needs_physical)
Gustavo Padovana912f122014-12-01 15:40:10 -080013731 addr = i915_gem_obj_ggtt_offset(obj);
Matt Roperf4a2cf22014-12-01 15:40:12 -080013732 else
Gustavo Padovana912f122014-12-01 15:40:10 -080013733 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080013734
Gustavo Padovana912f122014-12-01 15:40:10 -080013735 intel_crtc->cursor_addr = addr;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010013736 intel_crtc_update_cursor(crtc, state);
Matt Roper3d7d6512014-06-10 08:28:13 -070013737}
Gustavo Padovan852e7872014-09-05 17:22:31 -030013738
Matt Roper3d7d6512014-06-10 08:28:13 -070013739static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
13740 int pipe)
13741{
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013742 struct intel_plane *cursor = NULL;
13743 struct intel_plane_state *state = NULL;
13744 int ret;
Matt Roper3d7d6512014-06-10 08:28:13 -070013745
13746 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013747 if (!cursor)
13748 goto fail;
Matt Roper3d7d6512014-06-10 08:28:13 -070013749
Matt Roper8e7d6882015-01-21 16:35:41 -080013750 state = intel_create_plane_state(&cursor->base);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013751 if (!state)
13752 goto fail;
Matt Roper8e7d6882015-01-21 16:35:41 -080013753 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080013754
Matt Roper3d7d6512014-06-10 08:28:13 -070013755 cursor->can_scale = false;
13756 cursor->max_downscale = 1;
13757 cursor->pipe = pipe;
13758 cursor->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030013759 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080013760 cursor->check_plane = intel_check_cursor_plane;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010013761 cursor->update_plane = intel_update_cursor_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013762 cursor->disable_plane = intel_disable_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070013763
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013764 ret = drm_universal_plane_init(dev, &cursor->base, 0,
13765 &intel_plane_funcs,
13766 intel_cursor_formats,
13767 ARRAY_SIZE(intel_cursor_formats),
13768 DRM_PLANE_TYPE_CURSOR, NULL);
13769 if (ret)
13770 goto fail;
Ville Syrjälä4398ad42014-10-23 07:41:34 -070013771
13772 if (INTEL_INFO(dev)->gen >= 4) {
13773 if (!dev->mode_config.rotation_property)
13774 dev->mode_config.rotation_property =
13775 drm_mode_create_rotation_property(dev,
13776 BIT(DRM_ROTATE_0) |
13777 BIT(DRM_ROTATE_180));
13778 if (dev->mode_config.rotation_property)
13779 drm_object_attach_property(&cursor->base.base,
13780 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080013781 state->base.rotation);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070013782 }
13783
Chandra Konduruaf99ced2015-05-11 14:35:47 -070013784 if (INTEL_INFO(dev)->gen >=9)
13785 state->scaler_id = -1;
13786
Matt Roperea2c67b2014-12-23 10:41:52 -080013787 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
13788
Matt Roper3d7d6512014-06-10 08:28:13 -070013789 return &cursor->base;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013790
13791fail:
13792 kfree(state);
13793 kfree(cursor);
13794
13795 return NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070013796}
13797
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013798static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
13799 struct intel_crtc_state *crtc_state)
13800{
13801 int i;
13802 struct intel_scaler *intel_scaler;
13803 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
13804
13805 for (i = 0; i < intel_crtc->num_scalers; i++) {
13806 intel_scaler = &scaler_state->scalers[i];
13807 intel_scaler->in_use = 0;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013808 intel_scaler->mode = PS_SCALER_MODE_DYN;
13809 }
13810
13811 scaler_state->scaler_id = -1;
13812}
13813
Hannes Ederb358d0a2008-12-18 21:18:47 +010013814static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080013815{
Jani Nikulafbee40d2014-03-31 14:27:18 +030013816 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080013817 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013818 struct intel_crtc_state *crtc_state = NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070013819 struct drm_plane *primary = NULL;
13820 struct drm_plane *cursor = NULL;
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +000013821 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080013822
Daniel Vetter955382f2013-09-19 14:05:45 +020013823 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080013824 if (intel_crtc == NULL)
13825 return;
13826
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013827 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
13828 if (!crtc_state)
13829 goto fail;
Ander Conselvan de Oliveira550acef2015-04-21 17:13:24 +030013830 intel_crtc->config = crtc_state;
13831 intel_crtc->base.state = &crtc_state->base;
Matt Roper07878242015-02-25 11:43:26 -080013832 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013833
Maarten Lankhorst68858432016-05-17 15:07:52 +020013834 INIT_LIST_HEAD(&intel_crtc->flip_work);
13835
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013836 /* initialize shared scalers */
13837 if (INTEL_INFO(dev)->gen >= 9) {
13838 if (pipe == PIPE_C)
13839 intel_crtc->num_scalers = 1;
13840 else
13841 intel_crtc->num_scalers = SKL_NUM_SCALERS;
13842
13843 skl_init_scalers(dev, intel_crtc, crtc_state);
13844 }
13845
Matt Roper465c1202014-05-29 08:06:54 -070013846 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070013847 if (!primary)
13848 goto fail;
13849
13850 cursor = intel_cursor_plane_create(dev, pipe);
13851 if (!cursor)
13852 goto fail;
13853
Matt Roper465c1202014-05-29 08:06:54 -070013854 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Ville Syrjäläf9882872015-12-09 16:19:31 +020013855 cursor, &intel_crtc_funcs, NULL);
Matt Roper3d7d6512014-06-10 08:28:13 -070013856 if (ret)
13857 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080013858
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020013859 /*
13860 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020013861 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020013862 */
Jesse Barnes80824002009-09-10 15:28:06 -070013863 intel_crtc->pipe = pipe;
13864 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010013865 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080013866 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010013867 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070013868 }
13869
Chris Wilson4b0e3332014-05-30 16:35:26 +030013870 intel_crtc->cursor_base = ~0;
13871 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030013872 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030013873
Ville Syrjälä852eb002015-06-24 22:00:07 +030013874 intel_crtc->wm.cxsr_allowed = true;
13875
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080013876 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
13877 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
13878 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
13879 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
13880
Jesse Barnes79e53942008-11-07 14:24:08 -080013881 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020013882
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +000013883 intel_color_init(&intel_crtc->base);
13884
Daniel Vetter87b6b102014-05-15 15:33:46 +020013885 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070013886 return;
13887
13888fail:
13889 if (primary)
13890 drm_plane_cleanup(primary);
13891 if (cursor)
13892 drm_plane_cleanup(cursor);
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013893 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070013894 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080013895}
13896
Jesse Barnes752aa882013-10-31 18:55:49 +020013897enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
13898{
13899 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020013900 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020013901
Rob Clark51fd3712013-11-19 12:10:12 -050013902 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020013903
Ville Syrjäläd3babd32014-11-07 11:16:01 +020013904 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020013905 return INVALID_PIPE;
13906
13907 return to_intel_crtc(encoder->crtc)->pipe;
13908}
13909
Carl Worth08d7b3d2009-04-29 14:43:54 -070013910int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000013911 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070013912{
Carl Worth08d7b3d2009-04-29 14:43:54 -070013913 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040013914 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020013915 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013916
Rob Clark7707e652014-07-17 23:30:04 -040013917 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Carl Worth08d7b3d2009-04-29 14:43:54 -070013918
Rob Clark7707e652014-07-17 23:30:04 -040013919 if (!drmmode_crtc) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070013920 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030013921 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013922 }
13923
Rob Clark7707e652014-07-17 23:30:04 -040013924 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020013925 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013926
Daniel Vetterc05422d2009-08-11 16:05:30 +020013927 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013928}
13929
Daniel Vetter66a92782012-07-12 20:08:18 +020013930static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080013931{
Daniel Vetter66a92782012-07-12 20:08:18 +020013932 struct drm_device *dev = encoder->base.dev;
13933 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080013934 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080013935 int entry = 0;
13936
Damien Lespiaub2784e12014-08-05 11:29:37 +010013937 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020013938 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020013939 index_mask |= (1 << entry);
13940
Jesse Barnes79e53942008-11-07 14:24:08 -080013941 entry++;
13942 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010013943
Jesse Barnes79e53942008-11-07 14:24:08 -080013944 return index_mask;
13945}
13946
Chris Wilson4d302442010-12-14 19:21:29 +000013947static bool has_edp_a(struct drm_device *dev)
13948{
13949 struct drm_i915_private *dev_priv = dev->dev_private;
13950
13951 if (!IS_MOBILE(dev))
13952 return false;
13953
13954 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
13955 return false;
13956
Damien Lespiaue3589902014-02-07 19:12:50 +000013957 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000013958 return false;
13959
13960 return true;
13961}
13962
Jesse Barnes84b4e042014-06-25 08:24:29 -070013963static bool intel_crt_present(struct drm_device *dev)
13964{
13965 struct drm_i915_private *dev_priv = dev->dev_private;
13966
Damien Lespiau884497e2013-12-03 13:56:23 +000013967 if (INTEL_INFO(dev)->gen >= 9)
13968 return false;
13969
Damien Lespiaucf404ce2014-10-01 20:04:15 +010013970 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
Jesse Barnes84b4e042014-06-25 08:24:29 -070013971 return false;
13972
13973 if (IS_CHERRYVIEW(dev))
13974 return false;
13975
Ville Syrjälä65e472e2015-12-01 23:28:55 +020013976 if (HAS_PCH_LPT_H(dev) && I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
13977 return false;
13978
Ville Syrjälä70ac54d2015-12-01 23:29:56 +020013979 /* DDI E can't be used if DDI A requires 4 lanes */
13980 if (HAS_DDI(dev) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
13981 return false;
13982
Ville Syrjäläe4abb732015-12-01 23:31:33 +020013983 if (!dev_priv->vbt.int_crt_support)
Jesse Barnes84b4e042014-06-25 08:24:29 -070013984 return false;
13985
13986 return true;
13987}
13988
Jesse Barnes79e53942008-11-07 14:24:08 -080013989static void intel_setup_outputs(struct drm_device *dev)
13990{
Eric Anholt725e30a2009-01-22 13:01:02 -080013991 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010013992 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013993 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080013994
Daniel Vetterc9093352013-06-06 22:22:47 +020013995 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080013996
Jesse Barnes84b4e042014-06-25 08:24:29 -070013997 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020013998 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013999
Vandana Kannanc776eb22014-08-19 12:05:01 +053014000 if (IS_BROXTON(dev)) {
14001 /*
14002 * FIXME: Broxton doesn't support port detection via the
14003 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14004 * detect the ports.
14005 */
14006 intel_ddi_init(dev, PORT_A);
14007 intel_ddi_init(dev, PORT_B);
14008 intel_ddi_init(dev, PORT_C);
Shashank Sharmac6c794a2016-03-22 12:01:50 +020014009
14010 intel_dsi_init(dev);
Vandana Kannanc776eb22014-08-19 12:05:01 +053014011 } else if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014012 int found;
14013
Jesse Barnesde31fac2015-03-06 15:53:32 -080014014 /*
14015 * Haswell uses DDI functions to detect digital outputs.
14016 * On SKL pre-D0 the strap isn't connected, so we assume
14017 * it's there.
14018 */
Ville Syrjälä77179402015-09-18 20:03:35 +030014019 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080014020 /* WaIgnoreDDIAStrap: skl */
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070014021 if (found || IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014022 intel_ddi_init(dev, PORT_A);
14023
14024 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14025 * register */
14026 found = I915_READ(SFUSE_STRAP);
14027
14028 if (found & SFUSE_STRAP_DDIB_DETECTED)
14029 intel_ddi_init(dev, PORT_B);
14030 if (found & SFUSE_STRAP_DDIC_DETECTED)
14031 intel_ddi_init(dev, PORT_C);
14032 if (found & SFUSE_STRAP_DDID_DETECTED)
14033 intel_ddi_init(dev, PORT_D);
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014034 /*
14035 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
14036 */
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070014037 if ((IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) &&
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014038 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
14039 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
14040 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
14041 intel_ddi_init(dev, PORT_E);
14042
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014043 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014044 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020014045 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020014046
14047 if (has_edp_a(dev))
14048 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014049
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014050 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080014051 /* PCH SDVOB multiplex with HDMIB */
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014052 found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014053 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014054 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014055 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014056 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014057 }
14058
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014059 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014060 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014061
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014062 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014063 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014064
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014065 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014066 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014067
Daniel Vetter270b3042012-10-27 15:52:05 +020014068 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014069 intel_dp_init(dev, PCH_DP_D, PORT_D);
Wayne Boyer666a4532015-12-09 12:29:35 -080014070 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014071 /*
14072 * The DP_DETECTED bit is the latched state of the DDC
14073 * SDA pin at boot. However since eDP doesn't require DDC
14074 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14075 * eDP ports may have been muxed to an alternate function.
14076 * Thus we can't rely on the DP_DETECTED bit alone to detect
14077 * eDP ports. Consult the VBT as well as DP_DETECTED to
14078 * detect eDP ports.
14079 */
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014080 if (I915_READ(VLV_HDMIB) & SDVO_DETECTED &&
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014081 !intel_dp_is_edp(dev, PORT_B))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014082 intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
14083 if (I915_READ(VLV_DP_B) & DP_DETECTED ||
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014084 intel_dp_is_edp(dev, PORT_B))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014085 intel_dp_init(dev, VLV_DP_B, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014086
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014087 if (I915_READ(VLV_HDMIC) & SDVO_DETECTED &&
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014088 !intel_dp_is_edp(dev, PORT_C))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014089 intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
14090 if (I915_READ(VLV_DP_C) & DP_DETECTED ||
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014091 intel_dp_is_edp(dev, PORT_C))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014092 intel_dp_init(dev, VLV_DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053014093
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014094 if (IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014095 /* eDP not supported on port D, so don't check VBT */
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014096 if (I915_READ(CHV_HDMID) & SDVO_DETECTED)
14097 intel_hdmi_init(dev, CHV_HDMID, PORT_D);
14098 if (I915_READ(CHV_DP_D) & DP_DETECTED)
14099 intel_dp_init(dev, CHV_DP_D, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014100 }
14101
Jani Nikula3cfca972013-08-27 15:12:26 +030014102 intel_dsi_init(dev);
Daniel Vetter09da55d2015-07-07 11:44:32 +020014103 } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014104 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080014105
Paulo Zanonie2debe92013-02-18 19:00:27 -030014106 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014107 DRM_DEBUG_KMS("probing SDVOB\n");
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014108 found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014109 if (!found && IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014110 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014111 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014112 }
Ma Ling27185ae2009-08-24 13:50:23 +080014113
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014114 if (!found && IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014115 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080014116 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014117
14118 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014119
Paulo Zanonie2debe92013-02-18 19:00:27 -030014120 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014121 DRM_DEBUG_KMS("probing SDVOC\n");
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014122 found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014123 }
Ma Ling27185ae2009-08-24 13:50:23 +080014124
Paulo Zanonie2debe92013-02-18 19:00:27 -030014125 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014126
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014127 if (IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014128 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014129 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014130 }
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014131 if (IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014132 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080014133 }
Ma Ling27185ae2009-08-24 13:50:23 +080014134
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014135 if (IS_G4X(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030014136 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014137 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070014138 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014139 intel_dvo_init(dev);
14140
Zhenyu Wang103a1962009-11-27 11:44:36 +080014141 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014142 intel_tv_init(dev);
14143
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -080014144 intel_psr_init(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070014145
Damien Lespiaub2784e12014-08-05 11:29:37 +010014146 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010014147 encoder->base.possible_crtcs = encoder->crtc_mask;
14148 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020014149 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080014150 }
Chris Wilson47356eb2011-01-11 17:06:04 +000014151
Paulo Zanonidde86e22012-12-01 12:04:25 -020014152 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020014153
14154 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014155}
14156
14157static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14158{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014159 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080014160 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080014161
Daniel Vetteref2d6332014-02-10 18:00:38 +010014162 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014163 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010014164 WARN_ON(!intel_fb->obj->framebuffer_references--);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014165 drm_gem_object_unreference(&intel_fb->obj->base);
14166 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080014167 kfree(intel_fb);
14168}
14169
14170static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000014171 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080014172 unsigned int *handle)
14173{
14174 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000014175 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014176
Chris Wilsoncc917ab2015-10-13 14:22:26 +010014177 if (obj->userptr.mm) {
14178 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
14179 return -EINVAL;
14180 }
14181
Chris Wilson05394f32010-11-08 19:18:58 +000014182 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080014183}
14184
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014185static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
14186 struct drm_file *file,
14187 unsigned flags, unsigned color,
14188 struct drm_clip_rect *clips,
14189 unsigned num_clips)
14190{
14191 struct drm_device *dev = fb->dev;
14192 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
14193 struct drm_i915_gem_object *obj = intel_fb->obj;
14194
14195 mutex_lock(&dev->struct_mutex);
Paulo Zanoni74b4ea12015-07-14 16:29:14 -030014196 intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014197 mutex_unlock(&dev->struct_mutex);
14198
14199 return 0;
14200}
14201
Jesse Barnes79e53942008-11-07 14:24:08 -080014202static const struct drm_framebuffer_funcs intel_fb_funcs = {
14203 .destroy = intel_user_framebuffer_destroy,
14204 .create_handle = intel_user_framebuffer_create_handle,
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014205 .dirty = intel_user_framebuffer_dirty,
Jesse Barnes79e53942008-11-07 14:24:08 -080014206};
14207
Damien Lespiaub3218032015-02-27 11:15:18 +000014208static
14209u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14210 uint32_t pixel_format)
14211{
14212 u32 gen = INTEL_INFO(dev)->gen;
14213
14214 if (gen >= 9) {
Ville Syrjäläac484962016-01-20 21:05:26 +020014215 int cpp = drm_format_plane_cpp(pixel_format, 0);
14216
Damien Lespiaub3218032015-02-27 11:15:18 +000014217 /* "The stride in bytes must not exceed the of the size of 8K
14218 * pixels and 32K bytes."
14219 */
Ville Syrjäläac484962016-01-20 21:05:26 +020014220 return min(8192 * cpp, 32768);
Wayne Boyer666a4532015-12-09 12:29:35 -080014221 } else if (gen >= 5 && !IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014222 return 32*1024;
14223 } else if (gen >= 4) {
14224 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14225 return 16*1024;
14226 else
14227 return 32*1024;
14228 } else if (gen >= 3) {
14229 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14230 return 8*1024;
14231 else
14232 return 16*1024;
14233 } else {
14234 /* XXX DSPC is limited to 4k tiled */
14235 return 8*1024;
14236 }
14237}
14238
Daniel Vetterb5ea6422014-03-02 21:18:00 +010014239static int intel_framebuffer_init(struct drm_device *dev,
14240 struct intel_framebuffer *intel_fb,
14241 struct drm_mode_fb_cmd2 *mode_cmd,
14242 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080014243{
Ville Syrjälä7b49f942016-01-12 21:08:32 +020014244 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +000014245 unsigned int aligned_height;
Jesse Barnes79e53942008-11-07 14:24:08 -080014246 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000014247 u32 pitch_limit, stride_alignment;
Jesse Barnes79e53942008-11-07 14:24:08 -080014248
Daniel Vetterdd4916c2013-10-09 21:23:51 +020014249 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14250
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014251 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14252 /* Enforce that fb modifier and tiling mode match, but only for
14253 * X-tiled. This is needed for FBC. */
14254 if (!!(obj->tiling_mode == I915_TILING_X) !=
14255 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14256 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14257 return -EINVAL;
14258 }
14259 } else {
14260 if (obj->tiling_mode == I915_TILING_X)
14261 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14262 else if (obj->tiling_mode == I915_TILING_Y) {
14263 DRM_DEBUG("No Y tiling for legacy addfb\n");
14264 return -EINVAL;
14265 }
14266 }
14267
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014268 /* Passed in modifier sanity checking. */
14269 switch (mode_cmd->modifier[0]) {
14270 case I915_FORMAT_MOD_Y_TILED:
14271 case I915_FORMAT_MOD_Yf_TILED:
14272 if (INTEL_INFO(dev)->gen < 9) {
14273 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14274 mode_cmd->modifier[0]);
14275 return -EINVAL;
14276 }
14277 case DRM_FORMAT_MOD_NONE:
14278 case I915_FORMAT_MOD_X_TILED:
14279 break;
14280 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070014281 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14282 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010014283 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014284 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014285
Ville Syrjälä7b49f942016-01-12 21:08:32 +020014286 stride_alignment = intel_fb_stride_alignment(dev_priv,
14287 mode_cmd->modifier[0],
Damien Lespiaub3218032015-02-27 11:15:18 +000014288 mode_cmd->pixel_format);
14289 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14290 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14291 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010014292 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014293 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014294
Damien Lespiaub3218032015-02-27 11:15:18 +000014295 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14296 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014297 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014298 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14299 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014300 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014301 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014302 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014303 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014304
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014305 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014306 mode_cmd->pitches[0] != obj->stride) {
14307 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14308 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014309 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014310 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014311
Ville Syrjälä57779d02012-10-31 17:50:14 +020014312 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014313 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020014314 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014315 case DRM_FORMAT_RGB565:
14316 case DRM_FORMAT_XRGB8888:
14317 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014318 break;
14319 case DRM_FORMAT_XRGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014320 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014321 DRM_DEBUG("unsupported pixel format: %s\n",
14322 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014323 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014324 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020014325 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +020014326 case DRM_FORMAT_ABGR8888:
Wayne Boyer666a4532015-12-09 12:29:35 -080014327 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
14328 INTEL_INFO(dev)->gen < 9) {
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014329 DRM_DEBUG("unsupported pixel format: %s\n",
14330 drm_get_format_name(mode_cmd->pixel_format));
14331 return -EINVAL;
14332 }
14333 break;
14334 case DRM_FORMAT_XBGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014335 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014336 case DRM_FORMAT_XBGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014337 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014338 DRM_DEBUG("unsupported pixel format: %s\n",
14339 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014340 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014341 }
Jesse Barnesb5626742011-06-24 12:19:27 -070014342 break;
Damien Lespiau75312082015-05-15 19:06:01 +010014343 case DRM_FORMAT_ABGR2101010:
Wayne Boyer666a4532015-12-09 12:29:35 -080014344 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Damien Lespiau75312082015-05-15 19:06:01 +010014345 DRM_DEBUG("unsupported pixel format: %s\n",
14346 drm_get_format_name(mode_cmd->pixel_format));
14347 return -EINVAL;
14348 }
14349 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020014350 case DRM_FORMAT_YUYV:
14351 case DRM_FORMAT_UYVY:
14352 case DRM_FORMAT_YVYU:
14353 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014354 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014355 DRM_DEBUG("unsupported pixel format: %s\n",
14356 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014357 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014358 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014359 break;
14360 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014361 DRM_DEBUG("unsupported pixel format: %s\n",
14362 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010014363 return -EINVAL;
14364 }
14365
Ville Syrjälä90f9a332012-10-31 17:50:19 +020014366 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14367 if (mode_cmd->offsets[0] != 0)
14368 return -EINVAL;
14369
Damien Lespiauec2c9812015-01-20 12:51:45 +000014370 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +000014371 mode_cmd->pixel_format,
14372 mode_cmd->modifier[0]);
Daniel Vetter53155c02013-10-09 21:55:33 +020014373 /* FIXME drm helper for size checks (especially planar formats)? */
14374 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14375 return -EINVAL;
14376
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014377 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14378 intel_fb->obj = obj;
14379
Ville Syrjälä2d7a2152016-02-15 22:54:47 +020014380 intel_fill_fb_info(dev_priv, &intel_fb->base);
14381
Jesse Barnes79e53942008-11-07 14:24:08 -080014382 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14383 if (ret) {
14384 DRM_ERROR("framebuffer init failed %d\n", ret);
14385 return ret;
14386 }
14387
Ville Syrjälä0b05e1e2016-01-14 15:22:09 +020014388 intel_fb->obj->framebuffer_references++;
14389
Jesse Barnes79e53942008-11-07 14:24:08 -080014390 return 0;
14391}
14392
Jesse Barnes79e53942008-11-07 14:24:08 -080014393static struct drm_framebuffer *
14394intel_user_framebuffer_create(struct drm_device *dev,
14395 struct drm_file *filp,
Ville Syrjälä1eb834512015-11-11 19:11:29 +020014396 const struct drm_mode_fb_cmd2 *user_mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080014397{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020014398 struct drm_framebuffer *fb;
Chris Wilson05394f32010-11-08 19:18:58 +000014399 struct drm_i915_gem_object *obj;
Ville Syrjälä76dc3762015-11-11 19:11:28 +020014400 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
Jesse Barnes79e53942008-11-07 14:24:08 -080014401
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014402 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
Ville Syrjälä76dc3762015-11-11 19:11:28 +020014403 mode_cmd.handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000014404 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010014405 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080014406
Daniel Vetter92907cb2015-11-23 09:04:05 +010014407 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020014408 if (IS_ERR(fb))
14409 drm_gem_object_unreference_unlocked(&obj->base);
14410
14411 return fb;
Jesse Barnes79e53942008-11-07 14:24:08 -080014412}
14413
Daniel Vetter06957262015-08-10 13:34:08 +020014414#ifndef CONFIG_DRM_FBDEV_EMULATION
Daniel Vetter0632fef2013-10-08 17:44:49 +020014415static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020014416{
14417}
14418#endif
14419
Jesse Barnes79e53942008-11-07 14:24:08 -080014420static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080014421 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020014422 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080014423 .atomic_check = intel_atomic_check,
14424 .atomic_commit = intel_atomic_commit,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +020014425 .atomic_state_alloc = intel_atomic_state_alloc,
14426 .atomic_state_clear = intel_atomic_state_clear,
Jesse Barnes79e53942008-11-07 14:24:08 -080014427};
14428
Imre Deak88212942016-03-16 13:38:53 +020014429/**
14430 * intel_init_display_hooks - initialize the display modesetting hooks
14431 * @dev_priv: device private
14432 */
14433void intel_init_display_hooks(struct drm_i915_private *dev_priv)
Jesse Barnese70236a2009-09-21 10:42:27 -070014434{
Imre Deak88212942016-03-16 13:38:53 +020014435 if (INTEL_INFO(dev_priv)->gen >= 9) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014436 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014437 dev_priv->display.get_initial_plane_config =
14438 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014439 dev_priv->display.crtc_compute_clock =
14440 haswell_crtc_compute_clock;
14441 dev_priv->display.crtc_enable = haswell_crtc_enable;
14442 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020014443 } else if (HAS_DDI(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014444 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014445 dev_priv->display.get_initial_plane_config =
14446 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020014447 dev_priv->display.crtc_compute_clock =
14448 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020014449 dev_priv->display.crtc_enable = haswell_crtc_enable;
14450 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020014451 } else if (HAS_PCH_SPLIT(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014452 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014453 dev_priv->display.get_initial_plane_config =
14454 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020014455 dev_priv->display.crtc_compute_clock =
14456 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014457 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14458 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +020014459 } else if (IS_CHERRYVIEW(dev_priv)) {
Jesse Barnes89b667f2013-04-18 14:51:36 -070014460 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014461 dev_priv->display.get_initial_plane_config =
14462 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +020014463 dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
14464 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14465 dev_priv->display.crtc_disable = i9xx_crtc_disable;
14466 } else if (IS_VALLEYVIEW(dev_priv)) {
14467 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14468 dev_priv->display.get_initial_plane_config =
14469 i9xx_get_initial_plane_config;
14470 dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014471 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14472 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +020014473 } else if (IS_G4X(dev_priv)) {
14474 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14475 dev_priv->display.get_initial_plane_config =
14476 i9xx_get_initial_plane_config;
14477 dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
14478 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14479 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +020014480 } else if (IS_PINEVIEW(dev_priv)) {
14481 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14482 dev_priv->display.get_initial_plane_config =
14483 i9xx_get_initial_plane_config;
14484 dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
14485 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14486 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +020014487 } else if (!IS_GEN2(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014488 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014489 dev_priv->display.get_initial_plane_config =
14490 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014491 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014492 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14493 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +020014494 } else {
14495 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14496 dev_priv->display.get_initial_plane_config =
14497 i9xx_get_initial_plane_config;
14498 dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
14499 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14500 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Eric Anholtf564048e2011-03-30 13:01:02 -070014501 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014502
Jesse Barnese70236a2009-09-21 10:42:27 -070014503 /* Returns the core display clock speed */
Imre Deak88212942016-03-16 13:38:53 +020014504 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030014505 dev_priv->display.get_display_clock_speed =
14506 skylake_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014507 else if (IS_BROXTON(dev_priv))
Bob Paauweacd3f3d2015-06-23 14:14:26 -070014508 dev_priv->display.get_display_clock_speed =
14509 broxton_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014510 else if (IS_BROADWELL(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030014511 dev_priv->display.get_display_clock_speed =
14512 broadwell_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014513 else if (IS_HASWELL(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030014514 dev_priv->display.get_display_clock_speed =
14515 haswell_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014516 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070014517 dev_priv->display.get_display_clock_speed =
14518 valleyview_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014519 else if (IS_GEN5(dev_priv))
Ville Syrjäläb37a6432015-03-31 14:11:54 +030014520 dev_priv->display.get_display_clock_speed =
14521 ilk_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014522 else if (IS_I945G(dev_priv) || IS_BROADWATER(dev_priv) ||
14523 IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014524 dev_priv->display.get_display_clock_speed =
14525 i945_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014526 else if (IS_GM45(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030014527 dev_priv->display.get_display_clock_speed =
14528 gm45_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014529 else if (IS_CRESTLINE(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030014530 dev_priv->display.get_display_clock_speed =
14531 i965gm_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014532 else if (IS_PINEVIEW(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030014533 dev_priv->display.get_display_clock_speed =
14534 pnv_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014535 else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030014536 dev_priv->display.get_display_clock_speed =
14537 g33_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014538 else if (IS_I915G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014539 dev_priv->display.get_display_clock_speed =
14540 i915_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014541 else if (IS_I945GM(dev_priv) || IS_845G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014542 dev_priv->display.get_display_clock_speed =
14543 i9xx_misc_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014544 else if (IS_I915GM(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014545 dev_priv->display.get_display_clock_speed =
14546 i915gm_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014547 else if (IS_I865G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014548 dev_priv->display.get_display_clock_speed =
14549 i865_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014550 else if (IS_I85X(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014551 dev_priv->display.get_display_clock_speed =
Ville Syrjälä1b1d2712015-05-22 11:22:31 +030014552 i85x_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030014553 else { /* 830 */
Imre Deak88212942016-03-16 13:38:53 +020014554 WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
Jesse Barnese70236a2009-09-21 10:42:27 -070014555 dev_priv->display.get_display_clock_speed =
14556 i830_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030014557 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014558
Imre Deak88212942016-03-16 13:38:53 +020014559 if (IS_GEN5(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014560 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020014561 } else if (IS_GEN6(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014562 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020014563 } else if (IS_IVYBRIDGE(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014564 /* FIXME: detect B0+ stepping and use auto training */
14565 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020014566 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014567 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Ville Syrjälä445e7802016-05-11 22:44:42 +030014568 }
14569
14570 if (IS_BROADWELL(dev_priv)) {
14571 dev_priv->display.modeset_commit_cdclk =
14572 broadwell_modeset_commit_cdclk;
14573 dev_priv->display.modeset_calc_cdclk =
14574 broadwell_modeset_calc_cdclk;
Imre Deak88212942016-03-16 13:38:53 +020014575 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014576 dev_priv->display.modeset_commit_cdclk =
14577 valleyview_modeset_commit_cdclk;
14578 dev_priv->display.modeset_calc_cdclk =
14579 valleyview_modeset_calc_cdclk;
Imre Deak88212942016-03-16 13:38:53 +020014580 } else if (IS_BROXTON(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014581 dev_priv->display.modeset_commit_cdclk =
14582 broxton_modeset_commit_cdclk;
14583 dev_priv->display.modeset_calc_cdclk =
14584 broxton_modeset_calc_cdclk;
Clint Taylorc89e39f2016-05-13 23:41:21 +030014585 } else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
14586 dev_priv->display.modeset_commit_cdclk =
14587 skl_modeset_commit_cdclk;
14588 dev_priv->display.modeset_calc_cdclk =
14589 skl_modeset_calc_cdclk;
Jesse Barnese70236a2009-09-21 10:42:27 -070014590 }
14591}
14592
Jesse Barnesb690e962010-07-19 13:53:12 -070014593/*
14594 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
14595 * resume, or other times. This quirk makes sure that's the case for
14596 * affected systems.
14597 */
Akshay Joshi0206e352011-08-16 15:34:10 -040014598static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070014599{
14600 struct drm_i915_private *dev_priv = dev->dev_private;
14601
14602 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014603 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014604}
14605
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014606static void quirk_pipeb_force(struct drm_device *dev)
14607{
14608 struct drm_i915_private *dev_priv = dev->dev_private;
14609
14610 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
14611 DRM_INFO("applying pipe b force quirk\n");
14612}
14613
Keith Packard435793d2011-07-12 14:56:22 -070014614/*
14615 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
14616 */
14617static void quirk_ssc_force_disable(struct drm_device *dev)
14618{
14619 struct drm_i915_private *dev_priv = dev->dev_private;
14620 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014621 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070014622}
14623
Carsten Emde4dca20e2012-03-15 15:56:26 +010014624/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010014625 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
14626 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010014627 */
14628static void quirk_invert_brightness(struct drm_device *dev)
14629{
14630 struct drm_i915_private *dev_priv = dev->dev_private;
14631 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014632 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014633}
14634
Scot Doyle9c72cc62014-07-03 23:27:50 +000014635/* Some VBT's incorrectly indicate no backlight is present */
14636static void quirk_backlight_present(struct drm_device *dev)
14637{
14638 struct drm_i915_private *dev_priv = dev->dev_private;
14639 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
14640 DRM_INFO("applying backlight present quirk\n");
14641}
14642
Jesse Barnesb690e962010-07-19 13:53:12 -070014643struct intel_quirk {
14644 int device;
14645 int subsystem_vendor;
14646 int subsystem_device;
14647 void (*hook)(struct drm_device *dev);
14648};
14649
Egbert Eich5f85f172012-10-14 15:46:38 +020014650/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
14651struct intel_dmi_quirk {
14652 void (*hook)(struct drm_device *dev);
14653 const struct dmi_system_id (*dmi_id_list)[];
14654};
14655
14656static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
14657{
14658 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
14659 return 1;
14660}
14661
14662static const struct intel_dmi_quirk intel_dmi_quirks[] = {
14663 {
14664 .dmi_id_list = &(const struct dmi_system_id[]) {
14665 {
14666 .callback = intel_dmi_reverse_brightness,
14667 .ident = "NCR Corporation",
14668 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
14669 DMI_MATCH(DMI_PRODUCT_NAME, ""),
14670 },
14671 },
14672 { } /* terminating entry */
14673 },
14674 .hook = quirk_invert_brightness,
14675 },
14676};
14677
Ben Widawskyc43b5632012-04-16 14:07:40 -070014678static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070014679 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
14680 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
14681
Jesse Barnesb690e962010-07-19 13:53:12 -070014682 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
14683 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
14684
Ville Syrjälä5f080c02014-08-15 01:22:06 +030014685 /* 830 needs to leave pipe A & dpll A up */
14686 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
14687
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014688 /* 830 needs to leave pipe B & dpll B up */
14689 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
14690
Keith Packard435793d2011-07-12 14:56:22 -070014691 /* Lenovo U160 cannot use SSC on LVDS */
14692 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020014693
14694 /* Sony Vaio Y cannot use SSC on LVDS */
14695 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010014696
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010014697 /* Acer Aspire 5734Z must invert backlight brightness */
14698 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
14699
14700 /* Acer/eMachines G725 */
14701 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
14702
14703 /* Acer/eMachines e725 */
14704 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
14705
14706 /* Acer/Packard Bell NCL20 */
14707 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
14708
14709 /* Acer Aspire 4736Z */
14710 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020014711
14712 /* Acer Aspire 5336 */
14713 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000014714
14715 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
14716 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000014717
Scot Doyledfb3d47b2014-08-21 16:08:02 +000014718 /* Acer C720 Chromebook (Core i3 4005U) */
14719 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
14720
jens steinb2a96012014-10-28 20:25:53 +010014721 /* Apple Macbook 2,1 (Core 2 T7400) */
14722 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
14723
Jani Nikula1b9448b02015-11-05 11:49:59 +020014724 /* Apple Macbook 4,1 */
14725 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
14726
Scot Doyled4967d82014-07-03 23:27:52 +000014727 /* Toshiba CB35 Chromebook (Celeron 2955U) */
14728 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000014729
14730 /* HP Chromebook 14 (Celeron 2955U) */
14731 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020014732
14733 /* Dell Chromebook 11 */
14734 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jani Nikula9be64ee2015-10-30 14:50:24 +020014735
14736 /* Dell Chromebook 11 (2015 version) */
14737 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070014738};
14739
14740static void intel_init_quirks(struct drm_device *dev)
14741{
14742 struct pci_dev *d = dev->pdev;
14743 int i;
14744
14745 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
14746 struct intel_quirk *q = &intel_quirks[i];
14747
14748 if (d->device == q->device &&
14749 (d->subsystem_vendor == q->subsystem_vendor ||
14750 q->subsystem_vendor == PCI_ANY_ID) &&
14751 (d->subsystem_device == q->subsystem_device ||
14752 q->subsystem_device == PCI_ANY_ID))
14753 q->hook(dev);
14754 }
Egbert Eich5f85f172012-10-14 15:46:38 +020014755 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
14756 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
14757 intel_dmi_quirks[i].hook(dev);
14758 }
Jesse Barnesb690e962010-07-19 13:53:12 -070014759}
14760
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014761/* Disable the VGA plane that we never use */
14762static void i915_disable_vga(struct drm_device *dev)
14763{
14764 struct drm_i915_private *dev_priv = dev->dev_private;
14765 u8 sr1;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020014766 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014767
Ville Syrjälä2b37c612014-01-22 21:32:38 +020014768 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014769 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070014770 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014771 sr1 = inb(VGA_SR_DATA);
14772 outb(sr1 | 1<<5, VGA_SR_DATA);
14773 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
14774 udelay(300);
14775
Ville Syrjälä01f5a622014-12-16 18:38:37 +020014776 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014777 POSTING_READ(vga_reg);
14778}
14779
Daniel Vetterf8175862012-04-10 15:50:11 +020014780void intel_modeset_init_hw(struct drm_device *dev)
14781{
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010014782 struct drm_i915_private *dev_priv = dev->dev_private;
14783
Ville Syrjäläb6283052015-06-03 15:45:07 +030014784 intel_update_cdclk(dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010014785
14786 dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
14787
Daniel Vetterf8175862012-04-10 15:50:11 +020014788 intel_init_clock_gating(dev);
Chris Wilsondc979972016-05-10 14:10:04 +010014789 intel_enable_gt_powersave(dev_priv);
Daniel Vetterf8175862012-04-10 15:50:11 +020014790}
14791
Matt Roperd93c0372015-12-03 11:37:41 -080014792/*
14793 * Calculate what we think the watermarks should be for the state we've read
14794 * out of the hardware and then immediately program those watermarks so that
14795 * we ensure the hardware settings match our internal state.
14796 *
14797 * We can calculate what we think WM's should be by creating a duplicate of the
14798 * current state (which was constructed during hardware readout) and running it
14799 * through the atomic check code to calculate new watermark values in the
14800 * state object.
14801 */
14802static void sanitize_watermarks(struct drm_device *dev)
14803{
14804 struct drm_i915_private *dev_priv = to_i915(dev);
14805 struct drm_atomic_state *state;
14806 struct drm_crtc *crtc;
14807 struct drm_crtc_state *cstate;
14808 struct drm_modeset_acquire_ctx ctx;
14809 int ret;
14810 int i;
14811
14812 /* Only supported on platforms that use atomic watermark design */
Matt Ropered4a6a72016-02-23 17:20:13 -080014813 if (!dev_priv->display.optimize_watermarks)
Matt Roperd93c0372015-12-03 11:37:41 -080014814 return;
14815
14816 /*
14817 * We need to hold connection_mutex before calling duplicate_state so
14818 * that the connector loop is protected.
14819 */
14820 drm_modeset_acquire_init(&ctx, 0);
14821retry:
Matt Roper0cd12622016-01-12 07:13:37 -080014822 ret = drm_modeset_lock_all_ctx(dev, &ctx);
Matt Roperd93c0372015-12-03 11:37:41 -080014823 if (ret == -EDEADLK) {
14824 drm_modeset_backoff(&ctx);
14825 goto retry;
14826 } else if (WARN_ON(ret)) {
Matt Roper0cd12622016-01-12 07:13:37 -080014827 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080014828 }
14829
14830 state = drm_atomic_helper_duplicate_state(dev, &ctx);
14831 if (WARN_ON(IS_ERR(state)))
Matt Roper0cd12622016-01-12 07:13:37 -080014832 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080014833
Matt Ropered4a6a72016-02-23 17:20:13 -080014834 /*
14835 * Hardware readout is the only time we don't want to calculate
14836 * intermediate watermarks (since we don't trust the current
14837 * watermarks).
14838 */
14839 to_intel_atomic_state(state)->skip_intermediate_wm = true;
14840
Matt Roperd93c0372015-12-03 11:37:41 -080014841 ret = intel_atomic_check(dev, state);
14842 if (ret) {
14843 /*
14844 * If we fail here, it means that the hardware appears to be
14845 * programmed in a way that shouldn't be possible, given our
14846 * understanding of watermark requirements. This might mean a
14847 * mistake in the hardware readout code or a mistake in the
14848 * watermark calculations for a given platform. Raise a WARN
14849 * so that this is noticeable.
14850 *
14851 * If this actually happens, we'll have to just leave the
14852 * BIOS-programmed watermarks untouched and hope for the best.
14853 */
14854 WARN(true, "Could not determine valid watermarks for inherited state\n");
Matt Roper0cd12622016-01-12 07:13:37 -080014855 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080014856 }
14857
14858 /* Write calculated watermark values back */
Matt Roperd93c0372015-12-03 11:37:41 -080014859 for_each_crtc_in_state(state, crtc, cstate, i) {
14860 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
14861
Matt Ropered4a6a72016-02-23 17:20:13 -080014862 cs->wm.need_postvbl_update = true;
14863 dev_priv->display.optimize_watermarks(cs);
Matt Roperd93c0372015-12-03 11:37:41 -080014864 }
14865
14866 drm_atomic_state_free(state);
Matt Roper0cd12622016-01-12 07:13:37 -080014867fail:
Matt Roperd93c0372015-12-03 11:37:41 -080014868 drm_modeset_drop_locks(&ctx);
14869 drm_modeset_acquire_fini(&ctx);
14870}
14871
Jesse Barnes79e53942008-11-07 14:24:08 -080014872void intel_modeset_init(struct drm_device *dev)
14873{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +030014874 struct drm_i915_private *dev_priv = to_i915(dev);
14875 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Damien Lespiau1fe47782014-03-03 17:31:47 +000014876 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000014877 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080014878 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080014879
14880 drm_mode_config_init(dev);
14881
14882 dev->mode_config.min_width = 0;
14883 dev->mode_config.min_height = 0;
14884
Dave Airlie019d96c2011-09-29 16:20:42 +010014885 dev->mode_config.preferred_depth = 24;
14886 dev->mode_config.prefer_shadow = 1;
14887
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000014888 dev->mode_config.allow_fb_modifiers = true;
14889
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020014890 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080014891
Jesse Barnesb690e962010-07-19 13:53:12 -070014892 intel_init_quirks(dev);
14893
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030014894 intel_init_pm(dev);
14895
Ben Widawskye3c74752013-04-05 13:12:39 -070014896 if (INTEL_INFO(dev)->num_pipes == 0)
14897 return;
14898
Lukas Wunner69f92f62015-07-15 13:57:35 +020014899 /*
14900 * There may be no VBT; and if the BIOS enabled SSC we can
14901 * just keep using it to avoid unnecessary flicker. Whereas if the
14902 * BIOS isn't using it, don't assume it will work even if the VBT
14903 * indicates as much.
14904 */
14905 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
14906 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
14907 DREF_SSC1_ENABLE);
14908
14909 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
14910 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
14911 bios_lvds_use_ssc ? "en" : "dis",
14912 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
14913 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
14914 }
14915 }
14916
Chris Wilsona6c45cf2010-09-17 00:32:17 +010014917 if (IS_GEN2(dev)) {
14918 dev->mode_config.max_width = 2048;
14919 dev->mode_config.max_height = 2048;
14920 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070014921 dev->mode_config.max_width = 4096;
14922 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080014923 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010014924 dev->mode_config.max_width = 8192;
14925 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080014926 }
Damien Lespiau068be562014-03-28 14:17:49 +000014927
Ville Syrjälädc41c152014-08-13 11:57:05 +030014928 if (IS_845G(dev) || IS_I865G(dev)) {
14929 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
14930 dev->mode_config.cursor_height = 1023;
14931 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000014932 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
14933 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
14934 } else {
14935 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
14936 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
14937 }
14938
Joonas Lahtinen72e96d62016-03-30 16:57:10 +030014939 dev->mode_config.fb_base = ggtt->mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080014940
Zhao Yakui28c97732009-10-09 11:39:41 +080014941 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070014942 INTEL_INFO(dev)->num_pipes,
14943 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080014944
Damien Lespiau055e3932014-08-18 13:49:10 +010014945 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000014946 intel_crtc_init(dev, pipe);
Damien Lespiau3bdcfc02015-02-28 14:54:09 +000014947 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +000014948 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070014949 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030014950 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000014951 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070014952 }
Jesse Barnes79e53942008-11-07 14:24:08 -080014953 }
14954
Ville Syrjäläbfa7df02015-09-24 23:29:18 +030014955 intel_update_czclk(dev_priv);
14956 intel_update_cdclk(dev);
14957
Daniel Vettere72f9fb2013-06-05 13:34:06 +020014958 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010014959
Ville Syrjäläb2045352016-05-13 23:41:27 +030014960 if (dev_priv->max_cdclk_freq == 0)
14961 intel_update_max_cdclk(dev);
14962
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014963 /* Just disable it once at startup */
14964 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014965 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000014966
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014967 drm_modeset_lock_all(dev);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020014968 intel_modeset_setup_hw_state(dev);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014969 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080014970
Damien Lespiaud3fcc802014-05-13 23:32:22 +010014971 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020014972 struct intel_initial_plane_config plane_config = {};
14973
Jesse Barnes46f297f2014-03-07 08:57:48 -080014974 if (!crtc->active)
14975 continue;
14976
Jesse Barnes46f297f2014-03-07 08:57:48 -080014977 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080014978 * Note that reserving the BIOS fb up front prevents us
14979 * from stuffing other stolen allocations like the ring
14980 * on top. This prevents some ugliness at boot time, and
14981 * can even allow for smooth boot transitions if the BIOS
14982 * fb is large enough for the active pipe configuration.
14983 */
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020014984 dev_priv->display.get_initial_plane_config(crtc,
14985 &plane_config);
14986
14987 /*
14988 * If the fb is shared between multiple heads, we'll
14989 * just get the first one.
14990 */
14991 intel_find_initial_plane_obj(crtc, &plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080014992 }
Matt Roperd93c0372015-12-03 11:37:41 -080014993
14994 /*
14995 * Make sure hardware watermarks really match the state we read out.
14996 * Note that we need to do this after reconstructing the BIOS fb's
14997 * since the watermark calculation done here will use pstate->fb.
14998 */
14999 sanitize_watermarks(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010015000}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080015001
Daniel Vetter7fad7982012-07-04 17:51:47 +020015002static void intel_enable_pipe_a(struct drm_device *dev)
15003{
15004 struct intel_connector *connector;
15005 struct drm_connector *crt = NULL;
15006 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015007 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020015008
15009 /* We can't just switch on the pipe A, we need to set things up with a
15010 * proper mode and output configuration. As a gross hack, enable pipe A
15011 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015012 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020015013 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15014 crt = &connector->base;
15015 break;
15016 }
15017 }
15018
15019 if (!crt)
15020 return;
15021
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015022 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020015023 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020015024}
15025
Daniel Vetterfa555832012-10-10 23:14:00 +020015026static bool
15027intel_check_plane_mapping(struct intel_crtc *crtc)
15028{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015029 struct drm_device *dev = crtc->base.dev;
15030 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä649636e2015-09-22 19:50:01 +030015031 u32 val;
Daniel Vetterfa555832012-10-10 23:14:00 +020015032
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015033 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020015034 return true;
15035
Ville Syrjälä649636e2015-09-22 19:50:01 +030015036 val = I915_READ(DSPCNTR(!crtc->plane));
Daniel Vetterfa555832012-10-10 23:14:00 +020015037
15038 if ((val & DISPLAY_PLANE_ENABLE) &&
15039 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15040 return false;
15041
15042 return true;
15043}
15044
Ville Syrjälä02e93c32015-08-26 19:39:19 +030015045static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
15046{
15047 struct drm_device *dev = crtc->base.dev;
15048 struct intel_encoder *encoder;
15049
15050 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15051 return true;
15052
15053 return false;
15054}
15055
Ville Syrjälädd756192016-02-17 21:28:45 +020015056static bool intel_encoder_has_connectors(struct intel_encoder *encoder)
15057{
15058 struct drm_device *dev = encoder->base.dev;
15059 struct intel_connector *connector;
15060
15061 for_each_connector_on_encoder(dev, &encoder->base, connector)
15062 return true;
15063
15064 return false;
15065}
15066
Daniel Vetter24929352012-07-02 20:28:59 +020015067static void intel_sanitize_crtc(struct intel_crtc *crtc)
15068{
15069 struct drm_device *dev = crtc->base.dev;
15070 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula4d1de972016-03-18 17:05:42 +020015071 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter24929352012-07-02 20:28:59 +020015072
Daniel Vetter24929352012-07-02 20:28:59 +020015073 /* Clear any frame start delays used for debugging left by the BIOS */
Jani Nikula4d1de972016-03-18 17:05:42 +020015074 if (!transcoder_is_dsi(cpu_transcoder)) {
15075 i915_reg_t reg = PIPECONF(cpu_transcoder);
15076
15077 I915_WRITE(reg,
15078 I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15079 }
Daniel Vetter24929352012-07-02 20:28:59 +020015080
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015081 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010015082 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030015083 if (crtc->active) {
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015084 struct intel_plane *plane;
15085
Daniel Vetter96256042015-02-13 21:03:42 +010015086 drm_crtc_vblank_on(&crtc->base);
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015087
15088 /* Disable everything but the primary plane */
15089 for_each_intel_plane_on_crtc(dev, crtc, plane) {
15090 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
15091 continue;
15092
15093 plane->disable_plane(&plane->base, &crtc->base);
15094 }
Daniel Vetter96256042015-02-13 21:03:42 +010015095 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015096
Daniel Vetter24929352012-07-02 20:28:59 +020015097 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020015098 * disable the crtc (and hence change the state) if it is wrong. Note
15099 * that gen4+ has a fixed plane -> pipe mapping. */
15100 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020015101 bool plane;
15102
Daniel Vetter24929352012-07-02 20:28:59 +020015103 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
15104 crtc->base.base.id);
15105
15106 /* Pipe has the wrong plane attached and the plane is active.
15107 * Temporarily change the plane mapping and disable everything
15108 * ... */
15109 plane = crtc->plane;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015110 to_intel_plane_state(crtc->base.primary->state)->visible = true;
Daniel Vetter24929352012-07-02 20:28:59 +020015111 crtc->plane = !plane;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015112 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015113 crtc->plane = plane;
Daniel Vetter24929352012-07-02 20:28:59 +020015114 }
Daniel Vetter24929352012-07-02 20:28:59 +020015115
Daniel Vetter7fad7982012-07-04 17:51:47 +020015116 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15117 crtc->pipe == PIPE_A && !crtc->active) {
15118 /* BIOS forgot to enable pipe A, this mostly happens after
15119 * resume. Force-enable the pipe to fix this, the update_dpms
15120 * call below we restore the pipe to the right state, but leave
15121 * the required bits on. */
15122 intel_enable_pipe_a(dev);
15123 }
15124
Daniel Vetter24929352012-07-02 20:28:59 +020015125 /* Adjust the state of the output pipe according to whether we
15126 * have active connectors/encoders. */
Maarten Lankhorst842e0302016-03-02 15:48:01 +010015127 if (crtc->active && !intel_crtc_has_encoders(crtc))
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015128 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015129
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030015130 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010015131 /*
15132 * We start out with underrun reporting disabled to avoid races.
15133 * For correct bookkeeping mark this on active crtcs.
15134 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020015135 * Also on gmch platforms we dont have any hardware bits to
15136 * disable the underrun reporting. Which means we need to start
15137 * out with underrun reporting disabled also on inactive pipes,
15138 * since otherwise we'll complain about the garbage we read when
15139 * e.g. coming up after runtime pm.
15140 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010015141 * No protection against concurrent access is required - at
15142 * worst a fifo underrun happens which also sets this to false.
15143 */
15144 crtc->cpu_fifo_underrun_disabled = true;
15145 crtc->pch_fifo_underrun_disabled = true;
15146 }
Daniel Vetter24929352012-07-02 20:28:59 +020015147}
15148
15149static void intel_sanitize_encoder(struct intel_encoder *encoder)
15150{
15151 struct intel_connector *connector;
15152 struct drm_device *dev = encoder->base.dev;
15153
15154 /* We need to check both for a crtc link (meaning that the
15155 * encoder is active and trying to read from a pipe) and the
15156 * pipe itself being active. */
15157 bool has_active_crtc = encoder->base.crtc &&
15158 to_intel_crtc(encoder->base.crtc)->active;
15159
Ville Syrjälädd756192016-02-17 21:28:45 +020015160 if (intel_encoder_has_connectors(encoder) && !has_active_crtc) {
Daniel Vetter24929352012-07-02 20:28:59 +020015161 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15162 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015163 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015164
15165 /* Connector is active, but has no active pipe. This is
15166 * fallout from our resume register restoring. Disable
15167 * the encoder manually again. */
15168 if (encoder->base.crtc) {
15169 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15170 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015171 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015172 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030015173 if (encoder->post_disable)
15174 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020015175 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020015176 encoder->base.crtc = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015177
15178 /* Inconsistent output/port/pipe state happens presumably due to
15179 * a bug in one of the get_hw_state functions. Or someplace else
15180 * in our code, like the register restore mess on resume. Clamp
15181 * things to off as a safer default. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015182 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015183 if (connector->encoder != encoder)
15184 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020015185 connector->base.dpms = DRM_MODE_DPMS_OFF;
15186 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015187 }
15188 }
15189 /* Enabled encoders without active connectors will be fixed in
15190 * the crtc fixup. */
15191}
15192
Imre Deak04098752014-02-18 00:02:16 +020015193void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015194{
15195 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020015196 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015197
Imre Deak04098752014-02-18 00:02:16 +020015198 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15199 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15200 i915_disable_vga(dev);
15201 }
15202}
15203
15204void i915_redisable_vga(struct drm_device *dev)
15205{
15206 struct drm_i915_private *dev_priv = dev->dev_private;
15207
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015208 /* This function can be called both from intel_modeset_setup_hw_state or
15209 * at a very early point in our resume sequence, where the power well
15210 * structures are not yet restored. Since this function is at a very
15211 * paranoid "someone might have enabled VGA while we were not looking"
15212 * level, just check if the power well is enabled instead of trying to
15213 * follow the "don't touch the power well if we don't need it" policy
15214 * the rest of the driver uses. */
Imre Deak6392f842016-02-12 18:55:13 +020015215 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015216 return;
15217
Imre Deak04098752014-02-18 00:02:16 +020015218 i915_redisable_vga_power_on(dev);
Imre Deak6392f842016-02-12 18:55:13 +020015219
15220 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015221}
15222
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015223static bool primary_get_hw_state(struct intel_plane *plane)
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015224{
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015225 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015226
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015227 return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015228}
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015229
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015230/* FIXME read out full plane state for all planes */
15231static void readout_plane_state(struct intel_crtc *crtc)
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015232{
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015233 struct drm_plane *primary = crtc->base.primary;
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015234 struct intel_plane_state *plane_state =
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015235 to_intel_plane_state(primary->state);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015236
Matt Roper19b8d382015-09-24 15:53:17 -070015237 plane_state->visible = crtc->active &&
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015238 primary_get_hw_state(to_intel_plane(primary));
15239
15240 if (plane_state->visible)
15241 crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015242}
15243
Daniel Vetter30e984d2013-06-05 13:34:17 +020015244static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020015245{
15246 struct drm_i915_private *dev_priv = dev->dev_private;
15247 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020015248 struct intel_crtc *crtc;
15249 struct intel_encoder *encoder;
15250 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020015251 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020015252
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015253 dev_priv->active_crtcs = 0;
15254
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015255 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015256 struct intel_crtc_state *crtc_state = crtc->config;
15257 int pixclk = 0;
Daniel Vetter3b117c82013-04-17 20:15:07 +020015258
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015259 __drm_atomic_helper_crtc_destroy_state(&crtc->base, &crtc_state->base);
15260 memset(crtc_state, 0, sizeof(*crtc_state));
15261 crtc_state->base.crtc = &crtc->base;
Daniel Vetter24929352012-07-02 20:28:59 +020015262
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015263 crtc_state->base.active = crtc_state->base.enable =
15264 dev_priv->display.get_pipe_config(crtc, crtc_state);
15265
15266 crtc->base.enabled = crtc_state->base.enable;
15267 crtc->active = crtc_state->base.active;
15268
15269 if (crtc_state->base.active) {
15270 dev_priv->active_crtcs |= 1 << crtc->pipe;
15271
Clint Taylorc89e39f2016-05-13 23:41:21 +030015272 if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015273 pixclk = ilk_pipe_pixel_rate(crtc_state);
Ville Syrjälä9558d152016-05-13 23:41:20 +030015274 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015275 pixclk = crtc_state->base.adjusted_mode.crtc_clock;
15276 else
15277 WARN_ON(dev_priv->display.modeset_calc_cdclk);
Ville Syrjälä9558d152016-05-13 23:41:20 +030015278
15279 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
15280 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
15281 pixclk = DIV_ROUND_UP(pixclk * 100, 95);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015282 }
15283
15284 dev_priv->min_pixclk[crtc->pipe] = pixclk;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015285
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015286 readout_plane_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020015287
15288 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
15289 crtc->base.base.id,
15290 crtc->active ? "enabled" : "disabled");
15291 }
15292
Daniel Vetter53589012013-06-05 13:34:16 +020015293 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15294 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15295
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020015296 pll->on = pll->funcs.get_hw_state(dev_priv, pll,
15297 &pll->config.hw_state);
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015298 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015299 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010015300 if (crtc->active && crtc->config->shared_dpll == pll)
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015301 pll->config.crtc_mask |= 1 << crtc->pipe;
Daniel Vetter53589012013-06-05 13:34:16 +020015302 }
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010015303 pll->active_mask = pll->config.crtc_mask;
Daniel Vetter53589012013-06-05 13:34:16 +020015304
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015305 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015306 pll->name, pll->config.crtc_mask, pll->on);
Daniel Vetter53589012013-06-05 13:34:16 +020015307 }
15308
Damien Lespiaub2784e12014-08-05 11:29:37 +010015309 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015310 pipe = 0;
15311
15312 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070015313 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15314 encoder->base.crtc = &crtc->base;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015315 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020015316 } else {
15317 encoder->base.crtc = NULL;
15318 }
15319
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015320 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020015321 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015322 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015323 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015324 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020015325 }
15326
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015327 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015328 if (connector->get_hw_state(connector)) {
15329 connector->base.dpms = DRM_MODE_DPMS_ON;
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010015330
15331 encoder = connector->encoder;
15332 connector->base.encoder = &encoder->base;
15333
15334 if (encoder->base.crtc &&
15335 encoder->base.crtc->state->active) {
15336 /*
15337 * This has to be done during hardware readout
15338 * because anything calling .crtc_disable may
15339 * rely on the connector_mask being accurate.
15340 */
15341 encoder->base.crtc->state->connector_mask |=
15342 1 << drm_connector_index(&connector->base);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +010015343 encoder->base.crtc->state->encoder_mask |=
15344 1 << drm_encoder_index(&encoder->base);
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010015345 }
15346
Daniel Vetter24929352012-07-02 20:28:59 +020015347 } else {
15348 connector->base.dpms = DRM_MODE_DPMS_OFF;
15349 connector->base.encoder = NULL;
15350 }
15351 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
15352 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030015353 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015354 connector->base.encoder ? "enabled" : "disabled");
15355 }
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015356
15357 for_each_intel_crtc(dev, crtc) {
15358 crtc->base.hwmode = crtc->config->base.adjusted_mode;
15359
15360 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
15361 if (crtc->base.state->active) {
15362 intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
15363 intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
15364 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
15365
15366 /*
15367 * The initial mode needs to be set in order to keep
15368 * the atomic core happy. It wants a valid mode if the
15369 * crtc's enabled, so we do the above call.
15370 *
15371 * At this point some state updated by the connectors
15372 * in their ->detect() callback has not run yet, so
15373 * no recalculation can be done yet.
15374 *
15375 * Even if we could do a recalculation and modeset
15376 * right now it would cause a double modeset if
15377 * fbdev or userspace chooses a different initial mode.
15378 *
15379 * If that happens, someone indicated they wanted a
15380 * mode change, which means it's safe to do a full
15381 * recalculation.
15382 */
15383 crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
Ville Syrjälä9eca68322015-09-10 18:59:10 +030015384
15385 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
15386 update_scanline_offset(crtc);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015387 }
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020015388
15389 intel_pipe_config_sanity_check(dev_priv, crtc->config);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015390 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020015391}
15392
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015393/* Scan out the current hw modeset state,
15394 * and sanitizes it to the current state
15395 */
15396static void
15397intel_modeset_setup_hw_state(struct drm_device *dev)
Daniel Vetter30e984d2013-06-05 13:34:17 +020015398{
15399 struct drm_i915_private *dev_priv = dev->dev_private;
15400 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015401 struct intel_crtc *crtc;
15402 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020015403 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015404
15405 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020015406
15407 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010015408 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015409 intel_sanitize_encoder(encoder);
15410 }
15411
Damien Lespiau055e3932014-08-18 13:49:10 +010015412 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020015413 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15414 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015415 intel_dump_pipe_config(crtc, crtc->config,
15416 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020015417 }
Daniel Vetter9a935852012-07-05 22:34:27 +020015418
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020015419 intel_modeset_update_connector_atomic_state(dev);
15420
Daniel Vetter35c95372013-07-17 06:55:04 +020015421 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15422 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15423
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010015424 if (!pll->on || pll->active_mask)
Daniel Vetter35c95372013-07-17 06:55:04 +020015425 continue;
15426
15427 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15428
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020015429 pll->funcs.disable(dev_priv, pll);
Daniel Vetter35c95372013-07-17 06:55:04 +020015430 pll->on = false;
15431 }
15432
Wayne Boyer666a4532015-12-09 12:29:35 -080015433 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Ville Syrjälä6eb1a682015-06-24 22:00:03 +030015434 vlv_wm_get_hw_state(dev);
15435 else if (IS_GEN9(dev))
Pradeep Bhat30789992014-11-04 17:06:45 +000015436 skl_wm_get_hw_state(dev);
15437 else if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030015438 ilk_wm_get_hw_state(dev);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020015439
15440 for_each_intel_crtc(dev, crtc) {
15441 unsigned long put_domains;
15442
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +010015443 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020015444 if (WARN_ON(put_domains))
15445 modeset_put_power_domains(dev_priv, put_domains);
15446 }
15447 intel_display_set_init_power(dev_priv, false);
Paulo Zanoni010cf732016-01-19 11:35:48 -020015448
15449 intel_fbc_init_pipe_state(dev_priv);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015450}
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030015451
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015452void intel_display_resume(struct drm_device *dev)
15453{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015454 struct drm_i915_private *dev_priv = to_i915(dev);
15455 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
15456 struct drm_modeset_acquire_ctx ctx;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015457 int ret;
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015458 bool setup = false;
Daniel Vetterf30da182013-04-11 20:22:50 +020015459
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015460 dev_priv->modeset_restore_state = NULL;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015461
Maarten Lankhorstea49c9a2016-02-16 15:27:42 +010015462 /*
15463 * This is a cludge because with real atomic modeset mode_config.mutex
15464 * won't be taken. Unfortunately some probed state like
15465 * audio_codec_enable is still protected by mode_config.mutex, so lock
15466 * it here for now.
15467 */
15468 mutex_lock(&dev->mode_config.mutex);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015469 drm_modeset_acquire_init(&ctx, 0);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015470
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015471retry:
15472 ret = drm_modeset_lock_all_ctx(dev, &ctx);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015473
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015474 if (ret == 0 && !setup) {
15475 setup = true;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015476
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015477 intel_modeset_setup_hw_state(dev);
15478 i915_redisable_vga(dev);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010015479 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020015480
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015481 if (ret == 0 && state) {
15482 struct drm_crtc_state *crtc_state;
15483 struct drm_crtc *crtc;
15484 int i;
15485
15486 state->acquire_ctx = &ctx;
15487
Ville Syrjäläe3d54572016-05-13 10:10:42 -070015488 /* ignore any reset values/BIOS leftovers in the WM registers */
15489 to_intel_atomic_state(state)->skip_intermediate_wm = true;
15490
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015491 for_each_crtc_in_state(state, crtc, crtc_state, i) {
15492 /*
15493 * Force recalculation even if we restore
15494 * current state. With fast modeset this may not result
15495 * in a modeset when the state is compatible.
15496 */
15497 crtc_state->mode_changed = true;
15498 }
15499
15500 ret = drm_atomic_commit(state);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015501 }
15502
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015503 if (ret == -EDEADLK) {
15504 drm_modeset_backoff(&ctx);
15505 goto retry;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015506 }
15507
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015508 drm_modeset_drop_locks(&ctx);
15509 drm_modeset_acquire_fini(&ctx);
Maarten Lankhorstea49c9a2016-02-16 15:27:42 +010015510 mutex_unlock(&dev->mode_config.mutex);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015511
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015512 if (ret) {
15513 DRM_ERROR("Restoring old state failed with %i\n", ret);
15514 drm_atomic_state_free(state);
15515 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010015516}
15517
15518void intel_modeset_gem_init(struct drm_device *dev)
15519{
Chris Wilsondc979972016-05-10 14:10:04 +010015520 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080015521 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070015522 struct drm_i915_gem_object *obj;
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015523 int ret;
Jesse Barnes484b41d2014-03-07 08:57:55 -080015524
Chris Wilsondc979972016-05-10 14:10:04 +010015525 intel_init_gt_powersave(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +030015526
Chris Wilson1833b132012-05-09 11:56:28 +010015527 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020015528
Chris Wilson1ee8da62016-05-12 12:43:23 +010015529 intel_setup_overlay(dev_priv);
Jesse Barnes484b41d2014-03-07 08:57:55 -080015530
15531 /*
15532 * Make sure any fbs we allocated at startup are properly
15533 * pinned & fenced. When we do the allocation it's too early
15534 * for this.
15535 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010015536 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070015537 obj = intel_fb_obj(c->primary->fb);
15538 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080015539 continue;
15540
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015541 mutex_lock(&dev->struct_mutex);
Ville Syrjälä3465c582016-02-15 22:54:43 +020015542 ret = intel_pin_and_fence_fb_obj(c->primary->fb,
15543 c->primary->state->rotation);
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015544 mutex_unlock(&dev->struct_mutex);
15545 if (ret) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080015546 DRM_ERROR("failed to pin boot fb on pipe %d\n",
15547 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100015548 drm_framebuffer_unreference(c->primary->fb);
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020015549 drm_framebuffer_unreference(c->primary->state->fb);
15550 c->primary->fb = c->primary->state->fb = NULL;
Maarten Lankhorst36750f22015-06-01 12:49:54 +020015551 c->primary->crtc = c->primary->state->crtc = NULL;
Maarten Lankhorst36750f22015-06-01 12:49:54 +020015552 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
Jesse Barnes484b41d2014-03-07 08:57:55 -080015553 }
15554 }
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015555
15556 intel_backlight_register(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015557}
15558
Imre Deak4932e2c2014-02-11 17:12:48 +020015559void intel_connector_unregister(struct intel_connector *intel_connector)
15560{
15561 struct drm_connector *connector = &intel_connector->base;
15562
15563 intel_panel_destroy_backlight(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +010015564 drm_connector_unregister(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020015565}
15566
Jesse Barnes79e53942008-11-07 14:24:08 -080015567void intel_modeset_cleanup(struct drm_device *dev)
15568{
Jesse Barnes652c3932009-08-17 13:31:43 -070015569 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula19c80542015-12-16 12:48:16 +020015570 struct intel_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070015571
Chris Wilsondc979972016-05-10 14:10:04 +010015572 intel_disable_gt_powersave(dev_priv);
Imre Deak2eb52522014-11-19 15:30:05 +020015573
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015574 intel_backlight_unregister(dev);
15575
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015576 /*
15577 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020015578 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015579 * experience fancy races otherwise.
15580 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020015581 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070015582
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015583 /*
15584 * Due to the hpd irq storm handling the hotplug work can re-arm the
15585 * poll handlers. Hence disable polling after hpd handling is shut down.
15586 */
Keith Packardf87ea762010-10-03 19:36:26 -070015587 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015588
Jesse Barnes723bfd72010-10-07 16:01:13 -070015589 intel_unregister_dsm_handler();
15590
Paulo Zanonic937ab3e52016-01-19 11:35:46 -020015591 intel_fbc_global_disable(dev_priv);
Kristian Høgsberg69341a52009-11-11 12:19:17 -050015592
Chris Wilson1630fe72011-07-08 12:22:42 +010015593 /* flush any delayed tasks or pending work */
15594 flush_scheduled_work();
15595
Jani Nikuladb31af1d2013-11-08 16:48:53 +020015596 /* destroy the backlight and sysfs files before encoders/connectors */
Jani Nikula19c80542015-12-16 12:48:16 +020015597 for_each_intel_connector(dev, connector)
15598 connector->unregister(connector);
Paulo Zanonid9255d52013-09-26 20:05:59 -030015599
Jesse Barnes79e53942008-11-07 14:24:08 -080015600 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010015601
Chris Wilson1ee8da62016-05-12 12:43:23 +010015602 intel_cleanup_overlay(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +030015603
Chris Wilsondc979972016-05-10 14:10:04 +010015604 intel_cleanup_gt_powersave(dev_priv);
Daniel Vetterf5949142016-01-13 11:55:28 +010015605
15606 intel_teardown_gmbus(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015607}
15608
Dave Airlie28d52042009-09-21 14:33:58 +100015609/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080015610 * Return which encoder is currently attached for connector.
15611 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010015612struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080015613{
Chris Wilsondf0e9242010-09-09 16:20:55 +010015614 return &intel_attached_encoder(connector)->base;
15615}
Jesse Barnes79e53942008-11-07 14:24:08 -080015616
Chris Wilsondf0e9242010-09-09 16:20:55 +010015617void intel_connector_attach_encoder(struct intel_connector *connector,
15618 struct intel_encoder *encoder)
15619{
15620 connector->encoder = encoder;
15621 drm_mode_connector_attach_encoder(&connector->base,
15622 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080015623}
Dave Airlie28d52042009-09-21 14:33:58 +100015624
15625/*
15626 * set vga decode state - true == enable VGA decode
15627 */
15628int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
15629{
15630 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000015631 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100015632 u16 gmch_ctrl;
15633
Chris Wilson75fa0412014-02-07 18:37:02 -020015634 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
15635 DRM_ERROR("failed to read control word\n");
15636 return -EIO;
15637 }
15638
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020015639 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
15640 return 0;
15641
Dave Airlie28d52042009-09-21 14:33:58 +100015642 if (state)
15643 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
15644 else
15645 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020015646
15647 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
15648 DRM_ERROR("failed to write control word\n");
15649 return -EIO;
15650 }
15651
Dave Airlie28d52042009-09-21 14:33:58 +100015652 return 0;
15653}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015654
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015655struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015656
15657 u32 power_well_driver;
15658
Chris Wilson63b66e52013-08-08 15:12:06 +020015659 int num_transcoders;
15660
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015661 struct intel_cursor_error_state {
15662 u32 control;
15663 u32 position;
15664 u32 base;
15665 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010015666 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015667
15668 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015669 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015670 u32 source;
Imre Deakf301b1e12014-04-18 15:55:04 +030015671 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010015672 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015673
15674 struct intel_plane_error_state {
15675 u32 control;
15676 u32 stride;
15677 u32 size;
15678 u32 pos;
15679 u32 addr;
15680 u32 surface;
15681 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010015682 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020015683
15684 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015685 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020015686 enum transcoder cpu_transcoder;
15687
15688 u32 conf;
15689
15690 u32 htotal;
15691 u32 hblank;
15692 u32 hsync;
15693 u32 vtotal;
15694 u32 vblank;
15695 u32 vsync;
15696 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015697};
15698
15699struct intel_display_error_state *
Chris Wilsonc0336662016-05-06 15:40:21 +010015700intel_display_capture_error_state(struct drm_i915_private *dev_priv)
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015701{
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015702 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020015703 int transcoders[] = {
15704 TRANSCODER_A,
15705 TRANSCODER_B,
15706 TRANSCODER_C,
15707 TRANSCODER_EDP,
15708 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015709 int i;
15710
Chris Wilsonc0336662016-05-06 15:40:21 +010015711 if (INTEL_INFO(dev_priv)->num_pipes == 0)
Chris Wilson63b66e52013-08-08 15:12:06 +020015712 return NULL;
15713
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015714 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015715 if (error == NULL)
15716 return NULL;
15717
Chris Wilsonc0336662016-05-06 15:40:21 +010015718 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015719 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
15720
Damien Lespiau055e3932014-08-18 13:49:10 +010015721 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020015722 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015723 __intel_display_power_is_enabled(dev_priv,
15724 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020015725 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015726 continue;
15727
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030015728 error->cursor[i].control = I915_READ(CURCNTR(i));
15729 error->cursor[i].position = I915_READ(CURPOS(i));
15730 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015731
15732 error->plane[i].control = I915_READ(DSPCNTR(i));
15733 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Chris Wilsonc0336662016-05-06 15:40:21 +010015734 if (INTEL_GEN(dev_priv) <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030015735 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015736 error->plane[i].pos = I915_READ(DSPPOS(i));
15737 }
Chris Wilsonc0336662016-05-06 15:40:21 +010015738 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
Paulo Zanonica291362013-03-06 20:03:14 -030015739 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc0336662016-05-06 15:40:21 +010015740 if (INTEL_GEN(dev_priv) >= 4) {
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015741 error->plane[i].surface = I915_READ(DSPSURF(i));
15742 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
15743 }
15744
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015745 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e12014-04-18 15:55:04 +030015746
Chris Wilsonc0336662016-05-06 15:40:21 +010015747 if (HAS_GMCH_DISPLAY(dev_priv))
Imre Deakf301b1e12014-04-18 15:55:04 +030015748 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020015749 }
15750
Jani Nikula4d1de972016-03-18 17:05:42 +020015751 /* Note: this does not include DSI transcoders. */
Chris Wilsonc0336662016-05-06 15:40:21 +010015752 error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +030015753 if (HAS_DDI(dev_priv))
Chris Wilson63b66e52013-08-08 15:12:06 +020015754 error->num_transcoders++; /* Account for eDP. */
15755
15756 for (i = 0; i < error->num_transcoders; i++) {
15757 enum transcoder cpu_transcoder = transcoders[i];
15758
Imre Deakddf9c532013-11-27 22:02:02 +020015759 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015760 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020015761 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015762 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015763 continue;
15764
Chris Wilson63b66e52013-08-08 15:12:06 +020015765 error->transcoder[i].cpu_transcoder = cpu_transcoder;
15766
15767 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
15768 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
15769 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
15770 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
15771 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
15772 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
15773 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015774 }
15775
15776 return error;
15777}
15778
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015779#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
15780
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015781void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015782intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015783 struct drm_device *dev,
15784 struct intel_display_error_state *error)
15785{
Damien Lespiau055e3932014-08-18 13:49:10 +010015786 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015787 int i;
15788
Chris Wilson63b66e52013-08-08 15:12:06 +020015789 if (!error)
15790 return;
15791
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015792 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020015793 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015794 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015795 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010015796 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015797 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020015798 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020015799 onoff(error->pipe[i].power_domain_on));
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015800 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e12014-04-18 15:55:04 +030015801 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015802
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015803 err_printf(m, "Plane [%d]:\n", i);
15804 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
15805 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015806 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015807 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
15808 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015809 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030015810 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015811 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015812 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015813 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
15814 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015815 }
15816
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015817 err_printf(m, "Cursor [%d]:\n", i);
15818 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
15819 err_printf(m, " POS: %08x\n", error->cursor[i].position);
15820 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015821 }
Chris Wilson63b66e52013-08-08 15:12:06 +020015822
15823 for (i = 0; i < error->num_transcoders; i++) {
Jani Nikulada205632016-03-15 21:51:10 +020015824 err_printf(m, "CPU transcoder: %s\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020015825 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015826 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020015827 onoff(error->transcoder[i].power_domain_on));
Chris Wilson63b66e52013-08-08 15:12:06 +020015828 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
15829 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
15830 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
15831 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
15832 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
15833 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
15834 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
15835 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015836}